Async JWKS key fetching, caching, and JWT verification
Project description
auth-jwks
Async JWKS key fetching, caching, and JWT verification built on httpx + PyJWT.
Why
Validating JWT tokens against JWKS endpoints requires:
- fetching keys from a discovery URL or certs endpoint
- caching keys with TTL to avoid hitting the endpoint on every request
- thread-safe refresh under asyncio (double-checked locking)
- handling key rotation (
kidlookup + automatic refresh)
auth-jwks solves this with a single async client that handles
discovery, caching, and verification in one call.
Features
- OpenID Connect discovery (
.well-known/openid-configuration) - Direct JWKS endpoint access (skip discovery with
jwks_uri) - Strict issuer cross-check against OIDC discovery document (
IssuerMismatchError) - Cloudflare Access token validation + Starlette middleware
- Async-native (
httpx), no blocking I/O - Auto-caching with configurable TTL (default 15 min)
- RS256 + ES256 algorithms
- Bearer prefix auto-stripping
- Fully typed (
py.typed)
Installation
pip install auth-jwks
# With Cloudflare Access support:
pip install auth-jwks[cloudflare]
Usage
OAuth2 / OpenID Connect
Validate ID Tokens or JWT Access Tokens against any OIDC provider (Ory Hydra, Keycloak, Auth0, etc.):
from auth_jwks import JWKS
jwks = JWKS(
discovery_url="https://your-issuer/.well-known/openid-configuration",
issuer="https://your-issuer", # recommended: cross-checked against discovery
aud="your-client-id",
)
payload = await jwks.verify_token(token)
await jwks.close()
Token verification with automatic key discovery, caching, and rotation handling diagram
Security: strict issuer validation
When issuer= is provided in discovery mode, auth-jwks cross-checks the
configured value against the issuer field in the OIDC discovery document on
every cache refresh. If they disagree, an IssuerMismatchError (a subclass of
jwt.InvalidTokenError) is raised before any keys are trusted.
This prevents silent acceptance of tokens from a misconfigured or foreign IAM
tenant — for example, if discovery_url is accidentally pointed at the wrong
environment. The configured issuer becomes the authoritative anchor for PyJWT
validation instead of whatever the discovery document returns.
from auth_jwks import JWKS, IssuerMismatchError
import jwt
jwks = JWKS(
discovery_url="https://your-issuer/.well-known/openid-configuration",
issuer="https://your-issuer",
aud="your-client-id",
)
try:
payload = await jwks.verify_token(token)
except IssuerMismatchError:
# discovery URL pointed at wrong tenant — treat as configuration error
raise
except jwt.InvalidTokenError:
# token itself is invalid
raise
Without an explicit issuer=, the library trusts the issuer from the discovery
document unconditionally and emits a WARNING log recommending the explicit
form for production deployments.
Direct JWKS Endpoint
When you already know the JWKS URI and don't need OpenID Connect discovery (e.g., AWS Cognito, Firebase, or custom providers):
from auth_jwks import JWKS
jwks = JWKS(
jwks_uri="https://your-provider.example.com/.well-known/jwks.json",
issuer="https://your-provider.example.com",
aud="your-client-id",
)
payload = await jwks.verify_token(token)
await jwks.close()
This skips the discovery step and fetches keys directly from the JWKS endpoint.
The issuer parameter is optional; when omitted, issuer claim validation is skipped.
Cloudflare Access
Validate Cloudflare Access JWT tokens and extract user identity:
from auth_jwks.cloudflare import CloudFlareTokenValidation
cfa = CloudFlareTokenValidation(aud="your-aud", team="your-team")
user = await cfa.verify_user(token) # -> User(sub, email, country)
email = await cfa.verify_email(token) # -> str
identity = await cfa.get_identity(token) # -> Identity (full CF profile)
await cfa.close()
Token validation against Cloudflare's certs endpoint with optional identity enrichment diagram
Starlette / FastAPI Middleware
Protect routes with Cloudflare Access authentication:
from auth_jwks.cloudflare import CfaAuthMiddleware, CloudFlareTokenValidation
cfa = CloudFlareTokenValidation(aud="your-aud", team="your-team")
app.add_middleware(CfaAuthMiddleware, verify_token=cfa.verify_user)
# request.state.user -> User(sub, email, country)
Request authentication flow with dev bypass and automatic token extraction diagram
Configuration
JWKS
| Parameter | Default | Description |
|---|---|---|
discovery_url |
None |
OpenID Connect discovery endpoint |
jwks_uri |
None |
Direct JWKS endpoint URL (skips discovery) |
issuer |
None |
Expected issuer. In discovery mode: cross-checked against OIDC document (recommended for production). In direct mode: used for PyJWT issuer validation. Must be a non-empty string or None. |
aud |
None |
Expected audience (skip validation if None) |
allowed_algorithms |
{"RS256", "ES256"} |
Accepted signing algorithms |
cache_ttl |
900 |
Key cache lifetime in seconds |
leeway |
30.0 |
Clock skew tolerance in seconds |
timeout |
5.0 |
HTTP request timeout in seconds |
retries |
3 |
HTTP retry count |
fix_local_url |
True |
Normalize local/Docker URLs (scheme + host replacement) |
At least one of discovery_url or jwks_uri must be provided. When both are given, jwks_uri takes precedence.
Upgrading
issuer="" is now rejected — passing an empty string for issuer raises ValueError in both discovery and direct mode. Previously an empty string was silently accepted with ambiguous results: in discovery mode it raised a ValueError internally (no-op), while in direct jwks_uri mode PyJWT would validate the iss claim against an empty string and silently reject all real tokens. Neither behaviour was intentional or useful. To skip issuer validation explicitly, pass issuer=None (the default); to validate the issuer, pass a non-empty string.
CloudFlareTokenValidation
| Parameter | Default | Description |
|---|---|---|
aud |
— | Cloudflare Access application audience tag |
team |
— | Cloudflare Access team name |
allowed_algorithms |
{"RS256", "ES256"} |
Accepted signing algorithms |
cache_ttl |
900 |
Key cache lifetime in seconds |
leeway |
30.0 |
Clock skew tolerance in seconds |
All clients must be closed after use (await client.close()).
Token methods raise jwt.InvalidTokenError on validation failure.
IssuerMismatchError (importable from auth_jwks) is a subclass of jwt.InvalidTokenError
raised when a configured issuer disagrees with the OIDC discovery document.
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
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 auth_jwks-0.4.0.tar.gz.
File metadata
- Download URL: auth_jwks-0.4.0.tar.gz
- Upload date:
- Size: 54.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f40b77e636acec0692b2ef22f626abb0854e5f0099d7ab8d291d93b9b9974f0
|
|
| MD5 |
1c7107f5f0a319363cfa234a17bb0733
|
|
| BLAKE2b-256 |
75adfdd09b25faee0ed79ed1122c80aa457b745f509c0dda1b0c67fd131d4ede
|
Provenance
The following attestation bundles were made for auth_jwks-0.4.0.tar.gz:
Publisher:
release.yml on centum/auth-jwks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
auth_jwks-0.4.0.tar.gz -
Subject digest:
5f40b77e636acec0692b2ef22f626abb0854e5f0099d7ab8d291d93b9b9974f0 - Sigstore transparency entry: 1342940470
- Sigstore integration time:
-
Permalink:
centum/auth-jwks@2dee74e85c6e8f0d768f72093914ce97af8251c6 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/centum
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2dee74e85c6e8f0d768f72093914ce97af8251c6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file auth_jwks-0.4.0-py3-none-any.whl.
File metadata
- Download URL: auth_jwks-0.4.0-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
205de54cf854ba3cc06ee214cacd10b061b11fdbeb4f9956d20813d252983993
|
|
| MD5 |
1aae7599f1a565cc175a1330a64c9ed8
|
|
| BLAKE2b-256 |
cb509289fb748e83c8b81e4e8867efcd08ead00dc3e8b038226526913be8e8a7
|
Provenance
The following attestation bundles were made for auth_jwks-0.4.0-py3-none-any.whl:
Publisher:
release.yml on centum/auth-jwks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
auth_jwks-0.4.0-py3-none-any.whl -
Subject digest:
205de54cf854ba3cc06ee214cacd10b061b11fdbeb4f9956d20813d252983993 - Sigstore transparency entry: 1342940496
- Sigstore integration time:
-
Permalink:
centum/auth-jwks@2dee74e85c6e8f0d768f72093914ce97af8251c6 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/centum
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2dee74e85c6e8f0d768f72093914ce97af8251c6 -
Trigger Event:
release
-
Statement type: