A simple Flask blog with user registration and login.
- Author: Randall Degges
- Email: r@rdegges.com
- Site: https://www.rdegges.com
This example app showcases how to build simple Flask web apps. It demonstrates how to work with databases and handle user management (registration, login, etc.) using OpenID Connect and Okta.
I wrote this to showcase how to get stuff working in a simple way.
Note
I wrote a blog post showcasing how this example app was built. You can view it here: https://developer.okta.com/blog/2018/07/23/build-a-simple-crud-app-with-flask-and-python
To install the sample app you need to have Python 2.7 or 3.4+ installed. You can then install the project dependencies by running:
$ pip install -e .
This will install all the project dependencies.
This app requires Okta to run. Okta is a free-to-use API service that stores user accounts and makes authentication and authorization simpler. Go create a free Okta developer account before continuing: https://developer.okta.com/signup
Next, you need to create a client_secrets.json
file. This holds the OpenID
Connect information necessary for the app to function. Create a file named
client_secrets.json
in the root of your project folder and add the following
contents.
{
"web": {
"client_id": "{{ OKTA_CLIENT_ID }}",
"client_secret": "{{ OKTA_CLIENT_SECRET }}",
"auth_uri": "{{ OKTA_ORG_URL }}/oauth2/default/v1/authorize",
"token_uri": "{{ OKTA_ORG_URL }}/oauth2/default/v1/token",
"issuer": "{{ OKTA_ORG_URL }}/oauth2/default",
"userinfo_uri": "{{ OKTA_ORG_URL }}/oauth2/default/userinfo",
"redirect_uris": [
"http://localhost:5000",
"http://localhost:5000/oidc/callback"
]
}
}
Note
Be sure to replace the Okta variables above appropriately.
Next, define some necessary environment variables.
export FLASK_APP=blog
export SECRET_KEY={{ RANDOM_STRING_HERE }}
export OKTA_ORG_URL={{ OKTA_ORG_URL }}
export OKTA_AUTH_TOKEN={{ OKTA_AUTH_TOKEN }}
Set the SECRET_KEY
variable to a long, random string. This will be used to
secure your sessions (cookies). Then set the other two Okta variables
appropriately.
Next, run the web server.
flask run
Finally, go visit http://localhost:5000 and explore the site!