Security fix plus team access. Before this change any valid API key, including access keys the delivery system hands to BUYERS, authenticated as the merchant on the whole management API, and GET /v1/api-keys listed every organization's keys. Now only platform keys (issued at sign-up or via POST /v1/api-keys/platform, marked with the platform product id) can call the API, and each request needs its resource scope (<resource>:read|write, write implies read; platform:admin or * for full access), answered with 403 and details.requiredScope otherwise. Keys cannot mint broader keys, customer keys cannot carry management scopes, key listing is tenant-scoped and keys can be revoked by id. Dashboard sessions get team roles (owner, admin, developer, support, viewer) mapped to scopes. /v1/team manages members and email invitations (packages/invitations, token hash only, 7-day expiry); /v1/auth/invitations/accept joins the org and creates the account. The last owner is protected and only owners grant owner. Dashboard: Team page, invite accept page, and an API keys page that creates restricted keys and shows the secret once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
2.9 KiB
TypeScript
89 lines
2.9 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
|
|
interface CreatePlatformKeyProps {
|
|
/** Server action: returns the one-time plaintext or an error. */
|
|
action: (label: string, scopes: string[]) => Promise<{ plaintext?: string; error?: string }>;
|
|
scopes: readonly string[];
|
|
}
|
|
|
|
/** Create a restricted API key and show its secret exactly once. */
|
|
export function CreatePlatformKey({ action, scopes }: CreatePlatformKeyProps) {
|
|
const router = useRouter();
|
|
const [label, setLabel] = useState("");
|
|
const [picked, setPicked] = useState<Set<string>>(new Set(["payments:read"]));
|
|
const [full, setFull] = useState(false);
|
|
const [pending, setPending] = useState(false);
|
|
const [secret, setSecret] = useState<string | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
async function submit(event: React.FormEvent): Promise<void> {
|
|
event.preventDefault();
|
|
setPending(true);
|
|
setError(null);
|
|
const result = await action(label.trim() || "API key", full ? ["platform:admin"] : [...picked]);
|
|
setPending(false);
|
|
if (result.error || !result.plaintext) {
|
|
setError(result.error ?? "Could not create the key.");
|
|
return;
|
|
}
|
|
setSecret(result.plaintext);
|
|
router.refresh();
|
|
}
|
|
|
|
if (secret) {
|
|
return (
|
|
<div>
|
|
<p className="page-desc">Copy this key now. It is shown only once.</p>
|
|
<code className="mono" style={{ wordBreak: "break-all" }}>
|
|
{secret}
|
|
</code>
|
|
<p>
|
|
<button type="button" className="btn btn-small" onClick={() => setSecret(null)}>
|
|
Done
|
|
</button>
|
|
</p>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const toggle = (scope: string) =>
|
|
setPicked((prev) => {
|
|
const next = new Set(prev);
|
|
if (next.has(scope)) next.delete(scope);
|
|
else next.add(scope);
|
|
return next;
|
|
});
|
|
|
|
return (
|
|
<form className="form" onSubmit={submit}>
|
|
<div className="field">
|
|
<label htmlFor="key-label">What is it for</label>
|
|
<input id="key-label" className="input" value={label} onChange={(e) => setLabel(e.target.value)} placeholder="Reporting job" />
|
|
</div>
|
|
<label className="field-hint">
|
|
<input type="checkbox" checked={full} onChange={(e) => setFull(e.target.checked)} /> Full access
|
|
</label>
|
|
{!full ? (
|
|
<div className="tag-list" style={{ marginTop: 8 }}>
|
|
{scopes.map((scope) => (
|
|
<label key={scope} className="tag">
|
|
<input type="checkbox" checked={picked.has(scope)} onChange={() => toggle(scope)} /> {scope}
|
|
</label>
|
|
))}
|
|
</div>
|
|
) : null}
|
|
{error ? (
|
|
<p className="field-error" role="alert">
|
|
{error}
|
|
</p>
|
|
) : null}
|
|
<button type="submit" className="btn btn-primary" disabled={pending || (!full && picked.size === 0)}>
|
|
{pending ? "Creating..." : "Create key"}
|
|
</button>
|
|
</form>
|
|
);
|
|
}
|