Decode and verify Amazon Cognito JWT tokens
Maintained. Last release 9 days ago, commits in 2 of the last 26 weeks. Weaker on commit activity and contributor spread.
pip install cognitojwtDecode 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