feat: persistent sessions and OIDC sign-in (auth M2 + M3)

Sessions and users (M2)
- users and sessions tables in SQLite; only a digest of the session ID is stored
- idle 30 min / absolute 8 h deadlines, UPDATE-only touch, per-user revoke by generation
- emergency operator is a users row (issuer local:emergency); its sessions are
  bound to the operator hash fingerprint, so unsetting or rotating it ends them
- CSRF derived from the session ID; GET /api/v1/me

OIDC login (M3)
- openidconnect 4.1: authorization code + PKCE S256, static redirect URI, no redirects
- one-time login transactions bound to the browser by a Lax correlation cookie
- identity is (issuer, subject); admin only from configured subjects/groups;
  a signed-in user without a grant gets 403 on management APIs
- OIDC sessions are bound to the OIDC configuration fingerprint
- SSO button, no-access page and fixed error codes in the web UI
- tests against a mock IdP with signed and malformed tokens; rehearsed against
  Keycloak 26.1 in a real browser
- rsa 0.9 advisory (RUSTSEC-2023-0071) excepted with a written reason: runtime
  code only verifies signatures with public keys

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 13:43:09 +07:00
parent a9ebded5ba
commit 618681fa7f
23 changed files with 3695 additions and 139 deletions

View File

@@ -1,8 +1,25 @@
import { useState, type FormEvent, type ReactNode } from "react";
import { useEffect, useState, type FormEvent, type ReactNode } from "react";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { fetchOperatorSession, loginOperator, logoutOperator } 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;
}
export function OperatorGate({
children,
}: {
@@ -17,6 +34,7 @@ export function OperatorGate({
});
const [pending, setPending] = useState(false);
const [error, setError] = useState<string>();
const ssoError = useSsoError();
async function submit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
@@ -37,44 +55,70 @@ export function OperatorGate({
}
}
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, 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 ? async () => {
await queryClient.cancelQueries({ queryKey: ["operator-session"] });
await logoutOperator();
queryClient.setQueryData(["operator-session"], {
login_enabled: true, authorized: false, actor: null, csrf: null,
});
queryClient.removeQueries({ predicate: (query) => query.queryKey[0] !== "operator-session" });
} : undefined,
});
return children({ actor, onLogout: actor ? signOut : undefined });
}
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">Operator sign-in</h1>
<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.login_enabled ? (
<form onSubmit={submit} className="mt-5 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>
) : 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>