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
Built Distribution
File details
Details for the file cognito_token_validator-0.1.2.tar.gz
.
File metadata
- Download URL: cognito_token_validator-0.1.2.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a2478860f852f12668238d95213e3bef48a1b6ac5fd52e054c41580a673333f |
|
MD5 | 55ebca6504eab48b0eeefe870facc2db |
|
BLAKE2b-256 | d6d646e46ac5f32c644896093000f74bfdb2e4503bb93d73eb387348662b5140 |
File details
Details for the file cognito_token_validator-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: cognito_token_validator-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57172ae002b8c3d43c791f5311a6f99f29b911a644d26364c93346e368875a20 |
|
MD5 | a795268ec1933ea9c8d66291861ed6f4 |
|
BLAKE2b-256 | 9fe0204fb4117c821428ce14aff7b2e4d041165c8cf4a7d13f3290e20c01b4ae |