// Unified login for the app and the share extension. A Session is what the UI // and public-read paths need; `authedAgent` produces a write-capable Agent for // it. Sign-in is atproto OAuth only — the tokens and DPoP key live in MMKV and // the pointer below in the Keychain, both inside the App Group container, so the // share-extension process resolves the same session the app signed in. import { resolveIdentity, type Agent } from "@linkport/atproto"; import { oauthRestore, oauthSignIn, oauthSignOut } from "./oauth"; import { clearOAuthRef, clearStaleCredentials, loadOAuthRef, saveOAuthRef, type StaleReason, } from "./store"; export interface Session { /** Handle or DID — for display and resolving the public-read subject. */ identifier: string; /** PDS base URL, for public (unauthenticated) reads. */ service: string; /** * Set when the grant stopped working and the user has to sign in again. The * session is still worth having in this state — everything public still reads * — so callers that only read can ignore it, and callers that write must not. */ stale?: StaleReason; } /** The active login, or null when signed out. */ export async function loadSession(): Promise { const oauth = await loadOAuthRef(); if (!oauth) { // An install that last signed in with an app password has no OAuth pointer // and can't be restored, so it lands here — drop the orphaned credentials // rather than leaving them in the Keychain indefinitely. await clearStaleCredentials(); return null; } return { identifier: oauth.handle, service: oauth.service, ...(oauth.stale ? { stale: oauth.stale } : {}), }; } /** * A write-capable Agent for the active session, restored from the tokens and * DPoP key in shared MMKV — so this works in the share-extension process too. */ export async function authedAgent(): Promise { const ref = await loadOAuthRef(); if (!ref) throw new Error("Signed out — no OAuth session stored"); // Known dead: the OAuth client has already deleted the tokens it would restore // from, so attempting it can only fail again, one round-trip later. A `scope` // failure is different — the session itself still works, and only the one // caller that needs the missing scope has a problem — so it passes through. if (ref.stale === "expired") { throw new Error("Your linkport sign-in expired — sign in again"); } // The stored handle spares a getSession round-trip on every restore. return oauthRestore(ref.did, ref.handle); } /** * Complete the interactive OAuth flow, persist the pointer, return a Session. * * Also how a stale grant is repaired: the ref written below carries no `stale` * field, so a successful sign-in is what clears one. Nothing else needs to. */ export async function signInWithOAuth(handle: string): Promise { const agent = await oauthSignIn(handle); // The OAuth session carries the DID + handle but not the PDS; resolve it once // now so cold starts (and the share extension) need no network to read. const { pds } = await resolveIdentity(agent.did); await saveOAuthRef({ did: agent.did, handle: agent.handle, service: pds }); return { identifier: agent.handle, service: pds }; } /** * Clear the active login, best-effort revoking the OAuth tokens. * * Drops the pointer *before* revoking, not after. Revoking can itself discover * that the session was already dead, which marks the pointer stale — and a * pointer resurrected by the act of signing out would leave the user looking at * "sign in again" when what they asked for was to be signed out. With the * pointer already gone there is nothing left to mark. */ export async function signOut(): Promise { const oauth = await loadOAuthRef(); await clearOAuthRef(); if (oauth) await oauthSignOut(oauth.did); }