Skip to content

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

feat(auth): add AUTH_CUSTOM_SCOPE to change oauth scopes of custom oauth setups (#2459)

Marc Klingen6127f2375886
diff --git a/.env.prod.example b/.env.prod.example
index f49df4b565..3e51c79a68 100644
--- a/.env.prod.example
+++ b/.env.prod.example
@@ -67,6 +67,7 @@ LANGFUSE_CSP_ENFORCE_HTTPS="true"
# AUTH_CUSTOM_CLIENT_SECRET=
# AUTH_CUSTOM_ISSUER=
# AUTH_CUSTOM_NAME=
+# AUTH_CUSTOM_SCOPE="openid email profile" # optional
# AUTH_CUSTOM_ALLOW_ACCOUNT_LINKING=false
# Transactional email, optional
diff --git a/ee/src/sso/index.ts b/ee/src/sso/index.ts
index b76a348596..95735d1c40 100644
--- a/ee/src/sso/index.ts
+++ b/ee/src/sso/index.ts
@@ -179,6 +179,9 @@ const dbToNextAuthProvider = (provider: SsoProviderSchema): Provider | null => {
id: getAuthProviderIdForSsoConfig(provider), // use the domain as the provider id as we use domain-specific credentials
...provider.authConfig,
clientSecret: decrypt(provider.authConfig.clientSecret),
+ authorization: {
+ params: { scope: provider.authConfig.scope ?? "openid email profile" },
+ },
});
else {
// Type check to ensure we handle all providers
diff --git a/ee/src/sso/types.ts b/ee/src/sso/types.ts
index 65945f4529..080d8433fa 100644
--- a/ee/src/sso/types.ts
+++ b/ee/src/sso/types.ts
@@ -84,6 +84,7 @@ export const CustomProviderSchema = base.extend({
clientId: z.string(),
clientSecret: z.string(),
issuer: z.string(),
+ scope: z.string().nullish(),
allowDangerousEmailAccountLinking: z.boolean().optional().default(false),
})
.nullish(),
diff --git a/packages/shared/src/server/auth.ts b/packages/shared/src/server/auth.ts
index 94d03e6efb..6b5489583f 100644
--- a/packages/shared/src/server/auth.ts
+++ b/packages/shared/src/server/auth.ts
@@ -65,7 +65,7 @@ export function CustomSSOProvider<P extends CustomSSOUser>(
name: "CustomSSOProvider",
type: "oauth",
wellKnown: `${options.issuer}/.well-known/openid-configuration`,
- authorization: { params: { scope: "openid email profile" } },
+ authorization: { params: { scope: "openid email profile" } }, // overridden by options.authorization to be able to set custom scopes, deep merged with this default
checks: ["pkce", "state"],
idToken: true,
profile(profile) {
diff --git a/web/src/env.mjs b/web/src/env.mjs
index 35bdcd0a39..e13863b2c0 100644
--- a/web/src/env.mjs
+++ b/web/src/env.mjs
@@ -82,6 +82,7 @@ export const env = createEnv({
AUTH_CUSTOM_CLIENT_SECRET: z.string().optional(),
AUTH_CUSTOM_ISSUER: z.string().url().optional(),
AUTH_CUSTOM_NAME: z.string().optional(),
+ AUTH_CUSTOM_SCOPE: 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(),
@@ -192,6 +193,7 @@ export const env = createEnv({
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_SCOPE: process.env.AUTH_CUSTOM_SCOPE,
AUTH_CUSTOM_ALLOW_ACCOUNT_LINKING:
process.env.AUTH_CUSTOM_ALLOW_ACCOUNT_LINKING,
AUTH_DOMAINS_WITH_SSO_ENFORCEMENT:
diff --git a/web/src/server/auth.ts b/web/src/server/auth.ts
index c203d2247a..9edad92abb 100644
--- a/web/src/server/auth.ts
+++ b/web/src/server/auth.ts
@@ -154,6 +154,9 @@ if (
issuer: env.AUTH_CUSTOM_ISSUER,
allowDangerousEmailAccountLinking:
env.AUTH_CUSTOM_ALLOW_ACCOUNT_LINKING === "true",
+ authorization: {
+ params: { scope: env.AUTH_CUSTOM_SCOPE ?? "openid email profile" },
+ },
}),
);