Files
bdinvite/backend/app/config.py
T
kiskaadee 30d9a889a3 feat(security): harden authentication edge cases and verify deployment independence
- Reject forged, replayed, and expired CSRF state with HTTP 400 Bad Request
- Enforce cryptographic validation rejecting tampered ID token signatures and mismatched/missing nonces with HTTP 401 Unauthorized
- Reject missing, invalid, or mismatched PKCE code_verifier with HTTP 401 Unauthorized (InvalidCodeVerifierError)
- Invalidate expired ID tokens and session cookies returning HTTP 401 Unauthorized
- Enforce role-based access control blocking authenticated non-admin users from admin APIs with HTTP 403 Forbidden
- Eliminate legacy Remote-User trust ensuring session identity strictly governs authorization
- Document and verify dual deployment modes (Standalone Mode vs Homelab Production Mode behind Traefik TLS proxy)
- Add comprehensive security hardening regression suite (tests/test_security_hardening.py) and .env.example template
- Ensure clean multi-stage Docker build
2026-10-03 01:26:05 -05:00

73 lines
2.8 KiB
Python

from pathlib import Path
from typing import Literal
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
"""Application and deployment configuration supporting multi-mode deployment.
BDInvite supports two primary operational deployment modes via environment variables:
1. Direct Standalone Mode (Local Development & Testing):
- Runs directly or in lightweight container with direct port binding (e.g. 8000).
- Integrates directly with any standard OIDC identity provider (e.g. mock-oauth2-server,
local Keycloak, or Dex).
- Insecure session cookies (`SESSION_COOKIE_SECURE=False`) allow local HTTP testing.
- Example:
OIDC_ISSUER="http://localhost:8088/default"
OIDC_REDIRECT_URI="http://localhost:8000/birthday/api/auth/callback"
SESSION_COOKIE_SECURE=false
2. Homelab Production Mode (Reverse Proxy & Edge Ingress):
- Deployed behind Traefik reverse proxy terminating TLS on edge domain.
- Integrates with central Homelab OIDC Provider (such as Authelia, Keycloak, or Authentik).
- Secure session cookies (`SESSION_COOKIE_SECURE=True`) enforce HTTPS-only transmission.
- Zero trust in unvalidated client headers (e.g. legacy proxy headers are completely ignored).
- Example:
SERVICE_DOMAIN="demos.roadtotech.me"
OIDC_ISSUER="https://auth.roadtotech.me"
OIDC_CLIENT_ID="bdinvite"
OIDC_CLIENT_SECRET="<secret>"
OIDC_REDIRECT_URI="https://demos.roadtotech.me/birthday/api/auth/callback"
SESSION_COOKIE_SECURE=true
SESSION_COOKIE_NAME="bdinvite_session"
"""
SERVICE_NAME: str = "bdinvite"
SERVICE_DOMAIN: str = "demos.roadtotech.me"
BASE_PATH: str = "/birthday"
APP_PORT: int = 8000
DATABASE_URL: str = "sqlite:///./data/bdinvite.db"
DATA_DIR: str = "./data"
# OIDC & Auth Settings
OIDC_ISSUER: str = "http://localhost:8088/default"
OIDC_CLIENT_ID: str = "bdinvite-client"
OIDC_CLIENT_SECRET: str | None = "bdinvite-secret"
OIDC_REDIRECT_URI: str = "http://localhost:8000/birthday/api/auth/callback"
OIDC_GROUPS_CLAIM: str = "groups"
OIDC_GROUP_CLAIM_PATH: str | None = None
# Browser Session Management Settings
SESSION_COOKIE_NAME: str = "bdinvite_session"
SESSION_COOKIE_SECURE: bool = False
SESSION_COOKIE_SAMESITE: Literal["lax", "strict", "none"] = "lax"
SESSION_COOKIE_PATH: str = "/"
SESSION_COOKIE_DOMAIN: str | None = None
SESSION_EXPIRE_SECONDS: int = 86400
model_config = SettingsConfigDict(env_file=".env", extra="ignore")
@property
def data_path(self) -> Path:
return Path(self.DATA_DIR).resolve()
@property
def map_preview_file(self) -> Path:
return self.data_path / "map_preview.png"
settings = Settings()