Versions 28 to 318
new file, 141 lines
1+// abso login --web: a staging website session for the person a box is signed in as, opened in an agent's own browser, with 2+// 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) 3+// and answers a one-use link that works for a minute; opening it signs that browser in. Only the link is printed: never the 4+// box's sign-in, the PAT or the session cookie, which the website sets in the browser itself. 6+import { readFileSync } from "node:fs"; 7+import { UsageError } from "./args.ts"; 9+/** The website beside each staging server. Prod has none here: agents never sign in to the website on prod. */ 10+const STAGING_SITES: Readonly<Record<string, string>> = { 11+ "https://abso-staging.chrisazy.workers.dev": "https://abso-website-staging.chrisazy.workers.dev", 12+ "https://avcs-staging.chrisazy.workers.dev": "https://abso-website-staging.chrisazy.workers.dev", 15+/** What the website answers: the link, when it stops working, and who and until when it signs in. */ 16+export interface Handoff { 17+ readonly url: string; 18+ readonly expiresAt: string; 19+ readonly account: string; 20+ readonly name: string; 21+ readonly sessionEnds: string; 25+ * The website a handoff's credential goes to (Security condition 6, Code Review): the staging website beside the folder's own 26+ * staging server, from the fixed list above, and never an origin from a flag. A server that isn't on the list is refused, 27+ * 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 28+ * takes no --site at all. 30+export function siteFor(server: string, site: string | undefined, kind: "box" | "pat"): string { 31+ const origin = new URL(server).origin; 32+ const known = STAGING_SITES[origin]; 33+ if (known === undefined) 34+ throw new UsageError( 35+ `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.`, 37+ if (site === undefined) return known; 39+ throw new UsageError(`--site isn't taken with a box's sign-in, which goes only to ${known}. Nothing was sent.`); 40+ let url: URL | null = null; 44+ // Not an address: refused below. 46+ if (url === null || url.protocol !== "http:" || (url.hostname !== "localhost" && url.hostname !== "127.0.0.1")) 47+ throw new UsageError( 48+ "--site, with --pat-file, names this machine only (http://localhost:<port> or http://127.0.0.1:<port>). Nothing was sent.", 54+ * abso login --web: where the credential would go is settled before it is read, so a refusal reads and sends nothing 55+ * (Security). `credential` reads the box's sign-in or the PAT only once the website is one it may go to. 57+export async function webLogin(options: { 58+ readonly server: string; 59+ readonly site: string | undefined; 60+ /** A box's sign-in, or a test account's PAT (--pat-file). */ 61+ readonly kind: "box" | "pat"; 62+ readonly credential: () => string; 63+ readonly fetcher?: typeof fetch; 64+}): Promise<{ handoff: Handoff; site: string }> { 65+ const site = siteFor(options.server, options.site, options.kind); 66+ return { handoff: await webHandoff(site, options.credential(), options.fetcher), site }; 69+/** A PAT from the file --pat-file names: read here, never printed. */ 70+export function patFromFile(path: string): string { 73+ text = readFileSync(path, "utf8").trim(); 75+ throw new UsageError(`--pat-file names ${path}, which abso can't read`); 77+ if (!/^abso_pat_[A-Za-z0-9_-]{16,200}$/.test(text)) 78+ throw new UsageError(`--pat-file names ${path}, which doesn't hold a PAT`); 82+/** Words from the website, with any secret this call sent taken out. */ 83+function scrubbed(text: string, secret: string): string { 84+ return text.split(secret).join("[the credential]").slice(0, 300); 87+/** Asks the website for a handoff link with `credential` (a box sign-in or a PAT) in Authorization. */ 88+export async function webHandoff(site: string, credential: string, fetcher: typeof fetch = fetch): Promise<Handoff> { 91+ res = await fetcher(`${site}/login/handoff`, { 93+ headers: { authorization: `Bearer ${credential}`, accept: "application/json" }, 95+ signal: AbortSignal.timeout(30_000), 98+ throw new UsageError(`couldn't reach ${site}: ${scrubbed((err as Error).message, credential)}`); 100+ const text = await res.text().catch(() => ""); 101+ let body: Partial<Handoff> & { error?: unknown } = {}; 103+ body = JSON.parse(text) as typeof body; 105+ // Not JSON: the status says enough. 108+ const why = typeof body.error === "string" ? body.error : `it answered ${res.status}`; 109+ throw new UsageError( 111+ ? `${site} doesn't hand out sign-in links (its key sign-in is off, or it isn't staging)` 112+ : `${site} refused: ${scrubbed(why, credential)}`, 116+ typeof body.url !== "string" || 117+ !body.url.startsWith(`${site}/login/handoff#`) || 118+ typeof body.expiresAt !== "string" || 119+ typeof body.account !== "string" 121+ throw new UsageError(`${site} answered something that isn't a sign-in link`); 124+ expiresAt: body.expiresAt, 125+ account: body.account, 126+ name: typeof body.name === "string" ? body.name : body.account, 127+ sessionEnds: typeof body.sessionEnds === "string" ? body.sessionEnds : body.expiresAt, 131+/** What abso login --web says: the one link, how long it works, who it signs in as, and what it reaches (Security). */ 132+export function handoffText(h: Handoff, site: string, boxed = true, now = Date.now()): string { 133+ const seconds = Math.max(0, Math.round((Date.parse(h.expiresAt) - now) / 1000)); 135+ ? "only to the projects this box was granted, at most as a writer, until the box is revoked or ends" 136+ : "at most as a writer"; 138+ `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}:`,