Phase 3d (docs/design.md): two-factor sign-in, required for sensitive permissions. - **Sign-in:** Better Auth's `twoFactor` plugin, using an authenticator app (TOTP) with 10 backup codes. Once it's on, signing in asks for a code from the app or a backup code, and each backup code works once. - **Account page:** a new Two-Factor Sign-In card. - To turn it on: enter your password, scan the QR code (the key is also shown as text), keep the backup codes, and confirm with a code. - Once it's on, you can make new backup codes or turn it off; both ask for the password. - If you hold sensitive permissions, the card lists the ones that need it. - **Enforcement:** the sensitive permissions are `product:remove`, `person:ban`, `person:role`, `audit:read-sensitive`, `catalog:transfer`, `catalog:suspend`, `import:grant-on-behalf` and `settings:edit` (`permissions.ts` `sensitive`). Without two-factor on, they answer 403 `two_factor_required`, and API tokens get the same check through their owner. - Routes guarded only by the moderator check now also check the permission: removing products, ban and unban, roles, and settings. - **Console:** a reminder on every console page while two-factor is needed. The People list notes staff without two-factor, and each person's page shows "two-factor on". - **Closed Better Auth admin endpoints:** `/api/auth/admin/*` now answers 404. Admins could have used these endpoints to ban people or change roles without the check or the audit log. - **Tests:** a new end-to-end test, `test/two-factor.test.ts`. Moderation tests turn two-factor on for their accounts directly (`test/two-factor.ts`). All 57 API tests pass, and type checks pass. - **Checked on the scratch hub through the website:** - turning it on with a real code - the console reminder appearing and disappearing - signing in again needing a code - a Ban form while two-factor is off showing "Turn on two-factor sign-in on your account page first." - **Not checked in a browser:** the QR and sign-in code screens. - **Dependency:** `uqr` draws the QR code; it has no dependencies of its own. Phase 3d (docs/design.md): two-factor sign-in, required for sensitive permissions. - **Sign-in:** Better Auth's `twoFactor` plugin, using an authenticator app (TOTP) with 10 backup codes. Once it's on, signing in asks for a code from the app or a backup code, and each backup code works once. - **Account page:** a new Two-Factor Sign-In card. - To turn it on: enter your password, scan the QR code (the key is also shown as text), keep the backup codes, and confirm with a code. - Once it's on, you can make new backup codes or turn it off; both ask for the password. - If you hold sensitive permissions, the card lists the ones that need it. - **Enforcement:** the sensitive permissions are `product:remove`, `person:ban`, `person:role`, `audit:read-sensitive`, `catalog:transfer`, `catalog:suspend`, `import:grant-on-behalf` and `settings:edit` (`permissions.ts` `sensitive`). Without two-factor on, they answer 403 `two_factor_required`, and API tokens get the same check through their owner. - Routes guarded only by the moderator check now also check the permission: removing products, ban and unban, roles, and settings. - **Console:** a reminder on every console page while two-factor is needed. The People list notes staff without two-factor, and each person's page shows "two-factor on". - **Closed Better Auth admin endpoints:** `/api/auth/admin/*` now answers 404. Admins could have used these endpoints to ban people or change roles without the check or the audit log. - **Tests:** a new end-to-end test, `test/two-factor.test.ts`. Moderation tests turn two-factor on for their accounts directly (`test/two-factor.ts`). All 57 API tests pass, and type checks pass. - **Checked on the scratch hub through the website:** - turning it on with a real code - the console reminder appearing and disappearing - signing in again needing a code - a Ban form while two-factor is off showing "Turn on two-factor sign-in on your account page first." - **Not checked in a browser:** the QR and sign-in code screens. - **Dependency:** `uqr` draws the QR code; it has no dependencies of its own. Later: auditing when two-factor is turned on or off, and an optional "trust this browser". 🤖 Generated with [Claude Code](https://claude.com/claude-code) Later: auditing when two-factor is turned on or off, and an optional "trust this browser". 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: dhkatz <[email protected]>
97 lines
3.2 KiB
TypeScript
97 lines
3.2 KiB
TypeScript
import Link from "next/link";
|
|
import { notFound, redirect } from "next/navigation";
|
|
import { cache } from "react";
|
|
import { NavLink } from "@/components/header-client";
|
|
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
|
import {
|
|
api,
|
|
getMe,
|
|
isModerator,
|
|
unwrap,
|
|
type ConsoleData,
|
|
type Person,
|
|
} from "@/lib/api";
|
|
|
|
/** The signed-in moderator, or off to sign in; others get not found (these pages aren't for them). */
|
|
export async function requireModerator(path: string): Promise<Person> {
|
|
const me = await getMe();
|
|
if (!me) redirect(`/signin?next=${encodeURIComponent(path)}`);
|
|
if (!isModerator(me)) notFound();
|
|
return me;
|
|
}
|
|
|
|
/** The console as this person sees it (once per request); undefined if it isn't for them. */
|
|
const loadConsole = cache(async (): Promise<ConsoleData | undefined> => {
|
|
const answer = await api.console.get();
|
|
if (answer.error) return undefined;
|
|
return unwrap(answer);
|
|
});
|
|
|
|
/**
|
|
* The signed-in person and their console, or off to sign in. People who manage nothing get not found: the console
|
|
* is for those controlling a product, catalog or room.
|
|
*/
|
|
export async function requireConsole(
|
|
path: string,
|
|
): Promise<{ me: Person; console: ConsoleData }> {
|
|
const me = await getMe();
|
|
if (!me) redirect(`/signin?next=${encodeURIComponent(path)}`);
|
|
const data = await loadConsole();
|
|
if (!data) notFound();
|
|
return { me, console: data };
|
|
}
|
|
|
|
type Section = ConsoleData["sections"][number];
|
|
|
|
// Settings are on the overview.
|
|
const tabs: { section: Section; href: string; label: string }[] = [
|
|
{ section: "overview", href: "/console", label: "Overview" },
|
|
{ section: "products", href: "/console/products", label: "Products" },
|
|
{ section: "catalogs", href: "/console/catalogs", label: "Catalogs" },
|
|
{ section: "cases", href: "/console/cases", label: "Cases" },
|
|
{ section: "people", href: "/console/people", label: "People" },
|
|
{ section: "blocks", href: "/console/blocks", label: "Blocks" },
|
|
{ section: "audit", href: "/console/audit", label: "Audit" },
|
|
];
|
|
|
|
/** The console's sections this person may see, and a reminder when sensitive actions need two-factor sign-in. */
|
|
export async function ConsoleNav() {
|
|
const data = await loadConsole();
|
|
const sections = new Set<Section>(data?.sections ?? ["overview"]);
|
|
const needed = data?.twoFactorNeededFor ?? [];
|
|
return (
|
|
<>
|
|
{needed.length > 0 && (
|
|
<Alert className="mb-6">
|
|
<AlertTitle>Turn on two-factor sign-in</AlertTitle>
|
|
<AlertDescription>
|
|
<span>
|
|
Some of what you can do here needs it ({needed.join(", ")}).{" "}
|
|
<Link
|
|
href="/account"
|
|
className="text-foreground underline underline-offset-4"
|
|
>
|
|
Turn it on in your account
|
|
</Link>
|
|
.
|
|
</span>
|
|
</AlertDescription>
|
|
</Alert>
|
|
)}
|
|
<nav className="mb-8 flex gap-1 border-b pb-3 text-sm">
|
|
{tabs
|
|
.filter((t) => sections.has(t.section))
|
|
.map((t) => (
|
|
<NavLink
|
|
key={t.section}
|
|
href={t.href}
|
|
exact={t.section === "overview"}
|
|
>
|
|
{t.label}
|
|
</NavLink>
|
|
))}
|
|
</nav>
|
|
</>
|
|
);
|
|
}
|