{"draft":null,"from":175,"to":318,"files":[{"path":"client/src/box.ts","added":127,"removed":9,"diff":"--- a/client/src/box.ts\n+++ b/client/src/box.ts\n@@ -21,8 +21,15 @@\n  */\n export interface Grant {\n \treadonly projects: readonly { readonly project: string; readonly role: string }[];\n+\t/** Version 2 grants (abso 663): member-all is every project of the approver's, now and later, at most this role. */\n+\treadonly scopes?: readonly { readonly kind: string; readonly role: string }[];\n }\n \n+/** The role a box has in all its approver's projects, now and later, or null when its grant is a list (abso 663). */\n+export function allProjectsRole(grant: Grant): string | null {\n+\treturn grant.scopes?.find((scope) => scope.kind === \"member-all\")?.role ?? null;\n+}\n+\n /** Requested access is a suggestion for the person approving, never an authorization. */\n export interface LoginRequest {\n \treadonly projects?: readonly { readonly project: string; readonly role: \"reader\" | \"writer\" | \"owner\" }[];\n@@ -154,6 +161,7 @@\n \tbody: unknown,\n \tcredential: string | undefined,\n \tfetcher: typeof fetch,\n+\theaders: Readonly<Record<string, string>> = {},\n ): Promise<Answer> {\n \tconst secrets = credential ? [credential] : [];\n \tlet response: Response;\n@@ -162,6 +170,7 @@\n \t\t\tmethod: \"POST\",\n \t\t\theaders: {\n \t\t\t\t\"content-type\": \"application/json\",\n+\t\t\t\t...headers,\n \t\t\t\t...(credential ? { authorization: `Box ${credential}` } : {}),\n \t\t\t},\n \t\t\tbody: JSON.stringify(body),\n@@ -213,7 +222,7 @@\n }\n \n /** This machine's name, as the server takes a box's name: 1-64 letters, digits, '.', '_' or '-'. */\n-function boxName(): string {\n+export function boxName(): string {\n \tconst name = hostname()\n \t\t.replace(/[^A-Za-z0-9._-]/g, \"-\")\n \t\t.replace(/^[^A-Za-z0-9]+/, \"\")\n@@ -245,8 +254,29 @@\n \treadonly say: (line: string) => void;\n \t/** Opens the approve page in a browser, answering whether it tried. None where it shouldn't (--no-browser, SSH, CI). */\n \treadonly open?: (url: string) => boolean;\n+\t/**\n+\t * abso 684: in a joined folder, asks a person in its project to approve this sign-in, through an abso ask, so nobody\n+\t * relays the code. Answers who was asked, or null to fall back to the code alone.\n+\t */\n+\treadonly ask?: (codes: { readonly device: string; readonly user: string }) => Promise<LoginAsked | null>;\n }\n \n+/** A sign-in asked of people (abso 684). */\n+export interface LoginAsked {\n+\treadonly project: string;\n+\t/** \"the person who approved this agent's box\", or \"the owners of <project>\". */\n+\treadonly who: string;\n+\treadonly asks: readonly number[];\n+\t/** Milliseconds: when the asked sign-in runs out. */\n+\treadonly expiresAt: number;\n+\t/** The most the box may be granted: the asking agent's own role. */\n+\treadonly role: string;\n+\t/** Withdraws the asks once nobody answered in time. */\n+\treadonly withdraw: () => Promise<void>;\n+}\n+\n+const CENTRAL = new Intl.DateTimeFormat(\"en-US\", { timeZone: \"America/Chicago\", hour: \"numeric\", minute: \"2-digit\" });\n+\n /**\n  * Asks `server` for a code, shows it with its link, and waits for a person to\n  * approve it. Answers the box's record, already written to the config folder.\n@@ -269,16 +299,37 @@\n \tif (typeof code !== \"string\" || typeof link !== \"string\" || typeof device !== \"string\")\n \t\tthrow new UsageError(`${originOf(server)} didn't give a code to approve`);\n \t// The page opens with the code in it, so nobody copies a link by hand. The link and code are printed all the same.\n-\tif (io.open?.(link)) io.say(\"Opening the approve page in your browser.\");\n-\tio.say(`To sign this box in, open ${link} and approve the code ${code}.`);\n-\tio.say(\"Waiting for approval. Ctrl-C stops waiting, and abso stops by itself when the code runs out.\");\n-\tconst until = now() + expiresIn * 1000;\n+\t// A failed ask falls back to the code, and says why, so a person can still approve it (abso 684).\n+\tconst asked = io.ask\n+\t\t? await io.ask({ device, user: code }).catch((e: unknown) => {\n+\t\t\t\tio.say(\n+\t\t\t\t\t`Couldn't ask a person to approve this (${e instanceof Error ? e.message : String(e)}), so here is the code.`,\n+\t\t\t\t);\n+\t\t\t\treturn null;\n+\t\t\t})\n+\t\t: null;\n+\tif (asked) {\n+\t\tconst askWord = asked.asks.length === 1 ? `ask ${asked.asks[0]}` : `asks ${asked.asks.join(\", \")}`;\n+\t\tio.say(`Asked ${asked.who} (${askWord} in ${asked.project}) to sign this box in, as at most ${asked.role}.`);\n+\t\tio.say(\n+\t\t\t`Waiting for their answer, until ${CENTRAL.format(new Date(asked.expiresAt))} CT. To approve it yourself instead: ${link}`,\n+\t\t);\n+\t} else {\n+\t\tif (io.open?.(link)) io.say(\"Opening the approve page in your browser.\");\n+\t\tio.say(`To sign this box in, open ${link} and approve the code ${code}.`);\n+\t\tio.say(\"Waiting for approval. Ctrl-C stops waiting, and abso stops by itself when the code runs out.\");\n+\t}\n+\tconst until = asked ? Math.min(asked.expiresAt, now() + MAX_LOGIN_WAIT_S * 1000) : now() + expiresIn * 1000;\n \tlet toldAt = now();\n \twhile (now() < until) {\n \t\tawait sleep(interval * 1000);\n \t\t// A wait with no word looks like a hang, so say it's still waiting now and then.\n \t\tif (now() - toldAt >= STILL_WAITING_MS) {\n-\t\t\tio.say(`Still waiting for the code ${code} to be approved.`);\n+\t\t\tio.say(\n+\t\t\t\tasked\n+\t\t\t\t\t? `Still waiting (${asked.who} hasn't answered yet).`\n+\t\t\t\t\t: `Still waiting for the code ${code} to be approved.`,\n+\t\t\t);\n \t\t\ttoldAt = now();\n \t\t}\n \t\tconst a = await call(server, \"/login/token\", { device_code: device }, undefined, fetcher);\n@@ -288,8 +339,14 @@\n \t\t\t\tinterval += 5;\n \t\t\t\tcontinue;\n \t\t\t}\n-\t\t\tif (a.body.error === \"access_denied\") throw new UsageError(`the code ${code} was turned down.`);\n-\t\t\tthrow new UsageError(`the code ${code} ran out. Run abso login again.`);\n+\t\t\tif (a.body.error === \"access_denied\")\n+\t\t\t\tthrow new UsageError(asked ? `${asked.who} turned this sign-in down.` : `the code ${code} was turned down.`);\n+\t\t\tif (asked) await asked.withdraw().catch(() => undefined);\n+\t\t\tthrow new UsageError(\n+\t\t\t\tasked\n+\t\t\t\t\t? \"nobody answered in time, so nothing was granted. Run abso login again.\"\n+\t\t\t\t\t: `the code ${code} ran out. Run abso login again.`,\n+\t\t\t);\n \t\t}\n \t\tconst { box_token: credential, box_id: id, grant, expires_at } = a.body;\n \t\tconst expiresAt = typeof expires_at === \"string\" ? Date.parse(expires_at) : Number.NaN;\n@@ -299,6 +356,10 @@\n \t\twriteBox(record, home);\n \t\treturn record;\n \t}\n+\tif (asked) {\n+\t\tawait asked.withdraw().catch(() => undefined);\n+\t\tthrow new UsageError(\"nobody answered in time, so nothing was granted. Run abso login again.\");\n+\t}\n \tthrow new UsageError(`nobody approved the code ${code} in time. Run abso login again.`);\n }\n \n@@ -316,6 +377,60 @@\n \treturn { key, id };\n }\n \n+/** What a new project from this box starts with (abso 663): the website's own create, as the person who approved the box. */\n+export interface NewProject {\n+\treadonly name: string;\n+\treadonly owner?: string;\n+\treadonly visibility?: \"public\" | \"private\";\n+\treadonly files?: readonly (\n+\t\t| { readonly path: string; readonly text: string }\n+\t\t| { readonly path: string; readonly base64: string }\n+\t)[];\n+}\n+\n+/**\n+ * Makes a project as the person who approved this box (abso init, abso 663). One key for every try, so a retry after\n+ * a lost answer or a pending one is the same project, never a second.\n+ */\n+export async function boxCreateProject(\n+\trecord: BoxRecord,\n+\tproject: NewProject,\n+\tkey: string,\n+\tfetcher: typeof fetch = fetch,\n+\tsleep: (ms: number) => Promise<void> = (ms) => new Promise((done) => setTimeout(done, ms)),\n+): Promise<Record<string, unknown>> {\n+\tfor (let attempt = 1; ; attempt++) {\n+\t\tconst a = await call(record.server, \"/box/projects\", project, record.credential, fetcher, {\n+\t\t\t\"Idempotency-Key\": key,\n+\t\t});\n+\t\tconst pending = (a.body.operation as { state?: unknown } | undefined)?.state === \"pending\";\n+\t\tif (!pending) return a.body;\n+\t\tif (attempt >= 10)\n+\t\t\tthrow new UsageError(\n+\t\t\t\t`${originOf(record.server)} is still making ${project.name}. Run the same abso init again in a minute: it picks up where this left off.`,\n+\t\t\t);\n+\t\tawait sleep(1000 * attempt);\n+\t}\n+}\n+\n+/** Makes an organization as the person who approved this box (abso org new, abso 663). */\n+export async function boxCreateOrg(\n+\trecord: BoxRecord,\n+\tname: string,\n+\tdisplayName: string | undefined,\n+\tfetcher: typeof fetch = fetch,\n+): Promise<Record<string, unknown>> {\n+\treturn (\n+\t\tawait call(\n+\t\t\trecord.server,\n+\t\t\t\"/box/orgs\",\n+\t\t\t{ name, ...(displayName === undefined ? {} : { displayName }) },\n+\t\t\trecord.credential,\n+\t\t\tfetcher,\n+\t\t)\n+\t).body;\n+}\n+\n /** The agent name abso join takes when none is given: the box's name, lower-cased. */\n export function defaultAgentName(): string {\n \treturn boxName().toLowerCase();\n@@ -375,9 +490,12 @@\n \tif (!record) return `This box isn't signed in to ${originOf(server)}. abso login signs it in.`;\n \tconst g = record.grant;\n \tconst expired = record.expiresAt <= now;\n+\tconst all = allProjectsRole(g);\n \treturn [\n \t\t`Signed in to ${record.server} as box ${record.id}${expired ? \", but the sign-in has expired. abso login renews it.\" : \".\"}`,\n-\t\t`Projects: ${g.projects.map((p) => `${p.project} (${p.role})`).join(\", \")}.`,\n+\t\tall === null\n+\t\t\t? `Projects: ${g.projects.map((p) => `${p.project} (${p.role})`).join(\", \")}.`\n+\t\t\t: `Projects: all of yours, now and later (${all} at most). abso init makes a new one; abso org new makes an organization.`,\n \t\t`${expired ? \"Expired\" : \"Expires\"} ${day(record.expiresAt)} UTC.`,\n \t].join(\"\\n\");\n }"}],"unmatched":[]}