Tasks: 009 — Extend API Authentication¶
Conventions¶
- Status:
[ ]todo /[~]in-progress /[x]done. - Owner:
@copilot. - Each task references stable IDs from spec.md.
T-001 — make_api_key_dep factory in app.core.security¶
- Refs: FR-1, FR-2, FR-3, NFR-SEC-1, NFR-SEC-2, NFR-PERF-1, AC-8
- Owner: @copilot
- Status:
[x] - Files:
backend/app/core/security.py(edit — addmake_api_key_dep(*setting_names); refactorrequire_api_keyto call it)backend/app/core/config.py(edit — addANALYSIS_API_KEY,GRAPH_API_KEY,LSAT_API_KEYsettings)backend/tests/core/test_security.py(edit — add factory cases incl. precedence)- Steps:
- Add the three per-router settings, all default
None, with docstrings noting fallback semantics. - Implement
make_api_key_dep(*setting_names) -> Callable[..., None]that builds aDepends-able callable. Resolution: first non-emptygetattr(settings, name)wins; all empty → open. - Use
secrets.compare_digestfor the equality check (NFR-SEC-1). - Refactor
require_api_key = make_api_key_dep("AGENT_API_KEY")so spec-008 call sites are unchanged. - Add tests: empty, missing, wrong, correct (single name); precedence (override beats fallback); fallback only; all-empty.
- Verify:
cd backend && pytest tests/core/test_security.py -qpasses;mypy app/core/security.py --strictclean.
T-002 — Mount per-router dependencies in app.main¶
- Refs: FR-4, FR-5, NFR-AUDIT-1, NFR-COMPAT-1, AC-1, AC-2, AC-3, AC-4, AC-5, AC-6, AC-7, AC-9
- Owner: @copilot
- Status:
[x] - Files:
backend/app/main.py(edit — three newDependsoninclude_router; expand startup warning)backend/tests/conftest.py(edit — autouse_reset_api_keysfixture neutralisingAGENT_API_KEY,ANALYSIS_API_KEY,GRAPH_API_KEY,LSAT_API_KEY)backend/tests/api/test_analysis.py(edit — add AC-1..AC-4 cases)backend/tests/api/test_graph.py(edit — add AC-1..AC-4 cases)backend/tests/api/test_lsat.py(edit — add AC-1..AC-6 cases incl. AC-5/AC-6 isolation + override precedence)backend/tests/api/test_health.py(edit — AC-7:/healthopen under all key configs)backend/tests/api/test_agents.py(edit if needed — extend the AC-9 warning test to assert all four router groups are listed)- Steps:
- Mount each feature router with
dependencies=[Depends(make_api_key_dep("<ROUTER>_API_KEY", "AGENT_API_KEY"))]. - In
lifespan, whenAGENT_API_KEYis unset andENVIRONMENT != "development", log one WARNING listing the unprotected router groups (agents,analysis,graph,lsat). - Add the autouse fixture to
backend/tests/conftest.pyso every test starts with all four keys set toNone(per-testmonkeypatchstill wins). - Add per-router
with_shared_keyandwith_router_keyfixtures in each test module, and add the AC-numbered cases. - Verify:
cd backend && pytest -qpasses; coverage stays ≥ 75 %.
T-003 — Frontend apiFetch consolidation + header forwarding¶
- Refs: FR-6, AC-10, NFR-COVERAGE-1
- Owner: @copilot
- Status:
[x] - Files:
frontend/lib/api.ts(edit — introduceapiFetch(path, init?); refactorrunAnalysis,getGraph,gradeLsat,getJson,agentFetchto delegate)frontend/__tests__/components/AnalysisStudio.test.tsx(edit — add a "forwards X-API-Key when env set" case usingvi.stubEnv)frontend/__tests__/components/KnowledgeGraph.test.tsx(edit — same)- Steps:
- Implement
apiFetch(path: string, init?: RequestInit): Promise<Response>inlib/api.tsthat mergesinit.headerswith{ 'X-API-Key': ... }whenprocess.env.NEXT_PUBLIC_AGENT_API_KEYis non-empty. - Refactor existing helpers to use
apiFetch. KeepagentFetchas a tiny wrapper for source-compat with spec 008 callers. - Add MSW handler assertions on
/api/analysisand/api/graph/*covering both env-unset and env-set cases. - Verify:
cd frontend && npm test -- --rungreen; coverage stays ≥ 70 %.
T-004 — Document new env vars¶
- Refs: FR-7, R-2
- Owner: @copilot
- Status:
[x] - Files:
.env.example(edit — addANALYSIS_API_KEY=,GRAPH_API_KEY=,LSAT_API_KEY=under the existing Auth block; document fallback semantics and the spec-008 → 009 widening)- Steps:
- Append the three per-router keys with comments explaining the override-then-fallback rule.
- Add a one-liner that calls out the widening: "As of spec 009, AGENT_API_KEY also gates /api/analysis, /api/graph/, and /api/lsat/ unless overridden."
- Verify:
grep -nE 'ANALYSIS_API_KEY|GRAPH_API_KEY|LSAT_API_KEY' .env.exampleshows all three.
T-005 — Spec / BACKLOG hygiene¶
- Refs: constitution §11
- Owner: @copilot
- Status:
[x] - Files:
BACKLOG.md(edit — move BL-20260430-08 from## Opento## Done; append new follow-up BL items)specs/009-extend-api-auth/spec.md(editStatus:→shippedafter green CI)- Steps:
- After T-001..T-004 land green, set
Status: shippedin spec.md. - Move BL-20260430-08 to
## Donewith a one-line outcome. - Mirror new follow-ups (see below) into
BACKLOG.md ## Open. - Verify:
python scripts/spec_validator.py && python scripts/backlog_lint.py && cd backend && pytest --cov=app --cov-fail-under=65all exit 0.
Done-When (Definition of Done)¶
- All tasks
[x]. - All
AC-*fromspec.mdcovered by passing tests (AC-1..AC-10). make lint && make testgreen (backend ≥ 75 % cov on changed files, frontend ≥ 70 % cov).- BL-20260430-08 moved to
## Donein BACKLOG.md. ## Follow-ups Discoveredsection below populated.
Follow-ups Discovered¶
Append-only. Mirror each item into BACKLOG.md with a
BL-YYYYMMDD-NNid. CI (scripts/spec_validator.py) checks this section exists. CI (scripts/backlog_lint.py) checks the BACKLOG mirror.
- BL-20260430-11 — test-gap, low — Add a Playwright smoke test that toggles
AGENT_API_KEY/NEXT_PUBLIC_AGENT_API_KEYand verifies all four pages (/agents,/analysis,/graph,/lsat) load end-to-end against a Docker stack. Surfaced in: specs/009-extend-api-auth. - BL-20260430-12 — infra, low — Have
app/main.pylifespan refuse to start whenENVIRONMENT="production"andAGENT_API_KEYis unset (today it only warns). Promotes the dev-friendly default into a production guard rail. Surfaced in: specs/009-extend-api-auth. - BL-20260430-13 — doc-gap, low — Document the per-router fallback chain (
<ROUTER>_API_KEY→AGENT_API_KEY) in docs/architecture.md, including a short table mapping routes → settings. Surfaced in: specs/009-extend-api-auth.