generateApiKey 0.5.0__tar.gz → 0.6.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.
Files changed (68) hide show
  1. {generateapikey-0.5.0 → generateapikey-0.6.0}/CHANGELOG.md +21 -1
  2. {generateapikey-0.5.0 → generateapikey-0.6.0}/PKG-INFO +209 -37
  3. {generateapikey-0.5.0 → generateapikey-0.6.0}/README.md +200 -34
  4. generateapikey-0.6.0/benchmarks/compare.py +129 -0
  5. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/__init__.py +19 -2
  6. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/__main__.py +22 -0
  7. generateapikey-0.6.0/generateapikey/_jwt_asym.py +137 -0
  8. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/integrations/__init__.py +21 -2
  9. generateapikey-0.6.0/generateapikey/keystore.py +229 -0
  10. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/llms-full.txt +100 -0
  11. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/mfa.py +79 -0
  12. generateapikey-0.6.0/generateapikey/scanning.py +80 -0
  13. generateapikey-0.6.0/generateapikey/scopes.py +55 -0
  14. generateapikey-0.6.0/generateapikey/tokens.py +290 -0
  15. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/utils.py +11 -0
  16. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey.egg-info/PKG-INFO +209 -37
  17. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey.egg-info/SOURCES.txt +10 -0
  18. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey.egg-info/requires.txt +19 -1
  19. {generateapikey-0.5.0 → generateapikey-0.6.0}/llms.txt +9 -1
  20. {generateapikey-0.5.0 → generateapikey-0.6.0}/pyproject.toml +7 -7
  21. {generateapikey-0.5.0 → generateapikey-0.6.0}/skills/generateapikey/SKILL.md +10 -1
  22. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/test_compat.py +10 -7
  23. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/test_docs.py +9 -3
  24. generateapikey-0.6.0/tests/test_jwt.py +205 -0
  25. generateapikey-0.6.0/tests/test_keystore.py +81 -0
  26. generateapikey-0.6.0/tests/test_mfa_extras.py +98 -0
  27. generateapikey-0.6.0/tests/test_properties.py +106 -0
  28. generateapikey-0.6.0/tests/test_scopes_scanning.py +129 -0
  29. generateapikey-0.5.0/generateapikey/tokens.py +0 -123
  30. {generateapikey-0.5.0 → generateapikey-0.6.0}/AGENTS.md +0 -0
  31. {generateapikey-0.5.0 → generateapikey-0.6.0}/CONTRIBUTING.md +0 -0
  32. {generateapikey-0.5.0 → generateapikey-0.6.0}/LICENSE +0 -0
  33. {generateapikey-0.5.0 → generateapikey-0.6.0}/MANIFEST.in +0 -0
  34. {generateapikey-0.5.0 → generateapikey-0.6.0}/SECURITY.md +0 -0
  35. {generateapikey-0.5.0 → generateapikey-0.6.0}/benchmarks/bench.py +0 -0
  36. {generateapikey-0.5.0 → generateapikey-0.6.0}/examples/__init__.py +0 -0
  37. {generateapikey-0.5.0 → generateapikey-0.6.0}/examples/django_quickstart.md +0 -0
  38. {generateapikey-0.5.0 → generateapikey-0.6.0}/examples/fastapi_app.py +0 -0
  39. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateApiKey.py +0 -0
  40. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/crypto.py +0 -0
  41. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/integrations/aiohttp.py +0 -0
  42. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/integrations/django.py +0 -0
  43. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/integrations/drf.py +0 -0
  44. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/integrations/falcon.py +0 -0
  45. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/integrations/fastapi.py +0 -0
  46. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/integrations/flask.py +0 -0
  47. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/integrations/litestar.py +0 -0
  48. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/integrations/quart.py +0 -0
  49. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/integrations/sanic.py +0 -0
  50. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/integrations/starlette.py +0 -0
  51. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/limiters.py +0 -0
  52. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/py.typed +0 -0
  53. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey/qr.py +0 -0
  54. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey.egg-info/dependency_links.txt +0 -0
  55. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey.egg-info/entry_points.txt +0 -0
  56. {generateapikey-0.5.0 → generateapikey-0.6.0}/generateapikey.egg-info/top_level.txt +0 -0
  57. {generateapikey-0.5.0 → generateapikey-0.6.0}/setup.cfg +0 -0
  58. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/conftest.py +0 -0
  59. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/test_examples.py +0 -0
  60. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/test_extras.py +0 -0
  61. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/test_generate.py +0 -0
  62. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/test_integrations_asgi.py +0 -0
  63. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/test_integrations_core.py +0 -0
  64. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/test_integrations_django.py +0 -0
  65. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/test_integrations_flask.py +0 -0
  66. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/test_integrations_more.py +0 -0
  67. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/test_mfa.py +0 -0
  68. {generateapikey-0.5.0 → generateapikey-0.6.0}/tests/test_security.py +0 -0
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.0
4
+
5
+ ### Added
6
+ - `encode_jwt` / `decode_jwt`: standard JWTs with pinned algorithms, `exp`/`nbf`/`iss`/`aud` checks and no `alg: none`. Supports HS256/384/512, and RS256/384/512 and ES256/384/512 with PEM keys via the `crypto` extra. HS* and RS*/ES* can't be mixed in one decode (algorithm-confusion guard). Interoperates with PyJWT.
7
+ - Key scopes: `has_scope`, `has_all_scopes`, `has_any_scope`, `parse_scopes`, and `APIKeyAuth(required_scopes=...)` / `authenticate(key, scopes=...)` (403 when a valid key lacks a scope).
8
+ - `generateapikey.keystore`: `KeyStore` (SQLite, standard library) with create, lookup, list, revoke, last-used and rotation, plus `rotate_api_key` for your own database.
9
+ - Leak detection: `key_pattern`, `find_keys` and `scanning.gitleaks_rule`.
10
+ - JWT: PS256/384/512, EdDSA and `decode_jwt_jwks` (JWKS verification, `kid` only selects among trusted keys).
11
+ - MFA: `steam_code` / `verify_steam_code` (Steam Guard style codes) and `google_migration_uri` (Google Authenticator import).
12
+ - Tests: Hypothesis property and fuzz tests, a chi-square uniformity test (with a modulo-bias control), coverage reporting in CI.
13
+ - `generate-api-key --agent-rules` prints a snippet for `AGENTS.md` / `CLAUDE.md` / `.cursor/rules` that tells coding agents to use this library. Agent-facing docs (`llms.txt`, the LLM guide, the skill, `context7.json`) now include a "when to use" decision table, and PyPI keywords cover agent and JWT/scopes searches.
14
+ - New animated product tour `assets/tour.svg`, shown in the README.
15
+ - `benchmarks/compare.py` and a feature and speed comparison against secrets, pyotp, itsdangerous, PyJWT, shortuuid and nanoid in the README.
16
+
17
+ ### Performance
18
+ - `generate_api_key()` is about 2× faster (5.8 → 2.8 µs): the resolved alphabet is now cached instead of rebuilt on every call.
19
+
20
+ ### Fixed
21
+ - CI: Django extras on Python 3.9, UTF-8 doc reads on Windows, import-order lint.
22
+
3
23
  ## 0.5.0
