Registry / security / fastapi-cognito

fastapi-cognito

JSON →
library2.9.0pypypi✓ verified 85d ago

AWS Cognito JWT authentication library for FastAPI. Current version 2.9.0, release cadence irregular. Supports Cognito user pools, JWT validation, and role-based access control.

pip install fastapi-cognito
INSTALL
IMPORT
SIG · FASTAPI-COGNITO
F
fastapi-cognito
securitypythonv2.9.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CognitoAuth
from fastapi_cognito import CognitoAuth
from fastapi_cognito.cognito import CognitoAuth
Direct import from package is canonical and avoids import errors.
CognitoToken
from fastapi_cognito import CognitoToken

Minimal setup: initialize CognitoAuth with userpool_id and app_client_id, then protect routes with Depends.

from fastapi import FastAPI, Depends from fastapi_cognito import CognitoAuth, CognitoToken import os app = FastAPI() # Configure userpool_id = os.environ.get("COGNITO_USERPOOL_ID", "") app_client_id = os.environ.get("COGNITO_APP_CLIENT_ID", "") cognito_auth = CognitoAuth( userpool_id=userpool_id, app_client_id=app_client_id, client_secret=None, # Optional ) @app.get("/protected") async def protected_route(auth: CognitoToken = Depends(cognito_auth.get_current_user)): return {"message": "Authenticated", "username": auth.username}
Debug
Known issues
breakingIn v2.x, the attribute `app` (Flask app) was removed; FastAPI is now the only supported framework. Do not pass `app` parameter.
fix
Remove the `app` argument from CognitoAuth constructor.
affects: >=2.0.0
breakingIn v2.0+, `client_secret` parameter changed from required to optional. If your app client does not have a secret, omit or set to None.
fix
Set client_secret=None if not used, or pass the secret string as before.
affects: >=2.0.0
gotchaThe library expects the region to be part of the userpool_id in the format <region>_<poolId>. If using a different format, authentication will fail.
fix
Ensure userpool_id is in the form: us-east-1_xxxxxxxxx.
affects: all
Errors
Common errors & fixes
fastapi_cognito.exceptions.CognitoError: Unable to verify ID token
The region in userpool_id does not match the actual AWS region of the user pool.
fix
Verify userpool_id format: <region>_<userPoolId>. Example: us-east-1_abc123.
fastapi_cognito.exceptions.CognitoError: Invalid access token
The access token is expired or from a different app client.
fix
Ensure the token is fresh and originated from the configured app_client_id.
Upgrade
Version history
2.9.0latest on PyPI · released Apr 6, 2025
Audit
Dependencies
fastapirequiredRequired for FastAPI app integration
python-jose[cryptography]requiredRequired for JWT decoding
httpxrequiredRequired for JWKS fetch
Agent activity
18 hits · last 30 days
node
16
Amazon
1
OpenAI (training)
1
Resources
fastapi-cognito — pip install fastapi-cognito · libregistry