finished totp endpoints

This commit is contained in:
Milo Schwartz 2024-10-02 21:55:49 -04:00
parent e85c94d21d
commit 637007e060
No known key found for this signature in database
10 changed files with 126 additions and 534 deletions

View file

@ -41,20 +41,31 @@ export async function verifyTotp(
const { session, user } = await verifySession(req);
if (!session) {
return unauthorized();
return next(unauthorized());
}
if (user.twoFactorEnabled) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"Two-factor authentication is already enabled",
),
);
}
if (!user.twoFactorSecret) {
return createHttpError(
HttpCode.BAD_REQUEST,
"User has not requested two-factor authentication",
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"User has not requested two-factor authentication",
),
);
}
const totpController = new TOTPController();
const valid = await totpController.verify(
user.twoFactorSecret,
decodeHex(code),
code,
decodeHex(user.twoFactorSecret),
);
if (valid) {