// Keeping the sign-in alive, and noticing when it cannot be kept alive. // // ATProto's provider gives an ordinary public client a session that lasts two // weeks from the sign-in, regardless of activity, and refuses `prompt=none` // re-authorization for those clients. So a re-login every two weeks is a // server-side floor, not something the extension can refresh past. What this // module can do is use the whole window: refresh before the refresh token's // own idle lifetime can run out (some PDSes are stricter than the defaults), // and find out that the session is gone at the next alarm instead of the next // time the user happens to open the popup. // // The refresh has to run in a DOM context, hence `oauth-offscreen.ts`; this // module only decides when and what the answer means. import { restoreInOffscreen } from './lib/oauth-offscreen' import { decideRefresh } from './lib/session-health' import { SESSION_EXPIRED_KEY, getStoredSession, markSessionExpired } from './lib/session-store' import { ext } from './lib/platform' export const SESSION_REFRESH_ALARM = 'session-refresh' /** * Once a day. A refresh token is only tried when the client reaches the * server, so there is nothing to gain from more often; the access token's own * hour is refreshed on use by the OAuth client. Chrome accepts this period * without clamping. */ export const SESSION_REFRESH_PERIOD_MINUTES = 24 * 60 /** * (Re)create the alarm. Called on install, on update, and on browser start; * creating an alarm with an existing name replaces it, so this is also what * recovers from a profile whose alarm was lost. */ export function scheduleSessionRefresh(): void { void ext.alarms.create(SESSION_REFRESH_ALARM, { delayInMinutes: 1, periodInMinutes: SESSION_REFRESH_PERIOD_MINUTES, }) } /** The refresh already running, if any. */ let inFlight: Promise | undefined /** * Force a token rotation and act on what it proved. A session that is gone is * marked expired (the badge and popup both key off that marker); an * unreachable PDS changes nothing. * * Chrome fires startup and install together, so callers often overlap. They * share one restore rather than rotating the same refresh token twice: the * server retires a rotated token, and a loser of that race could leave the * winner's token out of storage. */ export function refreshStoredSession(reason: string): Promise { if (inFlight) { console.debug('[substandard] session refresh already in flight; joining it', reason) return inFlight } inFlight = rotateToken(reason).finally(() => { inFlight = undefined }) return inFlight } async function rotateToken(reason: string): Promise { const session = await getStoredSession() if (!session) return if (navigator.onLine === false) { console.debug('[substandard] session refresh skipped while offline', reason) return } const outcome = await restoreInOffscreen(session.did, true) switch (decideRefresh(outcome.status, true)) { case 'mark-expired': console.debug( '[substandard] session refresh found the session gone', reason, 'reason' in outcome ? outcome.reason : '', ) await markSessionExpired() return case 'clear-expiry': // Normally a sign-in already cleared it; this covers a note written // between that sign-in and this check. await ext.storage.local.remove(SESSION_EXPIRED_KEY) console.debug('[substandard] session refreshed', reason) return case 'retry-later': console.debug( '[substandard] session refresh could not reach the PDS; keeping the session', reason, 'reason' in outcome ? outcome.reason : '', ) } }