🪢 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 custom OIDC IDP provider (#2285)
Benjamin Bartels6a5a233749c2
diff --git a/.env.prod.example b/.env.prod.exampleindex 7404cf7e13..d4a53ecbc7 100644--- a/.env.prod.example+++ b/.env.prod.example@@ -62,7 +62,11 @@ LANGFUSE_CSP_ENFORCE_HTTPS="true"# AUTH_COGNITO_CLIENT_SECRET=# AUTH_COGNITO_ISSUER=# AUTH_COGNITO_ALLOW_ACCOUNT_LINKING=false-+# AUTH_CUSTOM_CLIENT_ID=+# AUTH_CUSTOM_CLIENT_SECRET=+# AUTH_CUSTOM_ISSUER=+# AUTH_CUSTOM_NAME=+# AUTH_CUSTOM_ALLOW_ACCOUNT_LINKING=false# Transactional email, optional# Defines the email address to use as the from address.diff --git a/ee/src/sso/index.ts b/ee/src/sso/index.tsindex d9ec40f603..986555df7f 100644--- a/ee/src/sso/index.ts+++ b/ee/src/sso/index.ts@@ -11,6 +11,7 @@ import { encrypt, decrypt } from "@langfuse/shared/encryption";import { SsoProviderSchema } from "./types";import { type NextApiRequest, type NextApiResponse } from "next";import { env } from "../env";+import { CustomSSOProvider } from "@langfuse/shared/src/server/auth";// Local cache for SSO configurationslet cachedSsoConfigs: {@@ -173,6 +174,12 @@ const dbToNextAuthProvider = (provider: SsoProviderSchema): Provider | null => {...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 credentials+ ...provider.authConfig,+ clientSecret: decrypt(provider.authConfig.clientSecret),+ });else {// Type check to ensure we handle all providers// eslint-disable-next-line no-unused-varsdiff --git a/ee/src/sso/types.ts b/ee/src/sso/types.tsindex 0a496c8e65..65945f4529 100644--- a/ee/src/sso/types.ts+++ b/ee/src/sso/types.ts@@ -76,12 +76,26 @@ export const CognitoProviderSchema = base.extend({.nullish(),});+export const CustomProviderSchema = base.extend({+ authProvider: z.literal("custom"),+ authConfig: z+ .object({+ name: z.string(),+ clientId: z.string(),+ clientSecret: z.string(),+ issuer: z.string(),+ allowDangerousEmailAccountLinking: z.boolean().optional().default(false),+ })+ .nullish(),+});+export type GoogleProviderSchema = z.infer<typeof GoogleProviderSchema>;export type GithubProviderSchema = z.infer<typeof GithubProviderSchema>;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 CustomProviderSchema = z.infer<typeof CustomProviderSchema>;export const SsoProviderSchema = z.discriminatedUnion("authProvider", [GoogleProviderSchema,@@ -90,6 +104,7 @@ export const SsoProviderSchema = z.discriminatedUnion("authProvider", [OktaProviderSchema,AzureAdProviderSchema,CognitoProviderSchema,+ CustomProviderSchema,]);export type SsoProviderSchema = z.infer<typeof SsoProviderSchema>;diff --git a/packages/shared/package.json b/packages/shared/package.jsonindex c6a3304411..b29439166b 100644--- a/packages/shared/package.json+++ b/packages/shared/package.json@@ -53,6 +53,7 @@"kysely": "^0.27.3","langchain": "^0.1.36","lodash": "^4.17.21",+ "next-auth": "^4.24.7","prisma-extension-kysely": "^2.1.0","zod": "^3.22.4","zod-to-json-schema": "^3.22.5"diff --git a/packages/shared/src/server/auth.ts b/packages/shared/src/server/auth.tsindex f610634c8b..94d03e6efb 100644--- a/packages/shared/src/server/auth.ts+++ b/packages/shared/src/server/auth.ts@@ -1,6 +1,7 @@import { compare, hash } from "bcryptjs";import { randomUUID } from "crypto";import * as crypto from "crypto";+import type { OAuthConfig, OAuthUserConfig } from "next-auth/providers/oauth";export function generateSecretKey() {return `sk-lf-${randomUUID()}`;@@ -48,3 +49,33 @@ export function createShaHash(privateKey: string, salt: string): string {return hash;}++export interface CustomSSOUser extends Record<string, any> {+ email: string;+ id: string;+ name: string;+ verified: boolean;+}++export function CustomSSOProvider<P extends CustomSSOUser>(+ options: OAuthUserConfig<P>+): OAuthConfig<P> {+ return {+ id: "custom",+ name: "CustomSSOProvider",+ type: "oauth",+ wellKnown: `${options.issuer}/.well-known/openid-configuration`,+ authorization: { params: { scope: "openid email profile" } },+ checks: ["pkce", "state"],+ idToken: true,+ profile(profile) {+ return {+ id: profile.sub,+ name: profile.name,+ email: profile.email,+ image: null,+ };+ },+ options,+ };+}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 7309474fe1..7135300d91 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -137,6 +137,9 @@ importers:lodash:specifier: ^4.17.21version: 4.17.21+ next-auth:+ specifier: ^4.24.7prisma-extension-kysely:specifier: ^2.1.0version: 2.1.0(@prisma/[email protected])@@ -1934,13 +1937,6 @@ packages:regenerator-runtime: 0.14.1dev: false- /@babel/[email protected]:- resolution: {integrity: sha512-dkxf7+hn8mFBwKjs9bvBlArzLVxVbS8usaPUDd5p2a9JCL9tB8OaOVN1isD4+Xyk4ns89/xeOmbQvgdK7IIVdA==}- engines: {node: '>=6.9.0'}- dependencies:- regenerator-runtime: 0.14.1- dev: false-/@babel/[email protected]:resolution: {integrity: sha512-Nms86NXrsaeU9vbBJKni6gXiEXZ4CVpYVzEjDH9Sb8vmZ3UljyA1GSOJl/6LGPO8EHLuSF9H+IxNXHPX8QHJ4g==}engines: {node: '>=6.9.0'}@@ -13316,7 +13312,7 @@ packages:nodemailer:optional: truedependencies:- '@babel/runtime': 7.24.4+ '@babel/runtime': 7.24.5'@panva/hkdf': 1.1.1cookie: 0.5.0jose: 4.15.5diff --git a/web/src/env.mjs b/web/src/env.mjsindex 4b6cab6ad5..787e239bed 100644--- a/web/src/env.mjs+++ b/web/src/env.mjs@@ -71,6 +71,11 @@ 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_CUSTOM_CLIENT_ID: z.string().optional(),+ AUTH_CUSTOM_CLIENT_SECRET: z.string().optional(),+ AUTH_CUSTOM_ISSUER: z.string().url().optional(),+ AUTH_CUSTOM_NAME: z.string().optional(),+ AUTH_CUSTOM_ALLOW_ACCOUNT_LINKING: z.enum(["true", "false"]).optional(),AUTH_DOMAINS_WITH_SSO_ENFORCEMENT: z.string().optional(),AUTH_DISABLE_USERNAME_PASSWORD: z.enum(["true", "false"]).optional(),AUTH_DISABLE_SIGNUP: z.enum(["true", "false"]).optional(),@@ -175,6 +180,12 @@ export const env = createEnv({AUTH_COGNITO_CLIENT_SECRET: process.env.AUTH_COGNITO_CLIENT_SECRET,AUTH_COGNITO_ISSUER: process.env.AUTH_COGNITO_ISSUER,AUTH_COGNITO_ALLOW_ACCOUNT_LINKING: process.env.AUTH_COGNITO_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,+ AUTH_CUSTOM_NAME: process.env.AUTH_CUSTOM_NAME,+ AUTH_CUSTOM_ALLOW_ACCOUNT_LINKING:+ process.env.AUTH_CUSTOM_ALLOW_ACCOUNT_LINKING,AUTH_DOMAINS_WITH_SSO_ENFORCEMENT:process.env.AUTH_DOMAINS_WITH_SSO_ENFORCEMENT,AUTH_DISABLE_USERNAME_PASSWORD: process.env.AUTH_DISABLE_USERNAME_PASSWORD,diff --git a/web/src/pages/auth/sign-in.tsx b/web/src/pages/auth/sign-in.tsxindex 0e0dd4c3f0..5a7d586d78 100644--- a/web/src/pages/auth/sign-in.tsx+++ b/web/src/pages/auth/sign-in.tsx@@ -15,7 +15,7 @@ import { zodResolver } from "@hookform/resolvers/zod";import { FcGoogle } from "react-icons/fc";import { FaGithub } from "react-icons/fa";import { SiOkta, SiAuth0, SiAmazoncognito } from "react-icons/si";-import { TbBrandAzure } from "react-icons/tb";+import { TbBrandAzure, TbBrandOauth } from "react-icons/tb";import { signIn } from "next-auth/react";import Head from "next/head";import Link from "next/link";@@ -50,6 +50,11 @@ export type PageProps = {azureAd: boolean;auth0: boolean;cognito: boolean;+ custom:+ | {+ name: string;+ }+ | false;sso: boolean;};signUpDisabled: boolean;@@ -85,6 +90,13 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async () => {env.AUTH_COGNITO_CLIENT_ID !== undefined &&env.AUTH_COGNITO_CLIENT_SECRET !== undefined &&env.AUTH_COGNITO_ISSUER !== undefined,+ custom:+ env.AUTH_CUSTOM_CLIENT_ID !== undefined &&+ env.AUTH_CUSTOM_CLIENT_SECRET !== undefined &&+ env.AUTH_CUSTOM_ISSUER !== undefined &&+ env.AUTH_CUSTOM_NAME !== undefined+ ? { name: env.AUTH_CUSTOM_NAME }+ : false,sso,},signUpDisabled: env.AUTH_DISABLE_SIGNUP === "true",@@ -186,6 +198,18 @@ export function SSOButtons({Cognito</Button>)}+ {authProviders.custom && (+ <Button+ onClick={() => {+ capture("sign_in:button_click", { provider: "custom" });+ void signIn("custom");+ }}+ variant="secondary"+ >+ <TbBrandOauth className="mr-3" size={18} />+ {authProviders.custom.name}+ </Button>+ )}</div></div>) : nulldiff --git a/web/src/server/auth.ts b/web/src/server/auth.tsindex ba99f4cb82..65d2e2b2ed 100644--- a/web/src/server/auth.ts+++ b/web/src/server/auth.ts@@ -29,6 +29,7 @@ import {} from "@langfuse/ee/sso";import { z } from "zod";import * as Sentry from "@sentry/nextjs";+import { CustomSSOProvider } from "@langfuse/shared/src/server/auth";const staticProviders: Provider[] = [CredentialsProvider({@@ -118,6 +119,22 @@ const staticProviders: Provider[] = [}),];+if (+ env.AUTH_CUSTOM_CLIENT_ID &&+ env.AUTH_CUSTOM_CLIENT_SECRET &&+ env.AUTH_CUSTOM_ISSUER &&+ env.AUTH_CUSTOM_NAME // name required by front-end, ignored here+)+ staticProviders.push(+ CustomSSOProvider({+ clientId: env.AUTH_CUSTOM_CLIENT_ID,+ clientSecret: env.AUTH_CUSTOM_CLIENT_SECRET,+ issuer: env.AUTH_CUSTOM_ISSUER,+ allowDangerousEmailAccountLinking:+ env.AUTH_CUSTOM_ALLOW_ACCOUNT_LINKING === "true",+ }),+ );+if (env.AUTH_GOOGLE_CLIENT_ID && env.AUTH_GOOGLE_CLIENT_SECRET)staticProviders.push(GoogleProvider({