Files
cal.diy/packages/features/ee/sso/lib/sso.ts
T

58 lines
1.5 KiB
TypeScript

import type { PrismaClient } from "@calcom/prisma";
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({
select: {
teamId: true,
},
where: {
accepted: true,
user: {
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
// We'll use the first one we find
const promises = memberships.map(({ teamId }) =>
connectionController.getConnections({
tenant: `${tenantPrefix}${teamId}`,
product: samlProductID,
})
);
const connectionResults = await Promise.allSettled(promises);
const connectionsFound = connectionResults
.filter((result) => result.status === "fulfilled")
.map((result) => (result.status === "fulfilled" ? result.value : []))
.filter((connections) => connections.length > 0);
if (connectionsFound.length === 0) {
throw new TRPCError({
code: "BAD_REQUEST",
message:
"Could not find a SSO Identity Provider for your email. Please contact your admin to ensure you have been given access to Cal",
});
}
return {
tenant: connectionsFound[0][0].tenant,
product: samlProductID,
};
};