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
|
|
@ -157,6 +157,20 @@
|
|||
.nav-item.primary .ico { color: var(--magenta); }
|
||||
.nav-sep { height: 1px; background: var(--line); margin: 0.35rem 0.3rem; }
|
||||
|
||||
/* ═══════════════ ADMIN GATE (UX only — the API is the real gate) ═══
|
||||
Elements marked data-admin stay hidden until /api/auth/me reports a
|
||||
valid admin session (body.is-admin). Fails closed: no JS => hidden. */
|
||||
body:not(.is-admin) [data-admin] { display: none !important; }
|
||||
.admin-link {
|
||||
font-size: 0.62rem; letter-spacing: 0.16em; text-transform: uppercase;
|
||||
color: var(--muted); text-decoration: none; border: 1px solid var(--line);
|
||||
border-radius: 3px; padding: 0.28rem 0.5rem; white-space: nowrap;
|
||||
transition: color .15s, border-color .15s;
|
||||
}
|
||||
.admin-link:hover, .admin-link:focus-visible { color: var(--cyan); border-color: var(--cyan); }
|
||||
body.is-admin .admin-link { color: var(--magenta); border-color: var(--magenta); }
|
||||
|
||||
|
||||
/* ═══════════════ STAGE / VIEWS ═══════════════ */
|
||||
.stage {
|
||||
/* 100vh first for old browsers; 100dvh shrinks with mobile URL chrome */
|
||||
|
|
@ -875,6 +889,7 @@
|
|||
<span class="sep hide-sm" id="swpc-sep" hidden>|</span>
|
||||
<span class="hide-sm" id="swpc-chip" hidden title="NOAA SWPC planetary K-index">Kp —</span>
|
||||
</div>
|
||||
<a class="admin-link" id="admin-link" href="/login" data-anon-label="SIGN IN">SIGN IN</a>
|
||||
<div class="nav">
|
||||
<button id="nav-btn" class="nav-btn" aria-haspopup="true" aria-expanded="false">
|
||||
MENU <span class="nav-caret">▾</span>
|
||||
|
|
@ -886,10 +901,10 @@
|
|||
<button class="nav-item" role="menuitem" data-view="events"><span class="ico">≣</span>Events</button>
|
||||
<button class="nav-item" role="menuitem" data-view="alerts"><span class="ico">⚠</span>Alerts</button>
|
||||
<button class="nav-item" role="menuitem" data-view="entities"><span class="ico">◎</span>Entities</button>
|
||||
<button class="nav-item" role="menuitem" data-view="ingest"><span class="ico">⇪</span>Ingest</button>
|
||||
<button class="nav-item" role="menuitem" data-view="keys"><span class="ico">⚿</span>API Keys</button>
|
||||
<div class="nav-sep" role="separator"></div>
|
||||
<button class="nav-item" role="menuitem" data-view="settings"><span class="ico">⚙</span>Settings</button>
|
||||
<button class="nav-item" role="menuitem" data-view="ingest" data-admin><span class="ico">⇪</span>Ingest</button>
|
||||
<button class="nav-item" role="menuitem" data-view="keys" data-admin><span class="ico">⚿</span>API Keys</button>
|
||||
<div class="nav-sep" role="separator" data-admin></div>
|
||||
<button class="nav-item" role="menuitem" data-view="settings" data-admin><span class="ico">⚙</span>Settings</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -933,7 +948,7 @@
|
|||
<div class="lp-opacity">Opacity <input type="range" id="lp-base-opacity" min="5" max="100" value="100" oninput="setBaseOpacity(this.value)"><b id="lp-base-val">100%</b></div>
|
||||
</div>
|
||||
<div class="lp-layer">
|
||||
<div class="lp-row gf-draw-row">
|
||||
<div class="lp-row gf-draw-row" data-admin>
|
||||
<button class="btn" id="gf-draw" type="button" onclick="toggleGeofenceDraw()" title="Tap vertices, then Finish">Draw geofence</button>
|
||||
<button class="btn" id="gf-finish" type="button" hidden onclick="onGfClose(event)">Finish</button>
|
||||
<button class="btn" id="gf-cancel" type="button" hidden onclick="cancelGeofenceDraw()">Cancel</button>
|
||||
|
|
@ -1238,7 +1253,7 @@
|
|||
</section>
|
||||
|
||||
<!-- ───────────── VIEW: INGEST ───────────── -->
|
||||
<section class="view" id="view-ingest" aria-label="Ingest">
|
||||
<section class="view" id="view-ingest" aria-label="Ingest" data-admin>
|
||||
<div class="subview">
|
||||
<h2><span class="tick">⇪</span> Data Ingestion</h2>
|
||||
<p class="sub">Manually trigger feed pulls & NATS processing</p>
|
||||
|
|
@ -1271,7 +1286,7 @@
|
|||
</section>
|
||||
|
||||
<!-- ───────────── VIEW: API KEYS ───────────── -->
|
||||
<section class="view" id="view-keys" aria-label="API keys">
|
||||
<section class="view" id="view-keys" aria-label="API keys" data-admin>
|
||||
<div class="subview">
|
||||
<h2><span class="tick">⚿</span> API Keys</h2>
|
||||
<p class="sub">Keys used by ingest services (FIRMS, Nous Portal, Telegram, AIS, …). Stored in Postgres, never shown again — only status + last 4 chars.</p>
|
||||
|
|
@ -1298,7 +1313,7 @@
|
|||
</div>
|
||||
<div class="set-saved" id="mkt-saved"></div>
|
||||
</div>
|
||||
<div class="panel">
|
||||
<div class="panel" data-admin>
|
||||
<h3>News Summarizer</h3>
|
||||
<p class="set-note" style="margin-bottom:0.9rem">Takes effect on the next 15-min run. No container restart.</p>
|
||||
<div class="set-row">
|
||||
|
|
@ -1626,6 +1641,40 @@ function initHudKeys() {
|
|||
});
|
||||
}
|
||||
|
||||
/* ═══════════════ ADMIN SESSION (UX gate — the API is the real gate) ═══════
|
||||
Anonymous visitors get the read-only map + news. Admin-only chrome is
|
||||
marked data-admin and revealed only when /api/auth/me reports a session. */
|
||||
let IS_ADMIN = false;
|
||||
let AUTH_READY = false;
|
||||
const ADMIN_VIEWS = ['keys', 'ingest', 'settings'];
|
||||
|
||||
async function initAuth() {
|
||||
try {
|
||||
const r = await fetch(`${API}/api/auth/me`, {cache: 'no-store', credentials: 'same-origin'});
|
||||
const d = r.ok ? await r.json() : {};
|
||||
IS_ADMIN = !!(d && d.admin);
|
||||
} catch (e) {
|
||||
IS_ADMIN = false;
|
||||
}
|
||||
AUTH_READY = true;
|
||||
document.body.classList.toggle('is-admin', IS_ADMIN);
|
||||
const link = document.getElementById('admin-link');
|
||||
if (link) {
|
||||
link.textContent = IS_ADMIN ? 'SIGN OUT' : (link.dataset.anonLabel || 'SIGN IN');
|
||||
link.title = IS_ADMIN ? 'End admin session' : 'Admin sign-in';
|
||||
link.setAttribute('aria-label', IS_ADMIN ? 'Sign out of admin session' : 'Admin sign-in');
|
||||
}
|
||||
}
|
||||
|
||||
async function onAdminLink(ev) {
|
||||
if (!IS_ADMIN) return; // anonymous: plain link to /login
|
||||
ev.preventDefault();
|
||||
try {
|
||||
await fetch(`${API}/api/auth/logout`, {method: 'POST', credentials: 'same-origin'});
|
||||
} catch (e) { /* cookie expires anyway */ }
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
/* ═══════════════ NAV (dropdown) ═══════════════ */
|
||||
const VIEWS = ['map', 'news', 'events', 'alerts', 'entities', 'ingest', 'keys', 'settings'];
|
||||
|
||||
|
|
@ -1672,6 +1721,7 @@ function initNav() {
|
|||
|
||||
function showView(name) {
|
||||
if (!VIEWS.includes(name)) name = 'map';
|
||||
if (AUTH_READY && !IS_ADMIN && ADMIN_VIEWS.includes(name)) name = 'map';
|
||||
VIEWS.forEach(v => {
|
||||
document.getElementById('view-' + v).classList.toggle('active', v === name);
|
||||
});
|
||||
|
|
@ -4154,6 +4204,8 @@ async function loadConflicts(paint) {
|
|||
}
|
||||
|
||||
/* ═══════════════ INITIAL LOAD ═══════════════ */
|
||||
document.getElementById('admin-link').addEventListener('click', onAdminLink);
|
||||
initAuth();
|
||||
initNav();
|
||||
initHudKeys();
|
||||
initSettings();
|
||||
|
|
@ -4493,13 +4545,13 @@ async function loadGeofences() {
|
|||
const on = f.active === false ? '' : ' checked';
|
||||
const sel = f.id === gfSelectedId ? ' selected' : '';
|
||||
return `<div class="gf-item${sel}" data-id="${id}">` +
|
||||
`<input class="gf-active" type="checkbox"${on} aria-label="Active" ` +
|
||||
`<input class="gf-active" type="checkbox"${on} aria-label="Active" data-admin ` +
|
||||
`onchange="patchGfActive('${id}', this.checked)">` +
|
||||
`<button type="button" class="gf-name" title="${name}" onclick="onGfNameClick('${id}')">${name}</button>` +
|
||||
`<span class="gf-actions">` +
|
||||
`<button class="gf-go" type="button" onclick="goGeofence('${id}')">Go</button>` +
|
||||
`<button class="gf-ren" type="button" aria-label="Rename" onclick="renameGeofence('${id}')">Aa</button>` +
|
||||
`<button class="gf-del" type="button" onclick="deleteGeofence('${id}')">Delete</button>` +
|
||||
`<button class="gf-ren" type="button" aria-label="Rename" data-admin onclick="renameGeofence('${id}')">Aa</button>` +
|
||||
`<button class="gf-del" type="button" data-admin onclick="deleteGeofence('${id}')">Delete</button>` +
|
||||
`</span></div>`;
|
||||
}).join('');
|
||||
}
|
||||
|
|
@ -4524,8 +4576,8 @@ async function loadGeofences() {
|
|||
const chk = active ? ' checked' : '';
|
||||
layer.bindPopup(
|
||||
`<div class="gf-pop"><b>${name}</b><br>` +
|
||||
`<label><input type="checkbox"${chk} onchange="patchGfActive('${newsEsc(id)}', this.checked)"> active</label><br>` +
|
||||
`<button class="gf-del" type="button" onclick="deleteGeofence('${newsEsc(id)}')">Delete</button></div>`
|
||||
`<label data-admin><input type="checkbox"${chk} onchange="patchGfActive('${newsEsc(id)}', this.checked)"> active</label><br>` +
|
||||
`<button class="gf-del" type="button" data-admin onclick="deleteGeofence('${newsEsc(id)}')">Delete</button></div>`
|
||||
);
|
||||
},
|
||||
}).addTo(map);
|
||||
|
|
|
|||
Loading…
Reference in a new issue