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) |
nonce_store |
MemoryNonceStore() |
Pluggable replay protection store |
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).
- Challenge: Server generates a random nonce and signs it with difficulty parameters into a token
- Solve: The Cap.js widget finds nonces whose SHA-256 hash starts with a PRNG-derived prefix
- Redeem: Server verifies the HMAC, checks solutions, and issues a signed verification token
- Validate: Server verifies the verification token's HMAC and expiry
Replay Protection
By default, each challenge can only be redeemed once per server process. This prevents attackers from replaying solved challenges to stockpile verification tokens.
The built-in MemoryNonceStore tracks used nonces in-memory with automatic expiry. For multi-process deployments (e.g. Kubernetes with multiple replicas), a challenge can be redeemed at most once per replica.
For strict single-use enforcement across replicas, provide a shared nonce store:
from capjs_server import CapServer, NonceStore
class RedisNonceStore:
def __init__(self, redis_client):
self.redis = redis_client
def mark_used(self, nonce: str, ttl_seconds: float) -> bool:
# SET NX returns True only if the key was newly created
return self.redis.set(f"cap:nonce:{nonce}", 1, nx=True, ex=int(ttl_seconds) + 1)
cap = CapServer(secret_key="...", nonce_store=RedisNonceStore(redis_client))
Django:
# settings.py
CAP_NONCE_STORE = RedisNonceStore(redis_client)
License
Apache-2.0 — same as Cap.js.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file capjs_server-0.2.0.tar.gz.
File metadata
- Download URL: capjs_server-0.2.0.tar.gz
- Upload date:
- Size: 42.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8bf22bdefd5ad35d08d03358f4320507f8fd784dec0f829b6c5b7aeecba74c2
|
|
| MD5 |
70d1cfa3028f7aaa45579f6cdffbc980
|
|
| BLAKE2b-256 |
9cdc3c1cc42cc58344c8aeade2f77268efb7aa2a319980cd8cfb362ac0a1340a
|
Provenance
The following attestation bundles were made for capjs_server-0.2.0.tar.gz:
Publisher:
ci.yml on vshn/capjs-server
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
capjs_server-0.2.0.tar.gz -
Subject digest:
f8bf22bdefd5ad35d08d03358f4320507f8fd784dec0f829b6c5b7aeecba74c2 - Sigstore transparency entry: 1018287661
- Sigstore integration time:
-
Permalink:
vshn/capjs-server@af72a70f774c59bd57cbe2c4bae9c8ebcb5fb48b -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/vshn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@af72a70f774c59bd57cbe2c4bae9c8ebcb5fb48b -
Trigger Event:
push
-
Statement type:
File details
Details for the file capjs_server-0.2.0-py3-none-any.whl.
File metadata
- Download URL: capjs_server-0.2.0-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20615f7c9601cf39018f60303c936560d62b1ae806db5724202011cadf94c415
|
|
| MD5 |
c8ca8a17b9929a30bd324140193661c7
|
|
| BLAKE2b-256 |
cc94c71e224b994b283c7fc70c84979590725d70697e6f89e894271d7bb718a1
|
Provenance
The following attestation bundles were made for capjs_server-0.2.0-py3-none-any.whl:
Publisher:
ci.yml on vshn/capjs-server
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
capjs_server-0.2.0-py3-none-any.whl -
Subject digest:
20615f7c9601cf39018f60303c936560d62b1ae806db5724202011cadf94c415 - Sigstore transparency entry: 1018287662
- Sigstore integration time:
-
Permalink:
vshn/capjs-server@af72a70f774c59bd57cbe2c4bae9c8ebcb5fb48b -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/vshn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@af72a70f774c59bd57cbe2c4bae9c8ebcb5fb48b -
Trigger Event:
push
-
Statement type: