Support OIDC (#6661)

* Support OIDC

* tweak to the Configure button

* fix the type import
This commit is contained in:
Kiran K authored and GitHub committed 2023-01-24 13:02:43 -07:00
1 parent 865663646a
commit abb5fd36f3
15 files changed
+685 -307

No files matched your search

+2 -2
View File
@@ -43,8 +43,8 @@ import { authRouter } from "./viewer/auth";
import { availabilityRouter } from "./viewer/availability";
import { bookingsRouter } from "./viewer/bookings";
import { eventTypesRouter } from "./viewer/eventTypes";
import { samlRouter } from "./viewer/saml";
import { slotsRouter } from "./viewer/slots";
import { ssoRouter } from "./viewer/sso";
import { viewerTeamsRouter } from "./viewer/teams";
import { webhookRouter } from "./viewer/webhook";
import { workflowsRouter } from "./viewer/workflows";
@@ -1156,7 +1156,7 @@ export const viewerRouter = mergeRouters(
apiKeys: apiKeysRouter,
slots: slotsRouter,
workflows: workflowsRouter,
saml: samlRouter,
saml: ssoRouter,
// NOTE: Add all app related routes in the bottom till the problem described in @calcom/app-store/trpc-routers.ts is solved.
// After that there would just one merge call here for all the apps.
appRoutingForms: app_RoutingForms,
@@ -1,14 +1,20 @@
import { z } from "zod";
import jackson from "@calcom/features/ee/sso/lib/jackson";
import { samlProductID, samlTenantID, tenantPrefix, canAccess } from "@calcom/features/ee/sso/lib/saml";
import {
samlProductID,
samlTenantID,
tenantPrefix,
canAccess,
oidcPath,
} from "@calcom/features/ee/sso/lib/saml";
import { TRPCError } from "@trpc/server";
import { router, authedProcedure } from "../../trpc";
export const samlRouter = router({
// Retrieve SAML Connection
export const ssoRouter = router({
// Retrieve SSO Connection
get: authedProcedure
.input(
z.object({
@@ -16,8 +22,6 @@ export const samlRouter = router({
})
)
.query(async ({ ctx, input }) => {
const { connectionController, samlSPConfig } = await jackson();
const { teamId } = input;
const { message, access } = await canAccess(ctx.user, teamId);
@@ -29,30 +33,34 @@ export const samlRouter = router({
});
}
const { connectionController, samlSPConfig } = await jackson();
// Retrieve the SP SAML Config
const SPConfig = await samlSPConfig.get();
const response = {
provider: "",
acsUrl: SPConfig.acsUrl,
entityId: SPConfig.entityId,
};
try {
const connections = await connectionController.getConnections({
tenant: teamId ? tenantPrefix + teamId : samlTenantID,
product: samlProductID,
});
if (connections.length > 0 && "idpMetadata" in connections[0]) {
response["provider"] = connections[0].idpMetadata.provider;
if (connections.length === 0) {
return null;
}
} catch (err) {
console.error("Error getting SAML config", err);
throw new TRPCError({ code: "BAD_REQUEST", message: "Fetching SAML Connection failed." });
}
return response;
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." });
}
}),
// Update the SAML Connection
update: authedProcedure
@@ -120,4 +128,44 @@ export const samlRouter = router({
throw new TRPCError({ code: "BAD_REQUEST", message: "Deleting SAML Connection failed." });
}
}),
// Update the OIDC Connection
updateOIDC: authedProcedure
.input(
z.object({
teamId: z.union([z.number(), z.null()]),
clientId: z.string(),
clientSecret: z.string(),
wellKnownUrl: z.string(),
})
)
.mutation(async ({ ctx, input }) => {
const { teamId, clientId, clientSecret, wellKnownUrl } = input;
const { message, access } = await canAccess(ctx.user, teamId);
if (!access) {
throw new TRPCError({
code: "BAD_REQUEST",
message,
});
}
const { connectionController } = await jackson();
try {
return await connectionController.createOIDCConnection({
defaultRedirectUrl: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/api/auth/saml/idp`,
redirectUrl: JSON.stringify([`${process.env.NEXT_PUBLIC_WEBAPP_URL}/*`]),
tenant: teamId ? tenantPrefix + teamId : samlTenantID,
product: samlProductID,
oidcClientId: clientId,
oidcClientSecret: clientSecret,
oidcDiscoveryUrl: wellKnownUrl,
});
} catch (err) {
console.error("Error updating OIDC connection", err);
throw new TRPCError({ code: "BAD_REQUEST", message: "Updating OIDC Connection failed." });
}
}),
});