Decode and verify Amazon Cognito JWT tokens
Note: requires Python >= 3.8, compatible with PEP-492 (async/await coroutines syntax)
Documentation: https://borisrozumnuk.github.io/cognitojwt/ (also as llms.txt for LLMs)
Installation
Package works in two modes: synchronous - requests as http-client and asynchronous - aiohttp as http-client. In order to avoid installing unnecessary dependencies I separated installation flow into two modes:
- Async mode -
pip install cognitojwt[async] - Sync mode -
pip install cognitojwt[sync]
Usage
import cognitojwt
id_token = '<YOUR_TOKEN_HERE>'
REGION = '**-****-*'
USERPOOL_ID = 'eu-west-1_*******'
APP_CLIENT_ID = '1p3*********'
# Sync mode
verified_claims: dict = cognitojwt.decode(
id_token,
REGION,
USERPOOL_ID,
app_client_id=APP_CLIENT_ID, # Optional
testmode=True # Disable token expiration check for testing purposes
)
# Async mode
verified_claims: dict = await cognitojwt.decode_async(
id_token,
REGION,
USERPOOL_ID,
app_client_id=APP_CLIENT_ID, # Optional
testmode=True # Disable token expiration check for testing purposes
)
It is possible to allow multiple app client ids by passing the value as a Container instance such as a list or tuple:
ALLOWED_CLIENT_IDS = ('client_one', 'client_two')
verified_claims: dict = cognitojwt.decode(
id_token,
REGION,
USERPOOL_ID,
app_client_id=ALLOWED_CLIENT_IDS,
testmode=True # Disable token expiration check for testing purposes
)
Optional keyword arguments of decode / decode_async:
token_use='id'or'access'(or both, as a tuple) accepts only that kind of token;leeway=30tolerates 30 seconds of clock skew forexpandiat.
Verifier
CognitoJWTVerifier keeps the configuration of one user pool and its own key cache,
so you don't repeat the arguments and can use several pools, each with its own JWKS:
verifier = cognitojwt.CognitoJWTVerifier(
REGION,
USERPOOL_ID,
APP_CLIENT_ID,
token_use='access', # Optional
leeway=30, # Optional
jwks_url=None, # Optional: URL or file path, e.g. of a local emulator
issuer=None, # Optional: expected "iss" when jwks_url is set
)
verified_claims: dict = verifier.verify(access_token) # Sync mode
verified_claims: dict = await verifier.verify_async(access_token) # Async mode
Errors
Any verification failure raises cognitojwt.CognitoJWTException or one of its subclasses:
| Exception | When |
|---|---|
MalformedTokenError |
the token can't be parsed (not a JWT, too large, no kid) |
InvalidSignatureError |
the signature doesn't match, or the key is not in the JWKS |
TokenExpiredError |
exp is in the past |
InvalidClaimError |
a wrong or missing exp, iat, iss, token_use, aud / client_id |
InvalidUserPoolError |
region or userpool_id is not a valid AWS value |
JWKSError |
the JWKS can't be downloaded, read or parsed |
JWKSError is a problem on the server side (e.g. answer 503); the others mean the
request should be rejected (e.g. 401):
try:
claims = cognitojwt.decode(token, REGION, USERPOOL_ID, APP_CLIENT_ID)
except cognitojwt.JWKSError:
... # 503
except cognitojwt.CognitoJWTException:
... # 401
What is verified
- the RS256 signature against the user pool JWKS (
https://cognito-idp.<region>.amazonaws.com/<userpool_id>/.well-known/jwks.json); exp: the token is rejected from itsexptime on (skipped whentestmode=True);iatmust not be in the future, with 5 minutes of tolerance (skipped whentestmode=True);issmust be the user pool (skipped when a custom JWKS is set viaAWS_COGNITO_JWKS_PATH);token_use, whentoken_useis passed;aud(ID token) /client_id(access token), whenapp_client_idis passed.
JWKS cache
- The JWKS is downloaded once per user pool and cached in memory; concurrent calls (threads or coroutines) share one download.
- When a token is signed with a key that is not in the cached JWKS (key rotation), the JWKS is downloaded again, at most once a minute.
- The cached JWKS is downloaded again after 6 hours, so removed keys stop being trusted.
If that download fails, the old keys are still used (and a warning is logged to the
cognitojwtlogger), and the download is retried a minute later. - A 4xx answer (e.g. a user pool that doesn't exist) is remembered for a minute.
- Up to 256 user pools are cached; the least recently used one is dropped first.
The limits are in cognitojwt.constants. cognitojwt.jwt_sync.get_keys.cache_clear() /
cache_info() (and the same for jwt_async.get_keys_async) clear and inspect the cache.
Private VPC and emulators
If the application is deployed inside a private vpc without internet gateway, the application will not be able to download the JWKS file.
In this case set the AWS_COGNITO_JWKS_PATH environment variable referencing the absolute or relative path of the jwks.json file
(or its URL, e.g. of a local Cognito emulator; a plain http:// URL other than localhost emits a warning).
With this variable set, region and userpool_id are not validated and iss is not checked.
To set a JWKS for one user pool only, pass jwks_url (and optionally issuer) to CognitoJWTVerifier.
Security notes
- Always pass
app_client_id. Without it a token issued for any app client of the pool is accepted. An empty value ('',[]) emits a warning, as it is usually a missing setting. - Don't take the region or the user pool id from the token you are verifying. If you serve several pools, look them up in your own list of trusted pools.
regionanduserpool_idmust look like real AWS values (eu-west-1,eu-west-1_AbC123), otherwiseInvalidUserPoolErroris raised before anything is downloaded.- Pass
token_usewhen your endpoint expects only access or only ID tokens. - Tokens larger than 128 KiB are rejected before they are parsed.
- Never use
testmode=Truein production.
Development
pip install -e '.[test]'
pytest
ruff check . && mypy # pip install ruff mypy types-requests
Tests use locally generated keys and don't need access to AWS.
Metadata
Release files for cognitojwt 1.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cognitojwt-1.7.0.tar.gz | 25.4 kB | With provenance | Details for cognitojwt-1.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cognitojwt-1.7.0-py3-none-any.whl | Python 3 | none | any | Details for cognitojwt-1.7.0-py3-none-any.whl |
Total release size: 44.8 kB
Release files / cognitojwt-1.7.0.tar.gz
| Download URL | cognitojwt-1.7.0.tar.gz |
|---|---|
| Size | 25.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2c6873cea8f5aa3a5d04b3e0f67e541f1747383d561fda4d2be49e5ce15fd489
|
|
BLAKE2b-256 checksum How to use checksums |
5897f3faea3e55da8cbdd38dc5431fa6c8eace8b31f8d65e5ccf3f73bef657a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.
Transparency logRelease files / cognitojwt-1.7.0-py3-none-any.whl
| Download URL | cognitojwt-1.7.0-py3-none-any.whl |
|---|---|
| Size | 19.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
afa2955cb7c6cdc26354ad4d9beec051268e6af3ee7722c594256256ca4e5dc5
|
|
BLAKE2b-256 checksum How to use checksums |
4fc0474eea6150fb5cb9ee3a2dc885c8d895101cafff69bed50b70e4afe9cb9f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.
Transparency logSecurity for cognitojwt 1.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for cognitojwt-1.7.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.