Skip to main content

Decode and verify Amazon Cognito JWT tokens

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, invalid region or user pool id) 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: the token is rejected from its exp time on (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, and concurrent calls 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.

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

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
)

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.
  • region and userpool_id must look like real AWS values (eu-west-1, eu-west-1_AbC123), otherwise CognitoJWTException is raised before anything is downloaded.
  • Tokens larger than 128 KiB are rejected before they are parsed.
  • Never use testmode=True in production.

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

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.1
File Size Uploaded Provenance Actions
cognitojwt-1.6.1.tar.gz 15.9 kB With provenance Details for cognitojwt-1.6.1.tar.gz

Built distribution (wheel)

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

Total release size: 28.5 kB

Release files / cognitojwt-1.6.1.tar.gz

Download URL cognitojwt-1.6.1.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
12d00db83b6d36e17487b710b812b7a2b4ff658474e0765dc22759ab6379bc2a
BLAKE2b-256 checksum
How to use checksums
163fecb5fc411184fd42de9528866f18a3c04cbb2d86479479e77b00c2f7eebf
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.1-py3-none-any.whl

Download URL cognitojwt-1.6.1-py3-none-any.whl
Size 12.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f5bd9712d3dfd70b2f28761784cf284ffe22ccd47b0ac962d5c34243cc5f9cc
BLAKE2b-256 checksum
How to use checksums
823f7c0fb797584511c24fac37d17962cc9b5b1f19e64c858fe12ebb51daca59
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.1

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