Files
cal.diy/packages/trpc/server/routers/viewer/sso/get.handler.ts
T
1eeb91a793 perf: lazy load tRPC routes (#8167)
* experiment: cold start perf

* fix: update failing test

* chore: add database indexes

* chore: use json protocol and add query batching back

* Update [status].tsx

* Update [trpc].ts

* Delete getSlimSession.ts

* Update createContext.ts

* remove trpc caller

* correctly import Prisma

* lazy ethRouter

* replace crypto with md5

* import fixes

* public event endpoint refactor

* Update yarn.lock

* Update yarn.lock

* Using yarn.lock from main

---------

Co-authored-by: Omar López <zomars@me.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Efraín Rochín <roae.85@gmail.com>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
2023-04-25 19:39:47 -03:00

63 lines
1.6 KiB
TypeScript

import jackson from "@calcom/features/ee/sso/lib/jackson";
import {
canAccess,
oidcPath,
samlProductID,
samlTenantID,
tenantPrefix,
} from "@calcom/features/ee/sso/lib/saml";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../trpc";
import type { TGetInputSchema } from "./get.schema";
type GetOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TGetInputSchema;
};
export const getHandler = async ({ ctx, input }: GetOptions) => {
const { teamId } = input;
const { message, access } = await canAccess(ctx.user, teamId);
if (!access) {
throw new TRPCError({
code: "BAD_REQUEST",
message,
});
}
const { connectionController, samlSPConfig } = await jackson();
// Retrieve the SP SAML Config
const SPConfig = await samlSPConfig.get();
try {
const connections = await connectionController.getConnections({
tenant: teamId ? tenantPrefix + teamId : samlTenantID,
product: samlProductID,
});
if (connections.length === 0) {
return null;
}
const type = "idpMetadata" in connections[0] ? "saml" : "oidc";
return {
...connections[0],
type,
acsUrl: type === "saml" ? SPConfig.acsUrl : null,
entityId: type === "saml" ? SPConfig.entityId : null,
callbackUrl: type === "oidc" ? `${process.env.NEXT_PUBLIC_WEBAPP_URL}${oidcPath}` : null,
};
} catch (err) {
console.error("Error getting SSO connection", err);
throw new TRPCError({ code: "BAD_REQUEST", message: "Fetching SSO connection failed." });
}
};