Skip to main content

Token Validator

A Python package for validating AWS Cognito tokens and using them as decorators. You can use this package with Flask or any other Python framework.

Installation

pip install cognito-token-validator

Usage

Initialization

First, initialize the TokenValidator with the necessary parameters:

  • region: The AWS region where your Cognito user pool is located.
  • user_pool_id: The ID of your Cognito user pool.
  • client_id: The client ID of your Cognito application.
  • get_auth_header: A function that retrieves the token from the request context.
  • whitelisted_emails (optional): A List of string containing the whitelist of emails. if the list is empty, all emails are allowed. If the list is not empty, only the emails in the list are allowed.
  • cache_max_size (optional): The maximum number of tokens to cache. Default is 10.
  • ttl (optional): The time-to-live for cached tokens in seconds. Default is 3600.
from cognito_token_validator import TokenValidator

# Initialize the TokenValidator
token_validator = TokenValidator(
    region='us-east-1',
    user_pool_id='your_user_pool_id',
    client_id='your_client_id',
    get_auth_header=lambda: request.headers.get('Authorization'),
    whitelisted_emails=['example@example.com']
)

Using with Flask

To use the TokenValidator in a Flask app, apply the token_required decorator to your routes:

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/protected')
@token_validator.token_required
def protected():
    return 'This is a protected route'

if __name__ == '__main__':
    app.run(debug=True)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for cognito-token-validator 0.1.3

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

Source distribution (sdist)

Source distribution for cognito-token-validator 0.1.3
File Size Uploaded
cognito_token_validator-0.1.3.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cognito-token-validator 0.1.3
File Interpreter ABI Platform
cognito_token_validator-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 14.4 kB

Release files / cognito_token_validator-0.1.3.tar.gz

Download URL cognito_token_validator-0.1.3.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b10997912a33558df7df84ed2d21154ea9290c092c67048f7e6d512c91642645
BLAKE2b-256 checksum
How to use checksums
c822572c7eabc020bcdcdaae32df6394aaef24c0bcb4c50ec5c78a91c145abab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release files / cognito_token_validator-0.1.3-py3-none-any.whl

Download URL cognito_token_validator-0.1.3-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f118404da9b934ed7224228ffadbb865db7783860839a32a8d062aa0f70900fc
BLAKE2b-256 checksum
How to use checksums
b1ffaf94bd4f138019236561ed9951ef1d451828564094ccd1dbeb3c8d9d1b46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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