django-powcaptcha 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ import django
2
+ from django.conf import settings
3
+ from django.core.exceptions import ImproperlyConfigured
4
+
5
+ SETTINGS_TYPES = {
6
+ 'POWCAPTCHA_API_URL': str,
7
+ 'POWCAPTCHA_API_KEY': str,
8
+ }
9
+
10
+ # Validate settings types.
11
+ for variable, instance_type in SETTINGS_TYPES.items():
12
+ if hasattr(settings, variable) and not isinstance(
13
+ getattr(settings, variable), instance_type
14
+ ):
15
+ raise ImproperlyConfigured(
16
+ 'Setting %s is not of type' % variable, instance_type
17
+ )
18
+
19
+ if django.VERSION < (3, 2):
20
+ default_app_config = 'django_recaptcha.apps.DjangoRecaptchaConfig'
@@ -0,0 +1,6 @@
1
+ from django.apps import AppConfig
2
+
3
+
4
+ class DjangoRecaptchaConfig(AppConfig):
5
+ name = 'django_powcaptcha'
6
+ verbose_name = 'Django PowCaptcha'
@@ -0,0 +1,49 @@
1
+ import json
2
+ from urllib.error import HTTPError
3
+ from urllib.request import Request, build_opener
4
+
5
+ from django.conf import settings
6
+
7
+
8
+ class PowCaptchaValidationException(Exception):
9
+ pass
10
+
11
+
12
+ def powcaptcha_request(path, params):
13
+ domain = getattr(settings, 'POWCAPTCHA_API_URL')
14
+ token = getattr(settings, 'POWCAPTCHA_API_TOKEN')
15
+
16
+ request_object = Request(
17
+ url=f'https://{domain}/{path}',
18
+ data=params,
19
+ headers={
20
+ 'Authorization': f'Bearer {token}',
21
+ },
22
+ )
23
+
24
+ opener_args = []
25
+ opener = build_opener(*opener_args)
26
+
27
+ return opener.open(
28
+ request_object,
29
+ timeout=5
30
+ )
31
+
32
+
33
+ def get_challenge():
34
+ path = 'GetChallenges?difficultyLevel=5'
35
+ response = powcaptcha_request(path, [])
36
+ challenges = json.loads(response.read().decode('utf-8'))
37
+
38
+ # @Todo: implement caching
39
+
40
+ return challenges[0]
41
+
42
+
43
+ def validate_captcha(challenge: str, nonce: str):
44
+ path = f'Verify?challenge={challenge}&nonce={nonce}'
45
+
46
+ try:
47
+ powcaptcha_request(path, [])
48
+ except HTTPError as error:
49
+ raise PowCaptchaValidationException(error.code)
@@ -0,0 +1,42 @@
1
+ import logging
2
+
3
+ from django import forms
4
+ from django.conf import settings
5
+ from django.core.exceptions import ValidationError
6
+ from django.utils.translation import gettext_lazy as _
7
+
8
+ from django_powcaptcha import client
9
+ from django_powcaptcha.client import PowCaptchaValidationException
10
+ from django_powcaptcha.widgets import PowCaptchaWidget
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+
15
+ class PowCaptchaField(forms.CharField):
16
+ widget = PowCaptchaWidget
17
+ default_error_messages = {
18
+ 'captcha_invalid': _('Error verifying reCAPTCHA, please try again.'),
19
+ 'captcha_error': _('Error verifying reCAPTCHA, please try again.'),
20
+ }
21
+
22
+ def __init__(self, api_key=None, api_url=None, *args, **kwargs):
23
+ super().__init__(*args, **kwargs)
24
+
25
+ self.required = True
26
+
27
+ # Setup instance variables.
28
+ self.api_key = api_key or getattr(settings, 'POWCAPTCHA_API_KEY')
29
+ self.api_url = api_url or getattr(settings, 'POWCAPTCHA_API_URL')
30
+
31
+ def validate(self, value):
32
+ super().validate(value)
33
+
34
+ try:
35
+ client.validate_captcha(
36
+ challenge='',
37
+ nonce='',
38
+ )
39
+ except PowCaptchaValidationException:
40
+ raise ValidationError(
41
+ self.error_messages['captcha_error'], code='captcha_error'
42
+ )
@@ -0,0 +1,27 @@
1
+ from django.conf import settings
2
+ from django.forms import widgets
3
+
4
+ from django_powcaptcha.client import get_challenge
5
+
6
+
7
+ class PowCaptchaWidget(widgets.Widget):
8
+ input_type = 'hidden'
9
+ template_name = 'django_recaptcha/widget.html'
10
+
11
+ def get_context(self, name, value, attrs):
12
+ context = super().get_context(name, value, attrs)
13
+ context.update(
14
+ {
15
+ 'captcha_url': settings.POWCAPTCHA_API_URL,
16
+ 'captcha_challenge': get_challenge(),
17
+ 'captcha_callback': 'myCaptchaCallback',
18
+ }
19
+ )
20
+ return context
21
+
22
+ # def build_attrs(self, base_attrs, extra_attrs=None):
23
+ # attrs = super().build_attrs(base_attrs, extra_attrs)
24
+ # attrs['data-sqr-captcha-url'] = settings.POWCAPTCHA_API_URL
25
+ # attrs['data-sqr-captcha-challenge'] = self.get_challenge()
26
+ # attrs['data-sqr-captcha-callback'] = 'myCaptchaCallback'
27
+ # return attrs
@@ -0,0 +1,69 @@
1
+ Metadata-Version: 2.1
2
+ Name: django-powcaptcha
3
+ Version: 0.0.1
4
+ Summary: Django PowCaptcha form field/widget app.
5
+ Home-page: https://github.com/aeyoll/django-powcaptcha
6
+ Author: Jean-Philippe Bidegain
7
+ Author-email: jp@bidega.in
8
+ License: BSD
9
+ Project-URL: Changelog, https://github.com/aeyoll/django-powcaptcha/blob/main/CHANGELOG.md
10
+ Project-URL: Issue Tracker, https://github.com/aeyoll/django-powcaptcha/issues
11
+ Keywords: django,powcaptcha
12
+ Classifier: Development Status :: 5 - Production/Stable
13
+ Classifier: Framework :: Django
14
+ Classifier: Framework :: Django :: 3.2
15
+ Classifier: Framework :: Django :: 4.1
16
+ Classifier: Framework :: Django :: 4.2
17
+ Classifier: Intended Audience :: Developers
18
+ Classifier: License :: OSI Approved :: BSD License
19
+ Classifier: Operating System :: OS Independent
20
+ Classifier: Programming Language :: Python
21
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
22
+ Classifier: Programming Language :: Python :: 3
23
+ Classifier: Programming Language :: Python :: 3.9
24
+ Classifier: Programming Language :: Python :: 3.10
25
+ Classifier: Programming Language :: Python :: 3.11
26
+ Description-Content-Type: text/markdown
27
+ Requires-Dist: django
28
+
29
+ # Django PowCaptcha
30
+
31
+ Django PowCaptcha form field/widget integration app.
32
+
33
+ ## Installation
34
+
35
+ 1. Install with `pip install django-powcaptcha`.
36
+
37
+ 2. Add `'django_powcaptcha'` to your `INSTALLED_APPS` setting.
38
+
39
+ ```python
40
+ INSTALLED_APPS = [
41
+ ...,
42
+ 'django_powcaptcha',
43
+ ...
44
+ ]
45
+ ```
46
+
47
+ 3. Add settings.
48
+
49
+ For example:
50
+
51
+ ```python
52
+ POWCAPTCHA_API_URL = 'https://captcha.yourdomain.com'
53
+ POWCAPTCHA_API_KEY = 'MyPOWCAPTCHAPrivateKey456'
54
+ ```
55
+
56
+ ## Usage
57
+
58
+ ### Fields
59
+
60
+ The quickest way to add PowCaptcha to a form is to use the included
61
+ `PowCaptchaField` field class. For example:
62
+
63
+ ```python
64
+ from django import forms
65
+ from django_powcaptcha.fields import PowCaptchaField
66
+
67
+ class FormWithCaptcha(forms.Form):
68
+ captcha = PowCaptchaField()
69
+ ```
@@ -0,0 +1,9 @@
1
+ django_powcaptcha/__init__.py,sha256=NL4ZCraIf4jIHz9kezy_N7qDJWeuUyy00B666kCRoCY,595
2
+ django_powcaptcha/apps.py,sha256=ShxcNSD7Dl2bnnzb6DCnJ4UcsxxVbXlmCGf9aFKUVz4,146
3
+ django_powcaptcha/client.py,sha256=aD3LW4jRrhSorNHuj5qYN0HSx_2O2oTdIy2OLW73nFQ,1120
4
+ django_powcaptcha/fields.py,sha256=jeFFiT9Eqe-CWUCRsOqjo1QbalrtpliOMPrZXFfkfBo,1323
5
+ django_powcaptcha/widgets.py,sha256=WzInlPFlZgQ8yM2tfn6e4zf0jiliSy0fMklXJtsB1Sc,951
6
+ django_powcaptcha-0.0.1.dist-info/METADATA,sha256=VMAKqNc9ClY3bwzlRQJKjf0NPxSJvDm_TUrSoher8DE,1878
7
+ django_powcaptcha-0.0.1.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
8
+ django_powcaptcha-0.0.1.dist-info/top_level.txt,sha256=aEMCJ3_ll_timVB-47n6gL3LGDhHHPkdkv3lnvaJBNQ,18
9
+ django_powcaptcha-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.42.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ django_powcaptcha