Skip to main content

Flask-Cognito-Auth

License: MIT PyPI CircleCI

Flask-Cognito-Auth is a Flask implementation of AWS Cognito Service with User Pool. This extension helps to implement authentication solutions based on AWS's Cognito Service. It contains helpful functions and properties to handle oauth2 and token based authentication flows. This extension requires to enable "Enabled Identity Providers" in Appllication client settings of User Pool within AWS Cognito Sevice.

Add Application


SAML Assertion settings (OPTIONAL)

If SAML provider is set then optionally SAML assertion can be set and reterieved post authentication.
These assertions are reterived under Session object under key "saml_assertions".

Example

SAML Assertion

* Replace "Email" with the key provided in SAML assertions sent by IDP and to be reterived as Email key.
* Replace "MemberOf" with the key provided in SAML assertions sent by IDP and to be reterived as profile key.
* Add as many assertions required post authentication.
pip install flask-cognito-auth

Usage

from flask import Flask
from flask import redirect
from flask import url_for
from flask import session
from flask import jsonify
from flask_cognito_auth import CognitoAuthManager
from flask_cognito_auth import login_handler
from flask_cognito_auth import logout_handler
from flask_cognito_auth import callback_handler

app = Flask(__name__)
app.secret_key = "my super secret key"

# Setup the flask-cognito-auth extention
app.config['COGNITO_REGION'] = "us-east-1"
app.config['COGNITO_USER_POOL_ID'] = "us-east-1_xxxxxxx"
app.config['COGNITO_CLIENT_ID'] = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
app.config['COGNITO_CLIENT_SECRET'] = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
app.config['COGNITO_DOMAIN'] = "https://yourdomainhere.com"
app.config["ERROR_REDIRECT_URI"] = "page500"        # Optional
app.config["COGNITO_STATE"] = "mysupersecrethash"   # Optional

app.config['COGNITO_REDIRECT_URI'] = "https://yourdomainhere/cognito/callback"  # Specify this url in Callback URLs section of Appllication client settings of User Pool within AWS Cognito Sevice. Post login application will redirect to this URL

app.config['COGNITO_SIGNOUT_URI'] = "https://yourdomainhere/login" # Specify this url in Sign out URLs section of Appllication client settings of User Pool within AWS Cognito Sevice. Post logout application will redirect to this URL


cognito = CognitoAuthManager(app)
# cognito = CognitoManager(app)
# cognito.init(app)


@app.route('/login', methods=['GET'])
def login():
    print("Do the stuff before login to AWS Cognito Service")
    response = redirect(url_for("cognitologin"))
    return response


@app.route('/logout', methods=['GET'])
def logout():
    print("Do the stuff before logout from AWS Cognito Service")
    response = redirect(url_for("cognitologout"))
    return response


# Use @login_handler decorator on cognito login route
@app.route('/cognito/login', methods=['GET'])
@login_handler
def cognitologin():
    pass


@app.route('/home', methods=['GET'])
def home():
    current_user = session["username"]
    return jsonify(logged_in_as=current_user), 200


# Use @callback_handler decorator on your cognito callback route
@app.route('/cognito/callback', methods=['GET'])
@callback_handler
def callback():
    print("Do the stuff before post successfull login to AWS Cognito Service")
    for key in list(session.keys()):
        print(f"Value for {key} is {session[key]}")
    response = redirect(url_for("home"))
    return response


# Use @logout_handler decorator on your cognito logout route
@app.route('/cognito/logout', methods=['GET'])
@logout_handler
def cognitologout():
    pass


@app.route('/page500', methods=['GET'])
def page500():
    return jsonify(Error="Something went wrong"), 500


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

Development Setup

Using pipenv

pipenv install --dev 

Using virtualenv

python3 -m venv env
source env/bin/activate
pip install .

Contributing

  1. Fork repo- https://github.com/shrivastava-v-ankit/flask-cognito-auth.git
  2. Create your feature branch - git checkout -b feature/name
  3. Add Python test (pytest) and covrage report for new/changed feature.
  4. Commit your changes - git commit -am "Added name"
  5. Push to the branch - git push origin feature/name
  6. Create a new pull request

Metadata

Release files for flask-cognito-auth 1.1.0

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

Source distribution (sdist)

Source distribution for flask-cognito-auth 1.1.0
File Size Uploaded Provenance Actions
flask-cognito-auth-1.1.0.tar.gz 12.0 kB Without provenance Details for flask-cognito-auth-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-cognito-auth 1.1.0
File Interpreter ABI Platform Actions
flask_cognito_auth-1.1.0-py3-none-any.whl Python 3 none any Details for flask_cognito_auth-1.1.0-py3-none-any.whl

Total release size: 21.9 kB

Release files / flask-cognito-auth-1.1.0.tar.gz

Download URL flask-cognito-auth-1.1.0.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d9c6b1ba6b8d53d5aad31c9b7ea4bc118ee303a01832294743b8bc7738acc78a
BLAKE2b-256 checksum
How to use checksums
3395551837f3322bb79a4cf0e9b6c32e97dc8831ef5660e7a58432e089b845bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.23.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.5

Release files / flask_cognito_auth-1.1.0-py3-none-any.whl

Download URL flask_cognito_auth-1.1.0-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
01fcf13c79985f1474fd30b826197cd809d67789dc9522b84c50ed6f61eb5c3c
BLAKE2b-256 checksum
How to use checksums
3169f07e87e827d2bd069f089cd6e3764efcbe121abbd2478585d53ec0e4dbc4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.23.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.5

Security for flask-cognito-auth 1.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.

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