41 lines
1.6 KiB
TypeScript
41 lines
1.6 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { withAuth } from "@/lib/db";
|
|
import { fail, json, readJson } from "@/lib/auth/http";
|
|
import { browserOrigin, clientIp } from "@/lib/auth/request";
|
|
import { setupOptionsBody } from "@/lib/auth/schemas";
|
|
import { setupCodeMatches } from "@/lib/auth/codes";
|
|
import { tooManyAttempts } from "@/lib/auth/throttle";
|
|
import { loadInstance, registrationOptions } from "@/lib/auth/webauthn";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
// Einrichtung, Schritt 1: Code prüfen, Passkey-Optionen für die erste Person.
|
|
export async function POST(request: Request) {
|
|
const body = setupOptionsBody.safeParse(await readJson(request));
|
|
const origin = browserOrigin(request);
|
|
return withAuth(async (client) => {
|
|
if (await loadInstance(client)) return fail("alreadySetUp", 409);
|
|
if (
|
|
(await tooManyAttempts(client, `setup:${clientIp(request)}`, 10, 15)) ||
|
|
(await tooManyAttempts(client, "setup:*", 100, 60))
|
|
) {
|
|
return fail("tooMany", 429);
|
|
}
|
|
if (!body.success) return fail("invalid", 400);
|
|
if (!origin) return fail("origin", 400);
|
|
if (!(await setupCodeMatches(body.data.code))) return fail("code", 401);
|
|
|
|
const memberId = randomUUID();
|
|
const householdId = randomUUID();
|
|
const instance = { origin: origin.origin, rpId: origin.hostname };
|
|
const { challengeId, options } = await registrationOptions(
|
|
client,
|
|
instance,
|
|
{ id: memberId, name: body.data.name },
|
|
"setup",
|
|
{ memberId, householdId, name: body.data.name, household: body.data.household, ...instance },
|
|
);
|
|
return json({ challengeId, options });
|
|
});
|
|
}
|