Merge pull request #4565 from Infisical/feat/ENG-3660

Improve 2FA flow
This commit is contained in:
carlosmonastyrski
2025-09-23 20:25:12 -03:00
committed by GitHub
11 changed files with 461 additions and 157 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: "Please use a valid TOTP 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) => {