Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed

Versions 301 to 302

Compare

+117 −0 in 1 place
new file, 117 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.
5+
6+import { readFileSync } from "node:fs";
7+import { UsageError } from "./args.ts";
8+
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",
13+};
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;
22+}
24+/** The website a server's handoff goes to: --site as given (https only), else the staging site beside it. */
25+export function siteFor(server: string, site?: string): string {
26+ if (site !== undefined) {
27+ let url: URL;
28+ try {
29+ url = new URL(site);
30+ } catch {
31+ throw new UsageError(
32+ `--site takes the website's address, like https://abso-website-staging.chrisazy.workers.dev`,
33+ );
34+ }
35+ if (url.protocol !== "https:" && url.hostname !== "localhost" && url.hostname !== "127.0.0.1")
36+ throw new UsageError("--site has to be https, or this machine");
37+ return url.origin;
38+ }
39+ const origin = new URL(server).origin;
40+ const known = STAGING_SITES[origin];
41+ if (known === undefined)
42+ throw new UsageError(
43+ `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. --site names one.`,
44+ );
45+ return known;
46+}
48+/** A PAT from the file --pat-file names: read here, never printed. */
49+export function patFromFile(path: string): string {
50+ let text: string;
51+ try {
52+ text = readFileSync(path, "utf8").trim();
53+ } catch {
54+ throw new UsageError(`--pat-file names ${path}, which abso can't read`);
55+ }
56+ if (!/^abso_pat_[A-Za-z0-9_-]{16,200}$/.test(text))
57+ throw new UsageError(`--pat-file names ${path}, which doesn't hold a PAT`);
58+ return text;
59+}
61+/** Words from the website, with any secret this call sent taken out. */
62+function scrubbed(text: string, secret: string): string {
63+ return text.split(secret).join("[the credential]").slice(0, 300);
64+}
66+/** Asks the website for a handoff link with `credential` (a box sign-in or a PAT) in Authorization. */
67+export async function webHandoff(site: string, credential: string, fetcher: typeof fetch = fetch): Promise<Handoff> {
68+ let res: Response;
69+ try {
70+ res = await fetcher(`${site}/login/handoff`, {
71+ method: "POST",
72+ headers: { authorization: `Bearer ${credential}`, accept: "application/json" },
73+ redirect: "error",
74+ signal: AbortSignal.timeout(30_000),
75+ });
76+ } catch (err) {
77+ throw new UsageError(`couldn't reach ${site}: ${scrubbed((err as Error).message, credential)}`);
78+ }
79+ const text = await res.text().catch(() => "");
80+ let body: Partial<Handoff> & { error?: unknown } = {};
81+ try {
82+ body = JSON.parse(text) as typeof body;
83+ } catch {
84+ // Not JSON: the status says enough.
85+ }
86+ if (!res.ok) {
87+ const why = typeof body.error === "string" ? body.error : `it answered ${res.status}`;
88+ throw new UsageError(
89+ res.status === 404
90+ ? `${site} doesn't hand out sign-in links (its key sign-in is off, or it isn't staging)`
91+ : `${site} refused: ${scrubbed(why, credential)}`,
92+ );
93+ }
94+ if (
95+ typeof body.url !== "string" ||
96+ !body.url.startsWith(`${site}/login/handoff#`) ||
97+ typeof body.expiresAt !== "string" ||
98+ typeof body.account !== "string"
99+ )
100+ throw new UsageError(`${site} answered something that isn't a sign-in link`);
101+ return {
102+ url: body.url,
103+ expiresAt: body.expiresAt,
104+ account: body.account,
105+ name: typeof body.name === "string" ? body.name : body.account,
106+ sessionEnds: typeof body.sessionEnds === "string" ? body.sessionEnds : body.expiresAt,
107+ };
108+}
110+/** What abso login --web says: the one link, how long it works, and who it signs in as. */
111+export function handoffText(h: Handoff, site: string, now = Date.now()): string {
112+ const seconds = Math.max(0, Math.round((Date.parse(h.expiresAt) - now) / 1000));
113+ return [
114+ `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:`,
115+ h.url,
116+ ].join("\n");
117+}
MCP
compare on this site's /mcp
CLI
abso diff 301 302 client/src/web-session.ts --json
JSON
/christopher/abso-core/compare/301..302?file=client%2Fsrc%2Fweb-session.ts&format=json