Backend (FastAPI): - core/auth.py: PBKDF2-HMAC-SHA256 password hashing (120k iterations, random salt), HS256 JWT creation/verification (7-day expiry, HN_SECRET_KEY via config) and get_current_user Bearer dependency - models/user.py: User model (username unique/indexed, password_hash, created_at); schemas/auth.py: register/login/token/user schemas - api/v1/auth.py: POST /api/v1/auth/register (201), /login, /logout (204), GET /me; 400 on duplicate username, 401 on bad credentials, invalid/expired/unknown tokens - requirements: PyJWT; env templates + docker-compose expose HN_SECRET_KEY (dev default, override in production) - tests: 24 new backend tests (hashing, tokens, all endpoints) Frontend (React + TypeScript): - api/client.ts: AUTH_TOKEN localStorage persistence, automatic Bearer token attachment, 401 clears the stored token, typed login/register/logout/me helpers - auth.tsx: AuthProvider (session restore + login/register/logout) and RequireAuth route guard - pages: Login, Register, SignOut and a protected /account placeholder; header shows sign-in/register or user/sign-out - styles: auth component classes in globals.css - tests: unit tests for the client, provider and pages; Playwright E2E spec covering register/login/logout, session restore, invalid token cleanup and protected-route redirects (API mocked) Note: the milestone's /api/auth/* paths map to the project's established /api/v1/* prefix (see M2); the HN_SECRET_KEY default is newzy-dev-secret-change-me.
52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
"""Application configuration.
|
|
|
|
Settings are read from environment variables so the same code runs
|
|
locally, in Docker (see ``docker-compose.yml``) and in CI.
|
|
See ``.env.example`` for the documented variable set.
|
|
"""
|
|
|
|
import os
|
|
from functools import lru_cache
|
|
|
|
# --- Application metadata -------------------------------------------------
|
|
APP_NAME = "newzy"
|
|
APP_VERSION = "0.1.0"
|
|
API_V1_PREFIX = "/api/v1"
|
|
|
|
# Default location of the SQLite database file (relative to the working
|
|
# directory). In Docker this resolves inside the mounted volume
|
|
# (``./data/db`` -> ``/app/data/db`` on the host's ``./data/db``).
|
|
DEFAULT_DATABASE_URL = "sqlite:///./data/db/newzy.db"
|
|
|
|
# Default origins allowed by CORS (frontend dev servers).
|
|
DEFAULT_CORS_ORIGINS = "http://localhost:3000,http://127.0.0.1:3000"
|
|
|
|
# Default JWT signing key (override via HN_SECRET_KEY in production —
|
|
# never run a public deployment with the dev default).
|
|
DEFAULT_SECRET_KEY = "newzy-dev-secret-change-me"
|
|
|
|
|
|
class Settings:
|
|
"""Runtime settings loaded from the environment."""
|
|
|
|
def __init__(self) -> None:
|
|
self.database_url: str = os.getenv("DATABASE_URL", DEFAULT_DATABASE_URL)
|
|
self.cors_origins: list[str] = [
|
|
origin.strip()
|
|
for origin in os.getenv("CORS_ORIGINS", DEFAULT_CORS_ORIGINS).split(",")
|
|
if origin.strip()
|
|
]
|
|
# JWT signing key for auth tokens (see core.auth).
|
|
self.secret_key: str = os.getenv("HN_SECRET_KEY", DEFAULT_SECRET_KEY)
|
|
|
|
@property
|
|
def is_sqlite(self) -> bool:
|
|
"""True when the configured database is SQLite."""
|
|
return self.database_url.startswith("sqlite")
|
|
|
|
|
|
@lru_cache
|
|
def get_settings() -> Settings:
|
|
"""Return the cached application settings."""
|
|
return Settings()
|