Skip to content

Commit f2547a2

Browse files
committed
Refine the Altcha implementation #235
Signed-off-by: tdruez <[email protected]>
1 parent 38e5496 commit f2547a2

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

dje/altcha.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@
1616
import altcha
1717
from altcha import ChallengeOptions
1818
from altcha import create_challenge
19+
from django.forms.widgets import HiddenInput
1920

2021
ALTCHA_HMAC_KEY = "your-altcha-hmac-key"
2122

2223

24+
class AltchaWidget(HiddenInput):
25+
template_name = "widgets/altcha.html"
26+
27+
2328
class AltchaField(forms.Field):
24-
widget = forms.HiddenInput
29+
widget = AltchaWidget
2530
default_error_messages = {
2631
"error": _("Failed to process CAPTCHA token"),
2732
"invalid": _("Invalid CAPTCHA token."),
@@ -48,19 +53,21 @@ def validate(self, value):
4853
raise forms.ValidationError(self.error_messages["invalid"], code="invalid")
4954

5055

56+
def get_altcha_challenge():
57+
# Create the challenge using your options
58+
challenge = create_challenge(
59+
ChallengeOptions(
60+
hmac_key=ALTCHA_HMAC_KEY,
61+
max_number=50000,
62+
)
63+
)
64+
return challenge
65+
66+
5167
@require_GET
5268
def get_altcha_challenge_view(request):
5369
try:
54-
# Create the challenge using your options
55-
challenge = create_challenge(
56-
ChallengeOptions(
57-
hmac_key=ALTCHA_HMAC_KEY,
58-
max_number=50000,
59-
)
60-
)
61-
# Return the challenge as a JSON response
62-
return JsonResponse(challenge.__dict__) # Use JsonResponse to send JSON data
63-
70+
challenge = get_altcha_challenge()
71+
return JsonResponse(challenge.__dict__)
6472
except Exception as e:
65-
# Handle exceptions and return an error response
6673
return JsonResponse({"error": f"Failed to create challenge: {str(e)}"}, status=500)

dje/registration.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ def helper(self):
166166
css_class="alert alert-primary px-2 py-2",
167167
),
168168
"altcha_token",
169-
# TODO: Integrate this into the field widget directly! Include the JS as well.
170-
HTML('<altcha-widget challengeurl="/altcha/" name="altcha_token"></altcha-widget>'),
171169
tos,
172170
Div(
173171
StrictSubmit(

dje/templates/django_registration/registration_form.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
{% block page_title %}{% trans 'Sign Up' %}{% endblock %}
66

7-
{% block header %}
8-
<script async defer src="https://eu.altcha.org/js/latest/altcha.min.js" type="module"></script>
9-
{% endblock %}
7+
{% block header %}{% endblock %}
108
{% block bodyclass %}signup-form{% endblock %}
119

1210
{% block content %}

dje/templates/widgets/altcha.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script async defer src="https://eu.altcha.org/js/latest/altcha.min.js" type="module"></script>
2+
{% include "django/forms/widgets/input.html" %}
3+
<altcha-widget challengeurl="/altcha/" name="altcha_token" floating></altcha-widget>

0 commit comments

Comments
 (0)