4
24
 
5
25
  ### Renamed
@@ -26,7 +46,7 @@ Fixes from an internal review. The issues below were found in this release's cod
26
46
  - **CLI secrets** can be passed via stdin (`-`) or `GENERATEAPIKEY_SECRET`, `GENERATEAPIKEY_PEPPER` and `GENERATEAPIKEY_TOTP_SECRET`, instead of argv.
27
47
  - **CI hardening:**
28
48
  - Read-only default permissions.
29
- - Publishing moved to PyPI Trusted Publishing in a separate job, triggered on `release: published`.
49
+ - The release workflow runs the full test suite and `twine check` before uploading.
30
50
  - A bandit scan runs on every push.
31
51
  - Dependabot is enabled.
32
52
  - `SECURITY.md` has been added.
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: generateapikey
3
- Version: 0.5.0
4
- Summary: Fast, secure, zero-dependency API keys and MFA for Python: keys, checksums, hashing, rotation, TOTP/HOTP, recovery codes, OTPs, magic links, plus FastAPI/Django/DRF/Flask/Starlette/Litestar/Sanic/aiohttp/Quart/Falcon integrations.
3
+ Version: 0.6.0
4
+ Summary: Zero-dependency API keys and MFA for Python: secure keys, hashing, scopes, rotation, TOTP, recovery codes, JWT, and 10 framework integrations.
5
5
  Author-email: Nuhman Pk <nuhmanpk7@gmail.com>
6
6
  License: MIT
7
7
  Project-URL: Homepage, https://github.com/nuhmanpk/generate-api-key
@@ -12,7 +12,7 @@ Project-URL: Changelog, https://github.com/nuhmanpk/generate-api-key/blob/main/C
12
12
  Project-URL: LLM docs, https://github.com/nuhmanpk/generate-api-key/blob/main/llms.txt
13
13
  Project-URL: Funding, https://github.com/sponsors/nuhmanpk
14
14
  Project-URL: Buy me a coffee, https://buymeacoffee.com/nuhmanpk
15
- Keywords: api key,api-key,token,secret,security,generator,authentication,mfa,2fa,totp,hotp,otp,recovery codes,magic link,fastapi,django,flask,starlette,litestar,django-rest-framework,sanic,aiohttp,quart,falcon
15
+ Keywords: api key,api-key,token,secret,security,generator,authentication,mfa,2fa,totp,hotp,otp,recovery codes,magic link,fastapi,django,flask,starlette,litestar,django-rest-framework,sanic,aiohttp,quart,falcon,jwt,jwks,scopes,api key authentication,api key rotation,secret scanning,password reset,one-time password,authenticator,secure random,ai agents,llm,claude,cursor,copilot,llms.txt,agent skill,vibe coding
16
16
  Classifier: Development Status :: 5 - Production/Stable
