Skip to main content

Decode and verify Amazon Cognito JWT tokens

Repository is archived. You can utilize any stable JWT library that supports JWKS.

Note: requires Python >= 3.8, compatible with PEP-492 (async/await coroutines syntax)

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
)

Any verification failure (malformed token, bad signature, unknown key, expired token, wrong issuer or client id, unavailable JWKS) raises cognitojwt.CognitoJWTException.

The following is verified:

  • the RS256 signature against the user pool JWKS (https://cognito-idp.<region>.amazonaws.com/<userpool_id>/.well-known/jwks.json);
  • exp (skipped when testmode=True);
  • iss must be the user pool (skipped when a custom JWKS is set via AWS_COGNITO_JWKS_PATH);
  • aud (ID token) / client_id (access token), when app_client_id is passed.

The JWKS is downloaded once and cached in memory. 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.

Note: 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.

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
)

Development

pip install -e '.[test]'
pytest

Tests use locally generated keys and don't need access to AWS.

Metadata

Release files for cognitojwt 1.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cognitojwt 1.6.0
File Size Uploaded Provenance Actions
cognitojwt-1.6.0.tar.gz 12.4 kB With provenance Details for cognitojwt-1.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cognitojwt 1.6.0
File Interpreter ABI Platform Actions
cognitojwt-1.6.0-py3-none-any.whl Python 3 none any Details for cognitojwt-1.6.0-py3-none-any.whl

Total release size: 23.0 kB

Release files / cognitojwt-1.6.0.tar.gz

Download URL cognitojwt-1.6.0.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
dc2188899a8bb58762c7380b661b310ead60e96be3c0b81223018485af4c2331
BLAKE2b-256 checksum
How to use checksums
c4c5ec293f7c7da6f418da8c6791bb773348ed3443f014fd845b3e9fc163bd64
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 log

Release files / cognitojwt-1.6.0-py3-none-any.whl

Download URL cognitojwt-1.6.0-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8cb4048d36cbde27e5fd5364044d75fee1ae17099624fbdeb2a0fbc3ff1b21ea
BLAKE2b-256 checksum
How to use checksums
9f4df115186c58fb34f82deeab927e812eb583ebcd4fa2835a0aea1dae9cbafd
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 log

Security for cognitojwt 1.6.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.

Select from 2 files

Attestation for cognitojwt-1.6.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 log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page