Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions components/user/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { useAuth } from "@/contexts/auth-context"
import { usePermissions } from "@/hooks/use-permissions"
import { useSidebar } from "@/components/ui/sidebar"
import { resolveAccountDisplayName } from "@/lib/account-display"
import { resolveAccountAvatarUrl, resolveAccountMenuName } from "@/lib/account-display"
import { getThemeManifest } from "@/lib/theme/manifest"

function resolveAvatarPath(path: string): string {
Expand Down Expand Up @@ -54,8 +54,25 @@ export function UserDropdown() {
const { state } = useSidebar()
const isCollapsed = state === "collapsed"
const theme = getThemeManifest()
const accountIdentity = userInfo as {
account_name?: string
username?: string
email?: string
display_name?: string
picture?: string
} | null
const identity = {
access_key: accountIdentity?.account_name ?? "",
username: accountIdentity?.username,
email: accountIdentity?.email,
display_name: accountIdentity?.display_name,
picture: accountIdentity?.picture,
}
const accountName = resolveAccountMenuName(identity)
const identityAvatar = resolveAccountAvatarUrl(identity)
const baseAvatarPath = theme.assets.userAvatar ?? "/img/userAvatar.png"
const preferredAvatarPath = resolvedTheme === "dark" ? withDarkVariant(baseAvatarPath) : baseAvatarPath
const preferredAvatarPath =
identityAvatar ?? (resolvedTheme === "dark" ? withDarkVariant(baseAvatarPath) : baseAvatarPath)
const [avatar, setAvatar] = useState(() => resolveAvatarPath(preferredAvatarPath))

useEffect(() => {
Expand All @@ -69,12 +86,6 @@ export function UserDropdown() {
}
}

const accountIdentity = userInfo as { account_name?: string; username?: string; email?: string } | null
const accountName = resolveAccountDisplayName({
access_key: accountIdentity?.account_name ?? "",
username: accountIdentity?.username,
email: accountIdentity?.email,
})
const roleLabel = isAdmin ? t("Administrator") : t("User")

return (
Expand All @@ -88,6 +99,7 @@ export function UserDropdown() {
alt=""
width={24}
height={24}
referrerPolicy="no-referrer"
className="size-6 rounded-full object-cover"
onError={() => {
const fallback = resolveAvatarPath(baseAvatarPath)
Expand Down
4 changes: 4 additions & 0 deletions hooks/use-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export interface AccountInfo {
access_key: string
username?: string
email?: string
/** Full name from the OIDC identity provider; display only. */
display_name?: string
/** Avatar URL from the OIDC identity provider; display only. */
picture?: string
identity_type: IdentityType
session_access_key?: string
is_admin: boolean
Expand Down
28 changes: 23 additions & 5 deletions lib/account-display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@ export interface AccountDisplayIdentity {
access_key: string
username?: string
email?: string
display_name?: string
picture?: string
}

function firstNonBlank(values: (string | undefined)[]): string {
return values.find((value): value is string => typeof value === "string" && value.trim().length > 0)?.trim() ?? ""
}

/** Resolve display-only identity metadata without changing the account principal. */
export function resolveAccountDisplayName(identity: AccountDisplayIdentity): string {
return (
[identity.username, identity.email, identity.access_key]
.find((value): value is string => typeof value === "string" && value.trim().length > 0)
?.trim() ?? ""
)
return firstNonBlank([identity.username, identity.email, identity.access_key])
}

/** Name for compact identity chrome (user menu): the full name from the identity provider when known. */
export function resolveAccountMenuName(identity: AccountDisplayIdentity): string {
return firstNonBlank([identity.display_name, resolveAccountDisplayName(identity)])
}

/** Avatar URL from the identity provider, accepted only as an absolute https URL. */
export function resolveAccountAvatarUrl(identity: AccountDisplayIdentity): string | undefined {
const picture = identity.picture?.trim()
if (!picture) return undefined
try {
return new URL(picture).protocol === "https:" ? picture : undefined
} catch {
return undefined
}
}
31 changes: 30 additions & 1 deletion tests/lib/account-display.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from "node:test"
import assert from "node:assert/strict"
import { resolveAccountDisplayName } from "../../lib/account-display"
import { resolveAccountAvatarUrl, resolveAccountDisplayName, resolveAccountMenuName } from "../../lib/account-display"

test("account display name prefers the configured OIDC username", () => {
assert.equal(
Expand All @@ -23,3 +23,32 @@ test("account display name falls back to email when username is absent or blank"
test("account display name remains compatible with responses that only contain access_key", () => {
assert.equal(resolveAccountDisplayName({ access_key: "legacy-account" }), "legacy-account")
})

test("account menu name prefers the OIDC full name", () => {
assert.equal(
resolveAccountMenuName({ access_key: "virtual-parent", username: "jdoe", display_name: " Jane Doe " }),
"Jane Doe",
)
})

test("account menu name falls back to the username chain when the full name is blank", () => {
assert.equal(resolveAccountMenuName({ access_key: "virtual-parent", username: "jdoe", display_name: " " }), "jdoe")
assert.equal(resolveAccountMenuName({ access_key: "legacy-account" }), "legacy-account")
})

test("account avatar accepts only absolute https picture URLs", () => {
assert.equal(
resolveAccountAvatarUrl({ access_key: "a", picture: "https://idp.example.test/a.png" }),
"https://idp.example.test/a.png",
)
for (const picture of [
undefined,
"",
"http://idp.example.test/a.png",
"javascript:alert(1)",
"/img/a.png",
"not a url",
]) {
assert.equal(resolveAccountAvatarUrl({ access_key: "a", picture }), undefined, String(picture))
}
})
7 changes: 5 additions & 2 deletions tests/lib/account-surface.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ test("the profile page distinguishes a failed read from an empty profile", () =>
test("the user menu prefers OIDC profile metadata without adding another account request", () => {
const source = read("components/user/dropdown.tsx")

assert.match(source, /account_name\?: string; username\?: string; email\?: string/)
assert.match(source, /resolveAccountDisplayName\(\{/)
for (const field of ["account_name", "username", "email", "display_name", "picture"]) {
assert.match(source, new RegExp(`${field}\\?: string`))
}
assert.match(source, /resolveAccountMenuName\(identity\)/)
assert.match(source, /resolveAccountAvatarUrl\(identity\)/)
assert.doesNotMatch(source, /getAccountInfo\(\)/)
})

Expand Down
6 changes: 5 additions & 1 deletion tests/lib/ui-layout-source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ test("the account menu names the signed-in identity and its authority", () => {

// A menu that opens on an avatar with no name answers neither "who am I" nor
// "with what authority", which is the gap this menu existed with before.
assert.match(source, /const accountName = resolveAccountDisplayName\(\{/)
assert.match(source, /const accountName = resolveAccountMenuName\(identity\)/)
// The identity provider's avatar replaces the theme placeholder, without
// leaking the console URL to the image host.
assert.match(source, /identityAvatar \?\? \(resolvedTheme === "dark"/)
assert.match(source, /referrerPolicy="no-referrer"/)
assert.match(source, /access_key: accountIdentity\?\.account_name \?\? ""/)
assert.match(source, /const roleLabel = isAdmin \? t\("Administrator"\) : t\("User"\)/)
assert.match(source, /\{accountName \|\| t\("Unknown user"\)\}/)
Expand Down