Dash Cognito Auth
Project description
Dash Cognito Auth
Dash Cognito Auth is a simple library using Cognito OAuth to authenticate and view a Dash app.
This Library uses Flask Dance and a modified version of Plotly's own dash auth for authentication.
This Library is heavily inspired by dash-google-oauth created by Lucas Chapin
Basic Use
Authentication can be added to your Dash application using the CognitoOAuth
class, i.e.
from dash import Dash
from flask import Flask
from dash_cognito_auth import CognitoOAuth
server = Flask(__name__)
server.config.update({
'COGNITO_OAUTH_CLIENT_ID': ...,
'COGNITO_OAUTH_CLIENT_SECRET': ...,
})
app = Dash(__name__, server=server, url_base_pathname='/')
additional_scopes = [...]
auth = CognitoOAuth(app, domain='mydomain', region='eu-west-1', authorized_emails, additional_scopes)
# your Dash app here :)
...
Example
Steps to try this out yourself:
-
Install the
dash-cognito-auth
library usingpip
:$ pip install dash-cognito-auth
-
Follow the Flask Dance Guide to create an app on the cognito admin console
-
Make a copy of app.py and set the variables (or set the corresponding environment variables):
server.config["COGNITO_OAUTH_CLIENT_ID"] = ... server.config["COGNITO_OAUTH_CLIENT_SECRET"] = ...
with values from the Cognito OAuth 2 client you should have set up in step 1. If you've set these up properly, you can find them at APIs & Services > Credentials under the section OAuth 2.0 client IDs.
-
Run
python app.py
and open localhost in a browser window to try it out! If the app loads automatically without prompting a Cognito login, that means you're already authenticated -- try using an incognito window in this case if you want to see the login experience for a new user.
Development
- Check out the repository
- Run
pip install -r requirements.txt
to install the package - Run
pip install -r requirements-dev.txt
to install additional dependencies for running the tests - Run the tests locally
- Use
python -m pytest tests --ignore-glob "*end_to_end*"
to exclude the integration / end to end tests that require a Cognito Setup - Use
python -m pytest tests
to run all tests
- Use
Integration Tests
There are integration tests against a Cognito User Pool + App Client, if you want to run those - either create a .env
file with this content or set the environment variables with the same name.
# Credentials for the user in the user pool
COGNITO_USER_NAME=<username>
COGNITO_EMAIL=<email-that-must-match>
COGNITO_PASSWORD=<password>
# Connection between the app and the user pool
COGNITO_DOMAIN=<just-the-prefix>
COGNITO_REGION=<aws-region-of-the-cognito-userpool>
COGNITO_OAUTH_CLIENT_ID=<app-client-id>
COGNITO_OAUTH_CLIENT_SECRET=<app-client-secret>
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
Hashes for dash_cognito_auth-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9634393dfdb8f50af227bf1f18d167cfcfb090da66abda85a6f37eb5924e86f0 |
|
MD5 | ee4cf8d0187a2507a041b5fb42d8d105 |
|
BLAKE2b-256 | f857efc6388f15a3a1fc9350c0ce6c4d5841035b2a0f74c2681da232988df82b |