Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto
Something went wrong. Try again.
1.9 kB · 49 lines
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950// Pure helpers for the interactive sign-in flow. Shared by the worker and the// offscreen document, and unit-testable without chrome.* APIs.
/** storage.session key for the last sign-in failure, shown by the popup. */export const AUTH_ERROR_KEY = 'authError'
/** * The https redirect URI the PDS sends the consent window back to. 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 `https://${extensionId}.chromiumapp.org/oauth2`}
function isRedirect(url: string, redirectUri: string): boolean { return url === redirectUri || url.startsWith(`${redirectUri}?`)}
/** * The OAuth callback URL carried by a tabs.onUpdated event, if any. The * redirect commits as an error page (chromiumapp.org 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 && isRedirect(url, redirectUri)) return url } 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}