Files
zitadel/apps/login/src/app/api/u2f/verify/route.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

import { getSession, verifyU2FRegistration } from "@/lib/zitadel";
2024-05-13 16:17:12 -04:00
import { getSessionCookieById } from "@/utils/cookies";
2024-05-07 09:52:46 +02:00
import { NextRequest, NextResponse, userAgent } from "next/server";
2024-08-06 09:34:45 +02:00
import { VerifyU2FRegistrationRequest } from "@zitadel/proto/zitadel/user/v2/user_service_pb";
2024-07-25 23:16:07 -04:00
import { PlainMessage } from "@zitadel/client";
2024-05-07 09:52:46 +02:00
export async function POST(request: NextRequest) {
const body = await request.json();
if (body) {
2024-05-07 10:04:03 +02:00
let { u2fId, passkeyName, publicKeyCredential, sessionId } = body;
2024-05-07 09:52:46 +02:00
if (!!!passkeyName) {
const { browser, device, os } = userAgent(request);
passkeyName = `${device.vendor ?? ""} ${device.model ?? ""}${
device.vendor || device.model ? ", " : ""
}${os.name}${os.name ? ", " : ""}${browser.name}`;
}
const sessionCookie = await getSessionCookieById(sessionId);
const session = await getSession(sessionCookie.id, sessionCookie.token);
2024-05-07 09:52:46 +02:00
const userId = session?.session?.factors?.user?.id;
if (userId) {
const req: PlainMessage<VerifyU2FRegistrationRequest> = {
2024-05-07 09:52:46 +02:00
publicKeyCredential,
2024-05-07 10:04:03 +02:00
u2fId,
2024-05-07 09:52:46 +02:00
userId,
tokenName: passkeyName,
};
return verifyU2FRegistration(req)
.then((resp) => {
return NextResponse.json(resp);
})
.catch((error) => {
return NextResponse.json(error, { status: 500 });
});
} else {
return NextResponse.json(
{ details: "could not get session" },
2024-05-13 16:17:12 -04:00
{ status: 500 },
2024-05-07 09:52:46 +02:00
);
}
} else {
return NextResponse.json({}, { status: 400 });
}
}