Flask-OIDC is an extension to Flask that allows you to add OpenID Connect based authentication to your website. It is currently at version 2.4.0 and sees regular releases, with several updates in the past year, indicating active maintenance and development.
pip install flask-oidcVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates a basic Flask application integrated with Flask-OIDC. It configures the OIDC extension, protects a route using `oidc.require_login`, and provides simple login/logout functionality. Configuration details for your OIDC provider are expected in a `client_secrets.json` file. Ensure `FLASK_SECRET_KEY` and the `client_secrets.json` path are set, ideally via environment variables, and configure `OIDC_REDIRECT_URI` to match your registered callback URL.
Review the official documentation and migration guides for Authlib and Flask-OIDC v2.x. Many configuration options and API calls have changed or been removed. For example, `oidc.credentials_store` and other constructor parameters were removed.
Replace calls to `oidc.user_getinfo()` or `oidc.user_getfield()` with direct access to `session["oidc_auth_profile"]` or the properties of `g.oidc_user` (available since 2.2.0).
For applications needing specific `redirect_uri` behavior, especially forcing HTTPS, explicitly set the `OIDC_OVERWRITE_REDIRECT_URI` configuration option. Review the behavior in versions 2.0.3 and 2.1.0 in the changelog.
If migrating from 1.x and encountering issues with `redirect_to_auth_server()`, ensure you are on version 2.2.2 or later if you need to use this specific method. Otherwise, adapt your code to newer 2.x patterns for redirection.
Always set `app.config['SECRET_KEY']` to a long, random string. It is highly recommended to manage this key via environment variables in production. For example, `os.environ.get('FLASK_SECRET_KEY', 'default_for_dev')`.Ensure a valid `client_secrets.json` file is present or that `OIDC_ENABLED` is set to `False` (requires version 2.3.1 or higher for `client_secrets.json` to be entirely optional in this disabled state). The structure requires a top-level `web` key.
Upgrade to version 2.4.0 or later to apply the security fix. Review any custom login/logout redirect logic to ensure it is not negatively impacted by the fix and adheres to secure redirect practices.
Ensure the exact callback URL (e.g., `https://yourdomain.com/oidc/callback`) is registered in your OIDC provider's client settings. In your Flask application, explicitly set `app.config['OVERWRITE_REDIRECT_URI'] = 'https://yourdomain.com/oidc/callback'` to force Flask-OIDC to use the correct absolute HTTPS URL.
Activate the correct Python virtual environment (if applicable) and install the library using pip: `pip install Flask-OIDC`.
Ensure your Flask application has a `SECRET_KEY` configured (`app.config['SECRET_KEY'] = 'your_strong_secret_key'`). If running with multiple workers/threads, ensure your Flask session configuration allows for proper state preservation across requests, potentially by using a shared session backend or verifying that cookie settings (like `SAMESITE`) are not inadvertently preventing the cookie from being sent.
1. Verify your `client_secrets.json` file is correctly formatted and contains accurate `client_id`, `client_secret` (if it's a confidential client), `issuer`, `auth_uri`, `token_uri`, and `userinfo_uri`. 2. Ensure the `OIDC_SCOPES` configured in your Flask app match the scopes allowed and expected by your Identity Provider. 3. If using a public client with Keycloak or similar, `flask-oidc` may still expect a `client_secret` to be present (even if empty in `client_secrets.json`), but the provider might reject it if it's truly a public client that doesn't use client secrets for token exchange. Verify the OIDC provider's specific requirements for public vs. confidential clients and the client registration details.