- FastAPI-App mit SQLite-Schema (admins, sessions, connections) - Login mit Argon2, serverseitige Sessions (Token nur gehasht gespeichert) - Cookie HttpOnly/Secure/SameSite=Strict, CSRF-Token für schreibende Requests - scripts/create_admin.py zum Anlegen und Zurücksetzen von Admins - Einfaches Frontend mit Login - pytest-Tests, gepinnte requirements.txt, .env.example, .gitignore - SETUP.md Teil 4 ergänzt Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
"""Konfiguration aus Umgebungsvariablen (optional aus einer .env-Datei)."""
|
|
|
|
import os
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
PROJEKT_ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
def _lade_env_datei(pfad: Path) -> None:
|
|
"""Einfacher .env-Leser: KEY=VALUE pro Zeile, # für Kommentare.
|
|
|
|
Bereits gesetzte Umgebungsvariablen werden nicht überschrieben.
|
|
"""
|
|
if not pfad.is_file():
|
|
return
|
|
for zeile in pfad.read_text(encoding="utf-8").splitlines():
|
|
zeile = zeile.strip()
|
|
if not zeile or zeile.startswith("#") or "=" not in zeile:
|
|
continue
|
|
key, value = zeile.split("=", 1)
|
|
os.environ.setdefault(key.strip(), value.strip().strip('"').strip("'"))
|
|
|
|
|
|
def _bool(wert: str) -> bool:
|
|
return wert.strip().lower() in ("1", "true", "yes", "ja", "on")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Settings:
|
|
db_path: Path
|
|
session_hours: int
|
|
cookie_secure: bool
|
|
|
|
|
|
def get_settings() -> Settings:
|
|
_lade_env_datei(PROJEKT_ROOT / ".env")
|
|
db_path = Path(os.environ.get("IT_CONNECT_DB_PATH", "./data/it-connect.db"))
|
|
if not db_path.is_absolute():
|
|
db_path = PROJEKT_ROOT / db_path
|
|
return Settings(
|
|
db_path=db_path,
|
|
session_hours=int(os.environ.get("IT_CONNECT_SESSION_HOURS", "12")),
|
|
cookie_secure=_bool(os.environ.get("IT_CONNECT_COOKIE_SECURE", "true")),
|
|
)
|