Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: updated the mfa check

=bed3847103d4
diff --git a/backend/src/@types/fastify.d.ts b/backend/src/@types/fastify.d.ts
index 5fecc81a79..fd62724e64 100644
--- a/backend/src/@types/fastify.d.ts
+++ b/backend/src/@types/fastify.d.ts
@@ -75,7 +75,7 @@ import { TApprovalPolicyServiceFactory } from "@app/services/approval-policy/app
import { TAuthLoginFactory } from "@app/services/auth/auth-login-service";
import { TAuthPasswordFactory } from "@app/services/auth/auth-password-service";
import { TAuthSignupFactory } from "@app/services/auth/auth-signup-service";
-import { ActorAuthMethod, ActorType, TProviderAuthCallback } from "@app/services/auth/auth-type";
+import { ActorAuthMethod, ActorType, MfaMethod, TProviderAuthCallback } from "@app/services/auth/auth-type";
import { TAuthTokenServiceFactory } from "@app/services/auth-token/auth-token-service";
import { TCertificateServiceFactory } from "@app/services/certificate/certificate-service";
import { TCaAutoRenewalQueueFactory } from "@app/services/certificate-authority/ca-auto-renewal-queue";
@@ -218,6 +218,7 @@ declare module "fastify" {
userId: string;
orgId?: string;
user: TUsers;
+ requiredMfaMethod: MfaMethod;
};
// identity injection. depending on which kinda of token the information is filled in auth
auth: TAuthMode;
diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.ts
index 788ab234d5..68904bc33f 100644
--- a/backend/src/keystore/keystore.ts
+++ b/backend/src/keystore/keystore.ts
@@ -116,7 +116,7 @@ export const KeyStoreTtls = {
WebAuthnChallengeInSeconds: 300, // 5 minutes
ProjectSSEConnectionTtlSeconds: 180, // Must be > heartbeat interval (60s) * 2
TelemetryIdentifyIdentityInSeconds: 86400, // 24 hours
- RefreshTokenGraceInSeconds: 30,
+ RefreshTokenGraceInSeconds: 10,
InsightsCacheInSeconds: 300 // 5 minutes
};
diff --git a/backend/src/server/routes/v2/mfa-router.ts b/backend/src/server/routes/v2/mfa-router.ts
index e85d09ffdf..f8c6e5e225 100644
--- a/backend/src/server/routes/v2/mfa-router.ts
+++ b/backend/src/server/routes/v2/mfa-router.ts
@@ -10,7 +10,7 @@ import { addAuthOriginDomainCookie } from "@app/server/lib/cookie";
import { AuthModeMfaJwtTokenPayload, AuthTokenType, MfaMethod } from "@app/services/auth/auth-type";
const handleMfaVerification = async (
- req: FastifyRequest & { mfa: { userId: string; orgId?: string; user: TUsers } },
+ req: FastifyRequest & { mfa: { userId: string; orgId?: string; user: TUsers; requiredMfaMethod: MfaMethod } },
res: FastifyReply,
server: FastifyZodProvider,
mfaToken: string,
@@ -31,6 +31,7 @@ const handleMfaVerification = async (
orgId: req.mfa.orgId,
mfaToken,
mfaMethod,
+ requiredMfaMethod: req.mfa.requiredMfaMethod,
isRecoveryCode
});
@@ -72,7 +73,12 @@ export const registerMfaRouter = async (server: FastifyZodProvider) => {
const user = await server.store.user.findById(decodedToken.userId);
if (!user) throw new Error("User not found");
- req.mfa = { userId: user.id, user, orgId: decodedToken.organizationId };
+ req.mfa = {
+ userId: user.id,
+ user,
+ orgId: decodedToken.organizationId,
+ requiredMfaMethod: decodedToken.requiredMfaMethod
+ };
});
server.route({
@@ -90,7 +96,7 @@ export const registerMfaRouter = async (server: FastifyZodProvider) => {
}
},
handler: async (req) => {
- await server.services.login.resendMfaToken(req.mfa.userId);
+ await server.services.login.resendMfaToken(req.mfa.userId, req.mfa.requiredMfaMethod);
return { message: "Successfully send new mfa code" };
}
});
diff --git a/backend/src/services/auth/auth-login-service.ts b/backend/src/services/auth/auth-login-service.ts
index 8c86549a1f..ed184b35b1 100644
--- a/backend/src/services/auth/auth-login-service.ts
+++ b/backend/src/services/auth/auth-login-service.ts
@@ -198,7 +198,7 @@ export const authLoginServiceFactory = ({
authTokenType: AuthTokenType.MFA_TOKEN,
userId,
organizationId,
- email
+ requiredMfaMethod
},
appCfg.AUTH_SECRET,
{ expiresIn: appCfg.JWT_MFA_LIFETIME }
@@ -600,7 +600,13 @@ export const authLoginServiceFactory = ({
* Multi factor authentication re-send code, Get user id from token
* saved in frontend
*/
- const resendMfaToken = async (userId: string) => {
+ const resendMfaToken = async (userId: string, requiredMfaMethod: MfaMethod) => {
+ if (requiredMfaMethod !== MfaMethod.EMAIL) {
+ throw new BadRequestError({
+ message: "Email MFA code cannot be sent when a different MFA method is required"
+ });
+ }
+
const user = await userDAL.findById(userId);
if (!user || !user.email) return;
enforceUserLockStatus(Boolean(user.isLocked), user.temporaryLockDateEnd);
@@ -667,6 +673,7 @@ export const authLoginServiceFactory = ({
userId,
mfaToken,
mfaMethod,
+ requiredMfaMethod,
mfaJwtToken,
ip,
userAgent,
@@ -676,6 +683,13 @@ export const authLoginServiceFactory = ({
const appCfg = getConfig();
const user = await userDAL.findById(userId);
+ // Recovery codes are allowed regardless of required MFA method (they're for TOTP recovery)
+ if (mfaMethod !== requiredMfaMethod) {
+ throw new BadRequestError({
+ message: `Invalid MFA method. ${requiredMfaMethod} verification is required.`
+ });
+ }
+
try {
enforceUserLockStatus(Boolean(user.isLocked), user.temporaryLockDateEnd);
if (mfaMethod === MfaMethod.EMAIL) {
diff --git a/backend/src/services/auth/auth-login-type.ts b/backend/src/services/auth/auth-login-type.ts
index 29b58c6a32..18a58a1c8c 100644
--- a/backend/src/services/auth/auth-login-type.ts
+++ b/backend/src/services/auth/auth-login-type.ts
@@ -20,6 +20,7 @@ export type TVerifyMfaTokenDTO = {
userId: string;
mfaToken: string;
mfaMethod: MfaMethod;
+ requiredMfaMethod: MfaMethod;
mfaJwtToken: string;
ip: string;
userAgent: string;
diff --git a/backend/src/services/auth/auth-signup-service.ts b/backend/src/services/auth/auth-signup-service.ts
index 23c2c98b91..c3de2cb32f 100644
--- a/backend/src/services/auth/auth-signup-service.ts
+++ b/backend/src/services/auth/auth-signup-service.ts
@@ -173,7 +173,7 @@ export const authSignupServiceFactory = ({
let organizationId: string | undefined;
if (dto.type === CompleteAccountType.Email) {
// Determine rejection before hashing, but don't throw yet
- const shouldReject = !user || user.isAccepted;
+ const shouldReject = !user || user.isAccepted || Boolean(decodedToken?.aliasId);
// Always hash the password so bcrypt cost is incurred regardless of validity
const hashedPassword = await crypto.hashing().createHash(dto.password, appCfg.SALT_ROUNDS);
diff --git a/backend/src/services/auth/auth-type.ts b/backend/src/services/auth/auth-type.ts
index 0be5638589..1374bcf085 100644
--- a/backend/src/services/auth/auth-type.ts
+++ b/backend/src/services/auth/auth-type.ts
@@ -89,6 +89,7 @@ export type AuthModeMfaJwtTokenPayload = {
authMethod: AuthMethod;
userId: string;
organizationId?: string;
+ requiredMfaMethod: MfaMethod;
};
export type AuthModeRefreshJwtTokenPayload = {