fastapi-cognito
AWS Cognito JWT authentication library for FastAPI
What it is and what it does
fastapi-cognito is a FastAPI middleware library that integrates AWS Cognito JWT authentication into your application. It handles the mechanics of fetching Cognito's public keys, validating token signatures, checking expiration, and parsing token claims—letting you protect endpoints with a simple dependency injection pattern. The library supports multiple Cognito userpools, optional authentication, custom token models for extended claims, and both HTTP and WebSocket connections.
You configure it by providing your Cognito region, userpool ID, and app client ID (either in code, YAML, or JSON), then use it as a FastAPI dependency on any route. It does not handle user sign-up or sign-in; it assumes tokens are issued by Cognito and focuses entirely on validation and parsing. The package relies on fastapi, pydantic, pyyaml, aiofile, async-lru, joserfc, and httpx to manage async I/O, settings, and cryptographic verification.
Use it for:
- Protect FastAPI endpoints with Cognito JWT tokens, extracting user identity and claims for authorization logic.
- Support optional authentication on public endpoints that may also serve authenticated users with different responses.
- Validate tokens from multiple Cognito userpools in a single application, routing by userpool name.
- Extend token validation with custom token models to parse application-specific claims beyond standard Cognito fields.
- Secure WebSocket connections by validating Cognito tokens during the handshake.
- Use custom JWKS URLs for local development with cognito-local or other Cognito-compatible identity providers.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Decodes, validates, and parses AWS Cognito JWT tokens in FastAPI applications, supporting both HTTP and WebSocket authentication without handling sign-up or sign-in.
Yes, if you are building a FastAPI application backed by AWS Cognito and need straightforward JWT validation. The library is permissively licensed, has low install friction, and supports modern Python versions. However, maintenance is aging (last commit 495 days ago), so evaluate whether the package's feature set meets your needs and whether you are comfortable with slower response to issues. No known security vulnerabilities as of the query date.
Install
fastapi-cognito on PyPI
pip
pip install fastapi-cognitouv
uv add fastapi-cognitopoetry
poetry add fastapi-cognitoInstalling fastapi-cognito
Before you install
Low install friction with a pure-Python wheel. Maintenance status is aging—last commit was 495 days ago—but the repository remains active and the package supports current Python versions through 3.13.
License in practice
MIT license is permissive; you may use, modify, and distribute this package freely in commercial or private projects with minimal restrictions.
Quickstart
pip install fastapi-cognito
from fastapi import FastAPI, Depends
from fastapi_cognito import CognitoAuth, CognitoSettings, CognitoToken
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
userpools: dict = {"default": {"region": "us-east-1", "userpool_id": "...", "app_client_id": "..."}}
settings = Settings()
cognito = CognitoAuth(settings=CognitoSettings.from_global_settings(settings))
app = FastAPI()
@app.get("/")
def protected(auth: CognitoToken = Depends(cognito.auth_required)):
return {"message": "authenticated"}
Requires AWS Cognito service credentials (region, userpool ID, app client ID) and Python >=3.8.
Verify before relying
- Whether the package handles token refresh or only validates existing tokens.
- Performance characteristics when validating tokens from multiple userpools concurrently.
- Whether custom JWKS URLs work reliably with self-hosted or local Cognito implementations.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (<4.0,>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 8 — fastapi, pydantic, pydantic-settings, pyyaml, aiofile, async-lru, joserfc, httpx |
| Maintenance | aging — 495 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 85,409/month — #13,924 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: fastapi_cognito-2.9.0-py3-none-any.whl
Tags
More Security packages
Provides Python bindings to the FreeDesktop.org…
permissive · top 1,000 on PyPI
msalMSAL for Python handles OAuth2 and OpenID…
permissive · top 1,000 on PyPI
joserfcjoserfc implements JOSE standards (JWS, JWE,…
permissive · top 1,000 on PyPI
AuthlibAuthlib provides a complete implementation of…
permissive · top 1,000 on PyPI
argon2-cffi-bindingsProvides low-level CFFI bindings to the…
permissive · top 1,000 on PyPI
adalADAL for Python authenticates applications with…
permissive · top 1,000 on PyPI
cognitojwtDecodes and verifies Amazon Cognito JWT tokens…
permissive · top 15,000 on PyPI
Flask-CognitoFlask-Cognito integrates AWS Cognito JWT…
permissive · top 15,000 on PyPI
fastapi-cloudauthIntegrates FastAPI applications with cloud…
permissive · top 15,000 on PyPI
axioms-fastapiAdds OAuth2/OIDC JWT token validation and…
permissive · top 15,000 on PyPI
pycognitoSimplifies AWS Cognito user authentication and…
permissive · top 5,000 on PyPI
warrantWarrant wraps AWS Cognito user management…
permissive · top 15,000 on PyPI
fastapi-loginAdds session and token-based user…
permissive · top 15,000 on PyPI
Flask-AWSCognitoIntegrates AWS Cognito user authentication into…
permissive · top 15,000 on PyPI
spiffeProvides Python bindings to the SPIFFE Workload…
permissive · top 15,000 on PyPI
propelauth-fastapiIntegrates PropelAuth's authentication and…
permissive · top 15,000 on PyPI