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.
Global Constraints
Section titled “Global Constraints”- No SQL in routers; no
HTTPExceptionin 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".
Task 1: Pure slug helper (TDD)
Section titled “Task 1: Pure slug helper (TDD)”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.pywhere 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
Task 3: Thin the router
Section titled “Task 3: Thin the router”Files:
-
Modify:
backend/routers/auth.py -
Wire register / login / update_me / me payload helper to service
-
Remove moved helpers and
build_set_clauseimport from router -
Hoist Core imports
Task 4: Verify
Section titled “Task 4: Verify”-
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
Phase 2 (after Core green)
Section titled “Phase 2 (after Core green)”- Extract MFA setup/enable/disable/verify orchestration into
auth_service - Re-verify route + unit tests (39 passed)