diff --git a/docs/SUPPORT_COMPOSITION.md b/docs/SUPPORT_COMPOSITION.md index 5cfd842..de6ba63 100644 --- a/docs/SUPPORT_COMPOSITION.md +++ b/docs/SUPPORT_COMPOSITION.md @@ -146,10 +146,45 @@ with no default and styles the two modes differently. Mail / outbound notification is still deferred (Task 10) — staff reply omits `notification` like customer create/reply. +### Role administration (`/api/admin/…`) + +Gated on the stored `Admin` role (`@pegma/authorization-admin`); the host +checks permissions before calling the service, which never re-checks its own +callers. + +| Method | Path | Notes | +| --- | --- | --- | +| `GET` | `/api/admin/state` | `bootstrapArmed` + CSRF token | +| `POST` | `/api/admin/lookup` | One search box: body `{ query }`, resolves an email **or** a principal id | +| `GET` | `/api/admin/principals/:id` | Principal + active roles with `managedBy` | +| `GET` | `/api/admin/principals/:id/history` | Audited lifecycle timeline | +| `POST` | `/api/admin/principals/:id/roles` | Assign `Support` or `Admin` | +| `DELETE` | `/api/admin/assignments/:id` | Revoke; last-administrator guard | + +**One search box, decided server-side.** `classifyLookup` treats a `@` +anywhere in the query as an email and everything else as a principal id: +Identity's one normalization function requires exactly one `@` in every +address, and principal ids never contain one. Email lookups call the +published `Identity.findUserByEmail`; the host passes the trimmed value +through **without normalizing it**, because exactly one normalization +function exists and it lives in Identity (test-pinned). + +**Why the lookup is a POST.** The query can be a real person's email +address, so it stays out of URLs, referrers, and access logs; the same +choice puts CSRF and same-origin in front of the one endpoint that can +confirm an address exists. Identity's public enumeration resistance is +untouched — this path is reachable only by an authenticated `Admin` holder, +and it is rate-limited so a stolen admin session cannot harvest the +directory. + Durable rate-limit policies (separate from Identity): - `pegma.support.ticket.create` - `pegma.support.ticket.reply` (customer replies and staff message/note posts) +- `pegma.admin.role.mutate` (assign / revoke, 30/h per operator) +- `pegma.admin.principal.lookup` (directory lookups, 120/h per operator) + +All four are swept by the same scheduled maintenance run. ## Site pages diff --git a/public/staff-roles.js b/public/staff-roles.js index 968f9c7..1cb5a16 100644 --- a/public/staff-roles.js +++ b/public/staff-roles.js @@ -4,10 +4,11 @@ const forbidden = document.querySelector('#roles-forbidden'); const panel = document.querySelector('#roles-panel'); const bootstrapWarning = document.querySelector('#bootstrap-warning'); const lookupForm = document.querySelector('#lookup-form'); -const principalInput = document.querySelector('#lookup-principal'); +const lookupInput = document.querySelector('#lookup-query'); const principalCard = document.querySelector('#principal-card'); const principalEmail = document.querySelector('#principal-email'); const principalMeta = document.querySelector('#principal-meta'); +const principalMatched = document.querySelector('#principal-matched'); const roleList = document.querySelector('#role-list'); const assignForm = document.querySelector('#assign-form'); const assignRole = document.querySelector('#assign-role'); @@ -107,10 +108,11 @@ function renderRoles(roles) { ? 'Revoked — the guard restored the principal to keep an administrator.' : 'Revoked.', ); - await loadPrincipal(currentPrincipal); + await refreshPrincipal(); } catch (error) { showStatus( - MUTATION_ERRORS[error.message] ?? `Revoke failed (${error.message}).`, + MUTATION_ERRORS[error.message] ?? + `Revoke failed (${error.message}).`, true, ); } @@ -139,37 +141,84 @@ function renderHistory(events) { } } -async function loadPrincipal(principalId) { - const [detail, history] = await Promise.all([ - api(`/api/admin/principals/${encodeURIComponent(principalId)}`), - api(`/api/admin/principals/${encodeURIComponent(principalId)}/history`), - ]); - currentPrincipal = principalId; +function renderPrincipal(detail, matchedBy) { + currentPrincipal = detail.principal.principalId; principalEmail.textContent = detail.principal.email; principalMeta.textContent = `${detail.principal.principalId} · ${detail.principal.status} · created ${detail.principal.createdAt}`; + if (matchedBy === undefined) { + principalMatched.hidden = true; + } else { + principalMatched.textContent = + matchedBy === 'email' + ? 'Matched by email address.' + : 'Matched by principal id.'; + principalMatched.hidden = false; + } renderRoles(detail.roles); - renderHistory(history.events); principalCard.hidden = false; } +/** + * Every load takes a ticket. A second lookup (or a refresh after a + * mutation) invalidates any in-flight one, so a slow response can never + * paint its principal's history under a newer principal's details. + */ +let loadTicket = 0; + +/** + * Fetch details and history, then render BOTH or NEITHER: the card only + * becomes interactive once the pair belongs to the same principal. + */ +async function loadPrincipal(fetchDetail) { + const ticket = (loadTicket += 1); + const detail = await fetchDetail(); + const id = encodeURIComponent(detail.principal.principalId); + const history = await api(`/api/admin/principals/${id}/history`); + if (ticket !== loadTicket) { + return; + } + renderPrincipal(detail, detail.matchedBy); + renderHistory(history.events); +} + +/** One box: the worker decides email vs principal id. */ +async function resolvePrincipal(query) { + await loadPrincipal(() => + api('/api/admin/lookup', { + method: 'POST', + body: JSON.stringify({ query }), + }), + ); +} + +/** Re-read the already-resolved principal after a mutation. */ +async function refreshPrincipal() { + const id = encodeURIComponent(currentPrincipal); + await loadPrincipal(() => api(`/api/admin/principals/${id}`)); +} + lookupForm.addEventListener('submit', async (event) => { event.preventDefault(); - const principalId = principalInput.value.trim(); - if (principalId === '') { + const query = lookupInput.value.trim(); + if (query === '') { return; } - showStatus('Loading principal…'); + showStatus('Looking up…'); try { - await loadPrincipal(principalId); + await resolvePrincipal(query); showStatus('Ready.'); } catch (error) { principalCard.hidden = true; - showStatus( - error.status === 404 - ? 'No account with that principal id.' - : `Lookup failed (${error.message}).`, - true, - ); + if (error.status === 404) { + showStatus('No account matches that email or principal id.', true); + } else if (error.status === 400) { + showStatus( + 'That does not look like an email address or a principal id.', + true, + ); + } else { + showStatus(`Lookup failed (${error.message}).`, true); + } } }); @@ -181,7 +230,9 @@ assignForm.addEventListener('submit', async (event) => { const role = assignRole.value; if ( role === 'Admin' && - !window.confirm('Grant the Admin role? Admins can change all role assignments.') + !window.confirm( + 'Grant the Admin role? Admins can change all role assignments.', + ) ) { return; } @@ -191,7 +242,7 @@ assignForm.addEventListener('submit', async (event) => { { method: 'POST', body: JSON.stringify({ role }) }, ); showStatus(`Assigned ${role}.`); - await loadPrincipal(currentPrincipal); + await refreshPrincipal(); } catch (error) { showStatus( MUTATION_ERRORS[error.message] ?? `Assign failed (${error.message}).`, diff --git a/src/pages/staff/roles.astro b/src/pages/staff/roles.astro index 62aa43a..e0e91d7 100644 --- a/src/pages/staff/roles.astro +++ b/src/pages/staff/roles.astro @@ -48,27 +48,27 @@ import Base from '../../layouts/Base.astro';
- +

- Exact Identity principal id. Principal search by email stays outside - this surface. + Exact match either way — the surface works out which you typed.