Skip to content

Auth Service Core Extraction — Implementation Plan

For agentic workers: Use TDD per task. Phase 1 only (no MFA). Do not commit unless the user asks.

Goal: Move register / login / tenant provision / profile update orchestration from backend/routers/auth.py into backend/services/auth_service.py without changing HTTP behaviour.

Architecture: Router keeps HTTP (cookies, JWT minting, raise_as_http). Service raises DomainError subtypes. Repository stays SQL-only. MFA endpoints stay in the router until Phase 2.

Tech Stack: FastAPI, psycopg AsyncConnection, pytest, existing auth_repo / domain_errors / field_crypto.

  • No SQL in routers; no HTTPException in services.
  • Preserve invite-only register behaviour and login timing/MFA-required response shape.
  • Hoist Core-related imports to module top; leave MFA inline imports for Phase 2 if still present.
  • Verify with pytest tests/test_auth_routes.py -m "not slow".

Files:

  • Create: backend/services/auth_service.py

  • Test: backend/tests/test_auth_service.py

  • Write failing test for slug_from_name

  • Implement slug_from_name

  • Green

Task 2: Register + login + update_profile service

Section titled “Task 2: Register + login + update_profile service”

Files:

  • Modify: backend/services/auth_service.py

  • Test: unit/integration via test_auth_service.py where practical; rely on route tests for full coverage

  • Add register_user, authenticate_password, provision_tenant, update_profile, user_profile_payload

  • Domain errors: Forbidden/BadRequest/Conflict + DomainError(..., http_status=401) for invalid credentials

Files:

  • Modify: backend/routers/auth.py

  • Wire register / login / update_me / me payload helper to service

  • Remove moved helpers and build_set_clause import from router

  • Hoist Core imports

  • pytest tests/test_auth_routes.py tests/test_auth_service.py -m "not slow" -q → 39 passed
  • python scripts/check_router_sql.py → OK
  • Update findings JSON remediation note
  • Extract MFA setup/enable/disable/verify orchestration into auth_service
  • Re-verify route + unit tests (39 passed)