Skip to content

Commit

Permalink
fix: profile name extraction is also needed in signin event
Browse files Browse the repository at this point in the history
  • Loading branch information
Meierschlumpf committed Jan 4, 2025
1 parent fe991d0 commit ac31f98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
19 changes: 13 additions & 6 deletions packages/auth/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NextAuthConfig["events"], undefined>["signIn"] => {
return async ({ user, profile }) => {
Expand Down Expand Up @@ -43,12 +44,18 @@ export const createSignInEventHandler = (db: Database): Exclude<NextAuthConfig["
);
}

const profileUsername = profile?.preferred_username?.includes("@") ? profile.name : profile?.preferred_username;
if (profileUsername && dbUser.name !== profileUsername) {
await db.update(users).set({ name: profileUsername }).where(eq(users.id, user.id));
logger.info(
`Username for user of oidc provider has changed. user=${user.id} old='${dbUser.name}' new='${profileUsername}'`,
);
if (profile) {
const profileUsername = extractProfileName(profile);
if (!profileUsername) {
throw new Error(`OIDC provider did not return a name properties='${Object.keys(profile).join(",")}'`);
}

if (dbUser.name !== profileUsername) {
await db.update(users).set({ name: profileUsername }).where(eq(users.id, user.id));
logger.info(
`Username for user of oidc provider has changed. user=${user.id} old='${dbUser.name}' new='${profileUsername}'`,
);
}
}

logger.info(`User '${dbUser.name}' logged in at ${dayjs().format()}`);
Expand Down
19 changes: 7 additions & 12 deletions packages/auth/providers/oidc/oidc-provider.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import type { ReadonlyHeaders } from "next/dist/server/web/spec-extension/adapters/headers";
import type { OIDCConfig } from "next-auth/providers";
import type { OIDCConfig } from "@auth/core/providers";
import type { Profile } from "@auth/core/types";

import { env } from "../../env.mjs";
import { createRedirectUri } from "../../redirect";

interface Profile {
sub: string;
name: string;
email: string;
groups: string[];
preferred_username?: string;
email_verified: boolean;
}

export const OidcProvider = (headers: ReadonlyHeaders | null): OIDCConfig<Profile> => ({
id: "oidc",
name: env.AUTH_OIDC_CLIENT_NAME,
Expand All @@ -28,7 +20,10 @@ export const OidcProvider = (headers: ReadonlyHeaders | null): OIDCConfig<Profil
},
},
profile(profile) {
const name = extractName(profile);
if (!profile.sub) {
throw new Error(`OIDC provider did not return a sub property='${Object.keys(profile).join(",")}'`);
}
const name = extractProfileName(profile);
if (!name) {
throw new Error(`OIDC provider did not return a name properties='${Object.keys(profile).join(",")}'`);
}
Expand All @@ -42,7 +37,7 @@ export const OidcProvider = (headers: ReadonlyHeaders | null): OIDCConfig<Profil
},
});

const extractName = (profile: Profile) => {
export const extractProfileName = (profile: Profile) => {
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;
Expand Down

0 comments on commit ac31f98

Please sign in to comment.