// abso login --web: a staging website session for the person a box is signed in as, opened in an agent's own browser, with // no secret in the agent's hands. The website's /login/handoff takes this box's sign-in (or a test account's PAT, from a file) // and answers a one-use link that works for a minute; opening it signs that browser in. Only the link is printed: never the // box's sign-in, the PAT or the session cookie, which the website sets in the browser itself. import { readFileSync } from "node:fs"; import { UsageError } from "./args.ts"; /** The website beside each staging server. Prod has none here: agents never sign in to the website on prod. */ const STAGING_SITES: Readonly> = { "https://abso-staging.chrisazy.workers.dev": "https://abso-website-staging.chrisazy.workers.dev", "https://avcs-staging.chrisazy.workers.dev": "https://abso-website-staging.chrisazy.workers.dev", }; /** What the website answers: the link, when it stops working, and who and until when it signs in. */ export interface Handoff { readonly url: string; readonly expiresAt: string; readonly account: string; readonly name: string; readonly sessionEnds: string; } /** * The website a handoff's credential goes to (Security condition 6, Code Review): the staging website beside the folder's own * staging server, from the fixed list above, and never an origin from a flag. A server that isn't on the list is refused, * prod included. --site is only for a test account's PAT on this machine (http://localhost or 127.0.0.1); a box's sign-in * takes no --site at all. */ export function siteFor(server: string, site: string | undefined, kind: "box" | "pat"): string { const origin = new URL(server).origin; const known = STAGING_SITES[origin]; if (known === undefined) throw new UsageError( `abso login --web is for staging: ${origin} has no staging website beside it that abso knows, and agents never sign in to the website on prod. Nothing was sent.`, ); if (site === undefined) return known; if (kind === "box") throw new UsageError(`--site isn't taken with a box's sign-in, which goes only to ${known}. Nothing was sent.`); let url: URL | null = null; try { url = new URL(site); } catch { // Not an address: refused below. } if (url === null || url.protocol !== "http:" || (url.hostname !== "localhost" && url.hostname !== "127.0.0.1")) throw new UsageError( "--site, with --pat-file, names this machine only (http://localhost: or http://127.0.0.1:). Nothing was sent.", ); return url.origin; } /** * abso login --web: where the credential would go is settled before it is read, so a refusal reads and sends nothing * (Security). `credential` reads the box's sign-in or the PAT only once the website is one it may go to. */ export async function webLogin(options: { readonly server: string; readonly site: string | undefined; /** A box's sign-in, or a test account's PAT (--pat-file). */ readonly kind: "box" | "pat"; readonly credential: () => string; readonly fetcher?: typeof fetch; }): Promise<{ handoff: Handoff; site: string }> { const site = siteFor(options.server, options.site, options.kind); return { handoff: await webHandoff(site, options.credential(), options.fetcher), site }; } /** A PAT from the file --pat-file names: read here, never printed. */ export function patFromFile(path: string): string { let text: string; try { text = readFileSync(path, "utf8").trim(); } catch { throw new UsageError(`--pat-file names ${path}, which abso can't read`); } if (!/^abso_pat_[A-Za-z0-9_-]{16,200}$/.test(text)) throw new UsageError(`--pat-file names ${path}, which doesn't hold a PAT`); return text; } /** Words from the website, with any secret this call sent taken out. */ function scrubbed(text: string, secret: string): string { return text.split(secret).join("[the credential]").slice(0, 300); } /** Asks the website for a handoff link with `credential` (a box sign-in or a PAT) in Authorization. */ export async function webHandoff(site: string, credential: string, fetcher: typeof fetch = fetch): Promise { let res: Response; try { res = await fetcher(`${site}/login/handoff`, { method: "POST", headers: { authorization: `Bearer ${credential}`, accept: "application/json" }, redirect: "error", signal: AbortSignal.timeout(30_000), }); } catch (err) { throw new UsageError(`couldn't reach ${site}: ${scrubbed((err as Error).message, credential)}`); } const text = await res.text().catch(() => ""); let body: Partial & { error?: unknown } = {}; try { body = JSON.parse(text) as typeof body; } catch { // Not JSON: the status says enough. } if (!res.ok) { const why = typeof body.error === "string" ? body.error : `it answered ${res.status}`; throw new UsageError( res.status === 404 ? `${site} doesn't hand out sign-in links (its key sign-in is off, or it isn't staging)` : `${site} refused: ${scrubbed(why, credential)}`, ); } if ( typeof body.url !== "string" || !body.url.startsWith(`${site}/login/handoff#`) || typeof body.expiresAt !== "string" || typeof body.account !== "string" ) throw new UsageError(`${site} answered something that isn't a sign-in link`); return { url: body.url, expiresAt: body.expiresAt, account: body.account, name: typeof body.name === "string" ? body.name : body.account, sessionEnds: typeof body.sessionEnds === "string" ? body.sessionEnds : body.expiresAt, }; } /** What abso login --web says: the one link, how long it works, who it signs in as, and what it reaches (Security). */ export function handoffText(h: Handoff, site: string, boxed = true, now = Date.now()): string { const seconds = Math.max(0, Math.round((Date.parse(h.expiresAt) - now) / 1000)); const reach = boxed ? "only to the projects this box was granted, at most as a writer, until the box is revoked or ends" : "at most as a writer"; return [ `Open this link in your browser within ${seconds} seconds. It signs that browser in to ${site} as ${h.name}, until ${h.sessionEnds}, and works once. It reaches ${reach}:`, h.url, ].join("\n"); }