This sample demonstrates how to add authentication to a Python Flask web app using Auth0.
This application is configured to run on a Ubuntu EC2 instance on AWS.
- From the AWS EC2 console select
Launch Instance - Select the Ubuntu 20.04 LTS instance family (type 'ubuntu' in the search field)
- Select the t2.micro instance family
- Select the proper VPC, Subnet and EC2 instance profile as necessary
- Under 'Advanced Details', copy and paste the
userdata.shfile found here in thebootstrappath - Add storage (minimum 8GB for Ubuntu)
- Add necessary tags
- Select an existing security group, or create a new one using the wizard. Ensure that ports 80 and 22 are open to your local IP address
- Click "Review and Launch" and select your SSH keypair
When the deployment completes, make note of the IPv4 Public DNS name.
(from the Auth0 Console)
Login to your Auth0 account and create a new application or use the default application that was created when you created your Auth0 account.
Configure your callback and logout URLs:
- The list of Callback URLs should contain
http://${EC2_IPv4_DNS_HERE}/callback - The list of Logout URLs should contain
http://${EC2_IPv4_DNS_HERE}/
(from the EC2 command line)
SSH into your EC2 instance and edit the file constants.py with the following values:
Copy .env.example to .env and replace the placeholders with the correct values. Callback URLs should reference your EC2 instance's Public IPv4 DNS address and not localhost. Emphasis: this method of secrets injection is not secure and should not be implemented within a production environment. Instead secrets should be managed by a bonafide secrets management utility
AUTH0_CLIENT_ID = '[Auth0 client ID goes here]'
AUTH0_CLIENT_SECRET = '[Auth0 client secret goes here'
AUTH0_CALLBACK_URL = 'http://[EC2_IPv4_DNS_HERE]/callback'
AUTH0_DOMAIN = '[Auth0 domain goes here]'
AUTH0_AUDIENCE = 'https://[AUTH0_DOMAIN_HERE]/api/v2/'
(from the EC2 command line)
SSH into your EC2 instance
- This Git repo should already be checked out in the directory
/home/ubuntu/app - navigate to the
configpath and execute the scriptconfig.sh. This will place both the nginx and gunicorn config files in the correct paths as well as start the application
cd app/config
sudo ./config.sh
- Once the application has been configured, hit the IPv4 Public DNS name of your EC2 instance. This will present a default screen with a simple 'Login' button.
- Click login and select 'Sign in with Google' or 'Sign Up'
- Sign Up: provide your email address and password. This will generate an email validation request (sent to the email address used to sign up), and it will create a new user in your Auth0 application
- Sign up with Google: This will request your Google username and password and will also create a new user in your Auth0 application.
- Linked accounts: If the user signs in using thier email address and then later chooses 'Sign in with Google' (or visa versa), the Auth0 account link extension can be configured automatically detect a duplicate user and provide the user with the option to link both accounts. If the user agrees, they will be prompted for the authentication information of the second account, and the accounts will be linked together presenting a single user within your Auth0 application.
- Email verification: this application demonstrates how to detect if a user has verified their email address. If the user has not validated thier email, then basic information is witheld from that user. Once the user has verified, that information is shown.
- Logout: Click the 'Logout' button to lot out
This application also uses an Auth0 Rule to detect users' country of origin as well as regional information. See: