Skip to main content

Flask wrapper with pre-configured OAuth2 and OIDC support

Project description

FlaskOIDC

This package relies purely on the Authlib package. Authlib

A wrapper of Flask with pre-configured OIDC support. Ideal for microservices architecture, each request will be authenticated using Flask's before_request middleware. Necassary endpoints can be whitelisted using an environment variable FLASK_OIDC_WHITELISTED_ENDPOINTS.

Installation:

pip3 install flaskoidc

Usage:

After simply installing the flaskoidc you can simply use it like below:

from flaskoidc import FlaskOIDC
app = FlaskOIDC(__name__)

Configurations:

Please make sure to extend your configurations from BaseConfig (only if you are sure what you are doing. Recommended way is to use the environment variables for the configuration.)

from flaskoidc import FlaskOIDC
from flaskoidc.config import BaseConfig

# Custom configuration class, a subclass of BaseConfig
CustomConfig(BaseConfig):
    DEBUG = True

app = FlaskOIDC(__name__)
app.config.from_object(CustomConfig)

Following ENVIRONMENT VARIABLES MUST be set to get the OIDC working.

FLASK_OIDC_PROVIDER_NAME

(default: 'google')

The name of the OIDC provider, like google, okta, keycloak etc. I have verified this package only for google, okta and keycloak. Please make sure to open a new issue if any of your OIDC provider is not working.

FLASK_OIDC_SCOPES

(default: 'openid email profile')

Scopes required to make your client works with the OIDC provider, separated by a space.

  • OKTA: make sure to add offline_access in your scopes in order to get the refresh_token.

FLASK_OIDC_USER_ID_FIELD

(default: 'email')

Different OIDC providers have different id field for the users. Make sure to adjust this according to what your provider returns in the user profile i.e., id_token.

FLASK_OIDC_CLIENT_ID

(default: '')

Client ID that you get once you create a new application on your OIDC provider.

FLASK_OIDC_CLIENT_SECRET

(default: '')

Client Secret that you get once you create a new application on your OIDC provider.

FLASK_OIDC_FORCE_SCHEME

(default: 'http')

Can be used to force a URL scheme when crafting a redirect_uri in '/login' route. Useful when Flask application is behind an ingress doing TLS termination.

FLASK_OIDC_REDIRECT_URI

(default: '/auth')

This is the endpoint that your OIDC provider hits to authenticate against your request. This is what you set as one of your REDIRECT URI in the OIDC provider client's settings.

FLASK_OIDC_CONFIG_URL

(default: '')

To simplify OIDC implementations and increase flexibility, OpenID Connect allows the use of a "Discovery document," a JSON document found at a well-known location containing key-value pairs which provide details about the OpenID Connect provider's configuration, including the URIs of the authorization, token, revocation, userinfo, and public-keys endpoints.

Discovery Documents may be retrieved from:

  • Google: https://accounts.google.com/.well-known/openid-configuration
  • OKTA
    • https://[YOUR_OKTA_DOMAIN]/.well-known/openid-configuration
    • https://[YOUR_OKTA_DOMAIN]/oauth2/[AUTH_SERVER_ID]/.well-known/openid-configuration
  • Auth0: https://[YOUR_DOMAIN]/.well-known/openid-configuration
  • Keycloak: http://[KEYCLOAK_HOST]:[KEYCLOAK_PORT]/auth/realms/[REALM]/.well-known/openid-configuration

FLASK_OIDC_OVERWRITE_REDIRECT_URI

(default: '/') In some cases you may need to redirect to a different endpoint after a successful login. This environment lets you set that endpoint. By default, this redirects to /.

FLASK_OIDC_PROVIDER_ADDITIONAL_PARAMETERS_FILE_PATH

(default: None)

The absolute path to a json file holding key value pairs of additional parameters ro be appended during client registration. This will overwrite any default parameters for a given OIDC provider.

....

A few other environment variables along with their default values are.

# Flask `SECRET_KEY` config value
FLASK_OIDC_SECRET_KEY: '!-flask-oidc-secret-key'

# Comma separated string of URLs which should be exposed without authentication, else all request will be authenticated.
FLASK_OIDC_WHITELISTED_ENDPOINTS: "status,healthcheck,health"

You can also set the config variables specific to Flask-SQLAlchemy using the same key as the environment variables.

# Details about this below in the "Session Management" section.
SQLALCHEMY_DATABASE_URI: 'sqlite:///sessions.db'

Known Issues:

  • Need to make sure it still works with the clients_secrets.json file or via env variables for each endpoint of a custom OIDC provider.
  • refresh_token is not yet working. I am still trying to figure out how to do this using Authlib.
  • You may enter problems when installing cryptography, check its official document

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

secoda-flaskoidc-1.0.7.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

secoda_flaskoidc-1.0.7-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file secoda-flaskoidc-1.0.7.tar.gz.

File metadata

  • Download URL: secoda-flaskoidc-1.0.7.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for secoda-flaskoidc-1.0.7.tar.gz
Algorithm Hash digest
SHA256 3235800fc0b837e9213958e44cb1875b1d409b541287f5060126f72842491d1f
MD5 03f397a56b7f469030b1eb8ed28eec88
BLAKE2b-256 e7f23ff0e93edf76bc92a91623e2d0fb4c85f36ab797a8a7a712c3e872801ce5

See more details on using hashes here.

File details

Details for the file secoda_flaskoidc-1.0.7-py3-none-any.whl.

File metadata

  • Download URL: secoda_flaskoidc-1.0.7-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for secoda_flaskoidc-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 33f6ad82a729469761e4fbd71a902694f632035a13419b8ff7db3c7fc2b68f4c
MD5 d040ca7a6871c4158d7f6bd7c2d57fcb
BLAKE2b-256 8006d1933b98ba1e1e2dcf5a3dc7b3a271ef7f63353eec3af50f00a476bb75cc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page