Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/ROLE_ADOPTION_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,16 @@ The lockout-safe order proven on retiregolden.org, transplanted:
- [x] Delete the env vars from the Worker configuration and update
`SUPPORT_COMPOSITION.md`'s staff section to role-only.

## Phase 5 — the shared admin surface (GATED)
## Phase 5 — the shared admin surface ✓ (2026-07-31)

**Executed:** `@pegma/authorization-admin@0.4.0` (extracted upstream from
both hosts) composes the surface: `Admin` mapped in policy version
`pegma.dev-policy-2` to the four `admin.*` permissions; one-time
`PEGMA_ADMIN_BOOTSTRAP_PRINCIPALS` seed via the package helper (fresh
opaque ids per attempt — lifecycle history is the already-seeded signal);
`/api/admin/*` + `/staff/roles` gated on `Admin`, with the
last-administrator guard live from the first request and the by-role
holder index at `role-holder-index.v1`. Original gating record follows.

Both hosts now want the same role-management surface (lookup → grants view →
audited assign/revoke → history). Per the ecosystem's
Expand Down
9 changes: 6 additions & 3 deletions docs/SUPPORT_COMPOSITION.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ Exact published versions:
- `@pegma/support-desk-contracts@0.1.0`
- `@pegma/support-desk-core@0.1.0`
- `@pegma/support-desk-templates@0.1.0`
- `@pegma/authorization-core@0.3.0` (AccessContext resolution)
- `@pegma/authorization-policy@0.3.0` (PolicyDocumentV1 schema validation)
- `@pegma/authorization-storage@0.3.0` (D1-backed role store)
- `@pegma/authorization-admin@0.4.0` (role administration service)
- `@pegma/authorization-core@0.4.0` (AccessContext resolution)
- `@pegma/authorization-policy@0.4.0` (PolicyDocumentV1 schema validation)
- `@pegma/authorization-storage@0.4.0` (D1-backed role store)

Peers already present on the host: `@pegma/storage-core@0.4.0`,
`@pegma/storage-cloudflare-d1@0.4.0`, `@pegma/sessions@0.1.0`,
Expand All @@ -33,6 +34,7 @@ collection name, not by sharing Identity mail cursors or session rows:
| Ticket numbers | `support-desk.ticket-numbers.v1` |
| Queue projection | `support-desk.queue-index.v1` |
| Host maintenance cursors | `pegma-dev-support-maintenance` |
| Role holder index (by-role guard) | `role-holder-index.v1` |

Identity keeps its own collections and `pegma-dev-maintenance` cursors. The
two never share a cursor key.
Expand Down Expand Up @@ -157,6 +159,7 @@ Durable rate-limit policies (separate from Identity):
| `/feedback/ticket/?id=…` | Customer read + reply |
| `/staff/support` | Staff queue (Support role holders) |
| `/staff/support/ticket/?id=…` | Staff ticket detail, compose, lifecycle |
| `/staff/roles` | Role administration (Admin role holders) |

## Maintenance and health

