captchakit 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- captchakit-0.1.0/.gitignore +70 -0
- captchakit-0.1.0/CHANGELOG.md +33 -0
- captchakit-0.1.0/LICENSE +21 -0
- captchakit-0.1.0/PKG-INFO +199 -0
- captchakit-0.1.0/README.md +145 -0
- captchakit-0.1.0/ROADMAP.md +422 -0
- captchakit-0.1.0/pyproject.toml +138 -0
- captchakit-0.1.0/src/captchakit/__init__.py +47 -0
- captchakit-0.1.0/src/captchakit/_clock.py +23 -0
- captchakit-0.1.0/src/captchakit/adapters/__init__.py +1 -0
- captchakit-0.1.0/src/captchakit/adapters/fastapi.py +143 -0
- captchakit-0.1.0/src/captchakit/challenges/__init__.py +15 -0
- captchakit-0.1.0/src/captchakit/challenges/base.py +52 -0
- captchakit-0.1.0/src/captchakit/challenges/math.py +50 -0
- captchakit-0.1.0/src/captchakit/challenges/text.py +37 -0
- captchakit-0.1.0/src/captchakit/errors.py +43 -0
- captchakit-0.1.0/src/captchakit/manager.py +91 -0
- captchakit-0.1.0/src/captchakit/py.typed +0 -0
- captchakit-0.1.0/src/captchakit/renderers/__init__.py +8 -0
- captchakit-0.1.0/src/captchakit/renderers/base.py +17 -0
- captchakit-0.1.0/src/captchakit/renderers/image.py +91 -0
- captchakit-0.1.0/src/captchakit/storage/__init__.py +8 -0
- captchakit-0.1.0/src/captchakit/storage/base.py +29 -0
- captchakit-0.1.0/src/captchakit/storage/memory.py +63 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
|
|
7
|
+
# Distribution / packaging
|
|
8
|
+
.Python
|
|
9
|
+
build/
|
|
10
|
+
develop-eggs/
|
|
11
|
+
dist/
|
|
12
|
+
downloads/
|
|
13
|
+
eggs/
|
|
14
|
+
.eggs/
|
|
15
|
+
lib64/
|
|
16
|
+
parts/
|
|
17
|
+
sdist/
|
|
18
|
+
var/
|
|
19
|
+
wheels/
|
|
20
|
+
*.egg-info/
|
|
21
|
+
*.egg
|
|
22
|
+
MANIFEST
|
|
23
|
+
|
|
24
|
+
# Virtual environments
|
|
25
|
+
.venv/
|
|
26
|
+
venv/
|
|
27
|
+
env/
|
|
28
|
+
ENV/
|
|
29
|
+
|
|
30
|
+
# uv
|
|
31
|
+
.uv/
|
|
32
|
+
# Note: uv.lock IS committed — it pins dev deps for reproducible CI builds.
|
|
33
|
+
|
|
34
|
+
# Testing / coverage
|
|
35
|
+
.tox/
|
|
36
|
+
.nox/
|
|
37
|
+
.coverage
|
|
38
|
+
.coverage.*
|
|
39
|
+
.cache
|
|
40
|
+
coverage.xml
|
|
41
|
+
*.cover
|
|
42
|
+
.hypothesis/
|
|
43
|
+
.pytest_cache/
|
|
44
|
+
htmlcov/
|
|
45
|
+
|
|
46
|
+
# Type checkers
|
|
47
|
+
.mypy_cache/
|
|
48
|
+
.dmypy.json
|
|
49
|
+
.pyre/
|
|
50
|
+
.pytype/
|
|
51
|
+
.ruff_cache/
|
|
52
|
+
|
|
53
|
+
# IDEs
|
|
54
|
+
.idea/
|
|
55
|
+
.vscode/
|
|
56
|
+
*.swp
|
|
57
|
+
*.swo
|
|
58
|
+
|
|
59
|
+
# OS
|
|
60
|
+
.DS_Store
|
|
61
|
+
Thumbs.db
|
|
62
|
+
|
|
63
|
+
# Docs
|
|
64
|
+
site/
|
|
65
|
+
|
|
66
|
+
# Project-local artifacts
|
|
67
|
+
*.png
|
|
68
|
+
*.wav
|
|
69
|
+
!src/captchakit/renderers/fonts/*.ttf
|
|
70
|
+
!tests/fixtures/*.png
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
### Planned for 0.2
|
|
11
|
+
- `aiogram` adapter (middleware + helper for chat-join verification).
|
|
12
|
+
- `RedisStorage` backend (async, native TTL via `SETEX`).
|
|
13
|
+
- `EmojiGridChallenge`.
|
|
14
|
+
- `mkdocs-material` site published to GitHub Pages.
|
|
15
|
+
|
|
16
|
+
## [0.1.0] - 2026-04-20
|
|
17
|
+
|
|
18
|
+
### Added
|
|
19
|
+
- Initial public release.
|
|
20
|
+
- Core `CaptchaManager` orchestrator with async `issue()` / `verify()` / `discard()` API.
|
|
21
|
+
- `TextChallengeFactory` (alphanumeric, visually-unambiguous charset by default).
|
|
22
|
+
- `MathChallengeFactory` (configurable operands / operators; non-negative results).
|
|
23
|
+
- `ImageRenderer` (Pillow, `asyncio.to_thread` offload, PNG output).
|
|
24
|
+
- `MemoryStorage` with attempt tracking and `cleanup_expired()`.
|
|
25
|
+
- FastAPI adapter: `captcha_router()` + `verify_captcha` dependency.
|
|
26
|
+
- Rich exception hierarchy (`CaptchaKitError` → `ChallengeError` →
|
|
27
|
+
`ChallengeNotFound` / `ChallengeExpired` / `TooManyAttempts`).
|
|
28
|
+
- Full type hints (`py.typed`), mypy `--strict` clean.
|
|
29
|
+
- Constant-time answer comparison via `hmac.compare_digest`.
|
|
30
|
+
- Cryptographically secure solution generation via `secrets`.
|
|
31
|
+
- Example: FastAPI login form (HTML + endpoint).
|
|
32
|
+
- CI: lint (ruff) + type (mypy) + tests (pytest) on Python 3.10–3.13, 3 OSes.
|
|
33
|
+
- 36 tests, 98% branch coverage.
|
captchakit-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 akerem16
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: captchakit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Async-first, fully type-hinted, minimal captcha library with adapters for aiogram, FastAPI and Discord.
|
|
5
|
+
Project-URL: Homepage, https://github.com/akerem16/captchakit
|
|
6
|
+
Project-URL: Repository, https://github.com/akerem16/captchakit
|
|
7
|
+
Project-URL: Issues, https://github.com/akerem16/captchakit/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/akerem16/captchakit/blob/main/CHANGELOG.md
|
|
9
|
+
Author-email: akerem16 <pypi@kerempy.com.tr>
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: aiogram,async,asyncio,bot,captcha,discord,fastapi,verification
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Framework :: AsyncIO
|
|
15
|
+
Classifier: Framework :: FastAPI
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
25
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
26
|
+
Classifier: Topic :: Security
|
|
27
|
+
Classifier: Typing :: Typed
|
|
28
|
+
Requires-Python: >=3.10
|
|
29
|
+
Requires-Dist: pillow<13,>=10.0
|
|
30
|
+
Provides-Extra: aiogram
|
|
31
|
+
Requires-Dist: aiogram>=3.4; extra == 'aiogram'
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: fastapi>=0.110; extra == 'dev'
|
|
34
|
+
Requires-Dist: httpx>=0.27; extra == 'dev'
|
|
35
|
+
Requires-Dist: hypothesis>=6.100; extra == 'dev'
|
|
36
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
37
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
38
|
+
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
|
|
39
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
40
|
+
Requires-Dist: python-multipart>=0.0.9; extra == 'dev'
|
|
41
|
+
Requires-Dist: ruff>=0.5; extra == 'dev'
|
|
42
|
+
Requires-Dist: uvicorn[standard]>=0.30; extra == 'dev'
|
|
43
|
+
Provides-Extra: discord
|
|
44
|
+
Requires-Dist: discord-py>=2.3; extra == 'discord'
|
|
45
|
+
Provides-Extra: docs
|
|
46
|
+
Requires-Dist: mkdocs-material>=9.5; extra == 'docs'
|
|
47
|
+
Requires-Dist: mkdocstrings[python]>=0.25; extra == 'docs'
|
|
48
|
+
Provides-Extra: fastapi
|
|
49
|
+
Requires-Dist: fastapi>=0.110; extra == 'fastapi'
|
|
50
|
+
Requires-Dist: python-multipart>=0.0.9; extra == 'fastapi'
|
|
51
|
+
Provides-Extra: redis
|
|
52
|
+
Requires-Dist: redis>=5.0; extra == 'redis'
|
|
53
|
+
Description-Content-Type: text/markdown
|
|
54
|
+
|
|
55
|
+
# captchakit
|
|
56
|
+
|
|
57
|
+
> **Async-first, fully type-hinted captcha library for Python 3.10+.**
|
|
58
|
+
> Zero runtime deps beyond Pillow. Drop-in adapters for FastAPI, aiogram and discord.py.
|
|
59
|
+
|
|
60
|
+
[](https://pypi.org/project/captchakit/)
|
|
61
|
+
[](https://pypi.org/project/captchakit/)
|
|
62
|
+
[](https://github.com/akerem16/captchakit/actions/workflows/ci.yml)
|
|
63
|
+
[](http://mypy-lang.org/)
|
|
64
|
+
[](https://github.com/astral-sh/ruff)
|
|
65
|
+
[](LICENSE)
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Why another one?
|
|
70
|
+
|
|
71
|
+
| Feature | `lepture/captcha` | `claptcha` | `multicolorcaptcha` | **captchakit** |
|
|
72
|
+
| ------------------------ | ----------------- | ---------- | ------------------- | --------------------------------- |
|
|
73
|
+
| Async API | ❌ | ❌ | ❌ | ✅ |
|
|
74
|
+
| `py.typed` + mypy strict | ⚠️ | ❌ | ❌ | ✅ |
|
|
75
|
+
| TTL & attempt tracking | ❌ | ❌ | ❌ | ✅ built-in |
|
|
76
|
+
| Pluggable storage | ❌ | ❌ | ❌ | ✅ `Protocol` |
|
|
77
|
+
| Framework adapters | ❌ | ❌ | ❌ | ✅ FastAPI · aiogram · discord.py |
|
|
78
|
+
| Runtime deps (core) | +Pillow | +Pillow | +Pillow | +Pillow |
|
|
79
|
+
|
|
80
|
+
## Install
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pip install captchakit # core
|
|
84
|
+
pip install "captchakit[fastapi]" # + FastAPI adapter
|
|
85
|
+
pip install "captchakit[aiogram]" # + aiogram adapter (coming in 0.2)
|
|
86
|
+
pip install "captchakit[discord]" # + discord.py adapter (coming in 0.3)
|
|
87
|
+
pip install "captchakit[redis]" # + Redis storage backend (coming in 0.2)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
> Adapters other than FastAPI are declared in the roadmap but not shipped in
|
|
91
|
+
> 0.1 yet. The extras install today but the adapter modules land in later
|
|
92
|
+
> releases — see [ROADMAP.md](ROADMAP.md).
|
|
93
|
+
|
|
94
|
+
## 30-second example
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
import asyncio
|
|
98
|
+
from captchakit import (
|
|
99
|
+
CaptchaManager, ImageRenderer, MemoryStorage, TextChallengeFactory,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
async def main() -> None:
|
|
103
|
+
manager = CaptchaManager(
|
|
104
|
+
factory=TextChallengeFactory(length=5),
|
|
105
|
+
renderer=ImageRenderer(),
|
|
106
|
+
storage=MemoryStorage(),
|
|
107
|
+
ttl=120.0,
|
|
108
|
+
max_attempts=3,
|
|
109
|
+
)
|
|
110
|
+
challenge_id, png_bytes = await manager.issue()
|
|
111
|
+
# ... show png_bytes to the user, receive their answer ...
|
|
112
|
+
ok = await manager.verify(challenge_id, user_input="ABCDE")
|
|
113
|
+
print("verified" if ok else "wrong answer, more attempts remain")
|
|
114
|
+
|
|
115
|
+
asyncio.run(main())
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## FastAPI in 10 lines
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from fastapi import Depends, FastAPI
|
|
122
|
+
from captchakit import CaptchaManager, ImageRenderer, MathChallengeFactory, MemoryStorage
|
|
123
|
+
from captchakit.adapters.fastapi import captcha_router, verify_captcha
|
|
124
|
+
|
|
125
|
+
manager = CaptchaManager(MathChallengeFactory(), ImageRenderer(), MemoryStorage())
|
|
126
|
+
app = FastAPI()
|
|
127
|
+
app.include_router(captcha_router(manager, prefix="/captcha"))
|
|
128
|
+
|
|
129
|
+
@app.post("/register")
|
|
130
|
+
async def register(_: None = Depends(verify_captcha(manager))) -> dict[str, bool]:
|
|
131
|
+
return {"ok": True}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Run the bundled demo:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
uv run python -m uvicorn examples.fastapi_login:app --reload
|
|
138
|
+
# open http://127.0.0.1:8000
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Design
|
|
142
|
+
|
|
143
|
+
```
|
|
144
|
+
┌─────────────────────────────────────────────────┐
|
|
145
|
+
│ Adapters (fastapi / aiogram / discord) │
|
|
146
|
+
├─────────────────────────────────────────────────┤
|
|
147
|
+
│ CaptchaManager (public facade) │
|
|
148
|
+
├──────────────┬──────────────┬───────────────────┤
|
|
149
|
+
│ Challenge │ Renderer │ Storage │
|
|
150
|
+
│ Text / Math │ Image (PIL) │ Memory / Redis │
|
|
151
|
+
└──────────────┴──────────────┴───────────────────┘
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
- **`Protocol`-based**, so you can drop in your own storage (Redis, Postgres,
|
|
155
|
+
Memcached), your own renderer (SVG, WebP, audio), or your own challenge
|
|
156
|
+
factory (wordlists, emoji grids, …).
|
|
157
|
+
- **Constant-time comparison** via `hmac.compare_digest`.
|
|
158
|
+
- **Crypto-safe randomness** via `secrets` for solution generation.
|
|
159
|
+
- **CPU-bound Pillow drawing** offloaded to a worker thread via
|
|
160
|
+
`asyncio.to_thread`, so the event loop never blocks.
|
|
161
|
+
|
|
162
|
+
## Security scope
|
|
163
|
+
|
|
164
|
+
**captchakit is a lightweight human-check**, not a bot-farm-grade security layer.
|
|
165
|
+
It is aimed at Telegram/Discord verification, simple FastAPI registration flows
|
|
166
|
+
and similar use cases where you just want to raise the cost for casual spam.
|
|
167
|
+
|
|
168
|
+
For high-value forms (login, payment) use **hCaptcha**, **Cloudflare Turnstile**
|
|
169
|
+
or **reCAPTCHA Enterprise** in addition. Combine captchakit with proper rate
|
|
170
|
+
limiting at the edge of your application.
|
|
171
|
+
|
|
172
|
+
## Roadmap
|
|
173
|
+
|
|
174
|
+
See [ROADMAP.md](ROADMAP.md) for the full development plan.
|
|
175
|
+
|
|
176
|
+
| Version | Highlights |
|
|
177
|
+
|---|---|
|
|
178
|
+
| 0.1 (MVP) | Core + Text/Math challenges + Image renderer + Memory storage + FastAPI |
|
|
179
|
+
| 0.2 | aiogram + Redis storage + EmojiGridChallenge + docs site |
|
|
180
|
+
| 0.3 | discord.py adapter + AudioChallenge (optional) + WordChallenge |
|
|
181
|
+
| 0.4 | i18n + theming + metrics hooks |
|
|
182
|
+
| 1.0 | Stable API, semver commitment |
|
|
183
|
+
|
|
184
|
+
## Contributing
|
|
185
|
+
|
|
186
|
+
Contributions welcome. Local development:
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
git clone https://github.com/akerem16/captchakit
|
|
190
|
+
cd captchakit
|
|
191
|
+
uv sync --all-extras
|
|
192
|
+
uv run ruff check .
|
|
193
|
+
uv run mypy
|
|
194
|
+
uv run pytest
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## License
|
|
198
|
+
|
|
199
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# captchakit
|
|
2
|
+
|
|
3
|
+
> **Async-first, fully type-hinted captcha library for Python 3.10+.**
|
|
4
|
+
> Zero runtime deps beyond Pillow. Drop-in adapters for FastAPI, aiogram and discord.py.
|
|
5
|
+
|
|
6
|
+
[](https://pypi.org/project/captchakit/)
|
|
7
|
+
[](https://pypi.org/project/captchakit/)
|
|
8
|
+
[](https://github.com/akerem16/captchakit/actions/workflows/ci.yml)
|
|
9
|
+
[](http://mypy-lang.org/)
|
|
10
|
+
[](https://github.com/astral-sh/ruff)
|
|
11
|
+
[](LICENSE)
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Why another one?
|
|
16
|
+
|
|
17
|
+
| Feature | `lepture/captcha` | `claptcha` | `multicolorcaptcha` | **captchakit** |
|
|
18
|
+
| ------------------------ | ----------------- | ---------- | ------------------- | --------------------------------- |
|
|
19
|
+
| Async API | ❌ | ❌ | ❌ | ✅ |
|
|
20
|
+
| `py.typed` + mypy strict | ⚠️ | ❌ | ❌ | ✅ |
|
|
21
|
+
| TTL & attempt tracking | ❌ | ❌ | ❌ | ✅ built-in |
|
|
22
|
+
| Pluggable storage | ❌ | ❌ | ❌ | ✅ `Protocol` |
|
|
23
|
+
| Framework adapters | ❌ | ❌ | ❌ | ✅ FastAPI · aiogram · discord.py |
|
|
24
|
+
| Runtime deps (core) | +Pillow | +Pillow | +Pillow | +Pillow |
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install captchakit # core
|
|
30
|
+
pip install "captchakit[fastapi]" # + FastAPI adapter
|
|
31
|
+
pip install "captchakit[aiogram]" # + aiogram adapter (coming in 0.2)
|
|
32
|
+
pip install "captchakit[discord]" # + discord.py adapter (coming in 0.3)
|
|
33
|
+
pip install "captchakit[redis]" # + Redis storage backend (coming in 0.2)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
> Adapters other than FastAPI are declared in the roadmap but not shipped in
|
|
37
|
+
> 0.1 yet. The extras install today but the adapter modules land in later
|
|
38
|
+
> releases — see [ROADMAP.md](ROADMAP.md).
|
|
39
|
+
|
|
40
|
+
## 30-second example
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
import asyncio
|
|
44
|
+
from captchakit import (
|
|
45
|
+
CaptchaManager, ImageRenderer, MemoryStorage, TextChallengeFactory,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
async def main() -> None:
|
|
49
|
+
manager = CaptchaManager(
|
|
50
|
+
factory=TextChallengeFactory(length=5),
|
|
51
|
+
renderer=ImageRenderer(),
|
|
52
|
+
storage=MemoryStorage(),
|
|
53
|
+
ttl=120.0,
|
|
54
|
+
max_attempts=3,
|
|
55
|
+
)
|
|
56
|
+
challenge_id, png_bytes = await manager.issue()
|
|
57
|
+
# ... show png_bytes to the user, receive their answer ...
|
|
58
|
+
ok = await manager.verify(challenge_id, user_input="ABCDE")
|
|
59
|
+
print("verified" if ok else "wrong answer, more attempts remain")
|
|
60
|
+
|
|
61
|
+
asyncio.run(main())
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## FastAPI in 10 lines
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from fastapi import Depends, FastAPI
|
|
68
|
+
from captchakit import CaptchaManager, ImageRenderer, MathChallengeFactory, MemoryStorage
|
|
69
|
+
from captchakit.adapters.fastapi import captcha_router, verify_captcha
|
|
70
|
+
|
|
71
|
+
manager = CaptchaManager(MathChallengeFactory(), ImageRenderer(), MemoryStorage())
|
|
72
|
+
app = FastAPI()
|
|
73
|
+
app.include_router(captcha_router(manager, prefix="/captcha"))
|
|
74
|
+
|
|
75
|
+
@app.post("/register")
|
|
76
|
+
async def register(_: None = Depends(verify_captcha(manager))) -> dict[str, bool]:
|
|
77
|
+
return {"ok": True}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Run the bundled demo:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
uv run python -m uvicorn examples.fastapi_login:app --reload
|
|
84
|
+
# open http://127.0.0.1:8000
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Design
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
┌─────────────────────────────────────────────────┐
|
|
91
|
+
│ Adapters (fastapi / aiogram / discord) │
|
|
92
|
+
├─────────────────────────────────────────────────┤
|
|
93
|
+
│ CaptchaManager (public facade) │
|
|
94
|
+
├──────────────┬──────────────┬───────────────────┤
|
|
95
|
+
│ Challenge │ Renderer │ Storage │
|
|
96
|
+
│ Text / Math │ Image (PIL) │ Memory / Redis │
|
|
97
|
+
└──────────────┴──────────────┴───────────────────┘
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
- **`Protocol`-based**, so you can drop in your own storage (Redis, Postgres,
|
|
101
|
+
Memcached), your own renderer (SVG, WebP, audio), or your own challenge
|
|
102
|
+
factory (wordlists, emoji grids, …).
|
|
103
|
+
- **Constant-time comparison** via `hmac.compare_digest`.
|
|
104
|
+
- **Crypto-safe randomness** via `secrets` for solution generation.
|
|
105
|
+
- **CPU-bound Pillow drawing** offloaded to a worker thread via
|
|
106
|
+
`asyncio.to_thread`, so the event loop never blocks.
|
|
107
|
+
|
|
108
|
+
## Security scope
|
|
109
|
+
|
|
110
|
+
**captchakit is a lightweight human-check**, not a bot-farm-grade security layer.
|
|
111
|
+
It is aimed at Telegram/Discord verification, simple FastAPI registration flows
|
|
112
|
+
and similar use cases where you just want to raise the cost for casual spam.
|
|
113
|
+
|
|
114
|
+
For high-value forms (login, payment) use **hCaptcha**, **Cloudflare Turnstile**
|
|
115
|
+
or **reCAPTCHA Enterprise** in addition. Combine captchakit with proper rate
|
|
116
|
+
limiting at the edge of your application.
|
|
117
|
+
|
|
118
|
+
## Roadmap
|
|
119
|
+
|
|
120
|
+
See [ROADMAP.md](ROADMAP.md) for the full development plan.
|
|
121
|
+
|
|
122
|
+
| Version | Highlights |
|
|
123
|
+
|---|---|
|
|
124
|
+
| 0.1 (MVP) | Core + Text/Math challenges + Image renderer + Memory storage + FastAPI |
|
|
125
|
+
| 0.2 | aiogram + Redis storage + EmojiGridChallenge + docs site |
|
|
126
|
+
| 0.3 | discord.py adapter + AudioChallenge (optional) + WordChallenge |
|
|
127
|
+
| 0.4 | i18n + theming + metrics hooks |
|
|
128
|
+
| 1.0 | Stable API, semver commitment |
|
|
129
|
+
|
|
130
|
+
## Contributing
|
|
131
|
+
|
|
132
|
+
Contributions welcome. Local development:
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
git clone https://github.com/akerem16/captchakit
|
|
136
|
+
cd captchakit
|
|
137
|
+
uv sync --all-extras
|
|
138
|
+
uv run ruff check .
|
|
139
|
+
uv run mypy
|
|
140
|
+
uv run pytest
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
MIT — see [LICENSE](LICENSE).
|