|
1 | 1 | # |
2 | 2 | # Copyright (c) nexB Inc. and others. All rights reserved. |
3 | | -# DejaCode is a trademark of nexB Inc. |
4 | | -# SPDX-License-Identifier: AGPL-3.0-only |
5 | | -# See https://github.com/aboutcode-org/dejacode for support or download. |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +# See https://github.com/aboutcode-org/django-altcha for support or download. |
6 | 5 | # See https://aboutcode.org for more information about AboutCode FOSS projects. |
7 | 6 | # |
8 | 7 |
|
| 8 | +import json |
| 9 | +import secrets |
9 | 10 |
|
10 | 11 | from django import forms |
| 12 | +from django.conf import settings |
11 | 13 | from django.forms.widgets import HiddenInput |
12 | 14 | from django.utils.translation import gettext_lazy as _ |
13 | 15 |
|
14 | 16 | # TODO: Add as a dependency |
15 | 17 | import altcha |
16 | | -from altcha import ChallengeOptions |
17 | | -from altcha import create_challenge |
18 | 18 |
|
19 | | -ALTCHA_HMAC_KEY = "your-altcha-hmac-key" |
| 19 | +""" |
| 20 | +1. Add "to INSTALLED_APPS: |
| 21 | +
|
| 22 | +INSTALLED_APPS = [ |
| 23 | + # ... |
| 24 | + "django_altcha" |
| 25 | +] |
| 26 | +
|
| 27 | +2. Add the field on your forms: |
| 28 | +
|
| 29 | +from django_altcha import AltchaField |
| 30 | +
|
| 31 | +class Form(forms.Form): |
| 32 | + captcha = AltchaField() |
| 33 | +
|
| 34 | +
|
| 35 | +You can provide any configuration options available at |
| 36 | +https://altcha.org/docs/website-integration/ such as: |
| 37 | +
|
| 38 | +class Form(forms.Form): |
| 39 | + captcha = AltchaField( |
| 40 | + floating=True, |
| 41 | + debug=True, |
| 42 | + # ... |
| 43 | + ) |
| 44 | +""" |
| 45 | + |
| 46 | +# Get the ALTCHA_HMAC_KEY from the settings, or generate one if not present |
| 47 | +ALTCHA_HMAC_KEY = getattr(settings, "ALTCHA_HMAC_KEY", secrets.token_hex(32)) |
| 48 | + |
| 49 | + |
| 50 | +def get_altcha_challenge(): |
| 51 | + """Return an ALTCHA challenge.""" |
| 52 | + challenge = altcha.create_challenge( |
| 53 | + altcha.ChallengeOptions( |
| 54 | + hmac_key=ALTCHA_HMAC_KEY, |
| 55 | + max_number=50000, |
| 56 | + ) |
| 57 | + ) |
| 58 | + return challenge |
20 | 59 |
|
21 | 60 |
|
22 | 61 | class AltchaWidget(HiddenInput): |
23 | | - template_name = "widgets/altcha.html" |
| 62 | + template_name = "altcha_widget.html" |
24 | 63 | default_options = { |
25 | 64 | # Required: URL of your server to fetch the challenge from. |
26 | 65 | "challengeurl": None, |
@@ -91,8 +130,6 @@ class AltchaField(forms.Field): |
91 | 130 | # TODO: This is only called once on Form declaration. |
92 | 131 | # Making the challenge always the same. |
93 | 132 | def __init__(self, *args, **kwargs): |
94 | | - import json |
95 | | - |
96 | 133 | challengeurl = kwargs.pop("challengeurl", None) |
97 | 134 | challengejson = kwargs.pop("challengejson", None) |
98 | 135 |
|
@@ -134,14 +171,3 @@ def validate(self, value): |
134 | 171 |
|
135 | 172 | if not verified: |
136 | 173 | raise forms.ValidationError(self.error_messages["invalid"], code="invalid") |
137 | | - |
138 | | - |
139 | | -def get_altcha_challenge(): |
140 | | - # Create the challenge using your options |
141 | | - challenge = create_challenge( |
142 | | - ChallengeOptions( |
143 | | - hmac_key=ALTCHA_HMAC_KEY, |
144 | | - max_number=50000, |
145 | | - ) |
146 | | - ) |
147 | | - return challenge |
|
0 commit comments