Improve 2FA flow

This commit is contained in:
Carlos Monastyrski
2025-09-19 18:39:04 -03:00
parent 3c9ad328a9
commit 082e11d603
10 changed files with 379 additions and 106 deletions
@@ -684,7 +684,8 @@ export const authLoginServiceFactory = ({
mfaJwtToken,
ip,
userAgent,
orgId
orgId,
isRecoveryCode = false
}: TVerifyMfaTokenDTO) => {
const appCfg = getConfig();
const user = await userDAL.findById(userId);
@@ -698,16 +699,21 @@ export const authLoginServiceFactory = ({
code: mfaToken
});
} else if (mfaMethod === MfaMethod.TOTP) {
if (mfaToken.length === 6) {
await totpService.verifyUserTotp({
userId,
totp: mfaToken
});
} else {
if (isRecoveryCode) {
await totpService.verifyWithUserRecoveryCode({
userId,
recoveryCode: mfaToken
});
} else {
if (mfaToken.length !== 6) {
throw new BadRequestError({
message: "Invalid TOTP code. Please use a valid recovery code."
});
}
await totpService.verifyUserTotp({
userId,
totp: mfaToken
});
}
}
} catch (err) {
@@ -24,6 +24,7 @@ export type TVerifyMfaTokenDTO = {
ip: string;
userAgent: string;
orgId?: string;
isRecoveryCode?: boolean;
};
export type TOauthLoginDTO = {
+10 -5
View File
@@ -131,15 +131,20 @@ export const totpServiceFactory = ({ totpConfigDAL, kmsService, userDAL }: TTotp
secret
});
if (isValid) {
await totpConfigDAL.updateById(totpConfig.id, {
isVerified: true
});
} else {
if (!isValid) {
throw new BadRequestError({
message: "Invalid TOTP token"
});
}
await totpConfigDAL.updateById(totpConfig.id, {
isVerified: true
});
const recoveryCodes = decryptWithRoot(totpConfig.encryptedRecoveryCodes).toString().split(",");
return {
recoveryCodes
};
};
const verifyUserTotp = async ({ userId, totp }: TVerifyUserTotpDTO) => {