refactor: share auth label suffix formatting

This commit is contained in:
Peter Steinberger 2026-03-13 22:19:57 +00:00
parent bd758bb438
commit da51e40638
1 changed files with 28 additions and 32 deletions

View File

@ -33,6 +33,22 @@ function resolveStoredCredentialLabel(params: {
return "missing"; return "missing";
} }
function formatExpirationLabel(
expires: unknown,
now: number,
formatUntil: (timestampMs: number) => string,
compactExpiredPrefix = " expired",
) {
if (typeof expires !== "number" || !Number.isFinite(expires) || expires <= 0) {
return "";
}
return expires <= now ? compactExpiredPrefix : ` exp ${formatUntil(expires)}`;
}
function formatFlagsSuffix(flags: string[]) {
return flags.length > 0 ? ` (${flags.join(", ")})` : "";
}
export const resolveAuthLabel = async ( export const resolveAuthLabel = async (
provider: string, provider: string,
cfg: OpenClawConfig, cfg: OpenClawConfig,
@ -89,14 +105,7 @@ export const resolveAuthLabel = async (
refValue: profile.tokenRef, refValue: profile.tokenRef,
mode, mode,
}); });
const exp = const exp = formatExpirationLabel(profile.expires, now, formatUntil);
typeof profile.expires === "number" &&
Number.isFinite(profile.expires) &&
profile.expires > 0
? profile.expires <= now
? " expired"
: ` exp ${formatUntil(profile.expires)}`
: "";
return { return {
label: `${profileId} token ${tokenLabel}${exp}${more}`, label: `${profileId} token ${tokenLabel}${exp}${more}`,
source: "", source: "",
@ -104,14 +113,7 @@ export const resolveAuthLabel = async (
} }
const display = resolveAuthProfileDisplayLabel({ cfg, store, profileId }); const display = resolveAuthProfileDisplayLabel({ cfg, store, profileId });
const label = display === profileId ? profileId : display; const label = display === profileId ? profileId : display;
const exp = const exp = formatExpirationLabel(profile.expires, now, formatUntil);
typeof profile.expires === "number" &&
Number.isFinite(profile.expires) &&
profile.expires > 0
? profile.expires <= now
? " expired"
: ` exp ${formatUntil(profile.expires)}`
: "";
return { label: `${label} oauth${exp}${more}`, source: "" }; return { label: `${label} oauth${exp}${more}`, source: "" };
} }
@ -140,7 +142,7 @@ export const resolveAuthLabel = async (
configProfile.mode !== profile.type && configProfile.mode !== profile.type &&
!(configProfile.mode === "oauth" && profile.type === "token")) !(configProfile.mode === "oauth" && profile.type === "token"))
) { ) {
const suffix = flags.length > 0 ? ` (${flags.join(", ")})` : ""; const suffix = formatFlagsSuffix(flags);
return `${profileId}=missing${suffix}`; return `${profileId}=missing${suffix}`;
} }
if (profile.type === "api_key") { if (profile.type === "api_key") {
@ -149,7 +151,7 @@ export const resolveAuthLabel = async (
refValue: profile.keyRef, refValue: profile.keyRef,
mode, mode,
}); });
const suffix = flags.length > 0 ? ` (${flags.join(", ")})` : ""; const suffix = formatFlagsSuffix(flags);
return `${profileId}=${keyLabel}${suffix}`; return `${profileId}=${keyLabel}${suffix}`;
} }
if (profile.type === "token") { if (profile.type === "token") {
@ -158,14 +160,11 @@ export const resolveAuthLabel = async (
refValue: profile.tokenRef, refValue: profile.tokenRef,
mode, mode,
}); });
if ( const expirationFlag = formatExpirationLabel(profile.expires, now, formatUntil, "expired");
typeof profile.expires === "number" && if (expirationFlag) {
Number.isFinite(profile.expires) && flags.push(expirationFlag);
profile.expires > 0
) {
flags.push(profile.expires <= now ? "expired" : `exp ${formatUntil(profile.expires)}`);
} }
const suffix = flags.length > 0 ? ` (${flags.join(", ")})` : ""; const suffix = formatFlagsSuffix(flags);
return `${profileId}=token:${tokenLabel}${suffix}`; return `${profileId}=token:${tokenLabel}${suffix}`;
} }
const display = resolveAuthProfileDisplayLabel({ const display = resolveAuthProfileDisplayLabel({
@ -179,15 +178,12 @@ export const resolveAuthLabel = async (
: display.startsWith(profileId) : display.startsWith(profileId)
? display.slice(profileId.length).trim() ? display.slice(profileId.length).trim()
: `(${display})`; : `(${display})`;
if ( const expirationFlag = formatExpirationLabel(profile.expires, now, formatUntil, "expired");
typeof profile.expires === "number" && if (expirationFlag) {
Number.isFinite(profile.expires) && flags.push(expirationFlag);
profile.expires > 0
) {
flags.push(profile.expires <= now ? "expired" : `exp ${formatUntil(profile.expires)}`);
} }
const suffixLabel = suffix ? ` ${suffix}` : ""; const suffixLabel = suffix ? ` ${suffix}` : "";
const suffixFlags = flags.length > 0 ? ` (${flags.join(", ")})` : ""; const suffixFlags = formatFlagsSuffix(flags);
return `${profileId}=OAuth${suffixLabel}${suffixFlags}`; return `${profileId}=OAuth${suffixLabel}${suffixFlags}`;
}); });
return { return {