17
17
  Classifier: Intended Audience :: Developers
18
18
  Classifier: License :: OSI Approved :: MIT License
@@ -44,9 +44,11 @@ Requires-Dist: fastapi>=0.100; extra == "fastapi"
44
44
  Provides-Extra: starlette
45
45
  Requires-Dist: starlette>=0.27; extra == "starlette"
46
46
  Provides-Extra: django
47
- Requires-Dist: django>=5.2.19; extra == "django"
47
+ Requires-Dist: django>=5.2.19; python_version >= "3.10" and extra == "django"
48
+ Requires-Dist: django>=4.2.30; python_version < "3.10" and extra == "django"
48
49
  Provides-Extra: drf
49
- Requires-Dist: django>=5.2.19; extra == "drf"
50
+ Requires-Dist: django>=5.2.19; python_version >= "3.10" and extra == "drf"
51
+ Requires-Dist: django>=4.2.30; python_version < "3.10" and extra == "drf"
50
52
  Requires-Dist: djangorestframework>=3.14; extra == "drf"
51
53
  Provides-Extra: flask
52
54
  Requires-Dist: flask>=2.2; extra == "flask"
@@ -63,11 +65,15 @@ Requires-Dist: falcon>=3.1; extra == "falcon"
63
65
  Provides-Extra: crypto
64
66
  Requires-Dist: cryptography>=42; extra == "crypto"
65
67
  Provides-Extra: redis
66
- Requires-Dist: redis>=8.1.0; extra == "redis"
68
+ Requires-Dist: redis>=8.1.0; python_version >= "3.10" and extra == "redis"
69
+ Requires-Dist: redis>=5.0; python_version < "3.10" and extra == "redis"
67
70
  Provides-Extra: all
68
71
  Requires-Dist: generateapikey[aiohttp,crypto,django,drf,falcon,fastapi,flask,litestar,qr,quart,redis,sanic]; extra == "all"
69
72
  Provides-Extra: test
70
73
  Requires-Dist: pytest>=7; extra == "test"
74
+ Requires-Dist: pytest-cov>=5; extra == "test"
75
+ Requires-Dist: hypothesis>=6.100; extra == "test"
76
+ Requires-Dist: pyjwt>=2.8; extra == "test"
71
77
  Requires-Dist: httpx>=0.24; extra == "test"
72
78
  Requires-Dist: fakeredis[lua]>=2.40.0; extra == "test"
73
79
  Requires-Dist: sanic-testing>=23.3; extra == "test"
@@ -91,31 +97,38 @@ API keys with prefixes, checksums, expiry and safe storage, plus everything for
91
97
  authenticator apps (TOTP), recovery codes, email/SMS codes and magic links.<br>
92
98
  Zero dependencies, fully typed, one function call.
93
99
 
