Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218import * as Linking from 'expo-linking'import {openAuthSessionAsync} from 'expo-web-browser'import { ExpoOAuthClient, TokenInvalidError, TokenRefreshError, TokenRevokedError,} from '@atproto/oauth-client-expo'
import {logger} from '#/logger'import { categorizeOauthError, emitOauthTelemetry,} from '#/state/session/oauth-telemetry'import {OAUTH_BASE_URL, OAUTH_CLIENT_NAME, OAUTH_SCOPE} from './oauth-config'import {emitOauthLifecycleEvent} from './oauth-lifecycle'
const LOCAL_OAUTH_HANDLE_RESOLVER = process.env.EXPO_PUBLIC_OAUTH_HANDLE_RESOLVERconst LOCAL_OAUTH_PLC_DIRECTORY_URL = process.env.EXPO_PUBLIC_OAUTH_PLC_DIRECTORY_URLconst USE_LOCAL_OAUTH_RESOLUTION = __DEV__ && !!LOCAL_OAUTH_HANDLE_RESOLVER && !!LOCAL_OAUTH_PLC_DIRECTORY_URL
const oauthResolutionOptions = { handleResolver: USE_LOCAL_OAUTH_RESOLUTION ? LOCAL_OAUTH_HANDLE_RESOLVER : 'https://blacksky.app', ...(USE_LOCAL_OAUTH_RESOLUTION ? {plcDirectoryUrl: LOCAL_OAUTH_PLC_DIRECTORY_URL, allowHttp: true} : {}),}
export const NATIVE_REDIRECT_URI = 'community.blacksky:/oauth/callback'
// The redirect deep-link can arrive with either a single (`:/oauth`) or double// (`://oauth`) slash depending on the Android/Hermes deep-link path, so match// both forms rather than relying on `startsWith(NATIVE_REDIRECT_URI)`.const OAUTH_CALLBACK_RE = /^community\.blacksky:\/\/?oauth\/callback\b/
// Debug fetch wrapper — logs all OAuth-related network requests to Metro consoleconst debugFetch: typeof fetch = async (input, init) => { const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url const method = init?.method?.toUpperCase() ?? 'GET' console.log(`[OAuth fetch] ${method} ${url}`) try { const res = await fetch(input, init) const cloned = res.clone() let body: string | undefined try { body = await cloned.text() } catch {} console.log(`[OAuth fetch] ${res.status} ${url}`, body?.slice(0, 500)) return res } catch (err) { console.error(`[OAuth fetch] FAILED ${url}`, err) throw err }}
// Session lifecycle hooks. Mirrors the web client at parity so production// debugging surfaces the same telemetry events on both platforms. The OAuth// client base class invokes onUpdate after each refresh and onDelete when a// session is invalidated (refresh/revocation/expiry).const sessionHooks = { onDelete(sub: string, cause: unknown) { const category = categorizeOauthError(cause) const message = cause instanceof Error ? cause.message : typeof cause === 'string' ? cause : undefined logger.warn('oauth: session deleted', {sub, cause: category, message}) emitOauthLifecycleEvent({type: 'deleted', did: sub}) emitOauthTelemetry({ type: 'oauth:sessionDeleted', payload: {cause: category, message: message?.slice(0, 200)}, }) }, onUpdate(_sub: string) { emitOauthTelemetry({type: 'oauth:sessionRefreshed', payload: {}}) },}
// eslint-disable-next-line @typescript-eslint/no-unsafe-call -- package constructor type does not resolve in Linux CIconst BSKY_OAUTH_CLIENT = new ExpoOAuthClient({ clientMetadata: { client_id: `${OAUTH_BASE_URL}/oauth-client-metadata-native.json`, client_name: OAUTH_CLIENT_NAME, client_uri: OAUTH_BASE_URL, redirect_uris: [NATIVE_REDIRECT_URI], scope: OAUTH_SCOPE, token_endpoint_auth_method: 'none', response_types: ['code'], grant_types: ['authorization_code', 'refresh_token'], application_type: 'native', dpop_bound_access_tokens: true, }, ...oauthResolutionOptions, fetch: debugFetch, ...sessionHooks,})
export function getOAuthClient() { return BSKY_OAUTH_CLIENT}
export const TERMINAL_OAUTH_ERRORS = [ TokenRefreshError, TokenRevokedError, TokenInvalidError,]
/** * Android-only OAuth sign-in. * * `client.signIn` relies on `openAuthSessionAsync`'s result, which on Android is * a fragile AppState-based polyfill: returning to the app (e.g. after switching * to an email app to fetch a 2FA code) makes it resolve `{type:'dismiss'}` even * though the Custom Tab is still open. See docs/plans/2026-07-29-android-oauth-2fa-signin-design.md. * * Instead we drive the flow with the public `authorize()`/`callback()` methods and * treat the redirect deep-link (delivered via `Linking`) as the only source of * truth. The browser promise result — including the phantom `dismiss` — is ignored. * * Pass an `AbortSignal` to support user cancellation (a genuine cancel produces no * redirect, so the UI must provide one). *//* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access -- Expo OAuth types do not resolve in Linux CI */export async function signInNativeAndroid( client: any, identifier: string, {signal}: {signal?: AbortSignal} = {},): Promise<any> { const redirectUri = NATIVE_REDIRECT_URI
let url try { url = await client.authorize(identifier, {display: 'touch', signal}) } catch (e) { // An abort during authorize() rejects with a DOMException `AbortError`; // normalize it so the UI's cancel branch recognizes it like every other path. if (signal?.aborted) throw new Error('OAUTH_CANCELLED') throw e }
return await new Promise((resolve, reject) => { let settled = false
const cleanup = () => { sub.remove() signal?.removeEventListener('abort', onAbort) // NOTE: we deliberately do NOT call dismissBrowser() here. It is not // implemented by the expo-web-browser Android native module (returns // undefined), so calling it throws; and Custom Tabs cannot be dismissed // programmatically on Android anyway. This helper is Android-only. }
const onAbort = () => { if (settled) return settled = true cleanup() reject(new Error('OAUTH_CANCELLED')) }
const sub = Linking.addEventListener('url', ({url: incoming}) => { if (settled) return if (!OAUTH_CALLBACK_RE.test(incoming)) return // ignore other deep-links settled = true cleanup() ;(async () => { // Slash-count-agnostic param extraction — do NOT rely on `new URL()`, // which is fragile on Hermes for the custom `community.blacksky:` scheme. const query = incoming.includes('?') ? incoming.slice(incoming.indexOf('?') + 1) : '' const params = new URLSearchParams(query) const {session} = await client.callback(params, { redirect_uri: redirectUri, }) resolve(session) })().catch(reject) })
if (signal) { if (signal.aborted) return onAbort() signal.addEventListener('abort', onAbort) }
// Fire the browser. Its RESULT (incl. Android's phantom `dismiss`) is // intentionally ignored — the redirect listener above completes the flow. // A genuine launch REJECTION, however, must surface, or the promise would // hang forever with no redirect ever arriving. // // `showInRecents: true` is critical for the 2FA flow. By default Expo // launches the Custom Tab with FLAG_ACTIVITY_NO_HISTORY + EXCLUDE_FROM_RECENTS, // so leaving the tab (e.g. to fetch a 2FA code from an email app) DESTROYS it // and it never reappears in the recents switcher — the user can never get back // to enter the code. showInRecents keeps the tab alive and in recents so they // can return to it. iOS ignores this option. openAuthSessionAsync(url.toString(), redirectUri, { showInRecents: true, }).catch(err => { if (settled) return settled = true cleanup() reject(err instanceof Error ? err : new Error(String(err))) }) }) /* eslint-enable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */}