Skip to content

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

35.6 KB
Raw Blame
/* eslint-disable @typescript-eslint/no-unsafe-call */
import { ForbiddenError } from "@casl/ability";
import { requestContext } from "@fastify/request-context";
import { Issuer, Issuer as OpenIdIssuer, Strategy as OpenIdStrategy, TokenSet } from "openid-client";

import { AccessScope, OrganizationActionScope, OrgMembershipStatus, TableName, TUsers } from "@app/db/schemas";
import { TOidcConfigsUpdate } from "@app/db/schemas/oidc-configs";
import { EventType, TAuditLogServiceFactory } from "@app/ee/services/audit-log/audit-log-types";
import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
import { TGroupDALFactory } from "@app/ee/services/group/group-dal";
import { addUsersToGroupByUserIds, removeUsersFromGroupByUserIds } from "@app/ee/services/group/group-fns";
import { TUserGroupMembershipDALFactory } from "@app/ee/services/group/user-group-membership-dal";
import { getEnforcedIdentityLimit, throwOnPlanSeatLimitReached } from "@app/ee/services/license/license-fns";
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import { TPamAccountDALFactory } from "@app/ee/services/pam-account/pam-account-dal";
import { TPamSessionDALFactory } from "@app/ee/services/pam-session/pam-session-dal";
import { OrgPermissionSsoActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { getConfig } from "@app/lib/config/env";
import { BadRequestError, ForbiddenRequestError, NotFoundError, OidcAuthError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import {
  AuthAttemptAuthMethod,
  AuthAttemptAuthResult,
  authAttemptCounter,
  recordAuthAttemptMetric,
  recordSsoConfigChangeMetric,
  SsoConfigAction,
  SsoProvider
} from "@app/lib/telemetry/metrics";
import { OrgServiceActor } from "@app/lib/types";
import {
  blockLocalAndPrivateIpAddresses,
  matchesAllowedEmailDomain,
  sanitizeEmail,
  validateEmail
} from "@app/lib/validator";
import { TAdditionalPrivilegeDALFactory } from "@app/services/additional-privilege/additional-privilege-dal";
import { TAlertChannelRecipientDALFactory } from "@app/services/alert/alert-channel-recipient-dal";
import { TAuthTokenServiceFactory } from "@app/services/auth-token/auth-token-service";
import { TokenType } from "@app/services/auth-token/auth-token-types";
import { TAuthLoginFactory } from "@app/services/auth/auth-login-service";
import { ActorType, AuthMethod } from "@app/services/auth/auth-type";
import { matchIdpMfaSignal } from "@app/services/auth/idp-mfa-signal";
import { TEventEmitter } from "@app/services/event-outbox/event-outbox-types";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { KmsDataKey } from "@app/services/kms/kms-types";
import { TUsageMeteringServiceFactory } from "@app/services/license-client/usage";
import { TMembershipGroupDALFactory } from "@app/services/membership-group/membership-group-dal";
import { TMembershipDALFactory } from "@app/services/membership/membership-dal";
import { TMembershipRoleDALFactory } from "@app/services/membership/membership-role-dal";
import { TOrgDALFactory } from "@app/services/org/org-dal";
import { getDefaultOrgMembershipRole } from "@app/services/org/org-role-fns";
import { TProjectBotDALFactory } from "@app/services/project-bot/project-bot-dal";
import { TProjectKeyDALFactory } from "@app/services/project-key/project-key-dal";
import { TProjectDALFactory } from "@app/services/project/project-dal";
import { SmtpTemplates, TSmtpService } from "@app/services/smtp/smtp-service";
import { getServerCfg } from "@app/services/super-admin/super-admin-service";
import { LoginMethod } from "@app/services/super-admin/super-admin-types";
import { TTelemetryServiceFactory } from "@app/services/telemetry/telemetry-service";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";
import { TUserAliasDALFactory } from "@app/services/user-alias/user-alias-dal";
import {
  adoptProvisionedShadowUser,
  ensureSsoAccountVerified,
  isStaleSsoAlias,
  resolveAssertedProfileName,
  syncSsoUserProfile
} from "@app/services/user-alias/user-alias-fns";
import { UserAliasType } from "@app/services/user-alias/user-alias-types";
import { TUserDALFactory } from "@app/services/user/user-dal";

import { TEmailDomainDALFactory } from "../email-domain/email-domain-dal";
import {
  findOrgIdByVerifiedDomain,
  isIdpAuthoritativeForDomain,
  verifyEmailDomainOwnership
} from "../email-domain/email-domain-fns";
import { TOidcConfigDALFactory } from "./oidc-config-dal";
import { resolveOidcGroupMembershipChanges } from "./oidc-config-fns";
import {
  OIDCConfigurationType,
  TCreateOidcCfgDTO,
  TGetOidcCfgDTO,
  TOidcLoginDTO,
  TUpdateOidcCfgDTO
} from "./oidc-config-types";

type TOidcConfigServiceFactoryDep = {
  userDAL: Pick<
    TUserDALFactory,
    | "create"
    | "findOne"
    | "updateById"
    | "findById"
    | "findUserEncKeyByUserId"
    | "findUserEncKeyByUserIdsBatch"
    | "find"
    | "transaction"
  >;
  userAliasDAL: Pick<TUserAliasDALFactory, "create" | "findOne" | "updateById">;
  orgDAL: Pick<
    TOrgDALFactory,
    | "createMembership"
    | "updateMembershipById"
    | "findMembership"
    | "findOrgById"
    | "findOne"
    | "find"
    | "updateById"
    | "lockActorsForOrgAccess"
  >;
  membershipGroupDAL: Pick<TMembershipGroupDALFactory, "find">;
  membershipDAL: Pick<TMembershipDALFactory, "findResourceMembershipsForActors">;
  membershipRoleDAL: Pick<TMembershipRoleDALFactory, "create" | "find">;
  licenseService: Pick<TLicenseServiceFactory, "getPlan" | "getOrgSeatUsage" | "updateSubscriptionOrgMemberCount">;
  tokenService: Pick<TAuthTokenServiceFactory, "createTokenForUser">;
  smtpService: Pick<TSmtpService, "sendMail" | "verify">;
  permissionService: Pick<TPermissionServiceFactory, "getOrgPermission">;
  additionalPrivilegeDAL: Pick<TAdditionalPrivilegeDALFactory, "delete">;
  oidcConfigDAL: Pick<TOidcConfigDALFactory, "findOne" | "update" | "create">;
  groupDAL: Pick<TGroupDALFactory, "findByOrgId" | "findGroupOrgIds">;
  userGroupMembershipDAL: Pick<
    TUserGroupMembershipDALFactory,
    | "find"
    | "transaction"
    | "insertMany"
    | "findGroupMembershipsByUserIdInOrg"
    | "delete"
    | "filterProjectsByUserMembership"
  >;
  projectKeyDAL: Pick<TProjectKeyDALFactory, "find" | "findLatestProjectKey" | "insertMany" | "delete">;
  alertChannelRecipientDAL: Pick<TAlertChannelRecipientDALFactory, "pruneOutOfScopeRecipients">;
  eventEmitter: TEventEmitter;
  projectDAL: Pick<TProjectDALFactory, "findProjectGhostUser" | "findById">;
  projectBotDAL: Pick<TProjectBotDALFactory, "findOne">;
  auditLogService: Pick<TAuditLogServiceFactory, "createAuditLog">;
  kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
  loginService: Pick<TAuthLoginFactory, "processProviderCallback">;
  emailDomainDAL: Pick<TEmailDomainDALFactory, "findOne">;
  telemetryService: Pick<TTelemetryServiceFactory, "sendPostHogEvents">;
  usageMeteringService: Pick<TUsageMeteringServiceFactory, "emit">;
  pamAccountDAL: Pick<TPamAccountDALFactory, "find">;
  pamSessionDAL: Pick<TPamSessionDALFactory, "find" | "update">;
  gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPAMConnectionDetails">;
};

export type TOidcConfigServiceFactory = ReturnType<typeof oidcConfigServiceFactory>;

export const oidcConfigServiceFactory = ({
  orgDAL,
  userDAL,
  userAliasDAL,
  licenseService,
  additionalPrivilegeDAL,
  permissionService,
  tokenService,
  smtpService,
  oidcConfigDAL,
  userGroupMembershipDAL,
  groupDAL,
  membershipGroupDAL,
  membershipDAL,
  membershipRoleDAL,
  projectKeyDAL,
  alertChannelRecipientDAL,
  eventEmitter,
  projectDAL,
  projectBotDAL,
  auditLogService,
  kmsService,
  loginService,
  emailDomainDAL,
  telemetryService,
  usageMeteringService,
  pamAccountDAL,
  pamSessionDAL,
  gatewayV2Service
}: TOidcConfigServiceFactoryDep) => {
  const getOidc = async (dto: TGetOidcCfgDTO) => {
    const oidcCfg = await oidcConfigDAL.findOne({
      orgId: dto.organizationId
    });
    if (!oidcCfg) {
      throw new NotFoundError({
        message: "Failed to find OIDC SSO data"
      });
    }

    if (dto.type === "external") {
      const { permission } = await permissionService.getOrgPermission({
        actorId: dto.actorId,
        actor: dto.actor,
        orgId: dto.organizationId,
        actorOrgId: dto.actorOrgId,
        actorAuthMethod: dto.actorAuthMethod,
        scope: OrganizationActionScope.ParentOrganization
      });
      ForbiddenError.from(permission).throwUnlessCan(OrgPermissionSsoActions.Read, OrgPermissionSubjects.Sso);
    }

    const { decryptor } = await kmsService.createCipherPairWithDataKey({
      type: KmsDataKey.Organization,
      orgId: oidcCfg.orgId
    });

    let clientId = "";
    if (oidcCfg.encryptedOidcClientId) {
      clientId = decryptor({ cipherTextBlob: oidcCfg.encryptedOidcClientId }).toString();
    }

    let clientSecret = "";
    if (oidcCfg.encryptedOidcClientSecret) {
      clientSecret = decryptor({ cipherTextBlob: oidcCfg.encryptedOidcClientSecret }).toString();
    }

    return {
      id: oidcCfg.id,
      issuer: oidcCfg.issuer,
      authorizationEndpoint: oidcCfg.authorizationEndpoint,
      configurationType: oidcCfg.configurationType,
      discoveryURL: oidcCfg.discoveryURL,
      jwksUri: oidcCfg.jwksUri,
      tokenEndpoint: oidcCfg.tokenEndpoint,
      userinfoEndpoint: oidcCfg.userinfoEndpoint,
      orgId: oidcCfg.orgId,
      isActive: oidcCfg.isActive,
      allowedEmailDomains: oidcCfg.allowedEmailDomains,
      clientId,
      clientSecret,
      manageGroupMemberships: oidcCfg.manageGroupMemberships,
      jwtSignatureAlgorithm: oidcCfg.jwtSignatureAlgorithm,
      mfaClaimName: oidcCfg.mfaClaimName,
      mfaAmrValues: oidcCfg.mfaAmrValues
    };
  };

  const oidcLogin = async ({
    email,
    externalId,
    firstName,
    lastName,
    orgId,
    ip,
    userAgent,
    callbackPort,
    groups = [],
    manageGroupMemberships,
    idpMfa
  }: TOidcLoginDTO) => {
    const serverCfg = await getServerCfg();

    if (serverCfg.enabledLoginMethods && !serverCfg.enabledLoginMethods.includes(LoginMethod.OIDC)) {
      throw new ForbiddenRequestError({
        message: "Login with OIDC is disabled by administrator."
      });
    }

    const ownedDomain = await verifyEmailDomainOwnership({ email, orgId, emailDomainDAL });
    const sanitizedEmail = sanitizeEmail(email);
    validateEmail(sanitizedEmail);

    let userAlias = await userAliasDAL.findOne({
      externalId,
      orgId,
      aliasType: UserAliasType.OIDC
    });

    const organization = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
    if (!organization) throw new NotFoundError({ message: `Organization with ID '${orgId}' not found` });

    // The IdP is authoritative, and the separate email-verification step is skipped, only when the
    // org enforces SSO and owns the asserted domain. Enforcement alone would promote an alias for
    // any address the IdP asserts; ownership is what makes the assertion proof of the mailbox.
    const isIdpAuthoritative = isIdpAuthoritativeForDomain({ isAuthEnforced: organization.authEnforced, ownedDomain });

    let user: TUsers;
    // A stale, still-unverified alias may point at another user's account. Don't mutate that
    // account's org membership / group state until the IdP proves control of it (the
    // email-verification fallback below issues no session). Resolved against the existing alias
    // before any mutation; freshly created aliases are never stale.
    let isStaleAlias = false;
    if (userAlias) {
      user = await userDAL.transaction(async (tx) => {
        const foundUser = await userDAL.findById(userAlias.userId, tx);
        // Verify the existing user's stored email domain + cross-org check
        await verifyEmailDomainOwnership({
          email: foundUser.username,
          orgId,
          emailDomainDAL
        });
        isStaleAlias = isStaleSsoAlias({ user: foundUser, userAlias, assertedEmail: sanitizedEmail });
        if (isStaleAlias) {
          return foundUser;
        }

        const [orgMembership] = await orgDAL.findMembership(
          {
            [`${TableName.Membership}.actorUserId` as "actorUserId"]: userAlias.userId,
            [`${TableName.Membership}.scopeOrgId` as "scopeOrgId"]: orgId,
            [`${TableName.Membership}.scope` as "scope"]: AccessScope.Organization
          },
          { tx }
        );
        if (!orgMembership) {
          const { role, roleId } = await getDefaultOrgMembershipRole(organization.defaultMembershipRole);

          const membership = await orgDAL.createMembership(
            {
              actorUserId: userAlias.userId,
              scopeOrgId: orgId,
              scope: AccessScope.Organization,
              status: OrgMembershipStatus.Invited,
              isActive: true
            },
            tx
          );
          await membershipRoleDAL.create(
            {
              membershipId: membership.id,
              role,
              customRoleId: roleId
            },
            tx
          );
        } else if (!orgMembership.isActive) {
          throw new ForbiddenRequestError({ message: "User organization membership is inactive" });
        }

        return foundUser;
      });
    } else {
      let isNewUser = false;
      let adoptedFromUsername: string | null = null;
      const identityLimit = getEnforcedIdentityLimit(await licenseService.getPlan(orgId));
      user = await userDAL.transaction(async (tx) => {
        let newUser: TUsers | undefined;
        // we prioritize getting the most complete user to create the new alias under
        newUser = await userDAL.findOne(
          {
            username: sanitizedEmail
          },
          tx
        );

        // Provisioning may have already made a placeholder for this person keyed on their IdP
        // identifier instead of their mailbox. Adopt it rather than creating a second account and
        // stranding whatever it was granted on one nobody can log into.
        if (!newUser) {
          const adoption = await adoptProvisionedShadowUser({
            externalId,
            assertedEmail: sanitizedEmail,
            orgId,
            rootOrgId: organization.rootOrgId,
            userDAL,
            userAliasDAL,
            orgDAL,
            tx
          });

          if (adoption) {
            newUser = adoption.user;
            adoptedFromUsername = adoption.adoptedFromUsername;
          }
        }

        if (!newUser) {
          newUser = await userDAL.create(
            {
              email: sanitizedEmail,
              firstName,
              username: sanitizedEmail,
              lastName,
              authMethods: [],
              isGhost: false,
              isEmailVerified: isIdpAuthoritative,
              isAccepted: isIdpAuthoritative
            },
            tx
          );
          isNewUser = true;
        } else if (!newUser.firstName && firstName) {
          newUser = await userDAL.updateById(newUser.id, { firstName, ...(lastName ? { lastName } : {}) }, tx);
        }

        userAlias = await userAliasDAL.create(
          {
            userId: newUser.id,
            aliasType: UserAliasType.OIDC,
            externalId,
            emails: sanitizedEmail ? [sanitizedEmail] : [],
            orgId,
            isEmailVerified: isIdpAuthoritative
          },
          tx
        );

        const [orgMembership] = await orgDAL.findMembership(
          {
            [`${TableName.Membership}.actorUserId` as "actorUserId"]: userAlias.userId,
            [`${TableName.Membership}.scopeOrgId` as "scopeOrgId"]: orgId,
            [`${TableName.Membership}.scope` as "scope"]: AccessScope.Organization
          },
          { tx }
        );

        if (!orgMembership) {
          await throwOnPlanSeatLimitReached({
            licenseService,
            orgId,
            identityLimit,
            tx,
            aliasType: UserAliasType.OIDC
          });

          const { role, roleId } = await getDefaultOrgMembershipRole(organization.defaultMembershipRole);

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

        return newUser;
      });

      if (adoptedFromUsername) {
        logger.info(
          { userId: user.id, orgId, externalId },
          "Adopted provisioned placeholder account on first OIDC login"
        );

        await auditLogService.createAuditLog({
          actor: {
            type: ActorType.PLATFORM,
            metadata: {}
          },
          orgId,
          event: {
            type: EventType.OIDC_PROVISIONED_PLACEHOLDER_ADOPTED,
            metadata: {
              userId: user.id,
              externalId,
              previousUsername: adoptedFromUsername,
              newUsername: sanitizedEmail
            }
          }
        });
      }

      if (isNewUser) {
        void telemetryService.sendPostHogEvents({
          event: PostHogEventTypes.UserSignedUp,
          distinctId: user.username ?? "",
          organizationId: orgId,
          properties: {
            username: user.username,
            email: user.email ?? "",
            signupMethod: "oidc"
          }
        });
      }
    }

    if (manageGroupMemberships && !isStaleAlias) {
      const userGroups = await userGroupMembershipDAL.findGroupMembershipsByUserIdInOrg(user.id, orgId);
      const orgGroups = await groupDAL.findByOrgId(orgId);

      const { groupsToAddUserTo, groupsToRemoveUserFrom } = resolveOidcGroupMembershipChanges({
        idpGroups: groups,
        userGroupMemberships: userGroups,
        orgGroups
      });

      for await (const group of groupsToAddUserTo) {
        await addUsersToGroupByUserIds({
          userIds: [user.id],
          group,
          userDAL,
          userGroupMembershipDAL,
          orgDAL,
          membershipGroupDAL,
          projectKeyDAL,
          projectDAL,
          projectBotDAL,
          usageMeteringService
        });
      }

      if (groupsToAddUserTo.length) {
        await auditLogService.createAuditLog({
          actor: {
            type: ActorType.PLATFORM,
            metadata: {}
          },
          orgId,
          event: {
            type: EventType.OIDC_GROUP_MEMBERSHIP_MAPPING_ASSIGN_USER,
            metadata: {
              userId: user.id,
              userEmail: user.email ?? user.username,
              assignedToGroups: groupsToAddUserTo.map(({ id, name }) => ({ id, name })),
              userGroupsClaim: groups
            }
          }
        });
      }

      for await (const group of groupsToRemoveUserFrom) {
        const { sendPamCancellations } = await removeUsersFromGroupByUserIds({
          userIds: [user.id],
          group,
          userDAL,
          userGroupMembershipDAL,
          membershipGroupDAL,
          groupDAL,
          projectKeyDAL,
          additionalPrivilegeDAL,
          usageMeteringService,
          alertChannelRecipientDAL,
          eventEmitter,
          membershipDAL,
          membershipRoleDAL,
          pamAccountDAL,
          pamSessionDAL,
          gatewayV2Service
        });
        sendPamCancellations();
      }

      if (groupsToRemoveUserFrom.length) {
        await auditLogService.createAuditLog({
          actor: {
            type: ActorType.PLATFORM,
            metadata: {}
          },
          orgId,
          event: {
            type: EventType.OIDC_GROUP_MEMBERSHIP_MAPPING_REMOVE_USER,
            metadata: {
              userId: user.id,
              userEmail: user.email ?? user.username,
              removedFromGroups: groupsToRemoveUserFrom.map(({ id, name }) => ({ id, name })),
              userGroupsClaim: groups
            }
          }
        });
      }
    }

    await licenseService.updateSubscriptionOrgMemberCount(organization.id);

    await oidcConfigDAL.update({ orgId }, { lastUsed: new Date() });

    // When the IdP is authoritative, mark the user + alias as verified/accepted before issuing a session.
    if (isIdpAuthoritative) {
      ({ user, userAlias } = await ensureSsoAccountVerified({
        user,
        userAlias,
        assertedEmail: sanitizedEmail,
        userDAL,
        userAliasDAL
      }));
    }

    // The IdP is authoritative for identity in an org that enforces SSO, so a mailbox or name
    // changed there is carried onto the account rather than left to go stale.
    user = await syncSsoUserProfile({
      user,
      userAlias,
      assertedEmail: sanitizedEmail,
      assertedFirstName: firstName,
      assertedLastName: lastName,
      orgId,
      isIdpAuthoritative,
      userDAL,
      userAliasDAL,
      emailDomainDAL,
      auditLogService
    });

    if (user.email && (!userAlias.isEmailVerified || !user.isAccepted)) {
      const token = await tokenService.createTokenForUser({
        type: TokenType.TOKEN_EMAIL_VERIFICATION,
        userId: user.id,
        aliasId: userAlias.id
      });

      await smtpService
        .sendMail({
          template: SmtpTemplates.EmailVerification,
          subjectLine: `Infisical confirmation code: ${token}`,
          recipients: [user.email],
          substitutions: {
            code: token
          }
        })
        .catch((err: Error) => {
          throw new OidcAuthError({
            message: `Error sending email confirmation code for user registration - contact the Infisical instance admin. ${err.message}`
          });
        });
    }

    const callbackResult = await loginService.processProviderCallback({
      user,
      authMethod: AuthMethod.OIDC,
      isEmailVerified: Boolean(userAlias.isEmailVerified),
      aliasId: userAlias.id,
      ip,
      userAgent,
      organizationId: organization.id,
      callbackPort: callbackPort ? Number(callbackPort) : undefined,
      idpMfa
    });

    return { ...callbackResult, userId: user.id };
  };

  const updateOidcCfg = async ({
    organizationId,
    allowedEmailDomains,
    configurationType,
    discoveryURL,
    actor,
    actorOrgId,
    actorAuthMethod,
    actorId,
    issuer,
    isActive,
    authorizationEndpoint,
    jwksUri,
    tokenEndpoint,
    userinfoEndpoint,
    clientId,
    clientSecret,
    manageGroupMemberships,
    jwtSignatureAlgorithm,
    mfaClaimName,
    mfaAmrValues
  }: TUpdateOidcCfgDTO) => {
    const org = await orgDAL.findOne({ id: organizationId });

    if (!org) {
      throw new NotFoundError({
        message: `Organization with ID '${organizationId}' not found`
      });
    }

    const plan = await licenseService.getPlan(org.id);
    if (!plan.oidcSSO)
      throw new BadRequestError({
        message:
          "Failed to update OIDC SSO configuration due to plan restriction. Upgrade plan to update SSO configuration."
      });

    const { permission } = await permissionService.getOrgPermission({
      actorId,
      actor,
      orgId: org.id,
      actorOrgId,
      actorAuthMethod,
      scope: OrganizationActionScope.ParentOrganization
    });
    ForbiddenError.from(permission).throwUnlessCan(OrgPermissionSsoActions.Edit, OrgPermissionSubjects.Sso);

    if (org.googleSsoAuthEnforced && isActive) {
      throw new BadRequestError({
        message:
          "You cannot enable OIDC SSO while Google OAuth is enforced. Disable Google OAuth enforcement to enable OIDC SSO."
      });
    }

    const { encryptor } = await kmsService.createCipherPairWithDataKey({
      type: KmsDataKey.Organization,
      orgId: org.id
    });

    if (isActive) {
      const isSmtpConnected = await smtpService.verify();
      if (!isSmtpConnected) {
        throw new BadRequestError({
          message:
            "Cannot enable OIDC when there are issues with the instance's SMTP configuration. Verify the instance's SMTP settings and try again."
        });
      }
    }

    if (discoveryURL) {
      await blockLocalAndPrivateIpAddresses(discoveryURL);
    }
    if (jwksUri) {
      await blockLocalAndPrivateIpAddresses(jwksUri);
    }
    if (tokenEndpoint) {
      await blockLocalAndPrivateIpAddresses(tokenEndpoint);
    }
    if (userinfoEndpoint) {
      await blockLocalAndPrivateIpAddresses(userinfoEndpoint);
    }

    const isMfaSignalOnlyUpdate = [
      allowedEmailDomains,
      configurationType,
      discoveryURL,
      issuer,
      authorizationEndpoint,
      tokenEndpoint,
      userinfoEndpoint,
      jwksUri,
      isActive,
      clientId,
      clientSecret,
      manageGroupMemberships,
      jwtSignatureAlgorithm
    ].every((value) => value === undefined);

    const updateQuery: TOidcConfigsUpdate = {
      allowedEmailDomains,
      configurationType,
      discoveryURL,
      issuer,
      authorizationEndpoint,
      tokenEndpoint,
      userinfoEndpoint,
      jwksUri,
      isActive,
      ...(!isMfaSignalOnlyUpdate && { lastUsed: null }),
      manageGroupMemberships,
      jwtSignatureAlgorithm,
      mfaClaimName,
      mfaAmrValues
    };

    if (clientId !== undefined) {
      updateQuery.encryptedOidcClientId = encryptor({ plainText: Buffer.from(clientId) }).cipherTextBlob;
    }

    if (clientSecret !== undefined) {
      updateQuery.encryptedOidcClientSecret = encryptor({ plainText: Buffer.from(clientSecret) }).cipherTextBlob;
    }

    const [ssoConfig] = await oidcConfigDAL.update({ orgId: org.id }, updateQuery);
    if (!isMfaSignalOnlyUpdate) {
      await orgDAL.updateById(org.id, { authEnforced: false, scimEnabled: false });
    }
    recordSsoConfigChangeMetric({ provider: SsoProvider.Oidc, action: SsoConfigAction.Update, orgId: org.id });
    return ssoConfig;
  };

  const createOidcCfg = async ({
    organizationId,
    allowedEmailDomains,
    configurationType,
    discoveryURL,
    actor,
    actorOrgId,
    actorAuthMethod,
    actorId,
    issuer,
    isActive,
    authorizationEndpoint,
    jwksUri,
    tokenEndpoint,
    userinfoEndpoint,
    clientId,
    clientSecret,
    manageGroupMemberships,
    jwtSignatureAlgorithm,
    mfaClaimName,
    mfaAmrValues
  }: TCreateOidcCfgDTO) => {
    const org = await orgDAL.findOne({ id: organizationId });
    if (!org) {
      throw new NotFoundError({
        message: `Organization with ID '${organizationId}' not found`
      });
    }

    const plan = await licenseService.getPlan(org.id);
    if (!plan.oidcSSO)
      throw new BadRequestError({
        message:
          "Failed to create OIDC SSO configuration due to plan restriction. Upgrade plan to update SSO configuration."
      });

    const { permission } = await permissionService.getOrgPermission({
      actorId,
      actor,
      orgId: org.id,
      actorOrgId,
      actorAuthMethod,
      scope: OrganizationActionScope.ParentOrganization
    });
    ForbiddenError.from(permission).throwUnlessCan(OrgPermissionSsoActions.Create, OrgPermissionSubjects.Sso);

    if (org.googleSsoAuthEnforced && isActive) {
      throw new BadRequestError({
        message:
          "You cannot enable OIDC SSO while Google OAuth is enforced. Disable Google OAuth enforcement to enable OIDC SSO."
      });
    }

    if (discoveryURL) {
      await blockLocalAndPrivateIpAddresses(discoveryURL);
    }
    if (jwksUri) {
      await blockLocalAndPrivateIpAddresses(jwksUri);
    }
    if (tokenEndpoint) {
      await blockLocalAndPrivateIpAddresses(tokenEndpoint);
    }
    if (userinfoEndpoint) {
      await blockLocalAndPrivateIpAddresses(userinfoEndpoint);
    }

    const { encryptor } = await kmsService.createCipherPairWithDataKey({
      type: KmsDataKey.Organization,
      orgId: org.id
    });

    const oidcCfg = await oidcConfigDAL.create({
      issuer,
      isActive,
      configurationType,
      discoveryURL,
      authorizationEndpoint,
      allowedEmailDomains,
      jwksUri,
      tokenEndpoint,
      userinfoEndpoint,
      orgId: org.id,
      manageGroupMemberships,
      jwtSignatureAlgorithm,
      mfaClaimName,
      mfaAmrValues,
      encryptedOidcClientId: encryptor({ plainText: Buffer.from(clientId) }).cipherTextBlob,
      encryptedOidcClientSecret: encryptor({ plainText: Buffer.from(clientSecret) }).cipherTextBlob
    });

    recordSsoConfigChangeMetric({ provider: SsoProvider.Oidc, action: SsoConfigAction.Create, orgId: org.id });

    return oidcCfg;
  };

  const getOrgAuthStrategy = async (
    identifier: string,
    identifierType: "domain" | "orgSlug" = "domain",
    callbackPort?: string
  ) => {
    const appCfg = getConfig();

    let resolvedOrgId: string;

    if (identifierType === "domain") {
      const verifiedDomain = await findOrgIdByVerifiedDomain({ domain: identifier, emailDomainDAL });
      if (!verifiedDomain) {
        throw new ForbiddenRequestError({ message: "Failed to authenticate with OIDC SSO" });
      }
      resolvedOrgId = verifiedDomain.orgId;
    } else {
      const org = await orgDAL.findOne({ slug: identifier, rootOrgId: null });
      if (!org) {
        throw new ForbiddenRequestError({ message: "Failed to authenticate with OIDC SSO" });
      }
      resolvedOrgId = org.id;
    }

    const oidcCfg = await getOidc({
      type: "internal",
      organizationId: resolvedOrgId
    });

    if (!oidcCfg || !oidcCfg.isActive) {
      throw new ForbiddenRequestError({
        message: "Failed to authenticate with OIDC SSO"
      });
    }
    const org = await orgDAL.findOne({ id: resolvedOrgId });

    let issuer: Issuer;
    if (oidcCfg.configurationType === OIDCConfigurationType.DISCOVERY_URL) {
      if (!oidcCfg.discoveryURL) {
        throw new BadRequestError({
          message: "OIDC not configured correctly"
        });
      }
      await blockLocalAndPrivateIpAddresses(oidcCfg.discoveryURL);
      issuer = await Issuer.discover(oidcCfg.discoveryURL);
    } else {
      if (
        !oidcCfg.issuer ||
        !oidcCfg.authorizationEndpoint ||
        !oidcCfg.jwksUri ||
        !oidcCfg.tokenEndpoint ||
        !oidcCfg.userinfoEndpoint
      ) {
        throw new BadRequestError({
          message: "OIDC not configured correctly"
        });
      }
      await blockLocalAndPrivateIpAddresses(oidcCfg.jwksUri);
      await blockLocalAndPrivateIpAddresses(oidcCfg.tokenEndpoint);
      await blockLocalAndPrivateIpAddresses(oidcCfg.userinfoEndpoint);
      issuer = new OpenIdIssuer({
        issuer: oidcCfg.issuer,
        authorization_endpoint: oidcCfg.authorizationEndpoint,
        jwks_uri: oidcCfg.jwksUri,
        token_endpoint: oidcCfg.tokenEndpoint,
        userinfo_endpoint: oidcCfg.userinfoEndpoint
      });
    }

    const client = new issuer.Client({
      client_id: oidcCfg.clientId,
      client_secret: oidcCfg.clientSecret,
      redirect_uris: [`${appCfg.SITE_URL}/api/v1/sso/oidc/callback`],
      id_token_signed_response_alg: oidcCfg.jwtSignatureAlgorithm
    });

    // Check if the OIDC provider supports PKCE
    const codeChallengeMethods = client.issuer.metadata.code_challenge_methods_supported;
    const supportsPKCE = Array.isArray(codeChallengeMethods) && codeChallengeMethods.includes("S256");

    const strategy = new OpenIdStrategy(
      {
        client,
        passReqToCallback: true,
        usePKCE: supportsPKCE,
        params: { prompt: "login", ...(supportsPKCE ? { code_challenge_method: "S256" } : {}) }
      },
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      (_req: any, tokenSet: TokenSet, cb: any) => {
        const authMetricStartTime = performance.now();
        const claims = tokenSet.claims();
        if (!claims.email) {
          throw new BadRequestError({
            message: "Invalid request. Missing email claim."
          });
        }

        if (!matchesAllowedEmailDomain(claims.email, oidcCfg.allowedEmailDomains ?? "")) {
          throw new ForbiddenRequestError({
            message: "Email not allowed."
          });
        }

        const assertedName = resolveAssertedProfileName({
          givenName: claims?.given_name,
          familyName: claims?.family_name,
          displayName: claims?.name
        });
        if (!assertedName) {
          throw new BadRequestError({
            message: "Invalid request. Missing name claim."
          });
        }

        const groups = typeof claims.groups === "string" ? [claims.groups] : (claims.groups as string[] | undefined);

        oidcLogin({
          email: claims.email.toLowerCase(),
          externalId: claims.sub,
          firstName: assertedName.firstName,
          lastName: assertedName.lastName,
          orgId: org.id,
          ip: requestContext.get("ip") || "",
          userAgent: requestContext.get("userAgent") || "",
          groups,
          callbackPort,
          manageGroupMemberships: oidcCfg.manageGroupMemberships,
          idpMfa: matchIdpMfaSignal({ kind: "oidc", claims, config: oidcCfg })
        })
          .then((loginResult) => {
            cb(null, loginResult);

            if (appCfg.OTEL_TELEMETRY_COLLECTION_ENABLED) {
              authAttemptCounter.add(1, {
                "infisical.user.email": claims?.email?.toLowerCase(),
                "infisical.user.id": loginResult.userId,
                "infisical.organization.id": org.id,
                "infisical.organization.name": org.name,
                "infisical.auth.method": AuthAttemptAuthMethod.OIDC,
                "infisical.auth.result": AuthAttemptAuthResult.SUCCESS,
                "client.address": requestContext.get(RequestContextKey.Ip),
                "user_agent.original": requestContext.get(RequestContextKey.UserAgent)
              });
            }

            recordAuthAttemptMetric({
              startTime: authMetricStartTime,
              method: AuthAttemptAuthMethod.OIDC,
              result: AuthAttemptAuthResult.SUCCESS,
              orgId: org.id
            });
          })
          .catch((error: unknown) => {
            if (appCfg.OTEL_TELEMETRY_COLLECTION_ENABLED) {
              authAttemptCounter.add(1, {
                "infisical.user.email": claims?.email?.toLowerCase(),
                "infisical.organization.id": org.id,
                "infisical.organization.name": org.name,
                "infisical.auth.method": AuthAttemptAuthMethod.OIDC,
                "infisical.auth.result": AuthAttemptAuthResult.FAILURE,
                "client.address": requestContext.get(RequestContextKey.Ip),
                "user_agent.original": requestContext.get(RequestContextKey.UserAgent)
              });
            }

            recordAuthAttemptMetric({
              startTime: authMetricStartTime,
              method: AuthAttemptAuthMethod.OIDC,
              result: AuthAttemptAuthResult.FAILURE,
              orgId: org.id,
              error
            });

            cb(error);
          });
      }
    );

    return strategy;
  };

  const isOidcManageGroupMembershipsEnabled = async (orgId: string, actor: OrgServiceActor) => {
    await permissionService.getOrgPermission({
      actor: ActorType.USER,
      actorId: actor.id,
      orgId,
      actorAuthMethod: actor.authMethod,
      actorOrgId: actor.orgId,
      scope: OrganizationActionScope.ParentOrganization
    });

    const oidcConfig = await oidcConfigDAL.findOne({
      orgId,
      isActive: true
    });

    return Boolean(oidcConfig?.manageGroupMemberships);
  };

  return { oidcLogin, getOrgAuthStrategy, getOidc, updateOidcCfg, createOidcCfg, isOidcManageGroupMembershipsEnabled };
};