aws-cognito-validation
Validación de JWT de Amazon Cognito contra uno o varios User Pools (multi-pool). Útil cuando distintos frontends/servicios loguean usuarios en distintos User Pools (ej. login nativo vs. login federado con un identity provider externo como Google) y un mismo backend necesita aceptar tokens de cualquiera de esos pools.
Instalación
pip install aws-cognito-validation
Uso
from aws_cognito_validation import (
parse_allowed_pools,
pool_for_issuer,
get_unverified_issuer,
validate_cognito_jwt,
)
# COGNITO_ALLOWED_POOLS como variable de entorno (JSON estándar):
# COGNITO_ALLOWED_POOLS=["us-east-1_AAAAAAAAA:us-east-1", "us-east-1_BBBBBBBBB:us-east-1"]
pools = parse_allowed_pools(os.environ["COGNITO_ALLOWED_POOLS"])
def get_current_token(token: str) -> dict:
issuer = get_unverified_issuer(token)
pool = pool_for_issuer(issuer, pools)
if pool is None:
raise PermissionError("Issuer no permitido")
is_valid, message, decoded = validate_cognito_jwt(token, pool)
if not is_valid:
raise PermissionError(message)
return decoded
También se soporta la forma compacta separada por comas, sin JSON:
COGNITO_ALLOWED_POOLS=us-east-1_AAAAAAAAA:us-east-1,us-east-1_BBBBBBBBB:us-east-1
Cómo funciona la validación multi-pool
- Se lee el claim
issdel token sin verificar la firma todavía (get_unverified_issuer). - Ese
issse compara contra una whitelist fija de pools conocidos por el backend (pool_for_issuer) — nunca se confía en elissdel token por sí solo para decidir con qué llave validar sin pasar por esta whitelist. - Solo si el
isscoincide con un pool de la whitelist, se descarga (o se toma de caché, víaPyJWKClient) el JWKS de ese pool y se valida la firma real, elissotra vez,token_useyexp(validate_cognito_jwt).
Notas
- Los access tokens de Cognito no llevan el claim
aud(solo los id tokens sí) — la librería no valida audience. Si necesitas restringir porclient_id/aud, revísalo aparte sobre el dict decodificado que devuelvevalidate_cognito_jwt. - El manejo de scopes (ej. permitir solo ciertos scopes por endpoint) no es
parte de esta librería — queda a cargo de cada proyecto sobre el
decoded["scope"]que devuelve la validación.
Licencia
MIT
Metadata
Release files for aws-cognito-validation 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| aws_cognito_validation-0.1.0.tar.gz | 4.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aws_cognito_validation-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.1 kB
Release files / aws_cognito_validation-0.1.0.tar.gz
| Download URL | aws_cognito_validation-0.1.0.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6595785dfefa0e03ffc42649425843cc2c6f4d6419cc43c71f231a6a2f62fd54
|
|
BLAKE2b-256 checksum How to use checksums |
87765b7f10e41372118b496942794fb18e4151c4d6995b7335395a21d4338cf7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.7
|
Release files / aws_cognito_validation-0.1.0-py3-none-any.whl
| Download URL | aws_cognito_validation-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4e9d153c8e4e6afcb78f72cd3d18e951c7d454dcd16b10befb609bd54d24c85d
|
|
BLAKE2b-256 checksum How to use checksums |
b9be9008a5bb6d64b2059f19aef266ce0d32c84086be143b865af170524794d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.7
|
Security for aws-cognito-validation 0.1.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.