feat: personal API tokens (#11)
Phase 3, part 3 (`docs/design.md`, "Next"): personal API tokens, for scripts. Two-factor sign-in comes next as its own PR. **Making them** - On the account page, in a new API Tokens card. You give each token: - a name - how long it lasts: 7 days, 30 days, 90 days or 1 year - what it may do: permissions you hold, picked for each product of yours or catalog whose team you're on (moderators and admins can also pick everywhere) - The token is shown once, with a Copy button. Only its hash is stored. - The card lists active tokens: what each grants, when it expires, and when it was last used. Each can be revoked; revoked and expired ones are folded away below. - Tokens are made and revoked only from a website session, so a token can't make tokens. Limits: 20 active tokens, 50 grants each, 1 year at most. **Using them** - Send `Authorization: Bearer t5de_pat_...`. - A token works only on routes that check a permission: - product info, publishing, locks and history - uploading and retracting revisions of a product: these now check `revision:upload` and `revision:retract` - a catalog's settings, key, team, members, invites and hosted products - file blocks, and assigning cases - Everywhere else answers 403 `token_not_allowed`, including making a new product, reports, `/me` and the console. A wrong, revoked or expired token gets 401 `bad_token`. - Each check is the intersection of what the token grants and what the person holds now: a token never does more than its owner can, and losing a role takes it from the token at once. **Audit** - Actions taken with a token are recorded as the person, with `token: <name>` added to the details. - Making and revoking a token are audited as `token.create` and `token.revoke`. **Migration** `0014_api_tokens`: the `api_token` table. **Checks** - 56 API tests pass. The new `tokens.test.ts` covers: - only permissions held now can be granted - grants are limited to their product or catalog - session-only routes refuse tokens - uploads by token - the audit marking and last use - revoked and expired tokens - a moderator demoted while their token was active - Type checks pass. - On a local hub, through the website: - the card shows what can be granted - making a token (refused without any permission chosen); the token was shown once - using it for a granted edit and the member list; refused for what it wasn't given, and on `/me` - its last use on the account page, and the audit entry naming it - Revoke, after which the token got 401 Phase 3, part 3 (`docs/design.md`, "Next"): personal API tokens, for scripts. Two-factor sign-in comes next as its own PR. **Making them** - On the account page, in a new API Tokens card. You give each token: - a name - how long it lasts: 7 days, 30 days, 90 days or 1 year - what it may do: permissions you hold, picked for each product of yours or catalog whose team you're on (moderators and admins can also pick everywhere) - The token is shown once, with a Copy button. Only its hash is stored. - The card lists active tokens: what each grants, when it expires, and when it was last used. Each can be revoked; revoked and expired ones are folded away below. - Tokens are made and revoked only from a website session, so a token can't make tokens. Limits: 20 active tokens, 50 grants each, 1 year at most. **Using them** - Send `Authorization: Bearer t5de_pat_...`. - A token works only on routes that check a permission: - product info, publishing, locks and history - uploading and retracting revisions of a product: these now check `revision:upload` and `revision:retract` - a catalog's settings, key, team, members, invites and hosted products - file blocks, and assigning cases - Everywhere else answers 403 `token_not_allowed`, including making a new product, reports, `/me` and the console. A wrong, revoked or expired token gets 401 `bad_token`. - Each check is the intersection of what the token grants and what the person holds now: a token never does more than its owner can, and losing a role takes it from the token at once. **Audit** - Actions taken with a token are recorded as the person, with `token: <name>` added to the details. - Making and revoking a token are audited as `token.create` and `token.revoke`. **Migration** `0014_api_tokens`: the `api_token` table. **Checks** - 56 API tests pass. The new `tokens.test.ts` covers: - only permissions held now can be granted - grants are limited to their product or catalog - session-only routes refuse tokens - uploads by token - the audit marking and last use - revoked and expired tokens - a moderator demoted while their token was active - Type checks pass. - On a local hub, through the website: - the card shows what can be granted - making a token (refused without any permission chosen); the token was shown once - using it for a granted edit and the member list; refused for what it wasn't given, and on `/me` - its last use on the account page, and the audit entry naming it - Revoke, after which the token got 401 Note: the permission list also offers permissions for which no route takes tokens yet, such as `import:*` and `stats:read`. They'll work once those features have routes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Note: the permission list also offers permissions for which no route takes tokens yet, such as `import:*` and `stats:read`. They'll work once those features have routes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: dhkatz <[email protected]>
This commit was merged in pull request #11.
This commit is contained in:
1 parent
58a3e70a63
commit
69c55a72fb
17 files changed
+4410
-98
No files matched your search
@@ -0,0 +1,168 @@
|
||||
"use client";
|
||||
// Making an API token: a name, how long it lasts, and what it may do where (some of the person's own permissions).
|
||||
import { ChevronRightIcon } from "lucide-react";
|
||||
import { useActionState, useState } from "react";
|
||||
import { CopyButton } from "@/components/copy-button";
|
||||
import { ErrorText } from "@/components/forms";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select";
|
||||
import type { TokenScope } from "@/lib/api";
|
||||
import { createToken } from "./actions";
|
||||
|
||||
const keyOf = (s: TokenScope["scope"]) => (s.type === "hub" ? "hub" : `${s.type}:${s.id}`);
|
||||
const kindOf = { hub: "Everything", catalog: "Catalog", product: "Product" } as const;
|
||||
|
||||
type Row = { scope: string; permissions: string[] };
|
||||
|
||||
// Permissions are `resource:action`; they're grouped by resource, in this order (new resources go last, by name).
|
||||
const groupNames: Record<string, string> = {
|
||||
product: "Products",
|
||||
revision: "Revisions",
|
||||
catalog: "Catalog",
|
||||
team: "Team",
|
||||
member: "Members",
|
||||
hosting: "Hosting",
|
||||
import: "Imports",
|
||||
case: "Cases",
|
||||
audit: "Audit Log",
|
||||
stats: "Stats",
|
||||
person: "People",
|
||||
room: "Rooms",
|
||||
settings: "Settings",
|
||||
};
|
||||
const order = Object.keys(groupNames);
|
||||
|
||||
/** A scope's permissions, grouped by resource: [group name, permissions]. */
|
||||
function groupsOf(permissions: string[]): [string, string[]][] {
|
||||
const by = new Map<string, string[]>();
|
||||
for (const p of permissions) {
|
||||
const resource = p.split(":")[0]!;
|
||||
by.set(resource, [...(by.get(resource) ?? []), p]);
|
||||
}
|
||||
const rank = (r: string) => (order.includes(r) ? order.indexOf(r) : order.length);
|
||||
return [...by.entries()]
|
||||
.sort(([a], [b]) => rank(a) - rank(b) || a.localeCompare(b))
|
||||
.map(([r, list]) => [groupNames[r] ?? r.charAt(0).toUpperCase() + r.slice(1), list]);
|
||||
}
|
||||
|
||||
export function TokenForm({ scopes, maxDays }: { scopes: TokenScope[]; maxDays: number }) {
|
||||
const [state, formAction, pending] = useActionState(createToken, null);
|
||||
const [rows, setRows] = useState<Row[]>([{ scope: keyOf(scopes[0]!.scope), permissions: [] }]);
|
||||
const byKey = new Map(scopes.map((s) => [keyOf(s.scope), s]));
|
||||
const grants = rows.flatMap((r) => r.permissions.map((permission) => ({ permission, scope: byKey.get(r.scope)!.scope })));
|
||||
const set = (i: number, row: Row) => setRows(rows.map((r, j) => (j === i ? row : r)));
|
||||
const unused = scopes.find((s) => !rows.some((r) => r.scope === keyOf(s.scope)));
|
||||
|
||||
return (
|
||||
<form action={formAction} className="space-y-4">
|
||||
<input type="hidden" name="grants" value={JSON.stringify(grants)} />
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Input name="name" required maxLength={64} placeholder="Name, e.g. Upload script" aria-label="Name" className="max-w-xs" />
|
||||
<NativeSelect name="days" defaultValue="90" aria-label="Lasts" className="w-36">
|
||||
{[7, 30, 90, maxDays].map((d) => (
|
||||
<NativeSelectOption key={d} value={d}>
|
||||
{d === 365 ? "1 year" : `${d} days`}
|
||||
</NativeSelectOption>
|
||||
))}
|
||||
</NativeSelect>
|
||||
</div>
|
||||
{rows.map((row, i) => {
|
||||
const scope = byKey.get(row.scope)!;
|
||||
return (
|
||||
<fieldset key={i} className="space-y-2 rounded-lg border p-3">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<NativeSelect
|
||||
value={row.scope}
|
||||
onChange={(e) => set(i, { scope: e.target.value, permissions: [] })}
|
||||
aria-label="Where"
|
||||
className="min-w-56"
|
||||
>
|
||||
{scopes.map((s) => {
|
||||
const k = keyOf(s.scope);
|
||||
if (k !== row.scope && rows.some((r) => r.scope === k)) return null;
|
||||
return (
|
||||
<NativeSelectOption key={k} value={k}>
|
||||
{s.scope.type === "hub" ? "Everything on T5DE" : `${kindOf[s.scope.type]}: ${s.name}`}
|
||||
</NativeSelectOption>
|
||||
);
|
||||
})}
|
||||
</NativeSelect>
|
||||
{rows.length > 1 && (
|
||||
<Button type="button" variant="ghost" size="sm" onClick={() => setRows(rows.filter((_, j) => j !== i))}>
|
||||
Remove
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
<div className="divide-y rounded-md border">
|
||||
{groupsOf(scope.permissions).map(([group, list]) => {
|
||||
const chosen = list.filter((p) => row.permissions.includes(p)).length;
|
||||
const toggleAll = (on: boolean) =>
|
||||
set(i, { ...row, permissions: on ? [...new Set([...row.permissions, ...list])] : row.permissions.filter((x) => !list.includes(x)) });
|
||||
return (
|
||||
<details key={group} className="group/perm">
|
||||
<summary className="flex cursor-pointer list-none items-center gap-2 px-3 py-2 text-sm select-none [&::-webkit-details-marker]:hidden">
|
||||
<ChevronRightIcon className="size-4 text-muted-foreground transition-transform group-open/perm:rotate-90" aria-hidden />
|
||||
<input
|
||||
type="checkbox"
|
||||
aria-label={`All ${group}`}
|
||||
checked={chosen === list.length}
|
||||
ref={(el) => {
|
||||
if (el) el.indeterminate = chosen > 0 && chosen < list.length;
|
||||
}}
|
||||
onChange={(e) => toggleAll(e.target.checked)}
|
||||
/>
|
||||
<span className="font-medium">{group}</span>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{chosen > 0 ? `${chosen} of ${list.length}` : list.length === 1 ? "1 permission" : `${list.length} permissions`}
|
||||
</span>
|
||||
</summary>
|
||||
<div className="grid gap-x-4 gap-y-1 px-3 pt-1 pb-3 pl-10 sm:grid-cols-2">
|
||||
{list.map((p) => (
|
||||
<label key={p} className="flex items-center gap-2 font-mono text-xs">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={row.permissions.includes(p)}
|
||||
onChange={(e) =>
|
||||
set(i, { ...row, permissions: e.target.checked ? [...row.permissions, p] : row.permissions.filter((x) => x !== p) })
|
||||
}
|
||||
/>
|
||||
{p.slice(p.indexOf(":") + 1)}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</details>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</fieldset>
|
||||
);
|
||||
})}
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{unused && (
|
||||
<Button type="button" variant="outline" onClick={() => setRows([...rows, { scope: keyOf(unused.scope), permissions: [] }])}>
|
||||
Add another place
|
||||
</Button>
|
||||
)}
|
||||
<Button type="submit" disabled={pending || grants.length === 0}>
|
||||
{pending ? "Making…" : "Make token"}
|
||||
</Button>
|
||||
</div>
|
||||
{state?.error && <ErrorText>{state.error}</ErrorText>}
|
||||
{state?.token && (
|
||||
<div role="status" className="space-y-2 rounded-lg border p-3 text-sm">
|
||||
<p>
|
||||
Your token <strong>{state.name}</strong>. Copy it now: it won't be shown again.
|
||||
</p>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<code className="min-w-0 flex-1 rounded bg-muted px-2 py-1 font-mono text-xs break-all">{state.token}</code>
|
||||
<CopyButton text={state.token} />
|
||||
</div>
|
||||
<p className="text-muted-foreground">
|
||||
Send it as <code className="font-mono text-xs">Authorization: Bearer <token></code>.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user