feat: SSO for orgs (#13794)
* WIP Adds the capability to add SSO setup in orgs if you are an Admin or an Owner of the org * remove sso setup from teams * allows account creation on first login * auto-add on login and dsync fixes * allow SP & idP initiated login for SAML * revert change to checkIfUserShouldbelongToOrg * fixes SP initiated Login for first time * removed stale comment * code improvement * -- * minor fixes * more fix * add description for non-admin sso settings page
This commit is contained in:
1 parent
452ea6a993
commit
fd91c5da79
9 files changed
+206
-52
No files matched your search
@@ -1,13 +1,14 @@
|
||||
import createUsersAndConnectToOrg from "@calcom/features/ee/dsync/lib/users/createUsersAndConnectToOrg";
|
||||
import { HOSTED_CAL_FEATURES } from "@calcom/lib/constants";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import { IdentityProvider } from "@calcom/prisma/enums";
|
||||
import { TRPCError } from "@calcom/trpc/server";
|
||||
|
||||
import jackson from "./jackson";
|
||||
import { tenantPrefix, samlProductID } from "./saml";
|
||||
|
||||
export const ssoTenantProduct = async (prisma: PrismaClient, email: string) => {
|
||||
const { connectionController } = await jackson();
|
||||
|
||||
const memberships = await prisma.membership.findMany({
|
||||
const getAllAcceptedMemberships = async ({ prisma, email }: { prisma: PrismaClient; email: string }) => {
|
||||
return await prisma.membership.findMany({
|
||||
select: {
|
||||
teamId: true,
|
||||
},
|
||||
@@ -18,12 +19,65 @@ export const ssoTenantProduct = async (prisma: PrismaClient, email: string) => {
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const getVerifiedOrganizationByAutoAcceptEmailDomain = async ({
|
||||
prisma,
|
||||
domain,
|
||||
}: {
|
||||
prisma: PrismaClient;
|
||||
domain: string;
|
||||
}) => {
|
||||
return await prisma.team.findFirst({
|
||||
where: {
|
||||
organizationSettings: {
|
||||
isOrganizationVerified: true,
|
||||
orgAutoAcceptEmail: domain,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const ssoTenantProduct = async (prisma: PrismaClient, email: string) => {
|
||||
const { connectionController } = await jackson();
|
||||
|
||||
let memberships = await getAllAcceptedMemberships({ prisma, email });
|
||||
|
||||
if (!memberships || memberships.length === 0) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "no_account_exists",
|
||||
});
|
||||
if (!HOSTED_CAL_FEATURES)
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "no_account_exists",
|
||||
});
|
||||
|
||||
const domain = email.split("@")[1];
|
||||
const organization = await getVerifiedOrganizationByAutoAcceptEmailDomain({ prisma, domain });
|
||||
|
||||
if (!organization)
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "no_account_exists",
|
||||
});
|
||||
|
||||
const organizationId = organization.id;
|
||||
const createUsersAndConnectToOrgProps = {
|
||||
emailsToCreate: [email],
|
||||
organizationId,
|
||||
identityProvider: IdentityProvider.SAML,
|
||||
identityProviderId: email,
|
||||
};
|
||||
|
||||
await createUsersAndConnectToOrg(createUsersAndConnectToOrgProps);
|
||||
memberships = await getAllAcceptedMemberships({ prisma, email });
|
||||
|
||||
if (!memberships || memberships.length === 0)
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "no_account_exists",
|
||||
});
|
||||
}
|
||||
|
||||
// Check SSO connections for each team user is a member of
|
||||
|
||||
Reference in new issue
Block a user