Skip to content

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

52.5 KB
Raw Blame
import { Knex } from "knex";

import {
  AccessScope,
  OrganizationActionScope,
  OrgMembershipRole,
  OrgMembershipStatus,
  TUsers,
  UserDeviceSchema
} from "@app/db/schemas";
import { EventType, TAuditLogServiceFactory, TMfaSatisfiedBy } from "@app/ee/services/audit-log/audit-log-types";
import { OrgPermissionSsoActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
import { FOREIGN_SSO_SESSION_ERROR_NAME } from "@app/ee/services/permission/permission-fns";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { getConfig } from "@app/lib/config/env";
import { crypto, generateSrpServerKey, srpCheckClientProof } from "@app/lib/crypto";
import { getUserPrivateKey } from "@app/lib/crypto/srp";
import {
  BadRequestError,
  DatabaseError,
  ForbiddenRequestError,
  NotFoundError,
  UnauthorizedError
} from "@app/lib/errors";
import { getMinExpiresIn, removeTrailingSlash } from "@app/lib/fn";
import { logger } from "@app/lib/logger";
import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import {
  AuthAttemptAuthMethod,
  AuthAttemptAuthResult,
  authAttemptCounter,
  recordAuthAttemptMetric,
  recordLegacyRootKeyUsageMetric
} from "@app/lib/telemetry/metrics";
import { matchesAllowedEmailDomain, sanitizeEmail, validateEmail } from "@app/lib/validator";
import { getUserAgentType } from "@app/server/plugins/audit-log";
import { getServerCfg } from "@app/services/super-admin/super-admin-service";

import { TAuthTokenServiceFactory } from "../auth-token/auth-token-service";
import { TokenType } from "../auth-token/auth-token-types";
import { TMembershipUserDALFactory } from "../membership-user/membership-user-dal";
import { TMembershipRoleDALFactory } from "../membership/membership-role-dal";
import { TMfaRecoveryCodeServiceFactory } from "../mfa-recovery-code/mfa-recovery-code-service";
import { TNotificationServiceFactory } from "../notification/notification-service";
import { NotificationType } from "../notification/notification-types";
import { TOrgDALFactory } from "../org/org-dal";
import { getDefaultOrgMembershipRole } from "../org/org-role-fns";
import { SmtpTemplates, throwIfSmtpError, TSmtpService } from "../smtp/smtp-service";
import { LoginMethod } from "../super-admin/super-admin-types";
import { TTotpServiceFactory } from "../totp/totp-service";
import { TUserAliasDALFactory } from "../user-alias/user-alias-dal";
import { ensureSsoAccountVerified, isStaleSsoAlias } from "../user-alias/user-alias-fns";
import { UserAliasType } from "../user-alias/user-alias-types";
import { TUserDALFactory } from "../user/user-dal";
import {
  assertOAuthLoginMethodEnabled,
  enforceUserLockStatus,
  getRequiredMfaMethod,
  isIdpMfaTrusted,
  isOAuthLoginMethodDisabled,
  isSsoAuthMethod,
  isSsoSessionIssuedByOrg,
  OAuthAuthMethod,
  RECOVERY_CODE_MFA_ASSURANCE
} from "./auth-fns";
import {
  TLoginClientProofDTO,
  TLoginGenServerPublicKeyDTO,
  TOauthLoginDTO,
  TProcessProviderCallbackDTO,
  TVerifyMfaTokenDTO
} from "./auth-login-type";
import {
  ActorType,
  AuthMethod,
  AuthModeMfaJwtTokenPayload,
  AuthTokenType,
  MfaMethod,
  ProviderAuthResult,
  TProviderAuthCallback
} from "./auth-type";
import { verifyCaptcha } from "./captcha-fns";
import { TMfaLockoutServiceFactory } from "./mfa-lockout-service";

type TAuthLoginServiceFactoryDep = {
  userDAL: TUserDALFactory;
  userAliasDAL: Pick<TUserAliasDALFactory, "findOne" | "create" | "updateById">;
  orgDAL: TOrgDALFactory;
  tokenService: TAuthTokenServiceFactory;
  smtpService: TSmtpService;
  totpService: Pick<TTotpServiceFactory, "verifyUserTotp">;
  mfaRecoveryCodeService: Pick<TMfaRecoveryCodeServiceFactory, "verifyAndConsumeRecoveryCode">;
  auditLogService: Pick<TAuditLogServiceFactory, "createAuditLog">;
  membershipUserDAL: TMembershipUserDALFactory;
  membershipRoleDAL: TMembershipRoleDALFactory;
  notificationService: Pick<TNotificationServiceFactory, "createUserNotifications">;
  permissionService: Pick<TPermissionServiceFactory, "getOrgPermission">;
  mfaLockoutService: Pick<
    TMfaLockoutServiceFactory,
    "handleFailedMfaAttempt" | "resetMfaLockStatus" | "recordRecentMfaAuth" | "clearRecentMfaAuth"
  >;
};

export type TAuthLoginFactory = ReturnType<typeof authLoginServiceFactory>;
export const authLoginServiceFactory = ({
  userDAL,
  userAliasDAL,
  tokenService,
  smtpService,
  orgDAL,
  totpService,
  mfaRecoveryCodeService,
  auditLogService,
  notificationService,
  membershipUserDAL,
  membershipRoleDAL,
  permissionService,
  mfaLockoutService
}: TAuthLoginServiceFactoryDep) => {
  /*
   * Private
   * Not exported. This is to update user device list
   * If new device is found. Will be saved and a mail will be send
   */
  const updateUserDeviceSession = async (user: TUsers, ip: string, userAgent: string, tx?: Knex) => {
    const devices = await UserDeviceSchema.parseAsync(user.devices || []);
    const isDeviceSeen = devices.some((device) => device.ip === ip && device.userAgent === userAgent);

    if (!isDeviceSeen) {
      const newDeviceList = devices.concat([{ ip, userAgent }]);
      await userDAL.updateById(user.id, { devices: JSON.stringify(newDeviceList) }, tx);

      await notificationService.createUserNotifications([
        {
          userId: user.id,
          type: NotificationType.LOGIN_FROM_NEW_DEVICE,
          title: "Login From New Device",
          body: `A new device with IP **${ip}** and User Agent **${userAgent}** has logged into your account.`
        }
      ]);

      if (user.email) {
        await smtpService
          .sendMail({
            template: SmtpTemplates.NewDeviceJoin,
            subjectLine: "Successful login from new device",
            recipients: [user.email],
            substitutions: {
              email: user.email,
              timestamp: new Date().toString(),
              ip,
              userAgent
            }
          })
          .catch((err) => logger.error(err, "Failed to send new device login email"));
      }
    }
  };

  /*
   * Private
   * Send mfa code via email
   * */
  const sendUserMfaCode = async ({ userId, email }: { userId: string; email: string }) => {
    const code = await tokenService.createTokenForUser({
      type: TokenType.TOKEN_EMAIL_MFA,
      userId
    });

    await smtpService
      .sendMail({
        template: SmtpTemplates.EmailMfa,
        subjectLine: "Infisical MFA code",
        recipients: [email],
        substitutions: {
          code,
          challengeFor: "sign-in",
          preview: "Sign-in attempt requires further verification."
        }
      })
      .catch((err) => throwIfSmtpError(err, "Failed to send MFA code email"));
  };

  /*
   * Private
   * Issues an MFA challenge: generates an MFA JWT token and sends the email code if needed.
   */
  const issueMfaChallenge = async ({
    userId,
    email,
    authMethod,
    organizationId,
    issuingRootOrgId,
    requiredMfaMethod,
    idpMfaOrgId
  }: {
    userId: string;
    email?: string | null;
    authMethod: AuthMethod;
    organizationId?: string;
    issuingRootOrgId?: string;
    requiredMfaMethod: MfaMethod;
    idpMfaOrgId?: string;
  }) => {
    const appCfg = getConfig();

    const mfaToken = crypto.jwt().sign(
      {
        authMethod,
        authTokenType: AuthTokenType.MFA_TOKEN,
        userId,
        organizationId,
        issuingRootOrgId,
        email,
        requiredMfaMethod,
        idpMfaOrgId
      },
      appCfg.AUTH_SECRET,
      { expiresIn: appCfg.JWT_MFA_LIFETIME }
    );

    if (requiredMfaMethod === MfaMethod.EMAIL && email) {
      await sendUserMfaCode({ userId, email });
    }

    return mfaToken;
  };

  /*
   * Generate the auth and refresh token.
   * Shared by mfa verification, login verification with mfa disabled, and select organization.
   * Note: device tracking (updateUserDeviceSession) is intentionally NOT called here —
   * it must only run after full authentication (including MFA) is complete.
   */
  const generateUserTokens = async (
    {
      userId,
      ip,
      userAgent,
      organizationId,
      subOrganizationId,
      authMethod,
      issuingRootOrgId,
      isMfaVerified,
      mfaMethod,
      idpMfaOrgId
    }: {
      userId: string;
      ip: string;
      userAgent: string;
      organizationId?: string;
      subOrganizationId?: string;
      authMethod: AuthMethod;
      issuingRootOrgId?: string;
      isMfaVerified?: boolean;
      mfaMethod?: MfaMethod;
      idpMfaOrgId?: string;
    },
    tx?: Knex
  ) => {
    const cfg = getConfig();
    const tokenSession = await tokenService.getUserTokenSession(
      {
        userAgent,
        ip,
        userId
      },
      tx
    );
    if (!tokenSession) throw new Error("Failed to create token");

    // Logins from the same IP and user agent reuse one session row, so a password-only
    // login would otherwise inherit the MFA an earlier login proved on it.
    if (!isMfaVerified) await mfaLockoutService.clearRecentMfaAuth(userId, tokenSession.id);

    let tokenSessionExpiresIn: string | number = cfg.JWT_AUTH_LIFETIME;
    let refreshTokenExpiresIn: string | number = cfg.JWT_REFRESH_LIFETIME;

    if (organizationId) {
      const org = await requestMemoize(requestMemoKeys.orgFindById(organizationId), () =>
        orgDAL.findById(organizationId)
      );
      if (org) {
        await membershipUserDAL.update(
          { actorUserId: userId, scopeOrgId: org.id, scope: AccessScope.Organization },
          { lastLoginAuthMethod: authMethod, lastLoginTime: new Date() }
        );
        if (org.userTokenExpiration) {
          tokenSessionExpiresIn = getMinExpiresIn(cfg.JWT_AUTH_LIFETIME, org.userTokenExpiration);
          refreshTokenExpiresIn = org.userTokenExpiration;
        }
      }
    }

    const accessToken = crypto.jwt().sign(
      {
        authMethod,
        authTokenType: AuthTokenType.ACCESS_TOKEN,
        userId,
        tokenVersionId: tokenSession.id,
        accessVersion: tokenSession.accessVersion,
        organizationId,
        subOrganizationId,
        issuingRootOrgId,
        isMfaVerified,
        mfaMethod,
        idpMfaOrgId
      },
      cfg.AUTH_SECRET,
      { expiresIn: tokenSessionExpiresIn }
    );

    const refreshToken = crypto.jwt().sign(
      {
        authMethod,
        authTokenType: AuthTokenType.REFRESH_TOKEN,
        userId,
        tokenVersionId: tokenSession.id,
        refreshVersion: tokenSession.refreshVersion,
        organizationId,
        subOrganizationId,
        issuingRootOrgId,
        isMfaVerified,
        mfaMethod,
        idpMfaOrgId
      },
      cfg.AUTH_SECRET,
      { expiresIn: refreshTokenExpiresIn }
    );

    return { access: accessToken, refresh: refreshToken, tokenVersionId: tokenSession.id };
  };

  const processProviderCallback = async ({
    user,
    authMethod,
    isEmailVerified,
    aliasId,
    ip,
    userAgent,
    organizationId,
    callbackPort,
    idpMfa
  }: TProcessProviderCallbackDTO): Promise<TProviderAuthCallback> => {
    const appCfg = getConfig();

    // SSO configs live on root orgs, so the org an IdP login names is the issuing root org. An
    // OAuth login may name an org too (the default auth org) and must not be bound to it.
    const issuingRootOrgId = isSsoAuthMethod(authMethod) ? organizationId : undefined;

    const idpMfaOrgId = idpMfa?.verified ? organizationId : undefined;

    // A first login that still has to verify its mailbox finishes in completeAccount, which mints the session
    // from this token, so the proof rides along or that one login would be prompted despite the trust.
    if (!user.isAccepted || !isEmailVerified) {
      const signupToken = crypto.jwt().sign(
        {
          authTokenType: AuthTokenType.SIGNUP_TOKEN,
          userId: user.id,
          authMethod,
          isEmailVerified,
          aliasId,
          organizationId,
          issuingRootOrgId,
          idpMfaOrgId,
          callbackPort,
          email: user.email,
          firstName: user.firstName,
          lastName: user.lastName
        },
        appCfg.AUTH_SECRET,
        { expiresIn: appCfg.JWT_SIGNUP_LIFETIME }
      );

      return { result: ProviderAuthResult.SIGNUP_REQUIRED, signupToken, callbackPort } as const;
    }

    // let them select the org and do the mfa
    const tokens = await generateUserTokens({
      userId: user.id,
      ip,
      userAgent,
      authMethod,
      issuingRootOrgId,
      idpMfaOrgId
    });

    if (organizationId) {
      await membershipUserDAL.update(
        {
          actorUserId: user.id,
          scopeOrgId: organizationId,
          scope: AccessScope.Organization,
          status: OrgMembershipStatus.Invited
        },
        { status: OrgMembershipStatus.Accepted }
      );

      // The session token only carries the verdict, so the raw IdP values are recorded here for admins.
      await auditLogService.createAuditLog({
        orgId: organizationId,
        ipAddress: ip,
        userAgent,
        userAgentType: getUserAgentType(userAgent),
        actor: {
          type: ActorType.USER,
          metadata: {
            email: user.email,
            userId: user.id,
            username: user.username ?? user.email ?? "",
            authMethod
          }
        },
        event: {
          type: EventType.USER_LOGIN,
          metadata: {
            organizationId,
            authProvider: authMethod,
            ...(idpMfa && { idpMfa })
          }
        }
      });
    }

    return { result: ProviderAuthResult.SESSION, tokens, callbackPort } as const;
  };

  /*
   * Step 1 of login. To get server public key in exchange of client public key
   */
  const loginGenServerPublicKey = async ({
    email,
    providerAuthToken,
    clientPublicKey
  }: TLoginGenServerPublicKeyDTO) => {
    // akhilmhdh: case sensitive email resolution
    const usersByUsername = await userDAL.findUserEncKeyByUsername({
      username: email
    });
    const userEnc =
      usersByUsername?.length > 1 ? usersByUsername.find((el) => el.username === email) : usersByUsername?.[0];

    const serverCfg = await getServerCfg();

    if (!userEnc || (userEnc && !userEnc.isAccepted)) {
      throw new Error("Failed to find user");
    }

    if (!userEnc.salt || !userEnc.verifier) {
      throw new BadRequestError({ message: "Salt or verifier not found" });
    }

    if (
      serverCfg.enabledLoginMethods &&
      !serverCfg.enabledLoginMethods.includes(LoginMethod.EMAIL) &&
      !providerAuthToken
    ) {
      // bypass server configuration when user is an organization admin - this is to prevent lockout
      const userOrgs = await orgDAL.findAllOrgsByUserId(userEnc.userId);
      if (!userOrgs.some((org) => org.userRole === OrgMembershipRole.Admin)) {
        throw new BadRequestError({
          message: "Login with email is disabled by administrator."
        });
      }
    }

    if (!userEnc.authMethods?.includes(AuthMethod.EMAIL)) {
      // Deprecated: provider token validation removed. This path is only reachable from legacy SRP clients.
      if (!providerAuthToken) throw new UnauthorizedError();
    }

    const serverSrpKey = await generateSrpServerKey(userEnc.salt, userEnc.verifier);
    const userEncKeys = await userDAL.updateUserEncryptionByUserId(userEnc.userId, {
      clientPublicKey,
      serverPrivateKey: serverSrpKey.privateKey
    });
    if (!userEncKeys) throw new Error("Failed  to update encryption key");
    return { salt: userEncKeys.salt, serverPublicKey: serverSrpKey.pubKey };
  };

  /*
   * Step 2 of login. Pass the client proof and with multi factor setup handle the required steps
   */
  const loginExchangeClientProof = async ({
    email,
    clientProof,
    ip,
    userAgent,
    captchaToken,
    password
  }: TLoginClientProofDTO) => {
    // akhilmhdh: case sensitive email resolution
    const usersByUsername = await userDAL.findUserEncKeyByUsername({
      username: email
    });
    const userEnc =
      usersByUsername?.length > 1 ? usersByUsername.find((el) => el.username === email) : usersByUsername?.[0];
    if (!userEnc) throw new Error("Failed to find user");
    const user = await userDAL.findById(userEnc.userId);
    const cfg = getConfig();

    // Deprecated SRP path: default to email auth. Provider token exchange is no longer supported.
    const authMethod = AuthMethod.EMAIL;
    const organizationId: string | undefined = undefined;
    await verifyCaptcha(user.consecutiveFailedPasswordAttempts, captchaToken);

    if (!userEnc.salt || !userEnc.verifier) {
      throw new BadRequestError({ message: "Salt or verifier not found" });
    }

    if (!userEnc.serverPrivateKey || !userEnc.clientPublicKey) throw new Error("Failed to authenticate. Try again?");
    const isValidClientProof = await srpCheckClientProof(
      userEnc.salt,
      userEnc.verifier,
      userEnc.serverPrivateKey,
      userEnc.clientPublicKey,
      clientProof
    );

    if (!isValidClientProof) {
      await userDAL.update(
        { id: userEnc.userId },
        {
          $incr: {
            consecutiveFailedPasswordAttempts: 1
          }
        }
      );

      throw new Error("Failed to authenticate. Try again?");
    }

    await userDAL.updateById(userEnc.userId, {
      consecutiveFailedPasswordAttempts: 0
    });
    // from password decrypt the private key
    if (password) {
      const privateKey = await getUserPrivateKey(password, userEnc).catch((err) => {
        logger.error(
          err,
          `loginExchangeClientProof: private key generation failed for [userId=${user.id}] and [email=${user.email}] `
        );
        return "";
      });

      const hashedPassword = await crypto.hashing().createHash(password, cfg.SALT_ROUNDS);

      recordLegacyRootKeyUsageMetric({ operation: "encrypt", surface: "user_private_key" });
      const { iv, tag, ciphertext, encoding } = crypto
        .encryption()
        .symmetric()
        .encryptWithRootEncryptionKey(privateKey);

      await userDAL.updateUserEncryptionByUserId(userEnc.userId, {
        serverPrivateKey: null,
        clientPublicKey: null,
        hashedPassword,
        serverEncryptedPrivateKey: ciphertext,
        serverEncryptedPrivateKeyIV: iv,
        serverEncryptedPrivateKeyTag: tag,
        serverEncryptedPrivateKeyEncoding: encoding
      });
    } else {
      await userDAL.updateUserEncryptionByUserId(userEnc.userId, {
        serverPrivateKey: null,
        clientPublicKey: null
      });
    }

    const token = await generateUserTokens({
      userId: userEnc.userId,
      ip,
      userAgent,
      authMethod,
      organizationId
    });

    return { token, user: userEnc } as const;
  };

  const login = async ({
    email: unsanitizedEmail,
    password,
    ip,
    userAgent,
    captchaToken
  }: {
    email: string;
    password: string;
    ip: string;
    userAgent: string;
    captchaToken?: string;
  }) => {
    const authMetricStartTime = performance.now();
    const appCfg = getConfig();
    const email = sanitizeEmail(unsanitizedEmail);

    try {
      const user = await userDAL.findOne({ username: email });
      if (!user) {
        logger.error(`Failed to find user for email ${email}`);
        throw new BadRequestError({ message: "Invalid credentials" });
      }

      if (!user.authMethods?.includes(AuthMethod.EMAIL) || !user.hashedPassword || !user.isEmailVerified) {
        logger.error(`User doesn't have email auth enabled ${email}`);
        throw new BadRequestError({ message: "Invalid credentials" });
      }

      const serverCfg = await getServerCfg();
      if (serverCfg.enabledLoginMethods && !serverCfg.enabledLoginMethods.includes(LoginMethod.EMAIL)) {
        const userOrgs = await orgDAL.findAllOrgsByUserId(user.id);
        if (!userOrgs.some((org) => org.userRole === OrgMembershipRole.Admin)) {
          throw new BadRequestError({ message: "Invalid credentials" });
        }
      }

      await verifyCaptcha(user.consecutiveFailedPasswordAttempts, captchaToken);

      if (!(await crypto.hashing().compareHash(password, user.hashedPassword))) {
        await userDAL.update(
          { id: user.id },
          {
            $incr: {
              consecutiveFailedPasswordAttempts: 1
            }
          }
        );

        throw new BadRequestError({ message: "Invalid credentials" });
      }

      await userDAL.updateById(user.id, {
        consecutiveFailedPasswordAttempts: 0
      });
      const token = await generateUserTokens({
        userId: user.id,
        ip,
        userAgent,
        authMethod: AuthMethod.EMAIL
      });

      if (appCfg.OTEL_TELEMETRY_COLLECTION_ENABLED) {
        authAttemptCounter.add(1, {
          "infisical.user.email": email,
          "infisical.user.id": user.id,
          "infisical.auth.method": AuthAttemptAuthMethod.EMAIL,
          "infisical.auth.result": AuthAttemptAuthResult.SUCCESS,
          "client.address": ip,
          "user_agent.original": userAgent
        });
      }

      recordAuthAttemptMetric({
        startTime: authMetricStartTime,
        method: AuthAttemptAuthMethod.EMAIL,
        result: AuthAttemptAuthResult.SUCCESS
      });

      return {
        tokens: {
          accessToken: token.access,
          refreshToken: token.refresh
        },
        user
      } as const;
    } catch (error) {
      if (appCfg.OTEL_TELEMETRY_COLLECTION_ENABLED) {
        authAttemptCounter.add(1, {
          "infisical.user.email": email,
          "infisical.auth.method": AuthAttemptAuthMethod.EMAIL,
          "infisical.auth.result": AuthAttemptAuthResult.FAILURE,
          "client.address": ip,
          "user_agent.original": userAgent
        });
      }

      recordAuthAttemptMetric({
        startTime: authMetricStartTime,
        method: AuthAttemptAuthMethod.EMAIL,
        result: AuthAttemptAuthResult.FAILURE,
        error
      });

      throw error;
    }
  };

  /*
   * Multi factor authentication re-send code, Get user id from token
   * saved in frontend
   */
  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);

    await sendUserMfaCode({
      userId: user.id,
      email: user.email
    });
  };

  /*
   * Multi factor authentication verification of code
   * Third step of login in which user completes with mfa
   * */
  const verifyMfaToken = async ({
    userId,
    mfaToken,
    mfaMethod,
    requiredMfaMethod,
    mfaJwtToken,
    ip,
    userAgent,
    orgId,
    isRecoveryCode = false
  }: TVerifyMfaTokenDTO) => {
    const user = await userDAL.findById(userId);

    try {
      enforceUserLockStatus(Boolean(user.isLocked), user.temporaryLockDateEnd);

      // Recovery codes are account-level and bypass the configured second
      // factor regardless of the required MFA method (email, TOTP or WebAuthn).
      if (isRecoveryCode) {
        await mfaRecoveryCodeService.verifyAndConsumeRecoveryCode({
          userId,
          recoveryCode: mfaToken
        });
      } else if (mfaMethod !== requiredMfaMethod) {
        throw new BadRequestError({
          message: `Invalid MFA method. ${requiredMfaMethod} verification is required.`
        });
      } else if (mfaMethod === MfaMethod.EMAIL) {
        await tokenService.validateTokenForUser({
          type: TokenType.TOKEN_EMAIL_MFA,
          userId,
          code: mfaToken
        });
      } else if (mfaMethod === MfaMethod.TOTP) {
        if (mfaToken.length !== 6) {
          throw new BadRequestError({
            message: "Please use a valid TOTP code."
          });
        }
        await totpService.verifyUserTotp({
          userId,
          totp: mfaToken
        });
      } else if (mfaMethod === MfaMethod.WEBAUTHN) {
        if (!mfaToken) {
          throw new BadRequestError({
            message: "WebAuthn session token is required"
          });
        }
        // Validate the one-time WebAuthn session token (passed as mfaToken)
        await tokenService.validateTokenForUser({
          type: TokenType.TOKEN_WEBAUTHN_SESSION,
          userId,
          code: mfaToken
        });
      }
    } catch (err) {
      if (isRecoveryCode && err instanceof NotFoundError) {
        throw new BadRequestError({
          message: "No recovery codes are configured for this account. Please use another MFA method."
        });
      }

      await mfaLockoutService.handleFailedMfaAttempt(userId);

      throw err;
    }

    const decodedToken = crypto.jwt().verify(mfaJwtToken, getConfig().AUTH_SECRET) as AuthModeMfaJwtTokenPayload;

    const userEnc = await userDAL.findById(userId);
    if (!userEnc) throw new Error("Failed to authenticate user");

    await mfaLockoutService.resetMfaLockStatus(userId);

    const token = await generateUserTokens({
      userId: user.id,
      ip,
      userAgent,
      organizationId: orgId,
      authMethod: decodedToken.authMethod,
      issuingRootOrgId: decodedToken.issuingRootOrgId,
      isMfaVerified: true,
      mfaMethod,
      idpMfaOrgId: decodedToken.idpMfaOrgId
    });

    // Open the grace window for THIS session only (the one that just proved MFA), so
    // MFA-management step-up isn't re-prompted right after login - including recovery-code
    // logins, which is what lets a user with a lost factor still manage their MFA. Keyed
    // by the new session's tokenVersionId so no other session inherits it. A non-recovery
    // login can only have proven the required method.
    await mfaLockoutService.recordRecentMfaAuth(
      user.id,
      token.tokenVersionId,
      isRecoveryCode ? RECOVERY_CODE_MFA_ASSURANCE : requiredMfaMethod
    );

    if (isRecoveryCode && userEnc.email) {
      await smtpService
        .sendMail({
          template: SmtpTemplates.MfaRecoveryCodeUsed,
          subjectLine: "A recovery code was used to sign in to your Infisical account",
          recipients: [userEnc.email],
          substitutions: {
            email: userEnc.email,
            timestamp: new Date().toString(),
            ip,
            userAgent
          }
        })
        .catch((err) => logger.error(err, "Failed to send MFA recovery code used email"));
    }

    return { token, user: { ...userEnc, hashedPassword: null } };
  };
  /*
   * OAuth2 login for google,github, and other oauth2 provider
   * */
  const authMethodToAliasType = (method: AuthMethod): UserAliasType => {
    switch (method) {
      case AuthMethod.GOOGLE:
        return UserAliasType.GOOGLE;
      case AuthMethod.GITHUB:
        return UserAliasType.GITHUB;
      case AuthMethod.GITLAB:
        return UserAliasType.GITLAB;
      default:
        throw new BadRequestError({ message: `Unsupported OAuth auth method: ${method}` });
    }
  };

  const oauth2Login = async ({
    email,
    firstName,
    lastName,
    authMethod,
    callbackPort,
    orgSlug,
    providerUserId,
    isEmailVerifiedByProvider,
    ip,
    userAgent
  }: TOauthLoginDTO) => {
    const aliasType = authMethodToAliasType(authMethod);
    const sanitizedEmail = sanitizeEmail(email);
    validateEmail(sanitizedEmail);

    // Step 1: Look up user by provider alias (stable, immutable ID)
    let user: TUsers | undefined;
    let isNewAlias = false;
    let existingAlias = await userAliasDAL.findOne({ externalId: providerUserId, aliasType });
    if (existingAlias) {
      user = await userDAL.findById(existingAlias.userId);
    }

    // Step 2: Fall back to email lookup for existing users without an alias yet
    if (!user) {
      user = await userDAL.findOne({ username: sanitizedEmail });
      if (user) {
        isNewAlias = true;
      }
    }

    // Captured before any mutation so we can tell whether this login is what completed the
    // user's signup (used by the caller to fire signup telemetry exactly once per account).
    const wasUserAcceptedBeforeLogin = Boolean(user?.isAccepted);

    // Mirror complete-account's invite detection: a not-yet-accepted user who already holds a
    // pending invitation was invited (the inviter created it), versus an organic signup that has
    // none. Only meaningful when this login completes the signup, so skip the read for accepted
    // users.
    const wasInvited =
      !wasUserAcceptedBeforeLogin && user
        ? (
            await orgDAL.findMembership({
              actorUserId: user.id,
              scope: AccessScope.Organization,
              status: OrgMembershipStatus.Invited
            })
          ).length > 0
        : false;

    const serverCfg = await getServerCfg();

    const oauthAuthMethod = authMethod as OAuthAuthMethod;
    let canBypassDisabledLoginMethod = false;

    if (user && isOAuthLoginMethodDisabled(oauthAuthMethod, serverCfg.enabledLoginMethods)) {
      // Existing organization admins retain access to prevent account lockout.
      const userOrgs = await orgDAL.findAllOrgsByUserId(user.id);
      canBypassDisabledLoginMethod = userOrgs.some((org) => org.userRole === OrgMembershipRole.Admin);
    }

    assertOAuthLoginMethodEnabled({
      authMethod: oauthAuthMethod,
      enabledLoginMethods: serverCfg.enabledLoginMethods,
      canBypass: canBypassDisabledLoginMethod
    });

    const appCfg = getConfig();

    let orgId = "";
    let orgName: undefined | string;
    if (!user) {
      // Create a new user based on oAuth
      if (!serverCfg?.allowSignUp) throw new BadRequestError({ message: "Sign up disabled", name: "Oauth 2 login" });

      if (serverCfg?.allowedSignUpDomain) {
        const domain = email.split("@")[1];
        if (!matchesAllowedEmailDomain(email, serverCfg.allowedSignUpDomain))
          throw new BadRequestError({
            message: `Email with a domain (@${domain}) is not supported`,
            name: "Oauth 2 login"
          });
      }

      user = await userDAL.transaction(async (tx) => {
        const newUser = await userDAL.create(
          {
            username: sanitizedEmail,
            email: sanitizedEmail,
            // when the provider already verified the email we skip our own verification step
            // and complete the signup immediately (same as the SAML/OIDC enforced flow)
            isEmailVerified: isEmailVerifiedByProvider,
            isAccepted: isEmailVerifiedByProvider,
            ...(authMethod === AuthMethod.GOOGLE && { isGoogleVerified: isEmailVerifiedByProvider }),
            ...(authMethod === AuthMethod.GITHUB && { isGitHubVerified: isEmailVerifiedByProvider }),
            ...(authMethod === AuthMethod.GITLAB && { isGitLabVerified: isEmailVerifiedByProvider }),
            firstName,
            lastName,
            authMethods: [authMethod],
            isGhost: false
          },
          tx
        );

        if (authMethod === AuthMethod.GITHUB && serverCfg.defaultAuthOrgId && !appCfg.isCloud) {
          const defaultOrg = await requestMemoize(requestMemoKeys.orgFindOrgById(serverCfg.defaultAuthOrgId), () =>
            orgDAL.findOrgById(serverCfg.defaultAuthOrgId as string)
          );
          if (!defaultOrg) {
            throw new BadRequestError({
              message: `Failed to find default organization with ID ${serverCfg.defaultAuthOrgId}`
            });
          }
          orgId = defaultOrg.id;
          const existingMembership = await membershipUserDAL.findOne({
            actorUserId: newUser.id,
            scopeOrgId: orgId,
            scope: AccessScope.Organization
          });

          if (!existingMembership) {
            const { role, roleId } = await getDefaultOrgMembershipRole(defaultOrg.defaultMembershipRole);

            const membership = await membershipUserDAL.create(
              {
                actorUserId: newUser.id,
                inviteEmail: sanitizedEmail,
                scopeOrgId: orgId,
                scope: AccessScope.Organization,
                status: OrgMembershipStatus.Accepted,
                isActive: true
              },
              tx
            );
            await membershipRoleDAL.create(
              {
                membershipId: membership.id,
                role,
                customRoleId: roleId
              },
              tx
            );
          }
        }

        existingAlias = await userAliasDAL.create(
          {
            userId: newUser.id,
            aliasType,
            externalId: providerUserId,
            emails: [sanitizedEmail],
            orgId: orgId || null,
            isEmailVerified: isEmailVerifiedByProvider
          },
          tx
        );

        return newUser;
      });
    } else {
      // Whether this login is trusted to mutate the matched account (link an auth method, overwrite
      // profile names, change the email / verified flags). The account was resolved either by a
      // stable provider alias (isNewAlias === false) or by an unverified email fallback (true):
      //  - alias match: trusted only when the alias is not stale AND control of the email is proven
      //    in this login, i.e. the alias is already verified or the provider verified the asserted
      //    email. A still-unverified alias is NOT trusted on a matching email alone: an attacker can
      //    plant such an alias (email-fallback backfill) under their own externalId for the victim's
      //    email, and knowing the email is not proof of owning it. (Stale = still-unverified and the
      //    asserted email matches none of the account's known emails; even a provider-verified email
      //    proves only that the caller owns *that* email, not the aliased account, so a stale alias
      //    must never mutate it.)
      //  - email-fallback match: the asserted email IS the account's username, so trust it only when
      //    the provider verified that email; an unverified assertion doesn't prove control of it.
      const isTrustedForAccount =
        existingAlias && !isNewAlias
          ? !isStaleSsoAlias({ user, userAlias: existingAlias, assertedEmail: sanitizedEmail }) &&
            (existingAlias.isEmailVerified || isEmailVerifiedByProvider)
          : isEmailVerifiedByProvider;

      const isLinkingRequired = !user?.authMethods?.includes(authMethod);
      if (isLinkingRequired && isTrustedForAccount) {
        // we update the names here because upon org invitation, the names are set to be NULL
        // if user is signing up with SSO after invitation, their names should be set based on their SSO profile
        user = await userDAL.updateById(user.id, {
          authMethods: [...(user.authMethods || []), authMethod],
          firstName,
          lastName,
          // trust the provider's verification of the email when linking a new SSO method
          ...(authMethod === AuthMethod.GOOGLE && { isGoogleVerified: isEmailVerifiedByProvider }),
          ...(authMethod === AuthMethod.GITHUB && { isGitHubVerified: isEmailVerifiedByProvider }),
          ...(authMethod === AuthMethod.GITLAB && { isGitLabVerified: isEmailVerifiedByProvider })
        });
      }

      // A provider-driven email change renames the account and marks the alias verified, so only do
      // it for a trusted login (see isTrustedForAccount above). For a stale alias this is skipped,
      // letting the stale-alias guard below decline promotion and fall through to the email-
      // verification flow (no session). With existingAlias set, isTrustedForAccount requires the
      // alias to be non-stale AND already verified or provider-verified in this login.
      if (existingAlias && user.email !== sanitizedEmail && isTrustedForAccount) {
        const conflictingUser = await userDAL.findOne({ username: sanitizedEmail });
        if (conflictingUser && conflictingUser.id !== user.id) {
          throw new BadRequestError({
            message:
              "Unable to complete login: the email associated with your SSO account is already in use by another Infisical user.",
            name: "Oauth 2 login"
          });
        }

        user = await userDAL.transaction(async (tx) => {
          const updatedUser = await userDAL.updateById(
            user!.id,
            {
              username: sanitizedEmail,
              email: sanitizedEmail,
              // the email changed at the provider: trust the provider's verification of the new
              // email for the current method, and preserve the other providers' statuses
              isGitHubVerified: authMethod === AuthMethod.GITHUB ? isEmailVerifiedByProvider : user?.isGitHubVerified,
              isGoogleVerified: authMethod === AuthMethod.GOOGLE ? isEmailVerifiedByProvider : user?.isGoogleVerified,
              isGitLabVerified: authMethod === AuthMethod.GITLAB ? isEmailVerifiedByProvider : user?.isGitLabVerified
            },
            tx
          );

          existingAlias = await userAliasDAL.updateById(
            existingAlias.id,
            {
              emails: [sanitizedEmail],
              isEmailVerified: isEmailVerifiedByProvider
            },
            tx
          );

          return updatedUser;
        });
      }
    }

    if (!orgId && orgSlug) {
      const org = await orgDAL.findOrgBySlug(orgSlug);

      if (org) {
        // checks for the membership and only sets the orgId / orgName if the user is a member of the specified org (direct or via group)
        const orgMembership = await orgDAL.findEffectiveOrgMembership({
          actorType: ActorType.USER,
          actorId: user.id,
          orgId: org.id,
          status: OrgMembershipStatus.Accepted
        });

        if (orgMembership?.isActive) {
          orgId = org.id;
          orgName = org.name;
        }
      }
    }

    // Promote an as-yet-unverified alias ONLY when the provider attests the email in THIS login. The
    // account-level isXVerified flag is per account, not per provider-identity, so it must not promote
    // an unverified alias: an attacker can plant an unverified alias (email-fallback backfill) for the
    // victim's email under their own externalId, and inheriting the flag on a later login would mint a
    // session as the victim. An already-verified alias stays trusted via existingAlias.isEmailVerified.
    const isAliasVerified = isEmailVerifiedByProvider;
    // Self-healing backfill: create alias for existing users found by email fallback
    if (isNewAlias) {
      try {
        existingAlias = await userAliasDAL.create({
          userId: user.id,
          aliasType,
          externalId: providerUserId,
          emails: [sanitizedEmail],
          isEmailVerified: isAliasVerified
        });
      } catch (err) {
        // Duplicate key error from the unique index (race condition: concurrent login already
        // created the alias), so recover the existing row instead
        if (err instanceof DatabaseError && (err.error as { code: string })?.code === "23505") {
          logger.warn(`OAuth alias backfill for user ${user.id} skipped: alias already exists`);
          const recoveredAlias = await userAliasDAL.findOne({ externalId: providerUserId, aliasType });
          if (!recoveredAlias || recoveredAlias.userId !== user.id) {
            throw new BadRequestError({ message: "Unable to complete login; please retry.", name: "Oauth 2 login" });
          }
          existingAlias = recoveredAlias;
        } else {
          throw err;
        }
      }
    }

    // Provider attested the email (or it was verified previously): promote the user + alias to
    // verified/accepted so a real session is issued, exactly like the SAML/OIDC enforced flow.
    // The alias.isEmailVerified term also heals the legacy "alias verified but user never
    // accepted" state, which would otherwise dead-end (no code is ever issued for it below).
    if (existingAlias && (isAliasVerified || existingAlias.isEmailVerified)) {
      ({ user, userAlias: existingAlias } = await ensureSsoAccountVerified({
        user,
        userAlias: existingAlias,
        assertedEmail: sanitizedEmail,
        userDAL,
        userAliasDAL
      }));
    }

    // Send verification email for OAuth providers that haven't verified the user's email
    if (user.email && existingAlias && !existingAlias.isEmailVerified) {
      const verificationCode = await tokenService.createTokenForUser({
        type: TokenType.TOKEN_EMAIL_VERIFICATION,
        userId: user.id,
        aliasId: existingAlias.id
      });

      await smtpService.sendMail({
        template: SmtpTemplates.EmailVerification,
        subjectLine: `Infisical confirmation code: ${verificationCode}`,
        recipients: [user.email],
        substitutions: {
          code: verificationCode
        }
      });
    }

    const callbackResult = await processProviderCallback({
      user,
      authMethod,
      // alias-based on purpose: if the promotion above was declined (stale alias) we must fall
      // back to the signup/verification flow rather than mint a session off a user-level flag
      isEmailVerified: Boolean(existingAlias?.isEmailVerified),
      aliasId: existingAlias?.id,
      ip,
      userAgent,
      organizationId: orgId || undefined,
      callbackPort
    });

    // True when this login is the one that completed the account (fresh provider-verified
    // signup, resumed signup, or an invited user's first verified OAuth login); the caller
    // uses it to fire signup telemetry that complete-account would otherwise have sent.
    const didCompleteSignup = !wasUserAcceptedBeforeLogin && Boolean(user.isAccepted);

    return {
      ...callbackResult,
      user: { ...user, hashedPassword: null },
      didCompleteSignup,
      // meaningful only alongside didCompleteSignup: tags the completed signup as an invite
      wasInvited,
      authMethod,
      orgId,
      orgName
    };
  };

  /*
   * logout user by incrementing the version by 1 meaning any old session will become invalid
   * as there number is behind
   * */
  const logout = async (userId: string, sessionId: string) => {
    await tokenService.clearTokenSessionById(userId, sessionId);
  };

  const selectOrganization = async ({
    userAgent,
    ipAddress,
    organizationId,
    userId,
    userAuthMethod,
    actorRootOrgId,
    issuingRootOrgId,
    isMfaVerified,
    mfaMethod,
    idpMfaOrgId
  }: {
    userAgent: string | undefined;
    ipAddress: string;
    organizationId: string;
    userId: string;
    userAuthMethod: AuthMethod;
    actorRootOrgId?: string;
    issuingRootOrgId?: string;
    isMfaVerified?: boolean;
    mfaMethod?: MfaMethod;
    idpMfaOrgId?: string;
  }) => {
    const cfg = getConfig();
    if (!userAgent) throw new UnauthorizedError({ name: "User-Agent header is required" });

    const user = await userDAL.findById(userId);
    if (!user || !user.isAccepted)
      throw new BadRequestError({ message: "User not found", name: "Find user from token" });

    // Check user membership in the organization (accept any status — promotion happens after auth)
    const orgMembership = await orgDAL.findEffectiveOrgMembership({
      actorType: ActorType.USER,
      actorId: user.id,
      orgId: organizationId,
      acceptAnyStatus: true
    });

    if (!orgMembership) {
      throw new ForbiddenRequestError({
        message: `User does not have access to the organization with ID ${organizationId}`
      });
    }

    const selectedOrg = await requestMemoize(requestMemoKeys.orgFindById(organizationId), () =>
      orgDAL.findById(organizationId)
    );
    if (!selectedOrg) {
      throw new NotFoundError({ message: `Organization with ID '${organizationId}' not found` });
    }

    const isSubOrganization = Boolean(selectedOrg.rootOrgId && selectedOrg.id !== selectedOrg.rootOrgId);

    if (!orgMembership.isActive) {
      throw new ForbiddenRequestError({ message: "User organization membership is inactive" });
    }

    let rootOrg = selectedOrg;

    if (isSubOrganization) {
      if (!selectedOrg.rootOrgId) {
        throw new BadRequestError({
          message: "Invalid sub-organization"
        });
      }

      rootOrg = await requestMemoize(requestMemoKeys.orgFindById(selectedOrg.rootOrgId), () =>
        orgDAL.findById(selectedOrg.rootOrgId as string)
      );
      if (!rootOrg) {
        throw new BadRequestError({
          message: "Invalid sub-organization"
        });
      }

      // Check user membership in the root organization (accept any status — promotion happens after auth)
      const rootOrgMembership = await membershipUserDAL.findOne({
        actorUserId: user.id,
        scopeOrgId: selectedOrg.rootOrgId,
        scope: AccessScope.Organization
      });

      if (!rootOrgMembership) {
        throw new ForbiddenRequestError({
          message: "User does not have access to the root organization"
        });
      }

      if (!rootOrgMembership.isActive) {
        throw new ForbiddenRequestError({ message: "User organization membership is inactive" });
      }
    }

    const { permission } = await permissionService
      .getOrgPermission({
        actor: ActorType.USER,
        actorId: user.id,
        orgId: rootOrg.id,
        actorAuthMethod: userAuthMethod,
        actorOrgId: rootOrg.id,
        scope: OrganizationActionScope.Any
      })
      .catch(async (error: unknown) => {
        // getOrgPermission enforces SSO, so it is what refuses a session another org's IdP issued.
        if (error instanceof ForbiddenRequestError && error.name === FOREIGN_SSO_SESSION_ERROR_NAME) {
          await auditLogService.createAuditLog({
            orgId: organizationId,
            ipAddress,
            userAgent,
            userAgentType: getUserAgentType(userAgent),
            actor: {
              type: ActorType.USER,
              metadata: {
                email: user.email,
                userId: user.id,
                username: user.username,
                authMethod: userAuthMethod as string
              }
            },
            event: {
              type: EventType.SELECT_ORGANIZATION_REFUSED,
              metadata: {
                organizationId,
                organizationName: selectedOrg.name
              }
            }
          });
        }
        throw error;
      });
    const canBypassSso =
      rootOrg.bypassOrgAuthEnabled &&
      permission.can(OrgPermissionSsoActions.BypassSsoEnforcement, OrgPermissionSubjects.Sso);

    if (rootOrg.googleSsoAuthEnforced && userAuthMethod !== AuthMethod.GOOGLE) {
      if (!canBypassSso) {
        throw new ForbiddenRequestError({
          message: "Google SSO is enforced for this organization. Please use Google SSO to login.",
          error: "GoogleSsoEnforced"
        });
      }
    }

    if (userAuthMethod === AuthMethod.GOOGLE) {
      await orgDAL.updateById(rootOrg.id, {
        googleSsoAuthLastUsed: new Date()
      });
    }

    if (!isSubOrganization && orgMembership.status !== OrgMembershipStatus.Accepted) {
      // Promote any Invited memberships to Accepted now that the user has authenticated into this org
      await membershipUserDAL.update(
        {
          actorUserId: user.id,
          scopeOrgId: organizationId,
          scope: AccessScope.Organization
        },
        { status: OrgMembershipStatus.Accepted }
      );
    }

    const { isMfaRequired, requiredMfaMethod } = getRequiredMfaMethod(rootOrg, user);
    // The MFA policy belongs to the root org, so moving between a root org and its sub-orgs is not a change.
    const hasOrganizationChanged = actorRootOrgId ? actorRootOrgId !== rootOrg.id : false;
    const hasMfaMethodChanged = mfaMethod !== requiredMfaMethod;
    // Checked on every selection so turning the trust off applies at the next switch. While the org enforces
    // MFA it waives the whole challenge, personal flag included: passing an enforced challenge turns personal
    // MFA on (the login MFA page activates it), so the two cannot be told apart. Personal MFA in an org that
    // does not enforce it still prompts.
    const isMfaSatisfiedByIdp = isIdpMfaTrusted({ org: rootOrg, authMethod: userAuthMethod, idpMfaOrgId });
    const shouldTriggerMfa =
      isMfaRequired && !isMfaSatisfiedByIdp && (!isMfaVerified || hasMfaMethodChanged || hasOrganizationChanged);

    if (shouldTriggerMfa) {
      enforceUserLockStatus(Boolean(user.isLocked), user.temporaryLockDateEnd);

      const mfaToken = await issueMfaChallenge({
        userId: user.id,
        email: user.email,
        authMethod: userAuthMethod,
        requiredMfaMethod,
        organizationId: rootOrg.id,
        issuingRootOrgId,
        idpMfaOrgId
      });

      return { isMfaEnabled: true, mfa: mfaToken, mfaMethod: requiredMfaMethod } as const;
    }

    await updateUserDeviceSession(user, ipAddress, userAgent);

    const tokens = await generateUserTokens({
      authMethod: userAuthMethod,
      userId: user.id,
      userAgent,
      ip: ipAddress,
      organizationId: isSubOrganization ? rootOrg.id : organizationId,
      subOrganizationId: isSubOrganization ? organizationId : undefined,
      issuingRootOrgId,
      isMfaVerified,
      mfaMethod,
      idpMfaOrgId
    });

    let mfaSatisfiedBy: TMfaSatisfiedBy = "infisical";
    if (!isMfaRequired) mfaSatisfiedBy = "not-required";
    else if (isMfaSatisfiedByIdp) mfaSatisfiedBy = "idp";

    // In the event of this being a break-glass request (not this org's own SAML / OIDC / LDAP session, nor Google, when any is enforced)
    const isAuthEnforcedBypass =
      rootOrg.authEnforced &&
      rootOrg.bypassOrgAuthEnabled &&
      !isSsoSessionIssuedByOrg({ authMethod: userAuthMethod, issuingRootOrgId, rootOrgId: rootOrg.id }) &&
      userAuthMethod !== AuthMethod.GOOGLE;

    const isGoogleSsoEnforcedBypass =
      rootOrg.googleSsoAuthEnforced && rootOrg.bypassOrgAuthEnabled && userAuthMethod !== AuthMethod.GOOGLE;
    if (isAuthEnforcedBypass || isGoogleSsoEnforcedBypass) {
      await auditLogService.createAuditLog({
        orgId: organizationId,
        ipAddress,
        userAgent,
        userAgentType: getUserAgentType(userAgent),
        actor: {
          type: ActorType.USER,
          metadata: {
            email: user.email,
            userId: user.id,
            username: user.username
          }
        },
        event: {
          type: EventType.ORG_ADMIN_BYPASS_SSO,
          metadata: {}
        }
      });

      // Notify all admins via email (besides the actor)
      const orgAdmins = await orgDAL.findOrgMembersByRole(organizationId, OrgMembershipRole.Admin);
      const adminEmails = orgAdmins
        .filter((admin) => admin.user.id !== user.id)
        .map((admin) => admin.user.email)
        .filter(Boolean) as string[];

      if (adminEmails.length > 0) {
        await notificationService.createUserNotifications(
          orgAdmins
            .filter((admin) => admin.user.id !== user.id)
            .map((admin) => ({
              userId: admin.user.id,
              orgId: organizationId,
              type: NotificationType.ADMIN_SSO_BYPASS,
              title: "Security Alert: SSO Bypass",
              body: `The organization member **${user.email}** has bypassed enforced SSO login.`
            }))
        );

        await smtpService
          .sendMail({
            recipients: adminEmails,
            subjectLine: "Security Alert: SSO Bypass",
            substitutions: {
              email: user.email,
              timestamp: new Date().toISOString(),
              ip: ipAddress,
              userAgent,
              siteUrl: removeTrailingSlash(cfg.SITE_URL || "https://app.infisical.com"),
              orgId: organizationId
            },
            template: SmtpTemplates.OrgAdminBreakglassAccess
          })
          .catch((err) => logger.error(err, "Failed to send SSO bypass alert email"));
      }
    }

    // Create audit log for organization selection
    await auditLogService.createAuditLog({
      orgId: organizationId,
      ipAddress,
      userAgent,
      userAgentType: getUserAgentType(userAgent),
      actor: {
        type: ActorType.USER,
        metadata: {
          email: user.email,
          userId: user.id,
          username: user.username,
          authMethod: userAuthMethod as string
        }
      },
      event: isSubOrganization
        ? {
            type: EventType.SELECT_SUB_ORGANIZATION,
            metadata: {
              organizationId,
              organizationName: selectedOrg.name,
              rootOrganizationId: selectedOrg.rootOrgId || "",
              mfaSatisfiedBy
            }
          }
        : {
            type: EventType.SELECT_ORGANIZATION,
            metadata: {
              organizationId,
              organizationName: selectedOrg.name,
              mfaSatisfiedBy
            }
          }
    });

    return {
      ...tokens,
      user,
      isMfaEnabled: false
    };
  };

  return {
    login,
    logout,
    oauth2Login,
    resendMfaToken,
    verifyMfaToken,
    selectOrganization,
    generateUserTokens,
    processProviderCallback,

    // deprecated completely
    loginGenServerPublicKey,
    loginExchangeClientProof
  };
};