Expand Down
60 changes: 37 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
"dependencies": {
"@modelcontextprotocol/server": "^2.0.0",
"@pegma/audit": "0.2.0",
"@pegma/authorization-core": "0.3.0",
"@pegma/authorization-identity": "0.3.0",
"@pegma/authorization-policy": "0.3.0",
"@pegma/authorization-storage": "0.3.0",
"@pegma/authorization-admin": "0.4.0",
"@pegma/authorization-core": "0.4.0",
"@pegma/authorization-identity": "0.4.0",
"@pegma/authorization-policy": "0.4.0",
"@pegma/authorization-storage": "0.4.0",
"@pegma/health": "^0.2.0",
"@pegma/identity": "0.1.2",
"@pegma/logger-cloudflare": "^0.1.1",
Expand Down
226 changes: 226 additions & 0 deletions public/staff-roles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
const statusNode = document.querySelector('#roles-status');
const signedOut = document.querySelector('#roles-signed-out');
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 principalCard = document.querySelector('#principal-card');
const principalEmail = document.querySelector('#principal-email');
const principalMeta = document.querySelector('#principal-meta');
const roleList = document.querySelector('#role-list');
const assignForm = document.querySelector('#assign-form');
const assignRole = document.querySelector('#assign-role');
const historyList = document.querySelector('#history-list');

let csrfToken = '';
let currentPrincipal = '';

function showStatus(message, isError = false) {
statusNode.textContent = message;
statusNode.dataset.tone = isError ? 'error' : 'neutral';
}

function hideAll() {
signedOut.hidden = true;
forbidden.hidden = true;
panel.hidden = true;
}

async function api(path, init = {}) {
const headers = new Headers(init.headers);
if (init.body !== undefined) {
headers.set('Content-Type', 'application/json');
}
if (csrfToken !== '') {
headers.set('X-Pegma-CSRF', csrfToken);
}
const response = await fetch(path, {
...init,
headers,
credentials: 'same-origin',
});
const body = await response.json();
if (!response.ok) {
const error = new Error(
typeof body.error === 'string' ? body.error : 'request_failed',
);
error.status = response.status;
throw error;
}
return body;
}

const MUTATION_ERRORS = {
last_administrator:
'Refused: that is the last active administrator. Grant Admin to someone else first.',
system_managed: 'Refused: that assignment is system-managed.',
duplicate_role: 'That principal already holds the role.',
already_revoked: 'That assignment is already revoked.',
rate_limited: 'Rate limited — wait a moment and try again.',
};

function describeActor(actor) {
return actor.kind === 'system'
? `system:${actor.systemId}`
: actor.principalId;
}

function renderRoles(roles) {
roleList.replaceChildren();
if (roles.length === 0) {
const empty = document.createElement('li');
empty.textContent = 'No active roles.';
roleList.append(empty);
return;
}
for (const role of roles) {
const li = document.createElement('li');
const label = document.createElement('strong');
label.textContent = role.role;
const meta = document.createElement('span');
meta.className = 'form-note';
meta.textContent = ` granted by ${describeActor(role.grantedBy)} · ${
role.managedBy === 'system' ? 'system-managed' : 'human-managed'
} `;
li.append(label, meta);
if (role.managedBy !== 'system') {
const revoke = document.createElement('button');
revoke.type = 'button';
revoke.className = 'secondary-button';
revoke.textContent = 'Revoke';
revoke.addEventListener('click', async () => {
if (
!window.confirm(
`Revoke ${role.role} from this principal? The audited record stays.`,
)
) {
return;
}
try {
const result = await api(
`/api/admin/assignments/${encodeURIComponent(role.assignmentId)}`,
{ method: 'DELETE' },
);
showStatus(
result.compensated
? 'Revoked — the guard restored the principal to keep an administrator.'
: 'Revoked.',
);
await loadPrincipal(currentPrincipal);
} catch (error) {
showStatus(
MUTATION_ERRORS[error.message] ?? `Revoke failed (${error.message}).`,
true,
);
}
});
li.append(revoke);
}
roleList.append(li);
}
}

function renderHistory(events) {
historyList.replaceChildren();
if (events.length === 0) {
const empty = document.createElement('li');
empty.textContent = 'No role history.';
historyList.append(empty);
return;
}
for (const event of events) {
const li = document.createElement('li');
const when = new Date(event.atEpochMs).toISOString();
li.textContent = `${when} — ${event.kind} ${event.role} by ${describeActor(
event.actor,
)}${event.reason ? ` (${event.reason})` : ''}`;
historyList.append(li);
}
}

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;
principalEmail.textContent = detail.principal.email;
principalMeta.textContent = `${detail.principal.principalId} · ${detail.principal.status} · created ${detail.principal.createdAt}`;
renderRoles(detail.roles);
renderHistory(history.events);
principalCard.hidden = false;
}

lookupForm.addEventListener('submit', async (event) => {
event.preventDefault();
const principalId = principalInput.value.trim();
if (principalId === '') {
return;
}
showStatus('Loading principal…');
try {
await loadPrincipal(principalId);
showStatus('Ready.');
} catch (error) {
principalCard.hidden = true;
showStatus(
error.status === 404
? 'No account with that principal id.'
: `Lookup failed (${error.message}).`,
true,
);
}
});

assignForm.addEventListener('submit', async (event) => {
event.preventDefault();
if (currentPrincipal === '') {
return;
}
const role = assignRole.value;
if (
role === 'Admin' &&
!window.confirm('Grant the Admin role? Admins can change all role assignments.')
) {
return;
}
try {
await api(
`/api/admin/principals/${encodeURIComponent(currentPrincipal)}/roles`,
{ method: 'POST', body: JSON.stringify({ role }) },
);
showStatus(`Assigned ${role}.`);
await loadPrincipal(currentPrincipal);
} catch (error) {
showStatus(
MUTATION_ERRORS[error.message] ?? `Assign failed (${error.message}).`,
true,
);
}
});

async function boot() {
hideAll();
try {
const state = await api('/api/admin/state');
csrfToken = state.csrfToken;
bootstrapWarning.hidden = state.bootstrapArmed !== true;
panel.hidden = false;
showStatus('Ready.');
} catch (error) {
if (error.status === 401) {
signedOut.hidden = false;
showStatus('Signed out.');
return;
}
if (error.status === 403) {
forbidden.hidden = false;
showStatus('No admin access.');
return;
}
showStatus(`Admin surface unavailable (${error.message}).`, true);
}
}

boot();
Loading