From cacbcdc7315d24fdde17ae03a7d750e7e99ac3a3 Mon Sep 17 00:00:00 2001
From: alice
Date: Mon, 28 Dec 2020 17:50:16 +0100
Subject: [PATCH] add signup captcha
---
ccvpn/context_processors.py | 1 +
lambdainst/views.py | 10 ++++++++++
templates/ccvpn/signup.html | 5 +++++
3 files changed, 16 insertions(+)
diff --git a/ccvpn/context_processors.py b/ccvpn/context_processors.py
index 365bf54..e8c45f9 100644
--- a/ccvpn/context_processors.py
+++ b/ccvpn/context_processors.py
@@ -12,4 +12,5 @@ def some_settings(request):
'ADDITIONAL_HTML': settings.ADDITIONAL_HTML,
'SUPPORT_TEMPLATE': settings.SUPPORT_TEMPLATE,
'ADDITIONAL_HEADER_HTML': settings.ADDITIONAL_HEADER_HTML,
+ 'HCAPTCHA_SITE_KEY': settings.HCAPTCHA_SITE_KEY,
}
diff --git a/lambdainst/views.py b/lambdainst/views.py
index 34604b0..246236c 100644
--- a/lambdainst/views.py
+++ b/lambdainst/views.py
@@ -71,6 +71,13 @@ def signup(request):
form = SignupForm(request.POST)
+ grr = request.POST.get('g-recaptcha-response', '')
+ if captcha_test(grr, request):
+ request.session['signup_captcha_pass'] = True
+ elif not request.session.get('signup_captcha_pass'):
+ messages.error(request, _("Invalid captcha. Please try again"))
+ return render(request, 'ccvpn/signup.html', dict(form=form))
+
if not form.is_valid():
return render(request, 'ccvpn/signup.html', dict(form=form))
@@ -92,6 +99,9 @@ def signup(request):
user.backend = 'django.contrib.auth.backends.ModelBackend'
auth.login(request, user)
+ # invalidate that captcha
+ request.session['signup_captcha_pass'] = False
+
return redirect('account:index')
diff --git a/templates/ccvpn/signup.html b/templates/ccvpn/signup.html
index 998ac04..1b6ffee 100644
--- a/templates/ccvpn/signup.html
+++ b/templates/ccvpn/signup.html
@@ -34,6 +34,11 @@
{% trans 'Used to recover your password and confirm stuff.' %}
+ {% if not request.session.signup_captcha_pass %}
+
+
+ {% endif %}
+