Django REST CAPTCHA Validator
Django REST CAPTCHA Validator is a Django package that’s essentially just CAPTCHA suitable for Django REST framework.
Requirements
A correctly setup cache is required, as the CAPTCHA keys are stored in the cache instead of the database. Django REST CAPTCHA Validator installs all other depedencies on it’s own.
Intallation
Install via pip:
$ pip install -e git+https://github.com/Tsuribori/django_rest_captcha_validator.git#egg=rest_validator
Add rest_validator and Django Simple Captcha to your INSTALLED_APPS:
INSTALLED_APPS = [
...
'captcha',
'rest_validator',
...
]
Remember to migrate:
$ python manage.py migrate
Add entries to your urls.py:
urlpatterns = [
...
path('captcha/', include('captcha.urls')),
path('validate/', include('rest_validator.urls')),
...
]
Usage
Django REST CAPTCHA Validator provides a RestCaptchaField that can be added to a serializer:
from rest_validator.fields import RestCaptchaField
from rest_framework import serializers
from .models import Item
class ItemSerializer(serializers.ModelSerializer):
captcha_key = RestCaptchaField()
class Meta:
model = Item
fields = ('item_text', 'captcha_key')
def create(self, validated_data):
validated_data.pop('captcha_key')
instance = super().create(validated_data)
return instance
The field is used in validating human input. It’s important that the “create” method of a ModelSerializer is overridden to delete the “captcha_key” from the “validated_data” dictionary, as otherwise a TypeError occurs due to “captcha_key” not being a field on the model.
The package also provides a RestCaptchaView that is mapped to the URL given to it, in this case /validate/. A GET request to the view will generate a new CAPTCHA challenge, and return a CAPTCHA key value and an URL to the challenge image, for example:
{
"captcha_key": "e0411286a3c3f5b57d859747eb8811d3bd023b3a",
"captcha_image": "http://localhost:8000/captcha/image/e0411286a3c3f5b57d859747eb8811d3bd023b3a/"
}
A POST request to the view will accept “captcha_key” and “captcha_value” fields. The value of “captcha_value” must be the value of the solved CAPTCHA that “captcha_key” points to. On a succesful POST request with valid data the following is returned:
{
"validated": true
}
A request with an expired “captcha_key” or invalid “captcha_value” will return:
{
"non_field_errors: [
"Invalid or expired CAPTCHA"
]
}
After a CAPTCHA is succesfully validated, the “captcha_key” of the CAPTCHA in question can be used in a serializer with a RestCaptchaField to validate human input. If a “captcha_key” that is expired or not validated is used in a serializer, the following error occurs during serializer validation:
{
"captcha_key": [
"Invalid or expired CAPTCHA"
]
}
Settings
There is currently two settings associated with Django REST CAPTCHA Validator. The first is REST_VALIDATOR_CACHE_TIMEOUT. This setting, in seconds, controls how long a validated CAPTCHA persists in the cache. The default is 300 seconds. REST_VALIDATOR_SINGLE_USE controls is a validated “captcha_key” valid for only a single use or as long as the validated value exists in the cache, i.e. the duration of REST_VALIDATOR_CACHE_TIMEOUT. The default is True.
All other CAPTCHA settings are controlled by settings associated with Django Simple Captcha. List of those can be viewed in their documentation.
Metadata
Release files for captcha-rest-validator 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| captcha_rest_validator-0.1.tar.gz | 6.8 kB | Without provenance | Details for captcha_rest_validator-0.1.tar.gz |
Release files / captcha_rest_validator-0.1.tar.gz
| Download URL | captcha_rest_validator-0.1.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3817d0a2a3ca8997f125f4cc25ac7a1c4ffaf561c2205bc1abb332169d129894
|
|
BLAKE2b-256 checksum How to use checksums |
982878ebcfd58cdabbd1066524b456b742868c724caf47a77e41c81015f6c082
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.6rc1
|
Security for captcha-rest-validator 0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.