94
- [![PyPI](https://img.shields.io/pypi/v/generateapikey?logo=pypi&logoColor=white&label=pypi)](https://pypi.org/project/generateapikey/)
95
- [![Python](https://img.shields.io/pypi/pyversions/generateapikey?logo=python&logoColor=white)](https://pypi.org/project/generateapikey/)
96
- [![Downloads](https://static.pepy.tech/personalized-badge/generateapikey?period=total&units=abbreviation&left_color=grey&right_color=orange&left_text=downloads)](https://pepy.tech/project/generateapikey)
97
- [![Downloads/month](https://img.shields.io/pypi/dm/generateapikey?label=downloads%2Fmonth)](https://pypi.org/project/generateapikey/)
98
- [![License](https://img.shields.io/github/license/nuhmanpk/generate-api-key?color=yellow)](LICENSE)
100
+ **Trusted by developers** (see the live download badges below).
99
101
 
100
- [![Tests](https://github.com/nuhmanpk/generate-api-key/actions/workflows/tests.yml/badge.svg)](https://github.com/nuhmanpk/generate-api-key/actions/workflows/tests.yml)
101
- [![Upload to PIP](https://github.com/nuhmanpk/generate-api-key/actions/workflows/pypi-upload.yml/badge.svg)](https://github.com/nuhmanpk/generate-api-key/actions/workflows/pypi-upload.yml)
102
- ![Dependencies](https://img.shields.io/badge/dependencies-0-brightgreen)
103
- ![Typed](https://img.shields.io/badge/typing-typed-blue)
104
- ![Platform](https://img.shields.io/badge/platform-linux%20%7C%20macOS%20%7C%20windows-lightgrey)
102
+ [![PyPI](https://img.shields.io/pypi/v/generateapikey?logo=pypi&logoColor=white&label=pypi)](https://pypi.org/project/generateapikey/) [![Python](https://img.shields.io/pypi/pyversions/generateapikey?logo=python&logoColor=white)](https://pypi.org/project/generateapikey/) [![Downloads](https://static.pepy.tech/personalized-badge/generateapikey?period=total&units=abbreviation&left_color=grey&right_color=orange&left_text=downloads)](https://pepy.tech/project/generateapikey) [![Downloads/month](https://img.shields.io/pypi/dm/generateapikey?label=downloads%2Fmonth)](https://pypi.org/project/generateapikey/) [![License](https://img.shields.io/github/license/nuhmanpk/generate-api-key?color=yellow)](LICENSE)
105
103
 
106
- [![Stars](https://img.shields.io/github/stars/nuhmanpk/generate-api-key?logo=github)](https://github.com/nuhmanpk/generate-api-key/stargazers)
107
- [![Forks](https://img.shields.io/github/forks/nuhmanpk/generate-api-key?logo=github)](https://github.com/nuhmanpk/generate-api-key/network/members)
108
- [![Issues](https://img.shields.io/github/issues/nuhmanpk/generate-api-key?logo=github)](https://github.com/nuhmanpk/generate-api-key/issues)
109
- [![Last commit](https://img.shields.io/github/last-commit/nuhmanpk/generate-api-key)](https://github.com/nuhmanpk/generate-api-key/commits/main)
110
- ![PRs welcome](https://img.shields.io/badge/PRs-welcome-brightgreen)
104
+ [![Tests](https://github.com/nuhmanpk/generate-api-key/actions/workflows/tests.yml/badge.svg)](https://github.com/nuhmanpk/generate-api-key/actions/workflows/tests.yml) [![Upload to PIP](https://github.com/nuhmanpk/generate-api-key/actions/workflows/pypi-upload.yml/badge.svg)](https://github.com/nuhmanpk/generate-api-key/actions/workflows/pypi-upload.yml) ![Dependencies](https://img.shields.io/badge/dependencies-0-brightgreen) ![Typed](https://img.shields.io/badge/typing-typed-blue) ![Platform](https://img.shields.io/badge/platform-linux%20%7C%20macOS%20%7C%20windows-lightgrey)
111
105
 
112
- [![Sponsor](https://img.shields.io/badge/sponsor-nuhmanpk-ea4aaa?logo=githubsponsors&logoColor=white)](https://github.com/sponsors/nuhmanpk)
113
- [![Buy me a coffee](https://img.shields.io/badge/buy%20me%20a%20coffee-nuhmanpk-FFDD00?logo=buymeacoffee&logoColor=black)](https://buymeacoffee.com/nuhmanpk)
106
+ [![Stars](https://img.shields.io/github/stars/nuhmanpk/generate-api-key?logo=github)](https://github.com/nuhmanpk/generate-api-key/stargazers) [![Forks](https://img.shields.io/github/forks/nuhmanpk/generate-api-key?logo=github)](https://github.com/nuhmanpk/generate-api-key/network/members) [![Issues](https://img.shields.io/github/issues/nuhmanpk/generate-api-key?logo=github)](https://github.com/nuhmanpk/generate-api-key/issues) [![Last commit](https://img.shields.io/github/last-commit/nuhmanpk/generate-api-key)](https://github.com/nuhmanpk/generate-api-key/commits/main) ![PRs welcome](https://img.shields.io/badge/PRs-welcome-brightgreen)
107
+
108
+ [![Sponsor](https://img.shields.io/badge/sponsor-nuhmanpk-ea4aaa?logo=githubsponsors&logoColor=white)](https://github.com/sponsors/nuhmanpk) [![Buy me a coffee](https://img.shields.io/badge/buy%20me%20a%20coffee-nuhmanpk-FFDD00?logo=buymeacoffee&logoColor=black)](https://buymeacoffee.com/nuhmanpk)
114
109
 
115
110
  </div>
116
111
 
117
112
  ---
118
113
 
114
+ ## See it in action
115
+
116
+ <div align="center">
117
+
118
+ <img src="https://raw.githubusercontent.com/nuhmanpk/generate-api-key/main/assets/tour.svg" alt="Animated tour: generate a key, validate its checksum, hash it for storage, verify it in constant time, then protect logins with TOTP" width="800">
119
+
120
+ </div>
121
+
122
+ ## Built for AI coding agents
123
+
124
+ If you code with an AI assistant (Claude Code, Cursor, Copilot, Codex, Windsurf), point it at this library so it stops hand-rolling key and 2FA code. One command prints a rules snippet to paste into `AGENTS.md`, `CLAUDE.md` or `.cursor/rules`:
125
+
126
+ ```bash
127
+ generate-api-key --agent-rules >> AGENTS.md
128
+ ```
129
+
130
+ It tells the agent to use generateapikey (instead of ad-hoc `secrets`/`random`/`uuid` code) for API keys, 2FA/TOTP, recovery codes, magic links and JWTs, and to read `generate-api-key --llms` rather than guess the API. See [For AI coding agents](#for-ai-coding-agents) for the full list of files agents can read.
131
+
119
132
  ## Install
120
133
 
121
134
  ```bash
@@ -154,21 +167,103 @@ generate-api-key --hash --json # key + storage hash as JSON
154
167
  - **Works with your framework**: drop-in API key auth for FastAPI, Django, DRF, Flask, Starlette, Litestar, Sanic, aiohttp, Quart and Falcon.
155
168
  - **Zero dependencies**: standard library only, fully typed (`py.typed`). QR codes and framework adapters are optional extras.
156
169
 
170
+ ## Comparison
171
+
172
+ generateapikey bundles what usually takes several packages. ✅ = built in, ❌ = not provided by that library (you'd add another one).
173
+
174
+ | Feature | **generateapikey** | secrets | pyotp | itsdangerous | PyJWT | drf-api-key | nanoid |
175
+ |---|---|---|---|---|---|---|---|
176
+ | Secure random keys | **✅** | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ |
177
+ | Unbiased custom alphabet | **✅** | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ |
178
+ | Prefix | **✅** | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ |
179
+ | Checksum (offline typo/leak detection) | **✅** | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
180
+ | Hash + constant-time verify | **✅** | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ |
181
+ | Key expiry | **✅** | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ |
182
+ | Key / pepper rotation | **✅** | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
183
+ | Derived keys (secret + seed) | **✅** | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
184
+ | TOTP / HOTP (RFC 4226/6238) | **✅** | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ |
185
+ | otpauth:// URI + QR code | **✅** | ❌ | URI only | ❌ | ❌ | ❌ | ❌ |
186
+ | Recovery codes | **✅** | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
187
+ | Email / SMS one-time codes | **✅** | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
188
+ | Signed expiring tokens (magic links) | **✅** | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ |
189
+ | Standard JWT (HS256/384/512) | **✅** | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ |
190
+ | Asymmetric JWT (RS/PS/ES/EdDSA) + JWKS | **✅ (`crypto` extra)** | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ |
191
+ | Key scopes (`read:*` wildcards) | **✅** | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
192
+ | Built-in key store (revoke, rotate, last-used) | **✅** | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ |
193
+ | Leak-detection regex (gitleaks / secret scanning) | **✅** | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
194
+ | Steam Guard + Google Authenticator import | **✅** | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
195
+ | Encrypted secrets at rest (AES-GCM) | **✅** | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
196
+ | Brute-force attempt limiter (+ Redis) | **✅** | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
197
+ | Framework adapters | **10 frameworks** | ❌ | ❌ | ❌ | ❌ | Django / DRF | ❌ |
198
+ | CLI | **✅** | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
199
+ | Zero runtime dependencies | **✅** | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ |
200
+
201
+ Where it isn't the right tool: JWE (encrypted JWTs), JWKS fetching/caching and OAuth flows still need other libraries, and a bare `secrets.token_urlsafe(32)` is the fastest way to get an unconstrained random string.
202
+
203
+ ### Speed
204
+
205
+ Measured with [`benchmarks/compare.py`](benchmarks/compare.py) (CPython 3.11, macOS, best of 5, 20,000 calls each). Your numbers will differ; the ranking is what matters. Lower is better.
206
+
207
+ **Generate a key**
208
+
209
+ | Library / call | µs per op | ops/s | vs fastest |
210
+ |---|--:|--:|--:|
211
+ | `secrets.token_urlsafe(32)` | 2.45 | 408,327 | 1.0× |
212
+ | **generateapikey `generate_api_key()`** | **2.81** | **356,445** | **1.1×** |
213
+ | **generateapikey `generate_api_key(prefix, checksum)`** | **4.22** | **237,152** | **1.7×** |
214
+ | `nanoid.generate()` | 4.56 | 219,369 | 1.9× |
215
+ | `shortuuid.uuid()` | 5.57 | 179,691 | 2.3× |
216
+ | `secrets.choice` loop (32 chars) | 42.81 | 23,358 | 17.5× |
217
+
218
+ **Verify a stored key**
219
+
220
+ | Library / call | µs per op | ops/s | vs fastest |
221
+ |---|--:|--:|--:|
222
+ | **generateapikey `verify_api_key`** | **5.32** | **188,040** | **1.0×** |
223
+
224
+ **TOTP**
225
+
226
+ | Library / call | µs per op | ops/s | vs fastest |
227
+ |---|--:|--:|--:|
228
+ | pyotp `provisioning_uri` | 6.03 | 165,794 | 1.0× |
229
+ | **generateapikey `totp_uri`** | **8.71** | **114,837** | **1.4×** |
230
+ | pyotp `TOTP.verify` | 22.37 | 44,712 | 3.7× |
231
+ | **generateapikey `verify_totp`** | **22.64** | **44,171** | **3.8×** |
232
+
233
+ **Signed, expiring tokens**
234
+
235
+ | Library / call | µs per op | ops/s | vs fastest |
236
+ |---|--:|--:|--:|
237
+ | **generateapikey `verify_signed_token`** | **9.68** | **103,331** | **1.0×** |
238
+ | **generateapikey `generate_signed_token`** | **10.91** | **91,656** | **1.1×** |
239
+ | itsdangerous `loads` | 12.76 | 78,359 | 1.3× |
240
+ | itsdangerous `dumps` | 14.34 | 69,740 | 1.5× |
241
+ | PyJWT `encode` (HS256) | 20.82 | 48,040 | 2.2× |
242
+ | PyJWT `decode` (HS256) | 32.31 | 30,946 | 3.3× |
243
+
244
+ Reading the numbers:
245
+
246
+ - `secrets.token_urlsafe` is still a bit faster (1.1–1.7× across runs) because it only base64-encodes random bytes. It can't do custom alphabets, prefixes or checksums, and it isn't unbiased over an alphabet like base62. Against the equivalent per-character `secrets.choice` loop, generateapikey is roughly 15–25× faster, and it beats nanoid and shortuuid.
247
+ - TOTP verification matches pyotp (generateapikey also rejects malformed input and checks the adjacent window without early exit).
248
+ - Signed tokens are faster than itsdangerous and PyJWT here, and `verify_signed_token` also enforces purpose binding.
249
+
250
+ Reproduce: `pip install pyotp itsdangerous pyjwt shortuuid nanoid && python benchmarks/compare.py --markdown`.
251
+
157
252
  ## Recipes
158
253
 
159
254
  ```python
160
255
  from generateapikey import generate_api_key
161
256
 
162
- generate_api_key(length=48) # longer key
163
- generate_api_key(prefix="sk") # 'sk-gRsNbb2lQ1cZxoT3...'
164
- generate_api_key(prefix="sk_live", separator="_") # 'sk_live_gRsNbb2l...'
165
- generate_api_key(dashes=True) # 'GdBA-6DhM-M3Fv-g99C-...'
166
- generate_api_key(charset="hex", length=32) # hex only
167
- generate_api_key(charset="base58") # no look-alike 0/O/I/l
168
- generate_api_key(charset="ABC123", length=10) # your own alphabet
169
- generate_api_key(case="upper", charset="alphabetic")
170
- generate_api_key(prefix="sk", checksum=True) # self-validating key
171
- generate_api_key(expiry=60) # expires in 60 minutes
257
+ generate_api_key(length=48) # 'nEWkMHMjcqzM25J2A1LsFS8PHK9yRsCxGCXLUPIRI25GhVK9' longer key
258
+ generate_api_key(prefix="sk") # 'sk-NMebYJWi5tqZerAkCbGzmeaBl4KbqGic'
259
+ generate_api_key(prefix="myapp_live", separator="_") # 'myapp_live_wDXrsqS5mGjknqXMhr3ecGhl0b4FpyRi'
260
+ generate_api_key(dashes=True) # 'AEpx-VKcN-PGm5-hYtf-rCC7-lcY3-yxve-XbiN'
261
+ generate_api_key(charset="hex", length=32) # '907b2d87ddf40faf8785a7426d921441' hex only
262
+ generate_api_key(charset="base58") # 'RV9cXiBGScEdRcn4Nwu7py1oGRKKswys' no look-alike 0/O/I/l
263
+ generate_api_key(charset="ABC123", length=10) # 'B11322C13A' your own alphabet (short: warns about low entropy)
264
+ generate_api_key(case="upper", charset="alphabetic") # 'IYGLHELTLVFSXALTKGJKJOCJWWLBGMWZ'
265
+ generate_api_key(prefix="sk", checksum=True) # 'sk-oTP7GmTSNeTOgyDuwpEjUvsZrcmHinT60mOs7L' self-validating key
266
+ generate_api_key(expiry=60) # 'ZX06sSdK7iz4Eg5o0RKVvfQywBKQcma1' expires in 60 minutes (key.expires_at)
172
267
  ```
173
268
 
174
269
  Need many keys at once:
@@ -179,6 +274,45 @@ from generateapikey import generate_api_keys
179
274
  keys = generate_api_keys(1000, prefix="sk", length=40)
180
275
  ```
181
276
 
277
+ ## Scopes, key store and leak detection
278
+
279
+ **Scopes.** Give keys permissions and check them per route. Wildcards work (`read:*`, `*`).
280
+
281
+ ```python
282
+ from generateapikey import has_scope
283
+ from generateapikey.integrations import APIKeyAuth
284
+
285
+ has_scope(["read:*"], "read:users") # True
286
+ auth = APIKeyAuth(lookup, pepper=PEPPER, required_scopes=["read:users"]) # valid key without the scope: 403
287
+ auth.authenticate(key, scopes=["write:users"]) # per-route override
288
+ ```
289
+
290
+ **Key store.** A zero-dependency SQLite store with hashing, scopes, expiry, revoke, last-used and rotation. It plugs straight into `APIKeyAuth` and every framework adapter.
291
+
292
+ ```python
293
+ from generateapikey.keystore import KeyStore
294
+
295
+ store = KeyStore("keys.db", pepper=PEPPER)
296
+ made = store.create("ci-bot", scopes=["read:*"], prefix="sk", expiry=60 * 24 * 90) # minutes
297
+ made.key # show once; only the hash is stored
298
+ auth = APIKeyAuth(store.lookup, pepper=PEPPER)
299
+ new = store.rotate(made.id, grace_minutes=24 * 60) # the old key keeps working for 24 h
300
+ store.revoke(new.id)
301
+ ```
302
+
303
+ Use `rotate_api_key(...)` for the same logic with your own database.
304
+
305
+ **Leak detection.** Keys with a prefix and checksum are easy for secret scanners to find, and the checksum removes false positives.
306
+
307
+ ```python
308
+ from generateapikey import find_keys, key_pattern
309
+ from generateapikey.scanning import gitleaks_rule
310
+
311
+ key_pattern("sk") # regex to paste into GitHub secret scanning / trufflehog
312
+ find_keys(log_text, "sk") # only strings with a valid checksum
313
+ print(gitleaks_rule("sk", "acme-api-key")) # [[rules]] block for .gitleaks.toml
314
+ ```
315
+
182
316
  ## Storing keys safely
183
317
 
184
318
  Never store raw API keys. Store a hash, show the key to the user once, then verify on each request:
@@ -579,6 +713,37 @@ verify_signed_token(token, SECRET, purpose="login", context=request.session.sess
579
713
 
580
714
  `context` is signed but never put in the token. Bind to a session or device cookie, not an IP address, since mobile IPs change.
581
715
 
716
+ ### Standard JWTs
717
+
718
+ For interoperability with other services, `encode_jwt` / `decode_jwt` read and write standard JWTs (RFC 7519): HS256/384/512 with a shared secret, and RS/PS/ES 256-384-512 or EdDSA with PEM keys, plus JWKS verification (`pip install "generateapikey[crypto]"`). Tokens interoperate with PyJWT in both directions. The accepted algorithms are pinned by you, never taken from the token header, `alg: none` is always rejected, and HS* and RS*/ES* can't be mixed in one `decode_jwt` call (that blocks algorithm-confusion attacks).
719
+
720
+ ```python
721
+ from generateapikey import encode_jwt, decode_jwt
722
+
723
+ token = encode_jwt({"sub": "42"}, SECRET, expiry=15, issuer="myapp", audience="api")
724
+ claims = decode_jwt(token, SECRET, issuer="myapp", audience="api") # dict, or None if invalid/expired
725
+
726
+ # Asymmetric: sign with the private PEM, verify anywhere with the public PEM
727
+ token = encode_jwt({"sub": "42"}, PRIVATE_PEM, algorithm="ES256")
728
+ claims = decode_jwt(token, PUBLIC_PEM, algorithms=("ES256",))
729
+
730
+ # RS/PS 256-384-512 and EdDSA work too. Verify against a JWKS (kid only selects among keys you trust):
731
+ from generateapikey import decode_jwt_jwks
732
+
733
+ claims = decode_jwt_jwks(token, jwks, algorithms=("RS256",), audience="api")
734
+ ```
735
+
736
+ ### Steam Guard codes and Google Authenticator import
737
+
738
+ ```python
739
+ from generateapikey import google_migration_uri, steam_code, verify_steam_code
740
+
741
+ steam_code(secret) # 5-character Steam Guard style code
742
+ verify_steam_code(secret, "K7BQ2") # True/False
743
+ google_migration_uri([{"secret": secret, "name": "alice@example.com", "issuer": "MyApp"}])
744
+ # otpauth-migration:// URI: render it as a QR code so users can import into Google Authenticator
745
+ ```
746
+
582
747
  ### Encrypting TOTP secrets at rest
583
748
 
584
749
  TOTP secrets can't be hashed, because the server needs them to compute codes, so encrypt them (`pip install "generateapikey[crypto]"`):
@@ -690,6 +855,11 @@ key = await generate_api_key_async(length=48, prefix="sk")
690
855
  | `generate_recovery_codes()` / `hash_recovery_code()` / `verify_recovery_code()` | Single-use backup codes. |
691
856
  | `generate_otp()` / `hash_otp()` / `verify_otp()` | Email/SMS one-time codes (salted PBKDF2). |
692
857
  | `generate_signed_token()` / `verify_signed_token()` | Magic links, email verification, password resets. |
858
+ | `encode_jwt()` / `decode_jwt()` / `decode_jwt_jwks()` | Standard JWTs: HS*, plus RS/PS/ES and EdDSA with the `crypto` extra, and JWKS verification. Algorithms pinned by the caller. |
859
+ | `has_scope()` / `has_all_scopes()` / `has_any_scope()` / `parse_scopes()` | Key scopes with `read:*` wildcards. |
860
+ | `key_pattern()` / `find_keys()` | Regex and finder for leaked keys; `scanning.gitleaks_rule()` for gitleaks. |
861
+ | `steam_code()` / `verify_steam_code()` / `google_migration_uri()` | Steam Guard codes; Google Authenticator import URI. |
862
+ | `keystore.KeyStore` / `keystore.rotate_api_key()` | SQLite key store; grace-period rotation helper. |
693
863
  | `AttemptLimiter` | Brute-force guard for code checks. |
694
864
  | `qr.qr_svg()` / `qr.qr_png()` / `qr.qr_ascii()` | QR codes (needs the `[qr]` extra). |
695
865
  | `ApiKey` | `str` subclass with `expires_at`, `expires_in`, `expires_at_datetime`, `is_expired`, `.hash()`. |
@@ -726,7 +896,9 @@ This library is set up so coding agents (Claude Code, Cursor, Copilot, Codex and
726
896
  | [`AGENTS.md`](AGENTS.md) | Conventions for agents contributing to this repo. |
727
897
  | [`context7.json`](context7.json) | Lets agents fetch the docs through the [Context7](https://context7.com) MCP server. |
728
898
 
729
- Tip: add `Use generateapikey for API keys and MFA (run "generate-api-key --llms" for its API).` to your project's `AGENTS.md` or `CLAUDE.md`.
899
+ **Where agents should look things up** (in this order): the installed version (`generate-api-key --llms`, `generateapikey.__all__`, `help(function)`), then the guide above, then this README, then the [CHANGELOG](CHANGELOG.md) for renames and breaking changes. If a name isn't in `__all__` or the guide, it doesn't exist. Don't rely on training data: the package was renamed in 0.5.0 and has grown since.
900
+
901
+ Tip: run `generate-api-key --agent-rules >> AGENTS.md`, or add `Use generateapikey for API keys and MFA (run "generate-api-key --llms" for its API).` to your project's `AGENTS.md` or `CLAUDE.md`.
730
902
 
731
903
  ## Security notes
732
904
 
@@ -743,7 +915,8 @@ Tip: add `Use generateapikey for API keys and MFA (run "generate-api-key --llms"
743
915
 
744
916
  - [`examples/fastapi_app.py`](examples/fastapi_app.py): a complete app with API keys, TOTP 2FA (encrypted secrets, replay protection), recovery codes and session-bound magic links. CI runs it end to end.
745
917
  - [`examples/django_quickstart.md`](examples/django_quickstart.md): model, settings, views, DRF and key rotation.
746
- - [`benchmarks/bench.py`](benchmarks/bench.py): `python benchmarks/bench.py`. Key generation runs about 23× faster than a `secrets.choice` loop (≈1 µs vs 24 µs for 32 characters).
918
+ - [`benchmarks/compare.py`](benchmarks/compare.py): head-to-head against `secrets`, pyotp, itsdangerous, PyJWT, shortuuid and nanoid.
919
+ - [`benchmarks/bench.py`](benchmarks/bench.py): `python benchmarks/bench.py`. Key generation runs roughly 15–25× faster than a `secrets.choice` loop.
747
920
 
748
921
  ## Contributing
749
922
 
@@ -760,8 +933,7 @@ Tested on Python 3.9–3.14 with every framework. The core is checked with stric
760
933
 
761
934
  If this saves you time, consider supporting the project:
762
935
 
763
- <a href="https://buymeacoffee.com/nuhmanpk"><img src="https://img.shields.io/badge/Buy%20me%20a%20coffee-nuhmanpk-FFDD00?style=for-the-badge&logo=buymeacoffee&logoColor=black" alt="Buy me a coffee"></a>
764
- <a href="https://github.com/sponsors/nuhmanpk"><img src="https://img.shields.io/badge/Sponsor-nuhmanpk-ea4aaa?style=for-the-badge&logo=githubsponsors&logoColor=white" alt="GitHub Sponsors"></a>
936
+ <a href="https://buymeacoffee.com/nuhmanpk"><img src="https://img.shields.io/badge/Buy%20me%20a%20coffee-nuhmanpk-FFDD00?style=for-the-badge&logo=buymeacoffee&logoColor=black" alt="Buy me a coffee"></a> <a href="https://github.com/sponsors/nuhmanpk"><img src="https://img.shields.io/badge/Sponsor-nuhmanpk-ea4aaa?style=for-the-badge&logo=githubsponsors&logoColor=white" alt="GitHub Sponsors"></a>
765
937
 
766
938
  ## License
767
939