* feat(auth): support nested OIDC claim paths * test(auth): cover OIDC profile name fallbacks * test(auth): cover missing OIDC username fallback * test(auth): preserve OIDC provider guard --------- Co-authored-by: Artur <145776561+arturict@users.noreply.github.com> Co-authored-by: arturict <arturict@users.noreply.github.com>
24 lines
906 B
TypeScript
24 lines
906 B
TypeScript
import type { Profile } from "@auth/core/types";
|
|
|
|
import { env } from "../../env";
|
|
|
|
export const getProfileValueByPath = (profile: Profile, path: string): unknown => {
|
|
return path.split(".").reduce<unknown>((value, key) => {
|
|
if (typeof value !== "object" || value === null || !Object.hasOwn(value, key)) {
|
|
return undefined;
|
|
}
|
|
|
|
return (value as Record<string, unknown>)[key];
|
|
}, profile);
|
|
};
|
|
|
|
export const extractProfileName = (profile: Profile): string | undefined => {
|
|
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) ?? undefined;
|
|
}
|
|
|
|
const profileName = getProfileValueByPath(profile, env.AUTH_OIDC_NAME_ATTRIBUTE_OVERWRITE);
|
|
return typeof profileName === "string" ? profileName : undefined;
|
|
};
|