Django rest framework recaptcha field serializer.
Project description
Django REST reCAPTCHA
Django REST reCAPTCHA v2 and v3 field serializer
Installation
- Sign up for reCAPTCHA
- Install with
pip install drf-recaptcha
- Add
"drf_recaptcha"
to yourINSTALLED_APPS
settings. - Set in settings
DRF_RECAPTCHA_SECRET_KEY
INSTALLED_APPS = [
...,
"drf_recaptcha",
...,
]
...
DRF_RECAPTCHA_SECRET_KEY = "YOUR SECRET KEY"
Usage
from rest_framework.serializers import Serializer
from drf_recaptcha.fields import ReCaptchaV2Field, ReCaptchaV3Field
class V2Serializer(Serializer):
recaptcha = ReCaptchaV2Field()
...
class V3Serializer(Serializer):
recaptcha = ReCaptchaV3Field(action="example")
...
class V3WithScoreSerializer(Serializer):
recaptcha = ReCaptchaV3Field(
action="example",
required_score=0.6,
)
...
Settings
DRF_RECAPTCHA_SECRET_KEY
- must be set.
DRF_RECAPTCHA_DEFAULT_V3_SCORE
- by default: 0.5
DRF_RECAPTCHA_ACTION_V3_SCORES
- by default: {}
, you can define specific score for each action e.g. {"login": 0.6, "feedback": 0.3"}
DRF_RECAPTCHA_DOMAIN
- by default: www.google.com
DRF_RECAPTCHA_PROXY
- by default: {}
e.g. {'http': 'http://127.0.0.1:8000', 'https': 'https://127.0.0.1:8000'}
DRF_RECAPTCHA_VERIFY_REQUEST_TIMEOUT
- by default: 10
reCAPTCHA v3
Validation is passed if the score value returned by Google is greater than or equal to required score.
Required score value: 0.0 - 1.0
Priority of score value
If not defined or zero in current item then value from next item.
- Value in argument
required_score
of field - Value for action in settings
DRF_RECAPTCHA_ACTION_V3_SCORES
- Default value in settings
DRF_RECAPTCHA_DEFAULT_V3_SCORE
- Default value
0.5
Testing
Set DRF_RECAPTCHA_TESTING=True
in settings, no request to Google, no warnings, DRF_RECAPTCHA_SECRET_KEY
is not required, set returning verification result in setting below.
DRF_RECAPTCHA_TESTING_PASS=True|False
- all responses are pass, default True
.
Use from django.test import override_settings
Credits
reCAPTCHA copyright 2012 Google.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for drf_recaptcha-1.5.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0243b5c41e294e844820d8a2e7693fcfffdfca17a0ddcc02ee44e6c7ddeb64a |
|
MD5 | 9f155801967220ea0523fd6968f1399a |
|
BLAKE2b-256 | d6ca017ee68f4ecfc5ce704d3f5d6a961f2b4b8e0da94f6f3033b53943c6df29 |