# Django REST reCAPTCHA
**Django REST reCAPTCHA v2 and v3 field serializer**
[![CI](https://github.com/llybin/drf-recaptcha/workflows/tests/badge.svg)](https://github.com/llybin/drf-recaptcha/actions)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/a9b44d24cba74c75bca6472b2ee8da67)](https://www.codacy.com/app/llybin/drf-recaptcha?utm_source=github.com&utm_medium=referral&utm_content=llybin/drf-recaptcha&utm_campaign=Badge_Grade)
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/a9b44d24cba74c75bca6472b2ee8da67)](https://www.codacy.com/app/llybin/drf-recaptcha?utm_source=github.com&utm_medium=referral&utm_content=llybin/drf-recaptcha&utm_campaign=Badge_Coverage)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![PyPI](https://img.shields.io/pypi/v/drf-recaptcha)](https://pypi.org/project/drf-recaptcha/)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/drf-recaptcha)](https://pypi.org/project/drf-recaptcha/)
[![PyPI - License](https://img.shields.io/pypi/l/drf-recaptcha)](https://pypi.org/project/drf-recaptcha/)
## Requirements
* Python: 3.7, 3.8, 3.9, 3.10
* Django: 3.2, 4.0, 4.1
* DRF: 3.11, 3.12, 3.13, 3.14
## Installation
1. [Sign up for reCAPTCHA](https://www.google.com/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`
```python
INSTALLED_APPS = [
...,
"drf_recaptcha",
...,
]
...
DRF_RECAPTCHA_SECRET_KEY = "YOUR SECRET KEY"
```
## Usage
```python
from rest_framework.serializers import Serializer, ModelSerializer
from drf_recaptcha.fields import ReCaptchaV2Field, ReCaptchaV3Field
from feedback.models import Feedback
class V2Serializer(Serializer):
recaptcha = ReCaptchaV2Field()
...
class GetOTPView(APIView):
def post(self, request):
serializer = V2Serializer(data=request.data, context={"request": request})
serializer.is_valid(raise_exception=True)
...
class V3Serializer(Serializer):
recaptcha = ReCaptchaV3Field(action="example")
...
class V3WithScoreSerializer(Serializer):
recaptcha = ReCaptchaV3Field(
action="example",
required_score=0.6,
)
...
class GetReCaptchaScore(APIView):
def post(self, request):
serializer = V3WithScoreSerializer(data=request.data, context={"request": request})
serializer.is_valid()
score = serializer.fields['recaptcha'].score
...
class FeedbackSerializer(ModelSerializer):
recaptcha = ReCaptchaV2Field()
class Meta:
model = Feedback
fields = ("phone", "full_name", "email", "comment", "recaptcha")
def validate(self, attrs):
attrs.pop("recaptcha")
...
return attrs
```
## Settings
`DRF_RECAPTCHA_SECRET_KEY` - set your Google reCAPTCHA secret key. Type: str.
`DRF_RECAPTCHA_DEFAULT_V3_SCORE` - by default: `0.5`. Type: float.
`DRF_RECAPTCHA_ACTION_V3_SCORES` - by default: `{}`. Type: dict. You can define specific score for each action e.g. `{"login": 0.6, "feedback": 0.3}`
`DRF_RECAPTCHA_DOMAIN` - by default: `www.google.com`. Type: str.
`DRF_RECAPTCHA_PROXY` - by default: `{}`. Type: dict. e.g. `{'http': 'http://127.0.0.1:8000', 'https': 'https://127.0.0.1:8000'}`
`DRF_RECAPTCHA_VERIFY_REQUEST_TIMEOUT` - by default: `10`. Type: int.
## 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 for action in settings `DRF_RECAPTCHA_ACTION_V3_SCORES`
2. Value in argument `required_score` of field
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
[django-recaptcha](https://github.com/praekelt/django-recaptcha)
reCAPTCHA copyright 2012 Google.
Raw data
{
"_id": null,
"home_page": "https://github.com/abhay033/drf-recaptcha.git",
"name": "drf-recaptcha-new",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "django,drf,rest,django-rest-framework,reCAPTCHA,reCAPTCHA v2,reCAPTCHA v3",
"author": "Abhay Bhati",
"author_email": "abhaybhati987@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/6a/92/0ebdbe580e5c81c20b890c184502fc11b5cd822b026ae35c2d6d3bab9b5f/drf-recaptcha_new-2.2.3.tar.gz",
"platform": null,
"description": "# Django REST reCAPTCHA\n\n**Django REST reCAPTCHA v2 and v3 field serializer**\n\n[![CI](https://github.com/llybin/drf-recaptcha/workflows/tests/badge.svg)](https://github.com/llybin/drf-recaptcha/actions)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/a9b44d24cba74c75bca6472b2ee8da67)](https://www.codacy.com/app/llybin/drf-recaptcha?utm_source=github.com&utm_medium=referral&utm_content=llybin/drf-recaptcha&utm_campaign=Badge_Grade)\n[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/a9b44d24cba74c75bca6472b2ee8da67)](https://www.codacy.com/app/llybin/drf-recaptcha?utm_source=github.com&utm_medium=referral&utm_content=llybin/drf-recaptcha&utm_campaign=Badge_Coverage)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![PyPI](https://img.shields.io/pypi/v/drf-recaptcha)](https://pypi.org/project/drf-recaptcha/)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/drf-recaptcha)](https://pypi.org/project/drf-recaptcha/)\n[![PyPI - License](https://img.shields.io/pypi/l/drf-recaptcha)](https://pypi.org/project/drf-recaptcha/)\n\n## Requirements\n\n* Python: 3.7, 3.8, 3.9, 3.10\n* Django: 3.2, 4.0, 4.1\n* DRF: 3.11, 3.12, 3.13, 3.14\n\n## Installation\n\n1. [Sign up for reCAPTCHA](https://www.google.com/recaptcha/)\n2. Install with `pip install drf-recaptcha`\n3. Add `\"drf_recaptcha\"` to your `INSTALLED_APPS` settings.\n4. Set in settings `DRF_RECAPTCHA_SECRET_KEY`\n\n```python\nINSTALLED_APPS = [\n ...,\n \"drf_recaptcha\",\n ...,\n]\n\n...\n\nDRF_RECAPTCHA_SECRET_KEY = \"YOUR SECRET KEY\"\n```\n\n## Usage\n\n```python\nfrom rest_framework.serializers import Serializer, ModelSerializer\nfrom drf_recaptcha.fields import ReCaptchaV2Field, ReCaptchaV3Field\nfrom feedback.models import Feedback\n\n\nclass V2Serializer(Serializer):\n recaptcha = ReCaptchaV2Field()\n ...\n\nclass GetOTPView(APIView):\n def post(self, request):\n serializer = V2Serializer(data=request.data, context={\"request\": request})\n serializer.is_valid(raise_exception=True)\n ...\n\nclass V3Serializer(Serializer):\n recaptcha = ReCaptchaV3Field(action=\"example\")\n ...\n\nclass V3WithScoreSerializer(Serializer):\n recaptcha = ReCaptchaV3Field(\n action=\"example\",\n required_score=0.6,\n )\n ...\n\nclass GetReCaptchaScore(APIView):\n def post(self, request):\n serializer = V3WithScoreSerializer(data=request.data, context={\"request\": request})\n serializer.is_valid()\n score = serializer.fields['recaptcha'].score\n ...\n\nclass FeedbackSerializer(ModelSerializer):\n recaptcha = ReCaptchaV2Field()\n\n class Meta:\n model = Feedback\n fields = (\"phone\", \"full_name\", \"email\", \"comment\", \"recaptcha\")\n\n def validate(self, attrs):\n attrs.pop(\"recaptcha\")\n ...\n return attrs\n```\n\n## Settings\n\n`DRF_RECAPTCHA_SECRET_KEY` - set your Google reCAPTCHA secret key. Type: str.\n\n`DRF_RECAPTCHA_DEFAULT_V3_SCORE` - by default: `0.5`. Type: float.\n\n`DRF_RECAPTCHA_ACTION_V3_SCORES` - by default: `{}`. Type: dict. You can define specific score for each action e.g. `{\"login\": 0.6, \"feedback\": 0.3}`\n\n`DRF_RECAPTCHA_DOMAIN` - by default: `www.google.com`. Type: str.\n\n`DRF_RECAPTCHA_PROXY` - by default: `{}`. Type: dict. e.g. `{'http': 'http://127.0.0.1:8000', 'https': 'https://127.0.0.1:8000'}`\n\n`DRF_RECAPTCHA_VERIFY_REQUEST_TIMEOUT` - by default: `10`. Type: int.\n\n## reCAPTCHA v3\n\nValidation is passed if the score value returned by Google is greater than or equal to required score.\n\nRequired score value: `0.0 - 1.0`\n\n### Priority of score value\n\nIf not defined or zero in current item then value from next item.\n\n1. Value for action in settings `DRF_RECAPTCHA_ACTION_V3_SCORES`\n2. Value in argument `required_score` of field\n3. Default value in settings `DRF_RECAPTCHA_DEFAULT_V3_SCORE`\n4. Default value `0.5`\n\n## Testing\n\nSet `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.\n\n`DRF_RECAPTCHA_TESTING_PASS=True|False` - all responses are pass, default `True`.\n\nUse `from django.test import override_settings`\n\n## Credits\n\n[django-recaptcha](https://github.com/praekelt/django-recaptcha)\n\nreCAPTCHA copyright 2012 Google.\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Django rest framework recaptcha field serializer.",
"version": "2.2.3",
"split_keywords": [
"django",
"drf",
"rest",
"django-rest-framework",
"recaptcha",
"recaptcha v2",
"recaptcha v3"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "230d6671581e815bea6145fd48b3da804faa154f9d7270180cb8898388273c85",
"md5": "aeca8ca2f7fcd13da9dfc60c6a5ac652",
"sha256": "87a2b3a5c527c1f20a9a4783c1de7f248200cfb43f7c65549eeb3b71251c77c9"
},
"downloads": -1,
"filename": "drf_recaptcha_new-2.2.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "aeca8ca2f7fcd13da9dfc60c6a5ac652",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 8917,
"upload_time": "2023-02-03T09:45:49",
"upload_time_iso_8601": "2023-02-03T09:45:49.966928Z",
"url": "https://files.pythonhosted.org/packages/23/0d/6671581e815bea6145fd48b3da804faa154f9d7270180cb8898388273c85/drf_recaptcha_new-2.2.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6a920ebdbe580e5c81c20b890c184502fc11b5cd822b026ae35c2d6d3bab9b5f",
"md5": "4455e108dd7d05ee8871ea324f262708",
"sha256": "97ddd4d608416719bf6d0039b5be34b6bde3480d33a437fd654c1d7b5e9eda2f"
},
"downloads": -1,
"filename": "drf-recaptcha_new-2.2.3.tar.gz",
"has_sig": false,
"md5_digest": "4455e108dd7d05ee8871ea324f262708",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 9729,
"upload_time": "2023-02-03T09:45:51",
"upload_time_iso_8601": "2023-02-03T09:45:51.907637Z",
"url": "https://files.pythonhosted.org/packages/6a/92/0ebdbe580e5c81c20b890c184502fc11b5cd822b026ae35c2d6d3bab9b5f/drf-recaptcha_new-2.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-02-03 09:45:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "abhay033",
"github_project": "drf-recaptcha.git",
"lcname": "drf-recaptcha-new"
}