Skip to main content

OIDC for FastAPI

Project description

OpenID Connect Plugin for FastAPI

fastapi-simple-oidc is a simple, configurable plugin for enabling OpenID Connect (OIDC) authentication in a FastAPI application. It supports multiple identity providers, session management, and callbacks for handling login and logout events.

Features

  • OpenID Connect (OIDC) integration for authentication.
  • Support for popular Identity Providers like Google, Microsoft, Apple, GitLab, Salesforce, and IBM.
  • Support for any custom OIDC backend.
  • Easy provider discovery using .well-known/openid-configuration.
  • Session-based user management using SessionMiddleware.
  • Callbacks for on_login and on_logout to customize user handling behavior.
  • Pre-built routes for login, logout, user information, and provider discovery.
  • Extendable to additional identity providers.

Installation

pip install fastapi-simple-oidc

Quick Start

Here’s a quick guide to get you started with FastAPI and OIDC.

1. Setup your FastAPI application

from fastapi import FastAPI
from fastapi_oidc import OIDC

app = FastAPI()

# Create an OIDC instance
oidc = OIDC(
    app=app,
    secret_key="your-session-secret",  # A secure key for the application session
    login_url='/login',                # Optional: Redirect URL for login page
    redirect_url='/dashboard'          # Optional: URL to redirect to after login
)

# Register OIDC routes
app.include_router(oidc.router)

2. Configure Identity Providers

Identity provider configurations must be set using environment variables or configuration files.

Environment Variable Example:

SSO_GOOGLE_ID=<google-client-id>
SSO_GOOGLE_SECRET=<google-client-secret>

SSO_MICROSOFT_ID=<microsoft-client-id>
SSO_MICROSOFT_SECRET=<microsoft-client-secret>
SSO_GOOGLE_NAME=Custom Google Name  # Optional: Display name for Google

# also Custom Provider
SSO_CUSTOM_ID=
SSO_CUSTOM_SECRET=
SSO_CUSTOM_URL=
SSO_CUSTOM_NAME=

Built-in Identity Providers

The library supports the following providers out of the box, you don't need to specify NAME and URL

  • Google
  • Microsoft
  • Apple
  • GitLab
  • Salesforce
  • IBM

3. Add Login and Logout Callbacks (Optional)

You can define custom logic that runs after login or logout events using the on_login and on_logout decorators.

from fastapi_oidc import OpenID

@oidc.on_login
def handle_login(user: OpenID):
    print(f"User logged in: {user.name} ({user.email})")

@oidc.on_logout
def handle_logout(user: OpenID):
    print(f"User logged out: {user.email}")

4. Secure Your Routes with Authentication

Use the get_logged_user dependency to secure your endpoints and access the currently authorized user.

from fastapi_oidc import get_logged_user, OpenID

@app.get("/protected")
async def protected_endpoint(user: OpenID = Depends(get_logged_user)):
    return {"message": f"Welcome {user.name}", "email": user.email}

Exception raised for unauthorized users:

  • 401 Unauthorized: If the user is not authenticated.

5. Test the Integration

Visit the built-in test page at the /.test route to verify the setup:

  • List all available Identity Providers.
  • Attempt login/logout.
  • Check the current session.

alt text

Routes

The plugin automatically registers the following routes:

Route Description
/<provider>/login Login redirection page.
/<provider>/callback OIDC callback
/logout Clears the session and logs the user out.
/me Returns the details of the currently logged-in user. Uses get_logged_user.
/providers Lists all available identity providers.
/.test A pre-built test page to test your OIDC configuration.

Each Identity Provider will also have its own login and callback routes under <provider>/login and <provider>/callback.


Environment Variables Reference

The following environment variables can be set to configure available identity providers:

Variable Required Description
SSO_<PROVIDER>_ID Yes The client ID for the identity provider.
SSO_<PROVIDER>_SECRET Yes The client secret for the identity provider.
SSO_<PROVIDER>_URL Only if not a default provider The OpenID Connect discovery URL of the identity provider.
SSO_<PROVIDER>_NAME No Custom display name for the provider in /providers.

Extending

Add New Providers

You can integrate custom providers by adding their configuration into environment variables as shown earlier. The package uses the .well-known/openid-configuration standard to fetch OpenID Connect metadata, so the provider needs to follow this specification.


Example App

Here’s an example app using Google as an Identity Provider:

from fastapi import FastAPI, Depends
from fastapi_oidc import OIDC, get_logged_user, OpenID

app = FastAPI()

oidc = OIDC(
    app=app,
    secret_key="random-secret-key",
    login_url='/login',
    redirect_url='/dashboard'
)

# Register OIDC routes
app.include_router(oidc.router)

# Secure route
@app.get("/dashboard")
async def dashboard(user: OpenID = Depends(get_logged_user)):
    return {"message": f"Hello, {user.name}!", "email": user.email}

@oidc.on_login
def handle_login(user: OpenID):
    print(f"User logged in: {user.email}")

@oidc.on_logout
def handle_logout(user: OpenID):
    print(f"User logged out: {user.email}")

Supported Defaults

When using the default providers, you don’t need to define URLs; they are managed internally.


License

This project is licensed under the MIT License.

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

fastapi_simple_oidc-0.0.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_simple_oidc-0.0.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_simple_oidc-0.0.1.tar.gz.

File metadata

  • Download URL: fastapi_simple_oidc-0.0.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for fastapi_simple_oidc-0.0.1.tar.gz
Algorithm Hash digest
SHA256 9461800faf21743ad5771012520da3f301c4b7da8c69cd1c738294502a185131
MD5 c855519d525fafd43b6f2fce849390c0
BLAKE2b-256 de968cf782df45827bdd2810ea7c0b89643714fa474df7737cf60563876296e5

See more details on using hashes here.

File details

Details for the file fastapi_simple_oidc-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_simple_oidc-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 62cdce3a0933f981324c79aa5ee8e92dc84a8a89e9b8cd3ee5c5879692dee0a3
MD5 b79cc31ef41f15641d220046b140926b
BLAKE2b-256 ebd453ff4107ea7e0057bb3ddf232ab31be83abf6363834c24807411dd2a8266

See more details on using hashes here.

Supported by

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