Skip to main content

Steam OIDC for FastAPI

Project description

pysteam

Steam OpenID Connect Plugin for FastAPI

Features

  • Steam OpenID Connect (OIDC) integration for authentication
  • Session-based user management using SessionMiddleware
  • Callbacks for on_login and on_logout to customize user handling behavior
  • Pre-built routes for login, logout
  • Extendable and configurable settings

Installation

pip install pysteam

Quick Start

Here's an example of how you can configure PySteam and use Steam as an OpenID Provider

1. Configure pysteam FastAPI settings

oidc_app_settings = OidcAppSettings(             # pysteam routes that will be included in FastAPI application
    base_url="http://localhost:8000",            # Base application url
    login_url="/login",                          # Login url
    logout_url="/logout",                        # Logout url
    callback_url="/callback",                    # Callback verification url
    post_callback_url="/me",                     # Post callback redirect
    auth_router_prefix="/steam",                 # OIDC router prefix (e.g. '/steam') | This prefixes supplied urls above
)

2. Configure pysteam OIDC settings

oidc_session_settings = OidcSessionSettings(      # OIDC settings
    secret_key="test",                            # Secret key
    session_cookie="steam_session",               # Session cookie name
    max_age=(60*60*24*14),                        # Session cookie max age (e.g. (60*60*24*14) = 14 days in seconds)
    same_site="lax",                              # CSRF same site
    https_only=False,                             # https_only (True in prod)
    csrf_state_ttl_seconds=600,                   # CSRF TTL
    openid_nonce_ttl_seconds=600,                 # NONCE TTL
    openid_nonce_clock_skew_seconds=300           # NONCE TTL skew
)

3. Create pysteam settings

pysteam_settings = PySteamSettings(
    app_config=oidc_app_settings,
    session_config=oidc_session_settings
)

4. Set up FastAPI application

app = FastAPI()
oidc = SteamOIDC(app, pysteam_settings)
app.include_router(oidc.router)

5. Add login and logout callbacks (optional)

@oidc.on_login
async def login_callback(steamid):    # Session steamid will be passed into the callback
  print(f"User: {steamid)")
@oidc.on_logout
async def logout_callback():          # Nothing will be passed to this as pysteam will clear the session
  print("Logged out!")

5. Secure protected routes

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

from fastapi import Depends
from pysteam import get_logged_user

@app.get("/protected")
async def protected_route(steamid = Depends(get_logged_user):
  return {"steamid": steamid}

Exception raised for unauthorized users:

  • 401 Unauthorized if the user is not authenticated

Pydantic Models

class PySteamSettings(BaseModel):
    app_config: OidcAppSettings
    session_config: OidcSessionSettings
class OidcAppSettings(BaseModel):
    base_url: str = Field(title="API Base URL (e.g 'localhost:8000')")
    login_url: str = Field(title="Login path (e.g. '/login')", default="/login")
    logout_url: str = Field(title="Logout path (e.g. '/logout')", default="/logout")
    callback_url: str = Field(title="OpenID return to path (e.g. '/callback')", default="/callback")
    post_callback_url: str = Field(title="URL Redirected to after successful login", default="/me")
    auth_router_prefix: str = Field(title="Auth router prefix (e.g '/auth', '/auth/steam', '/steam')")
class OidcSessionSettings(BaseModel):
    secret_key: str = Field(title="Session secret key", default="secretkey")
    session_cookie: str = Field(title="Session name", default="steam_session")
    max_age: int = Field(title="Session max age in seconds", default=(60 * 60 * 24 * 14))
    same_site: str = Field(title="CSRF", default="lax")
    https_only: bool = Field(title="https_only", default=False)
    csrf_state_ttl_seconds: int = Field(title="CSRF TTL Seconds", default=600)
    openid_nonce_ttl_seconds: int = Field(title="NONCE TTL Seconds", default=600)
    openid_nonce_clock_skew_seconds: int = Field(title="NONCE Skew Seconds", default=300)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

fastapi_steam_oidc-0.0.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_steam_oidc-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cc6ddecb782a2533dfdce607e211d761c6f55d1459dd842a421335ae8c8ca5ab
MD5 a21c3565ad2cae0d6cf6785011a6621e
BLAKE2b-256 47fdcb9b3e79b3d402e73487f59ca3dbbb8bb7d29943a4df8a58ec331f653421

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