// Pure helpers for the interactive sign-in flow. Shared by the worker and the // OAuth host, and unit-testable without extension APIs. import { isFirefox } from './platform' /** storage.session key for the last sign-in failure, shown by the popup. */ export const AUTH_ERROR_KEY = 'authError' /** * Firefox has no extension-id host to redirect to, so it uses one fixed * subdomain. That host deliberately has no DNS record: the navigation fails, * like chromiumapp.org, and the consent window reports the attempted URL in * its error page. It must never gain a DNS record, and must match the appended * entry in oauth/client-metadata.json exactly. * * The host cannot use a reserved TLD such as `.invalid`: the PDS validates * every redirect_uri in the client metadata, rejects special-use TLDs, and * then rejects the whole client before any browser's redirect is considered. */ export const FIREFOX_REDIRECT_URI = 'https://oauth2.substandard.blog/oauth2' /** * The https redirect URI the PDS sends the consent window back to. On Chrome * the host is the extension id — stable because the manifest pins `key` — and * must match a redirect_uri in oauth/client-metadata.json exactly. The domain * never resolves; we only need the navigation attempt to be observable. */ export function oauthRedirectUri(extensionId: string): string { return isFirefox ? FIREFOX_REDIRECT_URI : `https://${extensionId}.chromiumapp.org/oauth2` } function isRedirect(url: string, redirectUri: string): boolean { return url === redirectUri || url.startsWith(`${redirectUri}?`) } /** * Firefox reports a failed navigation to an unresolvable host as an * about:neterror page whose `u` parameter carries the original URL, encoded. * Without unwrapping it, a Firefox consent window would close after the * redirect without the callback ever being seen. */ function wrappedNavigation(url: string, redirectUri: string): string | undefined { if (!url.startsWith('about:neterror')) return undefined try { const original = new URL(url).searchParams.get('u') return original && isRedirect(original, redirectUri) ? original : undefined } catch { return undefined } } /** * The OAuth callback URL carried by a tabs.onUpdated event, if any. The * redirect commits as an error page (the redirect host has no DNS), so the URL * can surface in changeInfo.url, or only in the tab's pendingUrl while the * navigation is provisional. */ export function callbackFromUpdate( redirectUri: string, changeInfo: { url?: string }, tab: { pendingUrl?: string; url?: string }, ): string | undefined { for (const url of [changeInfo.url, tab.pendingUrl, tab.url]) { if (!url) continue if (isRedirect(url, redirectUri)) return url const wrapped = wrappedNavigation(url, redirectUri) if (wrapped) return wrapped } return undefined } /** * The sub of the session a completed sign-in has replaced, which should now * be revoked. Undefined when there was no previous session — or when it was * the same account: the fresh session is stored under the same sub, so * revoking it would sign the user right back out. */ export function replacedSession( prev: { did: string } | undefined, next: { did: string }, ): string | undefined { return prev && prev.did !== next.did ? prev.did : undefined }