Files
lunch/components/auth/passkey.ts
T
Kai BöhmandClaude Opus 5.5 f423dd67ce
ci / olares chart · sanity (pull_request) Successful in 3s
ci / app · types + tests + build (pull_request) Successful in 3m7s
markt / eintrag (pull_request) Successful in 29s
release / which version (pull_request) Successful in 3s
release / olares chart · sanity (pull_request) Successful in 4s
release / app (pull_request) Successful in 59s
release / tag and release (pull_request) Skipped
Own sign-in with passkeys, open the entrance, market entry (26.11.2)
Setup with a code from /app/data, passkeys with required user
verification, sessions as hashed cookies bound to the passkey they
started with, single-use invitation and device links, removing a
passkey or a member, and a recovery code for the owner. Cross-household
lookups before sign-in run in withAuth() (app.auth), allowed only on the
sign-in tables; a test pins that list. State-changing routes check the
session and the Origin header.

Security check with the login-sicherheit skill before the first
invitation: docs/sicherheit/login-2026-11-06.md (no open findings above
low). Entrance goes public now that the own sign-in exists; markt.yml
creates the Lunch entry in aimighty-market and proves it with wrangler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HUc2nv7rJVU2gp3Bgtha3q
2026-11-06 19:26:36 +00:00

67 lines
2.7 KiB
TypeScript

"use client";
import {
browserSupportsWebAuthn,
startAuthentication,
startRegistration,
WebAuthnError,
} from "@simplewebauthn/browser";
// Der Ablauf im Browser: Optionen holen, Passkey vom Gerät, Antwort prüfen
// lassen. Gibt den Schlüssel eines Fehlers zurück (messages: auth.errors)
// oder null, wenn es geklappt hat.
export type ErrorKey =
| "code" | "tooMany" | "invalid" | "expired" | "passkey" | "cancelled"
| "unsupported" | "invitation" | "alreadySetUp" | "origin" | "forbidden" | "lastPasskey" | "unknown";
const KNOWN: readonly string[] = [
"code", "tooMany", "invalid", "expired", "passkey", "invitation", "alreadySetUp", "origin", "forbidden", "lastPasskey",
];
async function post(url: string, body: unknown): Promise<{ ok: boolean; data: Record<string, unknown> }> {
const res = await fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
const data = (await res.json().catch(() => ({}))) as Record<string, unknown>;
return { ok: res.ok, data };
}
function errorKey(data: Record<string, unknown>): ErrorKey {
const e = data.error;
return typeof e === "string" && KNOWN.includes(e) ? (e as ErrorKey) : "unknown";
}
function fromBrowser(error: unknown): ErrorKey {
if (error instanceof WebAuthnError && error.code === "ERROR_CEREMONY_ABORTED") return "cancelled";
if (error instanceof Error && error.name === "NotAllowedError") return "cancelled";
if (error instanceof WebAuthnError && error.code === "ERROR_AUTHENTICATOR_PREVIOUSLY_REGISTERED") return "passkey";
return "unknown";
}
export async function register(optionsUrl: string, body: unknown, verifyUrl: string): Promise<ErrorKey | null> {
if (!browserSupportsWebAuthn()) return "unsupported";
const first = await post(optionsUrl, body);
if (!first.ok) return errorKey(first.data);
let response;
try {
response = await startRegistration({ optionsJSON: first.data.options as never });
} catch (error) {
return fromBrowser(error);
}
const second = await post(verifyUrl, { challengeId: first.data.challengeId, response });
return second.ok ? null : errorKey(second.data);
}
export async function signIn(): Promise<ErrorKey | null> {
if (!browserSupportsWebAuthn()) return "unsupported";
const first = await post("/api/auth/login/options", {});
if (!first.ok) return errorKey(first.data);
let response;
try {
response = await startAuthentication({ optionsJSON: first.data.options as never });
} catch (error) {
return fromBrowser(error);
}
const second = await post("/api/auth/login/verify", { challengeId: first.data.challengeId, response });
return second.ok ? null : errorKey(second.data);
}