feat(auth): public read-only HUD, admin-only API keys and mutations
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.
This commit is contained in:
parent
38eb4fe4d6
commit
c0a2454fdd
11 changed files with 1283 additions and 14 deletions
79
docs/auth.md
Normal file
79
docs/auth.md
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
# Admin authentication
|
||||
|
||||
The dashboard is **public read-only**: anyone can open the map and the news
|
||||
brief. One admin (the operator) additionally gets API-key management, settings
|
||||
and every state-changing route. There is no user table, no RBAC and no OAuth —
|
||||
one admin, one signed cookie (`app/auth.py`).
|
||||
|
||||
## Surface
|
||||
|
||||
| Route | Anonymous | Admin |
|
||||
| --- | --- | --- |
|
||||
| `GET /`, `GET /login`, `GET /static/*`, `WS /ws/live` | ✅ | ✅ |
|
||||
| Read allowlist (`GET /api/news`, `/api/map/*`, feeds, analytics, cameras, `POST /api/search`, …) | ✅ | ✅ |
|
||||
| `GET/POST/DELETE /api/keys*` | 401 | ✅ |
|
||||
| `GET/PUT /api/settings`, `GET /api/news/models` | 401 | ✅ |
|
||||
| Every other `POST/PUT/PATCH/DELETE` (`/api/ingest/*`, `/api/sources`, events, entities, alerts, geofences, `/api/vessels/subscribe`) | 401 | ✅ |
|
||||
| `GET /api/auth/me` | 200 `{"authenticated": false, "admin": false}` | 200, both `true` |
|
||||
| `POST /api/auth/login` | — | 200 + `Set-Cookie` |
|
||||
| `POST /api/auth/logout` | 200 (clears cookie) | 200 |
|
||||
|
||||
Enforcement is **deny-by-default on `/api/*`** in `AdminAuthMiddleware`
|
||||
(`app/auth.py`); the public list is an explicit allowlist (`PUBLIC_GET_EXACT`,
|
||||
`PUBLIC_GET_PATTERNS`, `PUBLIC_POST_EXACT`). Naming a path there is the only way
|
||||
to make it public — never a per-route decorator in `app/main.py`.
|
||||
|
||||
## Configuration
|
||||
|
||||
Three environment variables (cluster: Secret `osint-admin`):
|
||||
|
||||
| Variable | Meaning |
|
||||
| --- | --- |
|
||||
| `ADMIN_USER` | Admin username. |
|
||||
| `ADMIN_PASSWORD_HASH` | `pbkdf2_sha256$<iterations>$<salt_b64>$<hash_b64>`. |
|
||||
| `ADMIN_SESSION_SECRET` | HMAC-SHA256 key for the session token. |
|
||||
|
||||
```bash
|
||||
python app/auth.py hash 'your password' # password hash
|
||||
python -c 'import secrets; print(secrets.token_urlsafe(48))' # session secret
|
||||
```
|
||||
|
||||
Put the values into a Secret and reference them from the deployment; never
|
||||
commit them.
|
||||
|
||||
### Fail closed
|
||||
|
||||
If `ADMIN_PASSWORD_HASH` **or** `ADMIN_SESSION_SECRET` is unset/empty:
|
||||
|
||||
* every admin endpoint answers `403 {"detail": "admin auth not configured"}`,
|
||||
* `/login` renders that message instead of the form,
|
||||
* `/api/auth/login` also answers 403,
|
||||
* public pages and the read allowlist still work.
|
||||
|
||||
There is no default password and no bypass.
|
||||
|
||||
## Session cookie
|
||||
|
||||
`osint_session`, HMAC-SHA256 signed base64url JSON payload `{v,sub,iat,exp}`,
|
||||
`HttpOnly`, `SameSite=Lax`, `Path=/`, 7-day lifetime, `Secure` when the request
|
||||
arrived over https (`X-Forwarded-Proto`, i.e. behind Cloudflare). Signature and
|
||||
expiry are checked with `hmac.compare_digest`; a token for another username, or
|
||||
one signed with a different secret, is rejected.
|
||||
|
||||
## Abuse controls
|
||||
|
||||
* **Login rate limit:** 5 failed attempts per client IP (first hop of
|
||||
`X-Forwarded-For`, else the socket peer) per 15 minutes, then `429`.
|
||||
In-memory — fine for the single replica.
|
||||
* **CSRF:** state-changing admin requests that carry `Origin`/`Referer` must
|
||||
match the request host, else `403`. Header-less clients (curl,
|
||||
service-to-service) rely on the signed SameSite=Lax cookie.
|
||||
|
||||
## HUD
|
||||
|
||||
`app/static/index.html` hides admin chrome with `body:not(.is-admin)
|
||||
[data-admin]` and adds the class only when `GET /api/auth/me` reports
|
||||
`admin: true`. The menu's Ingest / API Keys / Settings entries, the summarizer
|
||||
panel and the geofence draw/rename/delete controls are admin-only; anonymous
|
||||
visitors see a discreet `SIGN IN` link to `/login`. This is UX only — the API
|
||||
gate is what actually protects the data.
|
||||
Loading…
Reference in a new issue