Skip to main content

Python server-side implementation of the Cap.js proof-of-work CAPTCHA protocol

Project description

capjs-server

Python server-side implementation of the Cap.js proof-of-work CAPTCHA protocol. Framework-agnostic core with optional Django integration.

Cap.js is a privacy-friendly, cookie-free CAPTCHA that uses proof-of-work challenges instead of image recognition. This package provides the server side — challenge creation, solution verification, and token validation.

Installation

pip install capjs-server

# With Django integration
pip install capjs-server[django]

Quick Start

from capjs_server import CapServer

cap = CapServer(secret_key="your-secret-key")

# Create challenge → return as JSON to the Cap.js widget
challenge = cap.create_challenge()

# Verify solutions from the widget → return as JSON
result = cap.redeem(challenge["token"], solutions)

# Validate verification token in your form handler
if cap.validate(request.POST["cap-token"]):
    process_form()

Django Integration

# settings.py (all optional — sensible defaults)
CAP_SECRET_KEY = SECRET_KEY         # defaults to Django's SECRET_KEY
CAP_CHALLENGE_COUNT = 50            # sub-challenges per solve
CAP_CHALLENGE_DIFFICULTY = 4        # target prefix length (hex chars)

# urls.py
from capjs_server.django.views import CapChallengeView, CapRedeemView

urlpatterns = [
    path("cap/challenge", CapChallengeView.as_view()),
    path("cap/redeem", CapRedeemView.as_view()),
]

# views.py
from capjs_server.django import validate_cap_token

def contact(request):
    if not validate_cap_token(request):
        return HttpResponseForbidden()
    # process form...

Configuration

Parameter Default Description
secret_key (required) HMAC signing key. Keep stable across deploys.
challenge_count 50 Sub-challenges per solve
challenge_size 32 Salt length in hex chars
challenge_difficulty 4 Target prefix length in hex chars
challenge_expiry_ms 600000 Challenge token lifetime (10 min)
token_expiry_ms 300000 Verification token lifetime (5 min)

Testing

A brute-force solver is included for writing integration tests without a browser:

from capjs_server import CapServer
from capjs_server.testing import solve

cap = CapServer(secret_key="test", challenge_count=2, challenge_difficulty=1)
challenge = cap.create_challenge()
solutions = solve(challenge["token"], challenge["challenge"])
result = cap.redeem(challenge["token"], solutions)
assert result["success"]

How It Works

All state is encoded in HMAC-signed tokens — no server-side storage. This makes it safe for multi-instance deployments (Kubernetes, Cloud Run, serverless).

  1. Challenge: Server generates a random nonce and signs it with difficulty parameters into a token
  2. Solve: The Cap.js widget finds nonces whose SHA-256 hash starts with a PRNG-derived prefix
  3. Redeem: Server verifies the HMAC, checks solutions, and issues a signed verification token
  4. Validate: Server verifies the verification token's HMAC and expiry

License

Apache-2.0 — same as Cap.js.

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

capjs_server-0.1.0.tar.gz (39.2 kB view details)

Uploaded Source

Built Distribution

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

capjs_server-0.1.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file capjs_server-0.1.0.tar.gz.

File metadata

  • Download URL: capjs_server-0.1.0.tar.gz
  • Upload date:
  • Size: 39.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for capjs_server-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2bcd5095bce27cad6de13490e58cb36934ee88f5acee1cb58922221dbcc1fb0b
MD5 d7cd6c71afff5e2dd14f5e089d8b2c20
BLAKE2b-256 4892ac10f7ef7d40dc06f71fedcdc1a15775843c5d18aae16d7b720caecaea6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for capjs_server-0.1.0.tar.gz:

Publisher: ci.yml on vshn/capjs-server

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

File details

Details for the file capjs_server-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: capjs_server-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for capjs_server-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aba2c04a325e498d24d90c6b6cd96fd2b408eca61f9ae347ffc24dcbf66368a7
MD5 5913f7fe1734c07049d49ec73724aaf8
BLAKE2b-256 635e365cb4515c85c2e53786b86bb0b6a26cac17e31a03a3f14d05b80792e13e

See more details on using hashes here.

Provenance

The following attestation bundles were made for capjs_server-0.1.0-py3-none-any.whl:

Publisher: ci.yml on vshn/capjs-server

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