Skip to content

Commit a6e81c1

Browse files
authored
Add captcha for user signup (#1822)
Signed-off-by: Tushar Goel <[email protected]>
1 parent cbda0ca commit a6e81c1

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Django==4.2.17
3131
django-crispy-forms==2.3
3232
django-environ==0.11.2
3333
django-filter==24.3
34+
django-recaptcha==4.0.0
3435
django-widget-tweaks==1.5.0
3536
djangorestframework==3.15.2
3637
doc8==0.11.1

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ install_requires =
9999
python-dotenv
100100
texttable
101101

102+
django-recaptcha>=4.0.0
103+
102104

103105
[options.extras_require]
104106
dev =

vulnerabilities/forms.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
from django import forms
1111
from django.core.validators import validate_email
12+
from django_recaptcha.fields import ReCaptchaField
13+
from django_recaptcha.widgets import ReCaptchaV2Checkbox
1214

1315
from vulnerabilities.models import ApiUser
1416

@@ -38,6 +40,10 @@ class ApiUserCreationForm(forms.ModelForm):
3840
Support a simplified creation for API-only users directly from the UI.
3941
"""
4042

43+
captcha = ReCaptchaField(
44+
error_messages={"required": ("Captcha is required")}, widget=ReCaptchaV2Checkbox
45+
)
46+
4147
class Meta:
4248
model = ApiUser
4349
fields = (

vulnerabilities/templates/api_user_creation_form.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414
</article>
1515
{% endfor %}
1616
<div id="form-errors" class="message is-danger {% if not form.errors %}is-hidden{% endif %}">
17-
{% for field_name, errors in form.errors.items %}
17+
{% if form.errors.captcha %}
1818
<div class="message-body">
19-
{{ errors }}
19+
{{ form.errors.captcha }}
2020
</div>
21-
{% endfor %}
21+
{% else %}
22+
<div class="message-body">
23+
{% for error in form.errors.values %}
24+
{{ error }}
25+
{% endfor %}
26+
</div>
27+
{% endif %}
2228
</div>
2329
<h2 class="subtitle mb-0 pt-2 mb-2">
2430
<b>VulnerableCode API key request</b>

vulnerablecode/settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,15 @@
8383
"drf_spectacular",
8484
# required for Django collectstatic discovery
8585
"drf_spectacular_sidecar",
86+
"django_recaptcha",
8687
)
8788

89+
RECAPTCHA_PUBLIC_KEY = env.str("RECAPTCHA_PUBLIC_KEY", "")
90+
RECAPTCHA_PRIVATE_KEY = env.str("RECAPTCHA_PRIVATE_KEY", "")
91+
SILENCED_SYSTEM_CHECKS = ["captcha.recaptcha_test_key_error"]
92+
RECAPTCHA_DOMAIN = env.str("RECAPTCHA_DOMAIN", "www.recaptcha.net")
93+
94+
8895
MIDDLEWARE = (
8996
"django.middleware.security.SecurityMiddleware",
9097
"django.contrib.sessions.middleware.SessionMiddleware",

0 commit comments

Comments
 (0)