hermes-agent/apps/desktop/electron/portal-cookies.ts
kshitijk4poor c725f80ac8 fix(desktop): portal windows settle on a new access cookie, forced renewal never short-circuits
Both portal windows in portal-session.ts now share one driver that settles
only when the jar holds an access cookie it did not hold when the window
opened. Silent renewal already required that (#114666); the interactive
login window still closed on mere presence, so a server-rejected token left
unexpired in Chromium's jar closed the sign-in window on its first navigation
and discovery 401'd again, looping the user through "Sign in" with no way in.

renewPortalAccessSilently({ force: true }) — the retry after a discovery 401
with the current cookie — could be handed an in-flight unforced renewal that
resolves true from its "access cookie present" short-circuit without touching
the portal. The short-circuit now runs per caller before joining the shared
window drive, so a forced caller always waits for a real renewal.

The live Electron fixture covers both: a rejected cookie no longer completes
the login window before /complete issues a new one, and a forced renewal
racing an unforced one still refreshes exactly once. Both assertions fail
against the previous portal-session.ts.
2026-09-18 16:24:07 +05:30

24 lines
972 B
TypeScript

export interface PortalCookie {
name: string
value: string
}
// NAS accepts either credential during migration. Provider-routing hints and
// WorkOS logout identifiers are not credentials; gateway cookies are unrelated.
const ACCESS_COOKIES = ['nas-session', '__Host-privy-token', '__Secure-privy-token', 'privy-token']
const REFRESH_COOKIES = ['nas-refresh', 'privy-session', 'privy-refresh-token']
export function portalAccessCookies(cookies: PortalCookie[]): PortalCookie[] {
return cookies.filter(cookie => cookie?.value && ACCESS_COOKIES.includes(cookie.name))
}
export function cookiesHavePortalAccessToken(cookies: unknown): boolean {
return Array.isArray(cookies) && portalAccessCookies(cookies).length > 0
}
export function cookiesHavePortalSession(cookies: unknown): boolean {
return (
cookiesHavePortalAccessToken(cookies) ||
(Array.isArray(cookies) && cookies.some(cookie => cookie?.value && REFRESH_COOKIES.includes(cookie.name)))
)
}