feat(auth): public read-only HUD, admin-only API keys and mutations #50
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/public-hud-admin-auth"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What this does
Implements the spec public read-only HUD, admin-only API keys (single admin).
Enforcement is deny-by-default on
/api/*with an explicit public read allowlist in one place —app/auth.py(AdminAuthMiddleware+PUBLIC_GET_EXACT/PUBLIC_GET_PATTERNS/PUBLIC_POST_EXACT).app/main.pygains exactly 6 lines (middleware + router); no per-route decorators, no CSS-only hiding.Auth surface
GET /,GET /login,GET /static/*,WS /ws/live/api/news*,/api/map/*,/api/events,/api/entities,/api/alerts,/api/analytics/*,/api/cameras*,/api/vessels,/api/tracks/range,/api/geofences*reads,POST /api/search, …)GET/POST/DELETE /api/keys*,GET/PUT /api/settings,GET /api/news/models401JSONPOST/PATCH/PUT/DELETE(sources, events, entities, alerts, geofences,/api/vessels/subscribe, all/api/ingest/*)401JSONGET /login— server-rendered form (no build step, no framework), posts to/api/auth/login, dark HUD styling.POST /api/auth/login— form-encoded or JSON; success →{"authenticated": true, "admin": true}+Set-Cookie; failure →401 {"detail": "invalid credentials"}(generic: never reveals which half was wrong).POST /api/auth/logout— clears the cookie.GET /api/auth/me— always200,{"authenticated": bool, "admin": bool}.Credentials, exact env names (cluster Secret
osint-admin):ADMIN_USERADMIN_PASSWORD_HASH— stdlibhashlib.pbkdf2_hmac, formatpbkdf2_sha256$<iters>$<salt_b64>$<hash_b64>, constant-time compare. No new dependency. Generate:python app/auth.py hash 'your password'.ADMIN_SESSION_SECRET— HMAC-SHA256 key. Generate:python -c 'import secrets; print(secrets.token_urlsafe(48))'.Cookie
osint_session:HttpOnly,SameSite=Lax,Path=/, 7-day expiry carryingiat/exp,SecurewhenX-Forwarded-Proto: https(Cloudflare). Signature verified withhmac.compare_digest; a token for a different username or signed with another secret is rejected.Hard requirements implemented:
ADMIN_PASSWORD_HASHorADMIN_SESSION_SECRET⇒ admin routes403 {"detail": "admin auth not configured"},/loginsays so, login POST 403, public site untouched. No default password, no bypass.X-Forwarded-Forfirst hop, else peer) per 15 min →429. In-memory (single replica).POST /api/auth/logoutdeliberately does not clear the limiter (regression-tested — it is unauthenticated).Origin/Referermust match the request host (X-Forwarded-Hostaware), else403. Header-less clients (curl, service-to-service) rely on the signed SameSite=Lax cookie; documented indocs/auth.md.GET /api/keysuntouched — still set/missing + last-4 only.body:not(.is-admin) [data-admin]hides the Ingest / API Keys / Settings nav entries, the Keys view, the Ingest view, the summarizer panel and the geofence draw/rename/delete controls.initAuth()adds.is-adminonly when/api/auth/mesays admin; anonymous visitors get a discreetSIGN INlink to/login. CSS gates fail closed with JS disabled. UX only — the API is the real gate.Verification (actually run)
Suite with no database (bare checkout):
333 passed, 17 skipped— the 17 are exactly theintegration-marked tests auto-skipping withtest database unreachable (set DB_* env or start the PG container):Suite against a live TimescaleDB+PostGIS (started
osint-dashboard-pgon127.0.0.1:55432,alembic upgrade head):350 passed, 0 skipped— all integration tests included.tests/test_auth.py(43 tests) covers the whole matrix viahttpx.ASGITransport: anonymousGET /api/news200 /GET /api/keys401 /POST /api/keys/NOUS_API_KEY401 /PUT /api/settings401; anonymousGET /200 containing the HUD;/api/auth/mefalse; wrong password 401; correct password 200 +Set-Cookie(HttpOnly/SameSite/Path asserted,SecureunderX-Forwarded-Proto: https); cookie unlocksGET /api/keys+PUT /api/settings; logout ⇒ 401; tampered, foreign-user, expired and non-ASCII cookies rejected; env unset ⇒ keys 403 while news stays 200; 6 rapid failures ⇒ 429; CSRF cross-origin 403 vs same-origin 200; public allowlist classification; HUDdata-adminmarkup.Manual proof — real
uvicorn main:app --port 8099with the three env vars and a live DB (actual status codes):Anonymous sweep of every public read (37 GETs +
POST /api/search) returned200or a handler-level422for missing params — never401/403. Fail-closed verified live on a second instance started withoutADMIN_*:/api/keys→403 {"detail":"admin auth not configured"},/api/news→200,/loginrenders the "not configured" notice.Premise flags (code disagreed with the card)
GET /api/stats,/api/conflicts,/api/place,/api/satellites,/api/infrastructure,/api/map/gpsjam,/api/map/times,/api/fire-incidents,/api/fire-perimeters,/api/weather-alerts,/api/storms,/api/geofences/{id}/at. All were audited as pure reads (no writes, no stored-key spend).POST /api/searchis a read-only tsvector query → public, as the card assumed.app/static/index.html) — gated in place.test_api_settings,test_api_live_layers,test_ingest_ssrf_and_sources,test_phase2_api) now send an admin session cookie from a sharedconftest.admin_cookies()helper; a session-scoped test credential pair is seeded inconftest.py.Non-goals respected
No user table/RBAC/OAuth/Cloudflare Access, no alembic migration, no changes to
news/*,Dockerfile*,docker-compose.yml,.forgejo/workflows/*, ingester/camera/summarizer processes, no account seeder, no cluster credentials.docs/auth.mddocuments the surface, env vars and controls;.env.examplegains the threeADMIN_*placeholders.Operator follow-up (after merge — this PR is not merged here)
osint-adminin namespaceosintwithADMIN_USER,ADMIN_PASSWORD_HASH,ADMIN_SESSION_SECRET.sirius/homek8sapps/base/osint/(add the Secret as envFrom). Merge here does not deploy — and the push tomasterwill fire the legacy compose workflow on the Pi, which is why the operator merges, not CI.Adds single-admin authentication to the dashboard. Anonymous visitors get a read-only site (map + news); the one admin unlocks everything else. - app/auth.py: deny-by-default ASGI middleware on /api/* with an explicit public read allowlist; /login page; /api/auth/{login,logout,me}; PBKDF2 password hashing; HMAC-signed HttpOnly SameSite=Lax session cookie (7d, Secure behind X-Forwarded-Proto: https); 5-fail/15-min per-IP login limit; Origin/Referer host check on state-changing admin requests. - Fails closed: missing ADMIN_PASSWORD_HASH or ADMIN_SESSION_SECRET -> admin routes 403 'admin auth not configured', public site unaffected. - app/main.py: mounts the middleware + router (6 lines, no per-route decorators). - app/static/index.html: hides Ingest/API Keys/Settings, the summarizer panel and geofence write controls until /api/auth/me says admin; adds a discreet SIGN IN link to /login. CSS gates fail closed without JS. - tests/test_auth.py: 42 tests (anonymous 401s, login/logout, tampered and expired cookies, fail-closed, rate limit, CSRF, HUD markup). - Existing API tests now send an admin session cookie for admin routes. Public allowlist extends the spec's list with the remaining read-only GETs the HUD calls (/api/stats, /api/conflicts, /api/place, /api/satellites, /api/infrastructure, /api/map/gpsjam, /api/map/times, /api/fire-incidents, /api/fire-perimeters, /api/weather-alerts, /api/storms, /api/geofences/{id}/at) so an anonymous HUD is not half-broken.