foundry-auth
OIDC authentication for Foundry agents: authorization-code + PKCE flow,
protocol-based session storage (SQLite),
a FastAPI router factory, auth dependencies, and WebSocket session
validation. Depends on: foundry-agent-core.
Install
Install a released wheel from this repo's GitHub Releases. See Adopting these packages for the full flow - pinning a release URL directly for evaluation, or hosting the wheel in your own index for production, plus verifying the SBOM/scan assets.
Quickstart
from fastapi import Depends, FastAPI
from foundry_auth import AuthConfig, SessionData, create_auth_router, create_require_session, get_session_store
app = FastAPI()
config = AuthConfig.from_env()
session_store = get_session_store(config)
app.include_router(create_auth_router(config=config, session_store=session_store))
require_session = create_require_session(config=config, session_store=session_store)
@app.get("/profile")
async def profile(session: SessionData = Depends(require_session)):
return {"user_id": session.user_id}
See AUTH_SETUP.md for a full walkthrough including Keycloak setup, and the Integration Guide for adding auth to an existing FastAPI service in three lines of code.
What's in the box
| Surface | Highlights |
|---|---|
| OIDC flow | Authorization-code + PKCE, prefix-based configuration for multiple service configs |
| Session storage | SessionStore protocol, SQLiteSessionStore |
| FastAPI integration | create_auth_router, create_require_session, create_optional_session |
| WebSocket support | register_connection, session validation loop, connection cleanup on session expiry/deletion |
Security
- Tokens encrypted at rest: OAuth access/refresh tokens and the
captured OIDC
id_tokenare encrypted in SQLite using Fernet (AES-128-CBC + HMAC-SHA256);SESSION_ENCRYPTION_KEYis required at startup. - Session ID rotation and masking: session IDs are rotated on refresh and redacted in log output.
- CSRF protection: OAuth state is bound to the initiating browser.
- Open-redirect protection:
sanitize_redirect_pathrejects absolute URLs, protocol-relative paths, and control characters (including CR/LF) in thereturnToparameter.
See the package's Security guide for the full control list.