Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778// 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, * reserved hostname. `.invalid` never resolves, like chromiumapp.org, and must * match the appended entry in oauth/client-metadata.json exactly. */export const FIREFOX_REDIRECT_URI = 'https://substandard.invalid/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}