Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(auth): add GitHub Enterprise Authentication Provider (#4463)

jay012944d856747a71
diff --git a/.env.prod.example b/.env.prod.example
index 124e32d128..b72258c7c0 100644
--- a/.env.prod.example
+++ b/.env.prod.example
@@ -67,6 +67,10 @@ OTEL_SERVICE_NAME="langfuse"
# AUTH_GITHUB_CLIENT_ID=
# AUTH_GITHUB_CLIENT_SECRET=
# AUTH_GITHUB_ALLOW_ACCOUNT_LINKING=false
+# AUTH_GITHUB_ENTERPRISE_CLIENT_ID=
+# AUTH_GITHUB_ENTERPRISE_CLIENT_SECRET=
+# AUTH_GITHUB_ENTERPRISE_BASE_URL=
+# AUTH_GITHUB_ENTERPRISE_ALLOW_ACCOUNT_LINKING=false
# AUTH_GITLAB_CLIENT_ID=
# AUTH_GITLAB_CLIENT_SECRET=
# AUTH_GITLAB_ALLOW_ACCOUNT_LINKING=false
diff --git a/packages/shared/src/server/auth/gitHubEnterpriseProvider.ts b/packages/shared/src/server/auth/gitHubEnterpriseProvider.ts
new file mode 100644
index 0000000000..b76ef74eb6
--- /dev/null
+++ b/packages/shared/src/server/auth/gitHubEnterpriseProvider.ts
@@ -0,0 +1,65 @@
+import type { OAuthConfig, OAuthUserConfig } from "next-auth/providers/oauth";
+import type { GithubProfile, GithubEmail } from "next-auth/providers/github";
+
+export function GitHubEnterpriseProvider<P extends GithubProfile>(
+ options: OAuthUserConfig<P> & {
+ enterprise?: {
+ baseUrl?: string;
+ };
+ }
+): OAuthConfig<P> {
+ const baseUrl = options?.enterprise?.baseUrl ?? "https://github.com"
+ const apiBaseUrl = options?.enterprise?.baseUrl
+ ? `${options?.enterprise?.baseUrl}/api/v3`
+ : "https://api.github.com"
+
+ return {
+ id: "github-enterprise",
+ name: "GitHub Enterprise",
+ type: "oauth",
+ authorization: {
+ url: `${baseUrl}/login/oauth/authorize`,
+ params: { scope: "read:user user:email" },
+ },
+ token: `${baseUrl}/login/oauth/access_token`,
+ userinfo: {
+ url: `${apiBaseUrl}/user`,
+ async request({ client, tokens }) {
+ const profile = await client.userinfo(tokens.access_token!)
+
+ if (!profile.email) {
+ // If the user does not have a public email, get another via the GitHub API
+ // See https://docs.github.com/en/rest/users/emails#list-email-addresses-for-the-authenticated-user
+ const res = await fetch(`${apiBaseUrl}/user/emails`, {
+ headers: { Authorization: `token ${tokens.access_token}` },
+ })
+
+ if (res.ok) {
+ const emails: GithubEmail[] = await res.json()
+ profile.email = (emails.find((e) => e.primary) ?? emails[0]).email
+ }
+ }
+
+ return profile
+ },
+ },
+ profile(profile) {
+ return {
+ id: profile.id.toString(),
+ name: profile.name ?? profile.login,
+ email: profile.email,
+ image: profile.avatar_url,
+ }
+ },
+ style: {
+ logo: "https://raw.githubusercontent.com/nextauthjs/next-auth/main/packages/next-auth/provider-logos/github.svg",
+ logoDark:
+ "https://raw.githubusercontent.com/nextauthjs/next-auth/main/packages/next-auth/provider-logos/github-dark.svg",
+ bg: "#fff",
+ bgDark: "#000",
+ text: "#000",
+ textDark: "#fff",
+ },
+ options,
+ }
+}
\ No newline at end of file
diff --git a/packages/shared/src/server/index.ts b/packages/shared/src/server/index.ts
index c12c5514ea..f722c00001 100644
--- a/packages/shared/src/server/index.ts
+++ b/packages/shared/src/server/index.ts
@@ -6,6 +6,7 @@ export * from "./services/PromptService";
export * from "./services/traces-ui-table-service";
export * from "./auth/apiKeys";
export * from "./auth/customSsoProvider";
+export * from "./auth/gitHubEnterpriseProvider";
export * from "./llm/fetchLLMCompletion";
export * from "./llm/types";
export * from "./utils/DatabaseReadStream";
diff --git a/web/src/ee/features/multi-tenant-sso/types.ts b/web/src/ee/features/multi-tenant-sso/types.ts
index ba6756f25f..9b3fb7d18f 100644
--- a/web/src/ee/features/multi-tenant-sso/types.ts
+++ b/web/src/ee/features/multi-tenant-sso/types.ts
@@ -28,6 +28,20 @@ export const GithubProviderSchema = base.extend({
.nullish(),
});
+export const GithubEnterpriseProviderSchema = base.extend({
+ authProvider: z.literal("github-enterprise"),
+ authConfig: z
+ .object({
+ clientId: z.string(),
+ clientSecret: z.string(),
+ enterprise: z.object({
+ baseUrl: z.string().url(),
+ }),
+ allowDangerousEmailAccountLinking: z.boolean().optional().default(false),
+ })
+ .nullish(),
+});
+
export const GitlabProviderSchema = base.extend({
authProvider: z.literal("gitlab"),
authConfig: z
@@ -104,6 +118,7 @@ export const CustomProviderSchema = base.extend({
export type GoogleProviderSchema = z.infer<typeof GoogleProviderSchema>;
export type GithubProviderSchema = z.infer<typeof GithubProviderSchema>;
+export type GithubEnterpriseProviderSchema = z.infer<typeof GithubEnterpriseProviderSchema>;
export type GitlabProviderSchema = z.infer<typeof GitlabProviderSchema>;
export type Auth0ProviderSchema = z.infer<typeof Auth0ProviderSchema>;
export type OktaProviderSchema = z.infer<typeof OktaProviderSchema>;
@@ -114,6 +129,7 @@ export type CustomProviderSchema = z.infer<typeof CustomProviderSchema>;
export const SsoProviderSchema = z.discriminatedUnion("authProvider", [
GoogleProviderSchema,
GithubProviderSchema,
+ GithubEnterpriseProviderSchema,
GitlabProviderSchema,
Auth0ProviderSchema,
OktaProviderSchema,
diff --git a/web/src/ee/features/multi-tenant-sso/utils.ts b/web/src/ee/features/multi-tenant-sso/utils.ts
index bef842ec10..1573b26598 100644
--- a/web/src/ee/features/multi-tenant-sso/utils.ts
+++ b/web/src/ee/features/multi-tenant-sso/utils.ts
@@ -12,6 +12,7 @@ import { decrypt } from "@langfuse/shared/encryption";
import { SsoProviderSchema } from "./types";
import {
CustomSSOProvider,
+ GitHubEnterpriseProvider,
logger,
traceException,
} from "@langfuse/shared/src/server";
@@ -197,6 +198,15 @@ const dbToNextAuthProvider = (provider: SsoProviderSchema): Provider | null => {
params: { scope: provider.authConfig.scope ?? "openid email profile" },
},
});
+ else if (provider.authProvider === "github-enterprise")
+ return GitHubEnterpriseProvider({
+ id: getAuthProviderIdForSsoConfig(provider), // use the domain as the provider id as we use domain-specific credentials
+ ...provider.authConfig,
+ clientSecret: decrypt(provider.authConfig.clientSecret),
+ enterprise: {
+ baseUrl: provider.authConfig.enterprise.baseUrl,
+ },
+ });
else {
// Type check to ensure we handle all providers
// eslint-disable-next-line @typescript-eslint/no-unused-vars
diff --git a/web/src/env.mjs b/web/src/env.mjs
index 5122dd60c6..c48a590f94 100644
--- a/web/src/env.mjs
+++ b/web/src/env.mjs
@@ -57,6 +57,10 @@ export const env = createEnv({
AUTH_GITHUB_CLIENT_ID: z.string().optional(),
AUTH_GITHUB_CLIENT_SECRET: z.string().optional(),
AUTH_GITHUB_ALLOW_ACCOUNT_LINKING: z.enum(["true", "false"]).optional(),
+ AUTH_GITHUB_ENTERPRISE_CLIENT_ID: z.string().optional(),
+ AUTH_GITHUB_ENTERPRISE_CLIENT_SECRET: z.string().optional(),
+ AUTH_GITHUB_ENTERPRISE_BASE_URL: z.string().optional(),
+ AUTH_GITHUB_ENTERPRISE_ALLOW_ACCOUNT_LINKING: z.enum(["true", "false"]).optional(),
AUTH_GITLAB_CLIENT_ID: z.string().optional(),
AUTH_GITLAB_CLIENT_SECRET: z.string().optional(),
AUTH_GITLAB_ALLOW_ACCOUNT_LINKING: z.enum(["true", "false"]).optional(),
@@ -291,6 +295,11 @@ export const env = createEnv({
AUTH_GITHUB_CLIENT_SECRET: process.env.AUTH_GITHUB_CLIENT_SECRET,
AUTH_GITHUB_ALLOW_ACCOUNT_LINKING:
process.env.AUTH_GITHUB_ALLOW_ACCOUNT_LINKING,
+ AUTH_GITHUB_ENTERPRISE_CLIENT_ID: process.env.AUTH_GITHUB_ENTERPRISE_CLIENT_ID,
+ AUTH_GITHUB_ENTERPRISE_CLIENT_SECRET: process.env.AUTH_GITHUB_ENTERPRISE_CLIENT_SECRET,
+ AUTH_GITHUB_ENTERPRISE_BASE_URL: process.env.AUTH_GITHUB_ENTERPRISE_BASE_URL,
+ AUTH_GITHUB_ENTERPRISE_ALLOW_ACCOUNT_LINKING:
+ process.env.AUTH_GITHUB_ENTERPRISE_ALLOW_ACCOUNT_LINKING,
AUTH_GITLAB_ISSUER: process.env.AUTH_GITLAB_ISSUER,
AUTH_GITLAB_CLIENT_ID: process.env.AUTH_GITLAB_CLIENT_ID,
AUTH_GITLAB_CLIENT_SECRET: process.env.AUTH_GITLAB_CLIENT_SECRET,
diff --git a/web/src/pages/auth/sign-in.tsx b/web/src/pages/auth/sign-in.tsx
index 075dc7b92e..967145b6ff 100644
--- a/web/src/pages/auth/sign-in.tsx
+++ b/web/src/pages/auth/sign-in.tsx
@@ -46,6 +46,7 @@ export type PageProps = {
credentials: boolean;
google: boolean;
github: boolean;
+ githubEnterprise: boolean;
gitlab: boolean;
okta: boolean;
azureAd: boolean;
@@ -74,6 +75,10 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async () => {
github:
env.AUTH_GITHUB_CLIENT_ID !== undefined &&
env.AUTH_GITHUB_CLIENT_SECRET !== undefined,
+ githubEnterprise:
+ env.AUTH_GITHUB_ENTERPRISE_CLIENT_ID !== undefined &&
+ env.AUTH_GITHUB_ENTERPRISE_CLIENT_SECRET !== undefined &&
+ env.AUTH_GITHUB_ENTERPRISE_BASE_URL !== undefined,
gitlab:
env.AUTH_GITLAB_CLIENT_ID !== undefined &&
env.AUTH_GITLAB_CLIENT_SECRET !== undefined,
@@ -165,6 +170,16 @@ export function SSOButtons({
Github
</Button>
)}
+ {authProviders.githubEnterprise && (
+ <Button
+ onClick={() => handleSignIn("github-enterprise")}
+ variant="secondary"
+ loading={providerSigningIn === "github-enterprise"}
+ >
+ <FaGithub className="mr-3" size={18} />
+ Github Enterprise
+ </Button>
+ )}
{authProviders.gitlab && (
<Button
onClick={() => handleSignIn("gitlab")}
diff --git a/web/src/server/auth.ts b/web/src/server/auth.ts
index 5d66d10399..c3bdefc303 100644
--- a/web/src/server/auth.ts
+++ b/web/src/server/auth.ts
@@ -33,6 +33,7 @@ import { z } from "zod";
import { CloudConfigSchema } from "@langfuse/shared";
import {
CustomSSOProvider,
+ GitHubEnterpriseProvider,
traceException,
sendResetPasswordVerificationRequest,
instrumentAsync,
@@ -227,6 +228,22 @@ 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
+) {
+ 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",
+ })
+ );
+}
+
if (env.AUTH_GITLAB_CLIENT_ID && env.AUTH_GITLAB_CLIENT_SECRET)
staticProviders.push(
GitLabProvider({