Skip to main content

Arcaptcha integration for Django REST Framework — field, mixin, and validator.

Project description

django-arcaptcha

Arcaptcha integration for Django REST Framework. Works exactly like django-recaptcha — add one line to any serializer.

PyPI CI


Requirements

  • Python 3.10+
  • Django 4.2+
  • Django REST Framework 3.14+
  • arcaptcha-python 0.1.2+

Installation

pip install django-arcaptcha

settings.py

INSTALLED_APPS = [
    ...
    "arcaptcha",
]

# Get your keys at https://arcaptcha.ir
ARCAPTCHA_SITE_KEY   = "your_site_key"
ARCAPTCHA_SECRET_KEY = "your_secret_key"

Verify your setup:

python manage.py check --deploy

Usage

Option 1 — Field (explicit, one line)

from arcaptcha import ArcaptchaField

class LoginSerializer(serializers.Serializer):
    email    = serializers.EmailField()
    password = serializers.CharField(write_only=True)
    captcha  = ArcaptchaField()

Option 2 — Mixin (automatic injection)

from arcaptcha import ArcaptchaMixin

class LoginSerializer(ArcaptchaMixin, serializers.Serializer):
    email    = serializers.EmailField()
    password = serializers.CharField(write_only=True)
    # `captcha` field is injected automatically

Option 3 — Validator (anywhere outside a serializer)

from arcaptcha import ArcaptchaValidator

# in a view, task, or any callable
ArcaptchaValidator()(request.data["captcha"])

What the client sends

POST /api/auth/login/
{
    "email":    "alice@example.com",
    "password": "secret",
    "captcha":  "<token-from-arcaptcha-widget>"
}

Error responses

// Invalid token → HTTP 400
{"captcha": ["Invalid or expired captcha. Please try again."]}

// Service down → HTTP 400
{"captcha": ["Captcha verification is temporarily unavailable. Please try again later."]}

All settings

Setting Default Description
ARCAPTCHA_SITE_KEY Required. Your site key from arcaptcha.ir
ARCAPTCHA_SECRET_KEY Required. Your secret key from arcaptcha.ir
ARCAPTCHA_DISABLE False Skip verification — test/local only
ARCAPTCHA_LANG "en" Widget language: "en" or "fa"
ARCAPTCHA_THEME "light" Widget theme: "light" or "dark"
ARCAPTCHA_SIZE "normal" Widget size: "normal" or "compact"

Testing in your project

Option A — disable in test settings (simplest)

# settings/test.py
ARCAPTCHA_DISABLE = True

Option B — mock the verify call per test

from unittest.mock import patch

@patch("arcaptcha.validators.verify_arcaptcha", return_value=True)
def test_login(mock_verify, api_client):
    res = api_client.post("/api/auth/login/", {
        "email": "a@b.com", "password": "secret", "captcha": "any"
    })
    assert res.status_code == 200

Contributing

git clone https://github.com/rassoulshah/django-arcaptcha
cd django-arcaptcha
pip install -e ".[dev]"
pytest

License

MIT

Project details


Download files

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

Source Distribution

django_arcaptcha-1.0.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_arcaptcha-1.0.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file django_arcaptcha-1.0.0.tar.gz.

File metadata

  • Download URL: django_arcaptcha-1.0.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_arcaptcha-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b3521699ee6950b1db994801e154b86d3338844d82552ab9b99a2a26de063a92
MD5 d7414860bd33358ee486c02afecf5bcc
BLAKE2b-256 bb55504393f979e9dabc65bca6bafe0fd38b9749d2a8dede4a3b5946b9db36f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_arcaptcha-1.0.0.tar.gz:

Publisher: publish.yml on rassoulshah/django-arcaptcha

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_arcaptcha-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_arcaptcha-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c531721060deda46ff9958774f913db40111d2415fa3e40f8b9e8af7d37c1dd4
MD5 4674840f5c28a6cb9bea35e113800cd3
BLAKE2b-256 a8eb28c3286267d6dc32a2d716539aeb3de8ad5e44b4b0f4432f189564d031e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_arcaptcha-1.0.0-py3-none-any.whl:

Publisher: publish.yml on rassoulshah/django-arcaptcha

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page