Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
add mfa to oauth consent as well
Thiago Araujo da Silva6a3d8cee0989
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.tsindex e27bfed496..a74dbcbdcb 100644--- a/backend/src/server/routes/index.ts+++ b/backend/src/server/routes/index.ts@@ -800,7 +800,8 @@ export const registerRoutes = async (permissionService,keyStore,tokenService,- orgDAL+ orgDAL,+ userDAL});const membershipUserService = membershipUserServiceFactory({diff --git a/backend/src/services/auth/auth-fns.ts b/backend/src/services/auth/auth-fns.tsindex e99406f1f6..07bdf015e3 100644--- a/backend/src/services/auth/auth-fns.ts+++ b/backend/src/services/auth/auth-fns.ts@@ -3,7 +3,28 @@ import { request } from "@app/lib/config/request";import { crypto } from "@app/lib/crypto";import { BadRequestError, ForbiddenRequestError, UnauthorizedError } from "@app/lib/errors";-import { AuthModeAccountRecoveryTokenPayload, AuthModeSignUpTokenPayload, AuthTokenType } from "./auth-type";+import { AuthModeAccountRecoveryTokenPayload, AuthModeSignUpTokenPayload, AuthTokenType, MfaMethod } from "./auth-type";++/*+ * Determines whether MFA is required and which method applies, based on org enforcement vs user+ * preference. Org enforcement takes precedence over the user's own selection. Shared between the+ * login flow (which challenges for MFA) and the OAuth consent flow (which must reject sessions that+ * never completed a required MFA challenge before issuing a delegation code).+ */+export const getRequiredMfaMethod = (+ org: { enforceMfa?: boolean | null; selectedMfaMethod?: string | null },+ user: { isMfaEnabled?: boolean | null; selectedMfaMethod?: string | null }+): { isMfaRequired: boolean; requiredMfaMethod: MfaMethod } => {+ const isOrgMfaEnforced = Boolean(org.enforceMfa);+ const isUserMfaEnabled = Boolean(user.isMfaEnabled);+ const isMfaRequired = isOrgMfaEnforced || isUserMfaEnabled;++ const requiredMfaMethod = isOrgMfaEnforced+ ? ((org.selectedMfaMethod as MfaMethod) ?? MfaMethod.EMAIL)+ : ((user.selectedMfaMethod as MfaMethod) ?? MfaMethod.EMAIL);++ return { isMfaRequired, requiredMfaMethod };+};export const extractBearerToken = (token?: string): string => {if (!token) {diff --git a/backend/src/services/auth/auth-login-service.ts b/backend/src/services/auth/auth-login-service.tsindex 2c409df4fe..14bcd07e96 100644--- a/backend/src/services/auth/auth-login-service.ts+++ b/backend/src/services/auth/auth-login-service.ts@@ -51,7 +51,7 @@ import { TTotpServiceFactory } from "../totp/totp-service";import { TUserDALFactory } from "../user/user-dal";import { TUserAliasDALFactory } from "../user-alias/user-alias-dal";import { UserAliasType } from "../user-alias/user-alias-types";-import { enforceUserLockStatus, verifyCaptcha } from "./auth-fns";+import { enforceUserLockStatus, getRequiredMfaMethod, verifyCaptcha } from "./auth-fns";import {TLoginClientProofDTO,TLoginGenServerPublicKeyDTO,@@ -161,25 +161,6 @@ export const authLoginServiceFactory = ({.catch((err) => throwIfSmtpError(err, "Failed to send MFA code email"));};- /*- * Private- * Determines the required MFA method based on org enforcement vs user preference.- */- const getRequiredMfaMethod = (- org: { enforceMfa?: boolean | null; selectedMfaMethod?: string | null },- user: { isMfaEnabled?: boolean | null; selectedMfaMethod?: string | null }- ): { isMfaRequired: boolean; requiredMfaMethod: MfaMethod } => {- const isOrgMfaEnforced = Boolean(org.enforceMfa);- const isUserMfaEnabled = Boolean(user.isMfaEnabled);- const isMfaRequired = isOrgMfaEnforced || isUserMfaEnabled;-- const requiredMfaMethod = isOrgMfaEnforced- ? ((org.selectedMfaMethod as MfaMethod) ?? MfaMethod.EMAIL)- : ((user.selectedMfaMethod as MfaMethod) ?? MfaMethod.EMAIL);-- return { isMfaRequired, requiredMfaMethod };- };-/** Private* Issues an MFA challenge: generates an MFA JWT token and sends the email code if needed.diff --git a/backend/src/services/oauth-client/oauth-client-service.ts b/backend/src/services/oauth-client/oauth-client-service.tsindex a23ceb7b64..a6af83c456 100644--- a/backend/src/services/oauth-client/oauth-client-service.ts+++ b/backend/src/services/oauth-client/oauth-client-service.ts@@ -10,9 +10,11 @@ import { BadRequestError, NotFoundError, UnauthorizedError } from "@app/lib/erroimport { getMinExpiresIn } from "@app/lib/fn";import { ms } from "@app/lib/ms";import { OrgServiceActor } from "@app/lib/types";+import { getRequiredMfaMethod } from "@app/services/auth/auth-fns";import { ActorType, AuthMethod, AuthTokenType, MfaMethod } from "@app/services/auth/auth-type";import { TAuthTokenServiceFactory } from "@app/services/auth-token/auth-token-service";import { TOrgDALFactory } from "@app/services/org/org-dal";+import { TUserDALFactory } from "@app/services/user/user-dal";import { TOauthClientDALFactory } from "./oauth-client-dal";import {@@ -45,6 +47,7 @@ type TOauthClientServiceFactoryDep = {| "revokeSessionsByUserAgent">;orgDAL: Pick<TOrgDALFactory, "findById">;+ userDAL: Pick<TUserDALFactory, "findById">;};export type TOauthClientServiceFactory = ReturnType<typeof oauthClientServiceFactory>;@@ -122,7 +125,8 @@ export const oauthClientServiceFactory = ({permissionService,keyStore,tokenService,- orgDAL+ orgDAL,+ userDAL}: TOauthClientServiceFactoryDep) => {const checkOauthClientPermission = async (actor: OrgServiceActor, action: OrgPermissionActions) => {const { permission } = await permissionService.getOrgPermission({@@ -281,6 +285,30 @@ export const oauthClientServiceFactory = ({scope: OrganizationActionScope.ParentOrganization});+ // The consent endpoint authenticates with AuthMode.JWT, which also accepts the pre-MFA access+ // token issued right after password verification (organization not yet selected, MFA not yet+ // completed). Issuing a delegation code from such a session would let anyone holding only the+ // password mint OAuth tokens, bypassing MFA entirely. So we re-derive whether MFA is required+ // for this user in the client's organization and reject the request unless the session actually+ // completed the matching MFA challenge. MFA enforcement lives on the root organization (matching+ // the login flow), so resolve it when the client belongs to a sub-organization.+ const clientOrg = await orgDAL.findById(client.orgId);+ if (!clientOrg) throw new NotFoundError({ message: "OAuth client organization not found" });++ const isSubOrganization = Boolean(clientOrg.rootOrgId && clientOrg.id !== clientOrg.rootOrgId);+ const rootOrg = isSubOrganization ? await orgDAL.findById(clientOrg.rootOrgId as string) : clientOrg;+ if (!rootOrg) throw new NotFoundError({ message: "OAuth client organization not found" });++ const user = await userDAL.findById(dto.userId);+ if (!user) throw new UnauthorizedError({ message: "User not found" });++ const { isMfaRequired, requiredMfaMethod } = getRequiredMfaMethod(rootOrg, user);+ if (isMfaRequired && (!dto.isMfaVerified || dto.mfaMethod !== requiredMfaMethod)) {+ throw new UnauthorizedError({+ message: "Multi-factor authentication is required before authorizing this application"+ });+ }+const tokenSession = await tokenService.getUserTokenSession({userId: dto.userId,ip: dto.ip,