// Two sign-ins, and they are different on purpose. // // **Atmosphere** (atproto OAuth) is the member's account: it is what reads and // writes their own repository, and it works with no server of ours in the path. // // **Corliss** is the sync server's authorization server. The relay accepts a // Corliss-issued JWT, not a PDS token, so attaching to a sync server is a second // consent. Public notes need only the first — that is the whole point of the // prototype: writing a note must not require our infrastructure. import { BrowserOAuthClient } from "@atproto/oauth-client-browser"; import { buildAtprotoLoopbackClientId } from "@atproto/oauth-types"; import { Agent } from "@atproto/api"; /** * What the app asks for at sign-in. * * `atproto` is the base requirement; the rest is the **granular** permission * model: write access to exactly one collection in the member's own repo, with * no `transition:generic` anywhere. Without this term the token is valid and the * session authenticated, and every write still comes back `ScopeMissingError`. * * Corliss asks for the same collection with the same actions, deliberately: two * clients writing one lexicon should request the same authority. * * It has to appear in **two** places — here, and in the client id, which is the * only metadata a loopback client has. */ export const SCOPE = "atproto repo:network.sharedcomputer.notes?action=create&action=update&action=delete"; /** * The loopback client id — **and the scope, which lives in it.** * * atproto lets a browser app be its own client without registering anywhere: * the client id *is* a URL naming the exact origin, and for a loopback client it * also carries the scope, because there is nowhere else to declare one. A * discoverable https client serves `client-metadata.json`; a loopback client has * no metadata document, so `http://localhost?scope=…&redirect_uri=…` **is** the * metadata. * * `buildAtprotoLoopbackClientId` omits either parameter when it matches the * default, which is why the string can look bare — `buildLoopbackClientId` from * the browser package emits only `redirect_uri`, and a client id without * `scope` declares no scope at all. That is the trap: the sign-in then grants * `atproto` (authenticate) and no write permission, and the first save is * refused. * * The redirect has to be the origin this page is served from, character for * character, which is another reason the dev server is pinned to 127.0.0.1. */ export function loopbackClientId(): string { const redirect = `http://${window.location.hostname}:${window.location.port}${window.location.pathname}`; return buildAtprotoLoopbackClientId({ scope: SCOPE, redirect_uris: [redirect] }); } function isLoopback(host: string): boolean { return host === "127.0.0.1" || host === "localhost" || host === "[::1]"; } /** * The client id, whichever kind of deployment this is. * * On `127.0.0.1` it is the loopback form, built from the location. **Deployed, * it is a document this app serves itself** — `client-metadata.json` beside * `index.html` — because a page with an https origin can host one, and that is * the whole reason CIMD exists. * * Resolved against `document.baseURI`, not the origin: the app is a **sub-path** * site at `/scn-local/`, and the metadata is served under that path with it. */ export function clientId(): string { if (isLoopback(window.location.hostname)) return loopbackClientId(); return new URL("client-metadata.json", document.baseURI).href; } export interface AtmosphereSession { did: string; agent: Agent; } let client: BrowserOAuthClient | null = null; async function oauthClient(): Promise { if (client) return client; client = await BrowserOAuthClient.load({ clientId: clientId(), handleResolver: "https://bsky.social", }); return client; } /** * Resume a session if one is stored, or return null. * * Called on boot before any sign-in UI renders: a member who signed in * yesterday should not see a login screen. */ export async function restore(): Promise { const oauth = await oauthClient(); const result = await oauth.init(); if (!result?.session) return null; const { session } = result; return { did: session.did, agent: new Agent(session), }; } export async function signIn(handle: string): Promise { const oauth = await oauthClient(); // `signIn` navigates the page on success, so anything thrown here is a // failure the person needs to see: a bad handle, a PDS that refused the // client id, or no network. The caller renders it. await oauth.signIn(handle.replace(/^@/, "").trim(), { state: crypto.randomUUID(), scope: SCOPE, }); } export async function signOut(did: string): Promise { const oauth = await oauthClient(); // `revoke` both drops the stored session and tells the authorization server, // which is what a sign-out should mean. The browser client has no `signOut`. await oauth.revoke(did); } // --- Public notes in the member's own repo -------------------------------- export interface RepoRecord { rkey: string; cid: string; value: Record; } export async function listNotes(session: AtmosphereSession, collection: string): Promise { const response = await session.agent.com.atproto.repo.listRecords({ repo: session.did, collection, limit: 100, }); return response.data.records.map((record) => ({ rkey: record.uri.split("/").pop() ?? "", cid: record.cid, value: record.value as Record, })); } export async function putNote( session: AtmosphereSession, collection: string, rkey: string, record: Record, ): Promise { await session.agent.com.atproto.repo.putRecord({ repo: session.did, collection, rkey, record, }); } export async function deleteNote(session: AtmosphereSession, collection: string, rkey: string): Promise { await session.agent.com.atproto.repo.deleteRecord({ repo: session.did, collection, rkey, }); }