A lightweight, self-hosted multi-tenant web publishing platform

refactor(backend): shared auth dependencies, services layer, leaner dependencies #13

Merged pre1ude.dev
targeting dev from refactor/backend-structure
Merged opened by pre1ude.dev
targeting dev from refactor/backend-structure

Why #

The audit found the backend working but structured against FastAPI conventions:

  • every protected handler repeated access_token: str | None = Cookie(default=None) plus a manual require_* call, and the current user came back in three different shapes (ORM User, two different dicts);
  • utils/ was really the service layer (users, pages, files, email defense, mailer…), mixed with true helpers;
  • request models were defined inside route files;
  • [project].dependencies pinned ~25 transitive packages, passlib was kept alive only by pinning bcrypt==4.0.1, and ruff ran with only its default rules.

No behaviour changes are intended. This is a mechanical restructuring, covered by the existing suite.

What changed #

  1. Shared auth dependencies (core/deps.py)
    • get_current_user resolves the session cookie to an active User; require_admin builds on it; new Role IntEnum (NONE/USER/ADMIN/OWNER).
    • All handlers in users, keys, notifications, pages, files and admin now declare user: User = Depends(get_current_user) (or require_admin).
    • The cookie name now honours AUTH_COOKIE_NAME in one place instead of 36 hard-coded access_token parameters.
    • Removed require_authenticated_user, _extract_user_auth and admin._require_admin; utils/files helpers take a User. current_admin["id"] became current_admin.id.
    • Two tests in files_test.py that built dict users now build User objects.
  2. Dependencies and tooling
    • pyproject.toml lists only direct dependencies (the lockfile pins the rest); dropped the dotenv shim.
    • Replaced passlib with bcrypt directly. Hashes keep the $2b$ format, and passwords are explicitly truncated to 72 bytes, so existing hashes keep verifying. New test covers round-trip, hashes written by another bcrypt implementation, malformed hashes (fail closed) and long passwords.
    • Ruff now selects E4,E7,E9,F,I,B,UP,ASYNC,RUF100 (ignoring B904 and ASYNC240, with reasons in pyproject.toml).
  3. utils/ → services/: users, pages, files, notifications, email_defense, mailer, access_keys (and their tests) moved with git mv; utils/ keeps errors and network. Imports and monkeypatch strings updated.
  4. schemas/: request models from routes/files.py, routes/admin.py and routes/users.py moved into schemas/files.py, schemas/admin.py, schemas/users.py.

Each commit is self-contained and passes the checks on its own, so it can be reviewed commit by commit.

Review notes #

  • Large diff, but almost all of it is moves and import changes; git diff -M / per-commit review is much easier than the combined diff.
  • Path-depth-sensitive code (srv_root(), mailer custom dir) moved from utils/ to services/ at the same depth, and its tests pass unchanged.
  • FastAPI caches get_db per request, so get_current_user and the handler share one session; tests that override get_db keep working.

Not in this PR #

Response models for every endpoint, pydantic-settings for config, domain exceptions instead of HTTPException in services, and the model/migration cleanup (relationships, timezone-aware datetimes, ondelete, naming convention). The migration work needs a MariaDB to test against.

Testing #

  • uv run ruff format --check ., uv run ruff check ., uv run basedpyright ., uv run pytest — all clean.
  • Not tested: running the app against MariaDB via make dev.

Merge notes #

Based on dev after the security and CI commits. pyproject.toml and uv.lock change, so the prod image build (uv sync --frozen --no-dev) pulls a different dependency set; worth a make build before deploying.

Commits 4

refactor(backend): move inline request models into schemas/
refactor(backend): move domain logic from utils/ to services/
refactor(backend): trim dependencies, drop passlib, enable more ruff rules
refactor(backend): shared auth dependencies (core/deps.py)
Sign up or Sign in to add to the discussion.