cognito-jwt-verifier
A utility for verifying JWTs issued by AWS Cognito.
✨ Features
- Async & non‑blocking verification using
aiohttp. - Automatic JWKS caching & key rollover.
- Validates both ID and access tokens out‑of‑the‑box.
- Zero heavy dependencies (only
aiohttp,PyJWT,cryptography).
📦 Installation
pip install cognito-jwt-verifier
🚀 Quick Start
import asyncio
from cognito_jwt_verifier import AsyncCognitoJwtVerifier
async def main():
verifier = AsyncCognitoJwtVerifier(
issuer="https://cognito-idp.us-east-2.amazonaws.com/<USER_POOL_ID>",
client_ids=["<APP_CLIENT_ID>"],
)
await verifier.init_keys() # optional warm‑up
claims = await verifier.verify_id_token("<ID_TOKEN>")
print(claims)
asyncio.run(main())
🛡️ FastAPI example
from contextlib import asynccontextmanager
from fastapi import FastAPI, Depends, HTTPException, status
from fastapi.security import OAuth2AuthorizationCodeBearer
from jwt import PyJWTError
from cognito_jwt_verifier import AsyncCognitoJwtVerifier
ISSUER = "https://cognito-idp.us-east-2.amazonaws.com/us-east-2_ae7uogn5r"
CLIENT_IDS = ["4pvqqexampleclientid"]
verifier = AsyncCognitoJwtVerifier(ISSUER, client_ids=CLIENT_IDS)
oauth2_scheme = OAuth2AuthorizationCodeBearer(
authorizationUrl=f"{ISSUER}/oauth2/authorize",
tokenUrl=f"{ISSUER}/oauth2/token",
)
@asynccontextmanager
async def lifespan(app: FastAPI):
await verifier.init_keys()
try:
yield
finally:
await verifier.close()
app = FastAPI(lifespan=lifespan)
async def get_current_user(token: str = Depends(oauth2_scheme)):
try:
return await verifier.verify_access_token(token)
except Exception as exc:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=str(exc),
headers={"WWW-Authenticate": "Bearer"},
)
@app.get("/user")
async def read_user(user: dict = Depends(get_current_user)):
return {"user": user}
📚 API at a glance
| Method | Description |
|---|---|
init_keys() |
Prefetch JWKS (optional). |
verify_id_token(token: str) |
Validate an ID token & return claims. |
verify_access_token(token: str) |
Validate an access token & return claims. |
close() |
Close the internal aiohttp session. |
If Cognito rotates its keys, the verifier fetches the new JWKS automatically.
Metadata
Release files for cognito-jwt-verifier 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cognito_jwt_verifier-0.0.4.tar.gz | 6.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cognito_jwt_verifier-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.0 kB
Release files / cognito_jwt_verifier-0.0.4.tar.gz
| Download URL | cognito_jwt_verifier-0.0.4.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
388e996dd45a3ca3f39c0098703e48518c9807aae05a03ec31a38ddb90f62857
|
|
BLAKE2b-256 checksum How to use checksums |
598701439f09dcda532b52d7f457d5ae255d3834d7e416fb12dd167e68c8b5ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Release files / cognito_jwt_verifier-0.0.4-py3-none-any.whl
| Download URL | cognito_jwt_verifier-0.0.4-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bc1e165f9ee229f143f72768a95e0435179bfa2cc14c67cd45e9c3f56aee5587
|
|
BLAKE2b-256 checksum How to use checksums |
5dc2bf40876d07ec93c6cecf59c6fe483906917dc1ca78fffb5612a3f1b8be89
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|