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>
150 lines
7.1 KiB
TypeScript
150 lines
7.1 KiB
TypeScript
import { useEffect, useState, type FormEvent, type ReactNode } from "react";
|
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { fetchMe, fetchOperatorSession, loginOperator, logoutOperator, type Me } from "../../api";
|
|
import { Button } from "../ui/Button";
|
|
|
|
const SSO_ERRORS: Record<string, string> = {
|
|
invalid_state: "That sign-in expired or was started in another browser. Please try again.",
|
|
denied: "The identity provider declined the sign-in.",
|
|
unavailable: "The identity provider is unreachable. Try again shortly.",
|
|
disabled: "This account is disabled.",
|
|
failed: "Sign-in could not be completed. Ask an administrator if it keeps happening.",
|
|
};
|
|
|
|
/** Read the fixed error code the SSO callback leaves in the URL, then remove it. */
|
|
function useSsoError() {
|
|
const [code] = useState(() => new URLSearchParams(window.location.search).get("auth_error"));
|
|
useEffect(() => {
|
|
if (code) window.history.replaceState(null, "", window.location.pathname);
|
|
}, [code]);
|
|
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>; caps: Caps }) => ReactNode;
|
|
}) {
|
|
const queryClient = useQueryClient();
|
|
const session = useQuery({
|
|
queryKey: ["operator-session"],
|
|
queryFn: ({ signal }) => fetchOperatorSession(signal),
|
|
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();
|
|
|
|
async function submit(event: FormEvent<HTMLFormElement>) {
|
|
event.preventDefault();
|
|
const form = event.currentTarget;
|
|
const password = new FormData(form).get("password");
|
|
if (pending || typeof password !== "string" || !password) return;
|
|
setPending(true);
|
|
setError(undefined);
|
|
form.reset();
|
|
await queryClient.cancelQueries({ queryKey: ["operator-session"] });
|
|
try {
|
|
const signedIn = await loginOperator(password);
|
|
queryClient.setQueryData(["operator-session"], signedIn);
|
|
} catch {
|
|
setError("Could not sign in. Check the password or try again shortly.");
|
|
} finally {
|
|
setPending(false);
|
|
}
|
|
}
|
|
|
|
async function signOut() {
|
|
if (!session.isSuccess) return;
|
|
await queryClient.cancelQueries({ queryKey: ["operator-session"] });
|
|
await logoutOperator();
|
|
queryClient.setQueryData(["operator-session"], {
|
|
login_enabled: session.data.login_enabled, oidc_enabled: session.data.oidc_enabled,
|
|
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;
|
|
// 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 (
|
|
<main className="vault-app flex min-h-screen items-center justify-center p-5">
|
|
<section className="w-full max-w-sm rounded-2xl border border-space-700 bg-space-900 p-6 sm:p-8">
|
|
<p className="instrument-label mb-3">Tessera / Backup control</p>
|
|
<h1 className="text-2xl font-semibold text-star-100">Sign in</h1>
|
|
{session.isPending ? <p role="status" className="mt-4 text-star-300">Connecting to the hub…</p>
|
|
: session.isError ? (
|
|
<div className="mt-4 space-y-3">
|
|
<p role="alert" className="text-star-300">Could not reach the hub.</p>
|
|
<Button onClick={() => void session.refetch()}>Try again</Button>
|
|
</div>
|
|
) : session.data.actor ? (
|
|
<div className="mt-4 space-y-4">
|
|
<h2 className="text-lg font-semibold text-star-100">No access yet</h2>
|
|
<p role="status" className="text-star-300">
|
|
You are signed in as {session.data.actor}, but this account has not been granted access to Tessera.
|
|
Ask an administrator to add you, then sign in again.
|
|
</p>
|
|
<Button onClick={() => void signOut()}>Sign out</Button>
|
|
</div>
|
|
) : session.data.login_enabled || session.data.oidc_enabled ? (
|
|
<div className="mt-5 space-y-5">
|
|
{ssoError && <p role="alert" className="text-sm text-orbit-red">{ssoError}</p>}
|
|
{session.data.oidc_enabled && (
|
|
<a href="/api/v1/auth/oidc/login"
|
|
className="block w-full rounded-xl bg-star-100 px-4 py-2 text-center font-medium text-space-950">
|
|
Sign in with SSO
|
|
</a>
|
|
)}
|
|
{session.data.login_enabled && (
|
|
<details open={!session.data.oidc_enabled} className="group">
|
|
{session.data.oidc_enabled && (
|
|
<summary className="cursor-pointer text-sm text-star-300">Emergency operator sign-in</summary>
|
|
)}
|
|
<form onSubmit={submit} className="mt-3 space-y-4">
|
|
<label className="block text-sm text-star-300">
|
|
Operator password
|
|
<input type="password" name="password" autoComplete="current-password" required
|
|
maxLength={1024} className="mt-1 block w-full rounded-xl border border-space-700 bg-space-950 px-3 py-2 text-star-100" />
|
|
</label>
|
|
{error && <p role="alert" className="text-sm text-orbit-red">{error}</p>}
|
|
<Button type="submit" variant="primary" className="w-full" disabled={pending}>
|
|
{pending ? "Signing in…" : "Sign in"}
|
|
</Button>
|
|
</form>
|
|
</details>
|
|
)}
|
|
</div>
|
|
) : <p className="mt-4 text-star-300">Browser sign-in is disabled on this hub.</p>}
|
|
</section>
|
|
</main>
|
|
);
|
|
}
|