🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(auth): add KeyCloak authentication option (#2866)
Natthanan Bhukanb9aaf05acb59
diff --git a/.env.prod.example b/.env.prod.exampleindex d29c701831..fa65a4782b 100644--- a/.env.prod.example+++ b/.env.prod.example@@ -91,6 +91,10 @@ OTEL_SERVICE_NAME="langfuse"# AUTH_COGNITO_CLIENT_SECRET=# AUTH_COGNITO_ISSUER=# AUTH_COGNITO_ALLOW_ACCOUNT_LINKING=false+# AUTH_KEYCLOAK_CLIENT_ID=+# AUTH_KEYCLOAK_CLIENT_SECRET=+# AUTH_KEYCLOAK_ISSUER=+# AUTH_KEYCLOAK_ALLOW_ACCOUNT_LINKING=false# AUTH_CUSTOM_CLIENT_ID=# AUTH_CUSTOM_CLIENT_SECRET=# AUTH_CUSTOM_ISSUER=diff --git a/web/src/ee/features/multi-tenant-sso/types.ts b/web/src/ee/features/multi-tenant-sso/types.tsindex 9b3fb7d18f..a067527a12 100644--- a/web/src/ee/features/multi-tenant-sso/types.ts+++ b/web/src/ee/features/multi-tenant-sso/types.ts@@ -102,6 +102,18 @@ export const CognitoProviderSchema = base.extend({.nullish(),});+export const KeycloakProviderSchema = base.extend({+ authProvider: z.literal("keycloak"),+ authConfig: z+ .object({+ clientId: z.string(),+ clientSecret: z.string(),+ issuer: z.string(),+ allowDangerousEmailAccountLinking: z.boolean().optional().default(false),+ })+ .nullish(),+});+export const CustomProviderSchema = base.extend({authProvider: z.literal("custom"),authConfig: z@@ -124,6 +136,7 @@ export type Auth0ProviderSchema = z.infer<typeof Auth0ProviderSchema>;export type OktaProviderSchema = z.infer<typeof OktaProviderSchema>;export type AzureAdProviderSchema = z.infer<typeof AzureAdProviderSchema>;export type CognitoProviderSchema = z.infer<typeof CognitoProviderSchema>;+export type KeycloakProviderSchema = z.infer<typeof KeycloakProviderSchema>;export type CustomProviderSchema = z.infer<typeof CustomProviderSchema>;export const SsoProviderSchema = z.discriminatedUnion("authProvider", [@@ -135,6 +148,7 @@ export const SsoProviderSchema = z.discriminatedUnion("authProvider", [OktaProviderSchema,AzureAdProviderSchema,CognitoProviderSchema,+ KeycloakProviderSchema,CustomProviderSchema,]);diff --git a/web/src/ee/features/multi-tenant-sso/utils.ts b/web/src/ee/features/multi-tenant-sso/utils.tsindex cbd29bb58d..9a52fec6e3 100644--- a/web/src/ee/features/multi-tenant-sso/utils.ts+++ b/web/src/ee/features/multi-tenant-sso/utils.ts@@ -4,6 +4,7 @@ import GitHubProvider from "next-auth/providers/github";import GitLabProvider from "next-auth/providers/gitlab";import OktaProvider from "next-auth/providers/okta";import CognitoProvider from "next-auth/providers/cognito";+import KeycloakProvider from "next-auth/providers/keycloak";import Auth0Provider from "next-auth/providers/auth0";import AzureADProvider from "next-auth/providers/azure-ad";import { multiTenantSsoAvailable } from "@/src/ee/features/multi-tenant-sso/multiTenantSsoAvailable";@@ -189,6 +190,12 @@ const dbToNextAuthProvider = (provider: SsoProviderSchema): Provider | null => {...provider.authConfig,clientSecret: decrypt(provider.authConfig.clientSecret),});+ else if (provider.authProvider === "keycloak")+ return KeycloakProvider({+ id: getAuthProviderIdForSsoConfig(provider), // use the domain as the provider id as we use domain-specific credentials+ ...provider.authConfig,+ clientSecret: decrypt(provider.authConfig.clientSecret),+ });else if (provider.authProvider === "custom")return CustomSSOProvider({id: getAuthProviderIdForSsoConfig(provider), // use the domain as the provider id as we use domain-specific credentialsdiff --git a/web/src/env.mjs b/web/src/env.mjsindex df02e96da6..4495e83cb1 100644--- a/web/src/env.mjs+++ b/web/src/env.mjs@@ -83,6 +83,10 @@ export const env = createEnv({AUTH_COGNITO_CLIENT_SECRET: z.string().optional(),AUTH_COGNITO_ISSUER: z.string().url().optional(),AUTH_COGNITO_ALLOW_ACCOUNT_LINKING: z.enum(["true", "false"]).optional(),+ AUTH_KEYCLOAK_CLIENT_ID: z.string().optional(),+ AUTH_KEYCLOAK_CLIENT_SECRET: z.string().optional(),+ AUTH_KEYCLOAK_ISSUER: z.string().optional(),+ AUTH_KEYCLOAK_ALLOW_ACCOUNT_LINKING: z.enum(["true", "false"]).optional(),AUTH_CUSTOM_CLIENT_ID: z.string().optional(),AUTH_CUSTOM_CLIENT_SECRET: z.string().optional(),AUTH_CUSTOM_ISSUER: z.string().url().optional(),@@ -349,6 +353,11 @@ export const env = createEnv({AUTH_COGNITO_ISSUER: process.env.AUTH_COGNITO_ISSUER,AUTH_COGNITO_ALLOW_ACCOUNT_LINKING:process.env.AUTH_COGNITO_ALLOW_ACCOUNT_LINKING,+ AUTH_KEYCLOAK_CLIENT_ID: process.env.AUTH_KEYCLOAK_CLIENT_ID,+ AUTH_KEYCLOAK_CLIENT_SECRET: process.env.AUTH_KEYCLOAK_CLIENT_SECRET,+ AUTH_KEYCLOAK_ISSUER: process.env.AUTH_KEYCLOAK_ISSUER,+ AUTH_KEYCLOAK_ALLOW_ACCOUNT_LINKING:+ process.env.AUTH_KEYCLOAK_ALLOW_ACCOUNT_LINKING,AUTH_CUSTOM_CLIENT_ID: process.env.AUTH_CUSTOM_CLIENT_ID,AUTH_CUSTOM_CLIENT_SECRET: process.env.AUTH_CUSTOM_CLIENT_SECRET,AUTH_CUSTOM_ISSUER: process.env.AUTH_CUSTOM_ISSUER,diff --git a/web/src/pages/auth/sign-in.tsx b/web/src/pages/auth/sign-in.tsxindex 967145b6ff..be48d8eabe 100644--- a/web/src/pages/auth/sign-in.tsx+++ b/web/src/pages/auth/sign-in.tsx@@ -14,7 +14,7 @@ import { env } from "@/src/env.mjs";import { zodResolver } from "@hookform/resolvers/zod";import { FcGoogle } from "react-icons/fc";import { FaGithub, FaGitlab } from "react-icons/fa";-import { SiOkta, SiAuth0, SiAmazoncognito } from "react-icons/si";+import { SiOkta, SiAuth0, SiAmazoncognito, SiKeycloak } from "react-icons/si";import { TbBrandAzure, TbBrandOauth } from "react-icons/tb";import { signIn } from "next-auth/react";import Head from "next/head";@@ -52,6 +52,7 @@ export type PageProps = {azureAd: boolean;auth0: boolean;cognito: boolean;+ keycloak: boolean;custom:| {name: string;@@ -99,6 +100,10 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async () => {env.AUTH_COGNITO_CLIENT_ID !== undefined &&env.AUTH_COGNITO_CLIENT_SECRET !== undefined &&env.AUTH_COGNITO_ISSUER !== undefined,+ keycloak:+ env.AUTH_KEYCLOAK_CLIENT_ID !== undefined &&+ env.AUTH_KEYCLOAK_CLIENT_SECRET !== undefined &&+ env.AUTH_KEYCLOAK_ISSUER !== undefined,custom:env.AUTH_CUSTOM_CLIENT_ID !== undefined &&env.AUTH_CUSTOM_CLIENT_SECRET !== undefined &&@@ -171,14 +176,14 @@ export function SSOButtons({</Button>)}{authProviders.githubEnterprise && (- <Button- onClick={() => handleSignIn("github-enterprise")}- variant="secondary"- loading={providerSigningIn === "github-enterprise"}- >- <FaGithub className="mr-3" size={18} />- Github Enterprise- </Button>+ <Button+ onClick={() => handleSignIn("github-enterprise")}+ variant="secondary"+ loading={providerSigningIn === "github-enterprise"}+ >+ <FaGithub className="mr-3" size={18} />+ Github Enterprise+ </Button>)}{authProviders.gitlab && (<Button@@ -230,6 +235,18 @@ export function SSOButtons({Cognito</Button>)}+ {authProviders.keycloak && (+ <Button+ onClick={() => {+ capture("sign_in:button_click", { provider: "keycloak" });+ void signIn("keycloak");+ }}+ variant="secondary"+ >+ <SiKeycloak className="mr-3" size={18} />+ Keycloak+ </Button>+ )}{authProviders.custom && (<ButtononClick={() => handleSignIn("custom")}diff --git a/web/src/server/auth.ts b/web/src/server/auth.tsindex 3ac3f24d6d..371b88a402 100644--- a/web/src/server/auth.ts+++ b/web/src/server/auth.ts@@ -23,6 +23,7 @@ import EmailProvider from "next-auth/providers/email";import Auth0Provider from "next-auth/providers/auth0";import CognitoProvider from "next-auth/providers/cognito";import AzureADProvider from "next-auth/providers/azure-ad";+import KeycloakProvider from "next-auth/providers/keycloak";import { type Provider } from "next-auth/providers/index";import { getCookieName, getCookieOptions } from "./utils/cookies";import {@@ -238,18 +239,18 @@ if (env.AUTH_GITHUB_CLIENT_ID && env.AUTH_GITHUB_CLIENT_SECRET));if (- env.AUTH_GITHUB_ENTERPRISE_CLIENT_ID &&- env.AUTH_GITHUB_ENTERPRISE_CLIENT_SECRET &&- env.AUTH_GITHUB_ENTERPRISE_BASE_URL+ env.AUTH_GITHUB_ENTERPRISE_CLIENT_ID &&+ env.AUTH_GITHUB_ENTERPRISE_CLIENT_SECRET &&+ env.AUTH_GITHUB_ENTERPRISE_BASE_URL) {staticProviders.push(- GitHubEnterpriseProvider({- clientId: env.AUTH_GITHUB_ENTERPRISE_CLIENT_ID,- clientSecret: env.AUTH_GITHUB_ENTERPRISE_CLIENT_SECRET,- enterprise: {baseUrl: env.AUTH_GITHUB_ENTERPRISE_BASE_URL},- allowDangerousEmailAccountLinking:- env.AUTH_GITHUB_ENTERPRISE_ALLOW_ACCOUNT_LINKING === "true",- })+ GitHubEnterpriseProvider({+ clientId: env.AUTH_GITHUB_ENTERPRISE_CLIENT_ID,+ clientSecret: env.AUTH_GITHUB_ENTERPRISE_CLIENT_SECRET,+ enterprise: { baseUrl: env.AUTH_GITHUB_ENTERPRISE_BASE_URL },+ allowDangerousEmailAccountLinking:+ env.AUTH_GITHUB_ENTERPRISE_ALLOW_ACCOUNT_LINKING === "true",+ }),);}@@ -295,6 +296,21 @@ if (}),);+if (+ env.AUTH_KEYCLOAK_CLIENT_ID &&+ env.AUTH_KEYCLOAK_CLIENT_SECRET &&+ env.AUTH_KEYCLOAK_ISSUER+)+ staticProviders.push(+ KeycloakProvider({+ clientId: env.AUTH_KEYCLOAK_CLIENT_ID,+ clientSecret: env.AUTH_KEYCLOAK_CLIENT_SECRET,+ issuer: env.AUTH_KEYCLOAK_ISSUER,+ allowDangerousEmailAccountLinking:+ env.AUTH_KEYCLOAK_ALLOW_ACCOUNT_LINKING === "true",+ }),+ );+// Extend Prisma Adapterconst prismaAdapter = PrismaAdapter(prisma);const extendedPrismaAdapter: Adapter = {@@ -321,6 +337,22 @@ const extendedPrismaAdapter: Adapter = {return user;},++ async linkAccount(data) {+ if (!prismaAdapter.linkAccount)+ throw new Error("NextAuth: prismaAdapter.linkAccount not implemented");++ // Keycloak returns incompatible data with the nextjs-auth schema+ // (refresh_expires_in and not-before-policy in).+ // So, we need to remove this data from the payload before linking an account.+ // https://github.com/nextauthjs/next-auth/issues/7655+ if (data.provider === "keycloak") {+ delete data["refresh_expires_in"];+ delete data["not-before-policy"];+ }++ await prismaAdapter.linkAccount(data);+ },};/**