feat: per-orbit access grants and an Access console (auth M5)
Authorization - one gate for every human endpoint: authenticate, resolve the resource, then admin_only / scoped / visible; require_admin* removed so each handler must choose - grants (user or IdP group; global or one orbit; optional end date) read live on every request; only orbit.read, snapshot.read, capture.run and snapshot.download can be granted, everything else stays with configured administrators - an orbit without orbit.read is 404, a visible one without the permission is 403, a caller with nothing to see gets 403 on lists; job history is filtered before LIMIT - jobs and audit record the stable principal id, not the IdP display name - restore targets and restore history are admin-only until self-service (M6) - sessions keep the groups verified at sign-in Administration - /api/v1/access grants and users (enable, disable, sign out everywhere), validated and audited by id; admins cannot disable themselves or the emergency operator - Access console with the shared Select and a themed DatePicker; non-administrators only see Vault and Activity and never call administrator endpoints - the Window/Radar view is stored per account (/api/v1/me/preferences) - UI tolerates older hubs and mocks (missing admin/grants) and a console that fails to render no longer blanks the app Tests: a matrix generated from the endpoint contract exercises every human route against real resources in two orbits for each kind of caller. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { useEffect, useState, type FormEvent, type ReactNode } from "react";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { fetchOperatorSession, loginOperator, logoutOperator } from "../../api";
|
||||
import { fetchMe, fetchOperatorSession, loginOperator, logoutOperator, type Me } from "../../api";
|
||||
import { Button } from "../ui/Button";
|
||||
|
||||
const SSO_ERRORS: Record<string, string> = {
|
||||
@@ -20,10 +20,25 @@ function useSsoError() {
|
||||
return code ? SSO_ERRORS[code] ?? SSO_ERRORS.failed : undefined;
|
||||
}
|
||||
|
||||
/** What the signed-in user may do. The hub enforces it; this only decides what is worth showing. */
|
||||
export type Caps = {
|
||||
admin: boolean;
|
||||
can: (permission: string, orbitId?: string) => boolean;
|
||||
};
|
||||
|
||||
export function capsFrom(me: Me | undefined, admin: boolean): Caps {
|
||||
return {
|
||||
admin,
|
||||
can: (permission, orbitId) =>
|
||||
// A hub or mock that does not send grants simply grants nothing here; the server decides anyway.
|
||||
admin || (Array.isArray(me?.grants) && me.grants.some((g) => g.permission === permission && (g.orbit_id === null || g.orbit_id === orbitId))),
|
||||
};
|
||||
}
|
||||
|
||||
export function OperatorGate({
|
||||
children,
|
||||
}: {
|
||||
children: (operator: { actor?: string; onLogout?: () => Promise<void> }) => ReactNode;
|
||||
children: (operator: { actor?: string; onLogout?: () => Promise<void>; caps: Caps }) => ReactNode;
|
||||
}) {
|
||||
const queryClient = useQueryClient();
|
||||
const session = useQuery({
|
||||
@@ -32,6 +47,8 @@ export function OperatorGate({
|
||||
refetchInterval: 15_000,
|
||||
retry: false,
|
||||
});
|
||||
const signedIn = session.isSuccess && session.data.authorized;
|
||||
const me = useQuery({ queryKey: ["me"], queryFn: ({ signal }) => fetchMe(signal), enabled: signedIn, refetchInterval: 15_000, retry: false });
|
||||
const [pending, setPending] = useState(false);
|
||||
const [error, setError] = useState<string>();
|
||||
const ssoError = useSsoError();
|
||||
@@ -61,14 +78,20 @@ export function OperatorGate({
|
||||
await logoutOperator();
|
||||
queryClient.setQueryData(["operator-session"], {
|
||||
login_enabled: session.data.login_enabled, oidc_enabled: session.data.oidc_enabled,
|
||||
authorized: false, actor: null, csrf: null, expires_at: null,
|
||||
authorized: false, admin: false, actor: null, csrf: null, expires_at: null,
|
||||
});
|
||||
queryClient.removeQueries({ predicate: (query) => query.queryKey[0] !== "operator-session" });
|
||||
}
|
||||
|
||||
if (session.isSuccess && session.data.authorized) {
|
||||
const actor = session.data.actor ?? undefined;
|
||||
return children({ actor, onLogout: actor ? signOut : undefined });
|
||||
// Hold the app back for a moment so a viewer never fires an administrator-only request while permissions load.
|
||||
// A hub older than the access model sends no `admin` flag and only ever had administrators.
|
||||
const admin = session.data.admin ?? true;
|
||||
if (!admin && !me.isSuccess) {
|
||||
return <main className="vault-app flex min-h-screen items-center justify-center"><p role="status" className="text-star-300">Loading your access…</p></main>;
|
||||
}
|
||||
return children({ actor, onLogout: actor ? signOut : undefined, caps: capsFrom(me.data, admin) });
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user