Flask-OIDC-SP - OIDC Service Provider Blueprint for Flask
FlaskOIDC is an OpenID Connect module providing authentication and authorization for Flask web framework. web apps.
FlaskOIDC supports OIDC auto discovery to simplify configuration and deployment.
Installing
# pip install Flask-OIDC-SP
This loads the necessary python modules including Flask, Flask-Session, requests, and PyJWT.
Using FlaskOIDC
from flask import Flask, request, session
from flask_session import Session
from FlaskOIDC import FlaskOIDC
from config import oidc_config, session_config
app = Flask(__name__)
app.config.from_mapping(session_config)
Session(app)
auth = FlaskOIDC(config=oidc_config, app=app)
@app.route('/login')
@auth.require_login
def login():
return f'hello {auth.my_username}'
@app.route('/bob')
@auth.require_user('bob')
return 'You must be bob'
Signature and Parameters
auth = FlaskOIDC(config, app)
app - the Flask() application context object. Optional. when provided FlaskOIDC registers itself with flask.
config - a python dict of configuration parameters and options. Required.
Configuration Options
FlaskOIDC is configured by passing a python dict with the necessary parameters:
Example Configuration
oidc_config = {
"discovery_url": "https://login.microsoftonline.com/<tenentid>/V2.0/.well-known/openid-configuration",
"client_id": "1b170767-1234-5678-abcd-90ff90ff90ff",
"client_secret": "MYCLIENTsecret",
"client_scope": ["openid", "email", "profile", ],
"user_attr" : "email",
}
discovery_url - oidc auto discovery url of the IdP. Required.
client_id - oidc client identifier of the app registered with IdP. Required.
client_secret - oidc client secret for the app provided by the IdP. Required.
client_scope - a Python list of requested scopes. Default is ['openid', 'email', 'profile']).
user_attr - attribute to set username. Default is email
logout_idp - on logout, initiate IdP logout process. Default is False.
FlaskOIDC Object Properties
auth.is_authenticated - Is True if the current session is authenticated.
auth.my_username - Returns None if the user is not authenticated. Returns user_attr value from the Id token, or 'AuthenticatedUser' if the attribute was not available in the Id token.
auth.my_attrs - Returns dict of attrs returned in the OIDC Id token, or {} if not authenticated.
Example using object properties:
@app.route('/status')
def view():
if auth.is_authenticated:
return {
'user': auth.my_username,
'data': auth.my_attrs
}
else:
return 'You are not Authenticated.'
FlaskOIDC methods
auth.initiate_login()
return auth.initiate_login(next, force_reauth, userhint)
init_login() returns OIDC code grant request redirect to iDP that initiates login. Arguments:
next - URL to redirect after login completed. Optional.
force_reauth - True requests IdP to require full reauth for this login. Default False
userhint - (where possible) provides the iDP with username hint. Default None
auth.initiate_logout()
return auth.initiate_logout(next)
initiate_logout() clears the Session data to log the user out locally. (To logout from IdP set the logout_idp config option to True.)
next - URL to redirect after logout completed. Default is '/', Optional.
@app.route('/logout')
def logout():
return auth.initiate_logout()
@auth.login_required
@app.route('/loginrequired')
@auth.login_required
def view():
return 'logged in'
Decorates a function to initiate login if the session is not authenticated. On successful authentication the browser will be redirected to the view.
@auth.add_login_hook
@oidc.add_login_hook
def hook(username, attrs):
return username, attrs
Decorates a function to runs after OIDC authentication is completed and tokens have been retrieved.
Login hooks can process and filter username and Id token attributes before the data is stored in the session. Hooks are run in the order they are added.
@auth.require_user
@auth.require_user(['bob', 'alice'])
def view():
return 'only bob or alice can get here'
Decorator adds authorization requirement to a view. If the sessions username is in the list, the view is reached and processed. Otherwise returns a 403 Unauthorized error if the user is not in the list.
@auth.require_attr(attr, value)
@auth.require_attr(attr='groups', value=['sysadmin', 'netadmin'])
def view():
return 'you are in sysadmin or netadmin'
Decorator adds authorization requirement to a view. If the session has the desired attribute (in the id token) and it matches one of the values listed, the view is reached and processed. Otherwise returns a 403 Unauthorized error.
Metadata
Release files for Flask-OIDC-SP 22.2.28
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| Flask-OIDC-SP-22.2.28.tar.gz | 13.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Flask_OIDC_SP-22.2.28-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.8 kB
Release files / Flask-OIDC-SP-22.2.28.tar.gz
| Download URL | Flask-OIDC-SP-22.2.28.tar.gz |
|---|---|
| Size | 13.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
49749dca82d51267d487096c1dde126c1fdb225fe6416f018e7e398cdf70a720
|
|
BLAKE2b-256 checksum How to use checksums |
294ae6038f746fe59e6b531ea993fd019131eef8d7efe32b048009669ef0e001
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.6.3 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6
|
Release files / Flask_OIDC_SP-22.2.28-py3-none-any.whl
| Download URL | Flask_OIDC_SP-22.2.28-py3-none-any.whl |
|---|---|
| Size | 14.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4e317c73e1636b339a778e60b3ed7d7c21c972b1d1bc675073256c46fb0a5c70
|
|
BLAKE2b-256 checksum How to use checksums |
c8b2363d63c8174e134a766c9d98868e4e29d02c253d505f27c0ee315f039ad0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.6.3 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6
|