mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
A step-up elevation minted from the account password satisfied the fresh-factor routes. That is a second-factor bypass: a stolen token plus a known password could rotate the recovery codes — which take no factor in the body — and then spend one of them to disable the second factor outright. The web has never allowed that chain, because a password login does not stamp mfaVerifiedAt. The method is now an authorisation input. Only totp, webauthn, and passkey satisfy a fresh-factor gate, which is exactly the set of ceremonies the web stamps a session for; password reaches what a plain cookie session reaches and stops. The mint gains TOTP and security-key arms so a TOTP-only account can still manage its factor natively, and the response says which reach it bought. The gate also returns a discriminated context. session.id meant a session row on one arm and a token row on the other, and the disable route passed the token id to a session-scoped query — excluding nothing, so it deleted every browser session and revoked the caller's own device login. The transport is now in the type and the compiler refuses the confusion. Redemption splits into validate-then-commit so a 429, a 422, or a wrong code no longer burns a proof against a five-per-fifteen-minute mint ceiling. The claim is UPDATE ... RETURNING, so the statement that decides is the one that reports. The mint is transactional behind a partial unique index. Sign-out-everywhere and both password-reset paths clear elevations. The status read drops to plain requireAuth — it carries no credential material — and every mutation stays gated.
51 lines
2.1 KiB
TypeScript
51 lines
2.1 KiB
TypeScript
/**
|
|
* GET /api/auth/me/mfa
|
|
*
|
|
* Second-factor status for the security settings hub: whether TOTP is active,
|
|
* how many recovery codes remain, and the registered WebAuthn security keys.
|
|
* Plain `requireAuth()` — a cookie session or a cookie-equivalent token. This
|
|
* read is NOT elevation-gated, and deliberately so: it returns metadata only
|
|
* (whether TOTP is on, how many recovery codes remain, and each key's name and
|
|
* dates) and no credential material at all — no secret, no code, no public key,
|
|
* no credential id. The web shows the same screen to any authenticated session,
|
|
* and `/api/auth/me` already exposes `totpConfirmedAt` over Bearer, so gating it
|
|
* would only force a factor re-proof to render a screen and buy nothing.
|
|
*
|
|
* Every MUTATION on this surface stays behind `requireMfaManagementAuth`. The
|
|
* argument-less `requireAuth()` keeps the fail-closed scope default, so a
|
|
* narrow-scope token gains no reach here.
|
|
*
|
|
* Returns only metadata: no secret, no recovery-code plaintext, no public key.
|
|
*/
|
|
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
|
import { apiSuccess } from "@/lib/api-response";
|
|
import { annotate } from "@/lib/logging/context";
|
|
import { prisma } from "@/lib/db";
|
|
import { countRemainingRecoveryCodes } from "@/lib/auth/mfa/recovery-codes";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export const GET = apiHandler(async () => {
|
|
const { user } = await requireAuth();
|
|
|
|
const [recoveryCodesRemaining, webauthn] = await Promise.all([
|
|
countRemainingRecoveryCodes(user.id),
|
|
prisma.webauthnMfaCredential.findMany({
|
|
where: { userId: user.id },
|
|
select: { id: true, name: true, createdAt: true, lastUsedAt: true },
|
|
orderBy: { createdAt: "desc" },
|
|
}),
|
|
]);
|
|
|
|
annotate({ action: { name: "auth.mfa.status" } });
|
|
|
|
return apiSuccess({
|
|
totp: { enabled: Boolean(user.totpConfirmedAt) },
|
|
recoveryCodesRemaining,
|
|
webauthn,
|
|
// The security hub also drives the passkey-upgrade nudge from this one
|
|
// status read, so it knows whether the user has already dismissed it.
|
|
passkeyNudgeDismissed: Boolean(user.passkeyUpgradeNudgeDismissed),
|
|
});
|
|
});
|