feat(auth): complete login with a security-key second factor

When a password is accepted for an account with a second factor, the
login response carries a single-use ticket instead of a session. The
login page now renders a second step: a TOTP or recovery code, or a
WebAuthn security-key assertion scoped to the account's registered keys.
A WebAuthn-only account (no TOTP) is recognised at the password step and
offered the security-key path.

The security-key verify endpoints validate the open ticket, verify the
assertion against the user's second-factor credentials, and only then
mint the session through the shared login tail, so no session exists
before the factor passes. The login page also arms passkey conditional-UI
autofill on the username field.
This commit is contained in:
Marc-André Bombeck committed 2026-06-27 20:40:45 +02:00
1 parent 402ea90d45
commit 86ac1e1d12
9 files changed
+879 -119

No files matched your search

@@ -0,0 +1,54 @@
/**
* MfaLoginStep rendering — the second step of login shown when the password
* response carried `meta.mfaRequired`. Pins which controls render for each
* factor mix (TOTP+recovery, TOTP+webauthn, webauthn-only).
*/
import { describe, expect, it, vi } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
vi.mock("@/lib/i18n/context", () => ({
useTranslations: () => ({ t: (key: string) => key }),
}));
import { MfaLoginStep } from "../mfa-login-step";
function render(methods: ("totp" | "recovery" | "webauthn")[]) {
return renderToStaticMarkup(
<MfaLoginStep
mfaTicket="ticket-1"
methods={methods}
onSuccess={() => {}}
onCancel={() => {}}
/>,
);
}
describe("MfaLoginStep", () => {
it("renders the code entry + recovery toggle for a TOTP account", () => {
const html = render(["totp", "recovery"]);
expect(html).toContain("mfa-login-step");
expect(html).toContain("auth.mfa.codeLabel");
expect(html).toContain("auth.mfa.verify");
expect(html).toContain("auth.mfa.useRecoveryCode");
// No security-key button when the account has no registered key.
expect(html).not.toContain("auth.mfa.useSecurityKey");
});
it("renders the security-key button alongside TOTP when both are available", () => {
const html = render(["totp", "recovery", "webauthn"]);
expect(html).toContain("auth.mfa.codeLabel");
expect(html).toContain("auth.mfa.useSecurityKey");
});
it("leads with the security key for a WebAuthn-only account", () => {
const html = render(["webauthn"]);
expect(html).toContain("auth.mfa.useSecurityKey");
// No code form and no recovery toggle without TOTP.
expect(html).not.toContain("auth.mfa.codeLabel");
expect(html).not.toContain("auth.mfa.useRecoveryCode");
});
it("always offers a way back to the password step", () => {
expect(render(["totp"])).toContain("auth.mfa.backToLogin");
});
});