{"draft":null,"from":267,"to":268,"files":[{"path":"client/src/join-commands.ts","added":76,"removed":8,"diff":"--- a/client/src/join-commands.ts\n+++ b/client/src/join-commands.ts\n@@ -19,7 +19,16 @@\n import { CLIENT_DIR } from \"./client-version.ts\";\n import { type Env, type Output, plural } from \"./command-env.ts\";\n import { leftOutText, syncFolder, uploadBlobs } from \"./commands.ts\";\n-import { findRoot, installKey, readConfig, type Settings, tokenFor, writeConfig } from \"./config.ts\";\n+import {\n+\tfindRoot,\n+\tinstallKey,\n+\treadConfig,\n+\trunKey,\n+\ttype Settings,\n+\ttokenFile,\n+\ttrustedToken,\n+\twriteConfig,\n+} from \"./config.ts\";\n import { elsewhereText } from \"./doctor.ts\";\n import { fromEnv } from \"./env-names.ts\";\n import { clearKnown, loadIgnore, readFolder, writeKnown } from \"./folder.ts\";\n@@ -59,6 +68,42 @@\n \treturn ['The project is empty. Put its files in this folder, then abso push -m \"why\" publishes them.'];\n }\n \n+/** A credential other than the box's, and how init and join name it when they say what they sign in with. */\n+export interface Credential {\n+\treadonly token: string | undefined;\n+\treadonly words: string;\n+}\n+\n+/**\n+ * What init and join sign in with when it isn't the box's sign-in. The machine's token file only with --token-file, and\n+ * never into a folder that already has a key; else a token the environment names (ABSO_TOKEN, or a pipeline run's\n+ * ABSO_KEY_FILE); else none. The token file is never read unasked, so a project is never made or joined with a shared\n+ * credential nobody chose.\n+ */\n+export function tokenCredential(\n+\targs: Parsed,\n+\tenv: Env[\"env\"],\n+\tserver: string,\n+\troot: string,\n+\tfile: () => string | undefined = tokenFile,\n+): Credential | undefined {\n+\tif (bool(args, \"token-file\")) {\n+\t\tif (existsSync(join(stateDir(root), \"key\")))\n+\t\t\tthrow new UsageError(`${root} already has a key, so it signs in with that: leave off --token-file`);\n+\t\tconst token = trustedToken(server, env, file());\n+\t\tif (!token) throw new UsageError(\"--token-file reads ~/.abso/token, and this machine has none\");\n+\t\treturn { token, words: \"the token file, ~/.abso/token, as --token-file asks\" };\n+\t}\n+\tconst named = fromEnv(env, \"TOKEN\")?.trim();\n+\tif (named) return { token: trustedToken(server, env, named), words: \"ABSO_TOKEN\" };\n+\tconst run = runKey(env);\n+\tif (run) return { token: trustedToken(server, env, run), words: \"a pipeline run's key (ABSO_KEY_FILE)\" };\n+\treturn undefined;\n+}\n+\n+/** Why a token file is no fallback: said when one is here and nothing else signs in. */\n+const TOKEN_FILE_ASKED = \"~/.abso/token is used only with --token-file.\";\n+\n export async function runInit(args: Parsed, env: Env): Promise<Output> {\n \tif (args.positionals.length > 1) throw new UsageError(\"init takes one folder\");\n \tconst root = resolve(env.cwd, args.positionals[0] ?? \".\");\n@@ -67,12 +112,21 @@\n \tconst project = str(args, \"project\");\n \tif (!project) throw new UsageError(\"give the project a name with --project <name>\");\n \tif (bool(args, \"public\") && bool(args, \"private\")) throw new UsageError(\"--public or --private, not both\");\n-\t// A box signed in here, and no token for this server: the project is made as the person who approved the box,\n-\t// the way the website makes one (abso 663).\n+\t// A box signed in here, and no other credential asked for: the project is made as the person who approved the box,\n+\t// the way the website makes one (abso 663). The token file only with --token-file, never by itself.\n \tconst server = serverFrom(args, env);\n \tconst box = readBox(server);\n-\tif (box && !tokenFor(server, env.env) && !fromEnv(env.env, \"KEY_FILE\"))\n+\tconst credential = tokenCredential(args, env.env, server, root);\n+\tconst say = env.writeErr ?? env.write;\n+\tif (box && credential === undefined) {\n+\t\tsay(`Signing in with this box's sign-in, ${box.id}.\n+`);\n \t\treturn initFromBox(args, env, root, project, server, box);\n+\t}\n+\tif (credential === undefined && tokenFile() !== undefined)\n+\t\tthrow new UsageError(\n+\t\t\t`abso init signs in with this box's sign-in, and this box isn't signed in to ${server}: abso login signs it in. ${TOKEN_FILE_ASKED}`,\n+\t\t);\n \tconst org = str(args, \"org\");\n \tif (org !== undefined) return initInOrg(org, server);\n \tconst config = makeSettings(args, env, project);\n@@ -83,7 +137,9 @@\n \t// What the server runs on each new version (#39), guessed from package.json; recorded with the rest.\n \tconst wroteProject = asIs ? \"exists\" : writeStartingProjectFile(root);\n \tconst { files, blobs, modes, leftOut } = readFolder(root, loadIgnore(root));\n-\tconst token = tokenFor(config.server, env.env);\n+\tconst token = credential?.token;\n+\tsay(`Signing in with ${credential?.words ?? `no credential: nothing here signs in to ${server}`}.\n+`);\n \tconst api = new Api({ ...config, ...(token ? { token } : {}) });\n \tawait uploadBlobs(api, blobs);\n \tconst answer = await api.init(\n@@ -309,11 +365,16 @@\n \tconst server = serverFrom(args, env);\n \tconst box = readBox(server);\n \tconst hasKey = existsSync(join(stateDir(root), \"key\"));\n+\tconst credential = tokenCredential(args, env.env, server, root);\n+\tconst tokenFileAsked = bool(args, \"token-file\");\n \tlet agent: string;\n \tlet minted: { key: string; id: string } | undefined;\n \tif (str(args, \"agent\") !== undefined) {\n \t\tagent = agentFrom(args);\n-\t\tminted = box && !hasKey && !fromEnv(env.env, \"KEY_FILE\") ? await mintKey(box, project, agent) : undefined;\n+\t\tminted =\n+\t\t\tbox && !hasKey && !fromEnv(env.env, \"KEY_FILE\") && !tokenFileAsked\n+\t\t\t\t? await mintKey(box, project, agent)\n+\t\t\t\t: undefined;\n \t} else if (fromEnv(env.env, \"KEY_FILE\")) {\n \t\t// A pipeline run's key (ABSO_KEY_FILE) belongs to one agent, which the runner names in ABSO_AGENT.\n \t\tconst named = fromEnv(env.env, \"AGENT\");\n@@ -337,7 +398,13 @@\n \twriteConfig(root, config);\n \tclearKnown(root);\n \tif (minted) installKey(root, JSON.stringify(minted));\n-\tconst settings = readConfig(root, env.env);\n+\t// The token file only with --token-file: readConfig reads it otherwise.\n+\tconst read = readConfig(root, env.env, false);\n+\tconst settings = tokenFileAsked ? { ...read, token: credential?.token } : read;\n+\t(env.writeErr ?? env.write)(\n+\t\t`Signing in with ${minted && box ? `a new key from this box's sign-in, ${box.id}` : hasKey ? \"this folder's key\" : (credential?.words ?? `no credential: nothing here signs in to ${server}`)}.\n+`,\n+\t);\n \tconst api = new Api(settings);\n \tlet synced: Awaited<ReturnType<typeof syncFolder>>;\n \ttry {\n@@ -348,7 +415,8 @@\n \t} catch (error) {\n \t\tif (!(error instanceof ApiError && error.status === 401 && !box && !settings.token)) throw error;\n \t\trmSync(stateDir(root), { recursive: true, force: true });\n-\t\tthrow new UsageError(await signedOut(config.server, project));\n+\t\tconst why = await signedOut(config.server, project);\n+\t\tthrow new UsageError(!tokenFileAsked && tokenFile() !== undefined ? `${why} ${TOKEN_FILE_ASKED}` : why);\n \t}\n \treturn {\n \t\ttext: ["}],"unmatched":[]}