diff --git a/packages/auth/env.mjs b/packages/auth/env.mjs index ebc4555df..9105ee776 100644 --- a/packages/auth/env.mjs +++ b/packages/auth/env.mjs @@ -74,6 +74,7 @@ export const env = createEnv({ AUTH_OIDC_AUTO_LOGIN: booleanSchema, AUTH_OIDC_SCOPE_OVERWRITE: z.string().min(1).default("openid email profile groups"), AUTH_OIDC_GROUPS_ATTRIBUTE: z.string().default("groups"), // Is used in the signIn event to assign the correct groups, key is from object of decoded id_token + AUTH_OIDC_NAME_ATTRIBUTE_OVERWRITE: z.string().optional(), } : {}), ...(authProviders.includes("ldap") @@ -117,6 +118,7 @@ export const env = createEnv({ AUTH_LDAP_USER_MAIL_ATTRIBUTE: process.env.AUTH_LDAP_USER_MAIL_ATTRIBUTE, AUTH_LDAP_USERNAME_FILTER_EXTRA_ARG: process.env.AUTH_LDAP_USERNAME_FILTER_EXTRA_ARG, AUTH_OIDC_AUTO_LOGIN: process.env.AUTH_OIDC_AUTO_LOGIN, + AUTH_OIDC_NAME_ATTRIBUTE_OVERWRITE: process.env.AUTH_OIDC_NAME_ATTRIBUTE_OVERWRITE, }, skipValidation, }); diff --git a/packages/auth/events.ts b/packages/auth/events.ts index 6b299d0a9..31713ac7d 100644 --- a/packages/auth/events.ts +++ b/packages/auth/events.ts @@ -9,6 +9,7 @@ import { colorSchemeCookieKey, everyoneGroup } from "@homarr/definitions"; import { logger } from "@homarr/log"; import { env } from "./env.mjs"; +import { extractProfileName } from "./providers/oidc/oidc-provider"; export const createSignInEventHandler = (db: Database): Exclude["signIn"] => { return async ({ user, profile }) => { @@ -43,12 +44,18 @@ export const createSignInEventHandler = (db: Database): Exclude => ({ id: "oidc", name: env.AUTH_OIDC_CLIENT_NAME, @@ -28,12 +20,28 @@ export const OidcProvider = (headers: ReadonlyHeaders | null): OIDCConfig { + if (!env.AUTH_OIDC_NAME_ATTRIBUTE_OVERWRITE) { + // Use the name as the username if the preferred_username is an email address + return profile.preferred_username?.includes("@") ? profile.name : profile.preferred_username; + } + + return profile[env.AUTH_OIDC_NAME_ATTRIBUTE_OVERWRITE as keyof typeof profile] as string; +}; diff --git a/turbo.json b/turbo.json index b3732c66e..e259bcb22 100644 --- a/turbo.json +++ b/turbo.json @@ -17,6 +17,7 @@ "AUTH_OIDC_ISSUER", "AUTH_OIDC_SCOPE_OVERWRITE", "AUTH_OIDC_GROUPS_ATTRIBUTE", + "AUTH_OIDC_NAME_ATTRIBUTE_OVERWRITE", "AUTH_LDAP_USERNAME_ATTRIBUTE", "AUTH_LDAP_USER_MAIL_ATTRIBUTE", "AUTH_LDAP_USERNAME_FILTER_EXTRA_ARG",