Skip to main content

A package for validating AWS Cognito tokens and using them as decorators in Flask routes or standalone Python functions

Project description

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',
    permissions_str='{"whitelist": ["example@example.com"]}',
    get_auth_header=lambda: request.headers.get('Authorization'),
)

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cognito_token_validator-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

cognito_token_validator-0.1.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file cognito_token_validator-0.1.1.tar.gz.

File metadata

File hashes

Hashes for cognito_token_validator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 098e78661421506094affcf5ff80641cd85824cc3d04fe7d052dd60d5595a619
MD5 0ae38e6219f4c3efcbff9ffaf8e4479c
BLAKE2b-256 8c56b5d08dab4d1926531c1869106a50b87eaf757a41b89fb90693cd4318cfc8

See more details on using hashes here.

File details

Details for the file cognito_token_validator-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cognito_token_validator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7e19395d748769dc93c5f2471d543343a20fecbea6ad81b7f85962a6836fe1ab
MD5 401d1b5b222cb750bc2fe7ab2a9da501
BLAKE2b-256 618bc882143eb7e3ec2fb1914318fdf3f513457c7e7170cb56efd3589347ca21

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page