Skip to main content

Django rest framework recaptcha field serializer.

Project description

Django REST reCAPTCHA

Django REST reCAPTCHA v2 and v3 field serializer

CI Codacy Badge Codacy Badge Code style: black PyPI PyPI - Downloads PyPI - License PyPI - Python Version PyPI - Django Version DRF Version

Installation

  1. Sign up for reCAPTCHA
  2. Install with pip install drf-recaptcha
  3. Add "drf_recaptcha" to your INSTALLED_APPS settings.
  4. 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.

  1. Value in argument required_score of field
  2. Value for action in settings DRF_RECAPTCHA_ACTION_V3_SCORES
  3. Default value in settings DRF_RECAPTCHA_DEFAULT_V3_SCORE
  4. 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

timeforimage.ru

django-recaptcha

reCAPTCHA copyright 2012 Google.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for drf-recaptcha, version 1.6.0
Filename, size File type Python version Upload date Hashes
Filename, size drf_recaptcha-1.6.0-py3-none-any.whl (8.2 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size drf-recaptcha-1.6.0.tar.gz (8.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page