overvues 0.0.0-stage → 0.4.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -2
- package/dist/ansi.js +24 -0
- package/dist/attach-client.js +586 -0
- package/dist/auth-connect-browser.js +112 -0
- package/dist/auth-connect.js +306 -0
- package/dist/browser-bootstrap.js +190 -0
- package/dist/browser-loopback.js +289 -0
- package/dist/build-identity.js +6 -0
- package/dist/codex-reviewer-apparmor.json +4 -0
- package/dist/codex-reviewer-seccomp.json +1238 -0
- package/dist/codex-runtime-protocol.js +1 -0
- package/dist/codex-subscription.js +53 -0
- package/dist/config.js +297 -0
- package/dist/connect-comparison-code.js +26 -0
- package/dist/development-runner.js +109 -0
- package/dist/dns-doctor.js +390 -0
- package/dist/docker-names.js +47 -0
- package/dist/doctor.js +114 -0
- package/dist/gadget-job.js +804 -0
- package/dist/health-check.js +112 -0
- package/dist/help.js +71 -0
- package/dist/host-auth.js +22 -0
- package/dist/host-diagnostics.js +20 -0
- package/dist/host-runtime-identity.js +39 -0
- package/dist/host-runtime-metadata.js +56 -0
- package/dist/host-service.js +377 -0
- package/dist/host-stack.js +484 -0
- package/dist/host-supervisor.js +237 -0
- package/dist/host-update.js +387 -0
- package/dist/hub-address.js +100 -0
- package/dist/hub-client.js +459 -0
- package/dist/hub-kind.js +88 -0
- package/dist/identity.js +73 -0
- package/dist/index.js +185 -0
- package/dist/init.js +295 -0
- package/dist/isolated-run-cache.js +116 -0
- package/dist/local-stack.js +685 -0
- package/dist/machine-capacity.js +128 -0
- package/dist/managed-runner-image.js +13 -0
- package/dist/os-acted-record.js +49 -0
- package/dist/os-adapter.js +8190 -0
- package/dist/os-command.js +292 -0
- package/dist/os-handoff.js +274 -0
- package/dist/os-keychain.js +247 -0
- package/dist/os-machine.js +818 -0
- package/dist/os-owner.js +61 -0
- package/dist/os-private-file.js +98 -0
- package/dist/os-protocol.js +468 -0
- package/dist/os-session-store.js +133 -0
- package/dist/os-status.js +102 -0
- package/dist/picker.js +863 -0
- package/dist/portable-workspace-capability.js +60 -0
- package/dist/project-environment-capability.js +62 -0
- package/dist/project-environment.js +28 -0
- package/dist/project-preparation.js +134 -0
- package/dist/readiness.js +28 -0
- package/dist/redaction.js +27 -0
- package/dist/reviewer-isolation-capability.js +61 -0
- package/dist/runner-api-auth.js +80 -0
- package/dist/runner-command-fence.js +536 -0
- package/dist/runner-inspection.js +57 -0
- package/dist/runner-inventory.js +587 -0
- package/dist/runner-server.js +2520 -0
- package/dist/runner-session.js +470 -0
- package/dist/runner-slug-folder.js +39 -0
- package/dist/runner-slug.js +40 -0
- package/dist/setup.js +139 -0
- package/dist/shutdown.js +102 -0
- package/dist/splash.js +65 -0
- package/dist/staged-workspaces.js +441 -0
- package/dist/staging-disk.js +97 -0
- package/dist/terminal-lane.js +339 -0
- package/dist/terminal-recovery.js +502 -0
- package/licenses/capnweb.txt +23 -0
- package/licenses/moby-profiles.txt +212 -0
- package/licenses/ws.txt +22 -0
- package/package.json +30 -4
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { compactDecrypt } from "jose";
|
|
2
|
+
import { exactBrowserHubOrigin, normalizeBrowserConnectionCode, runBrowserLoopback, } from "./browser-loopback.js";
|
|
3
|
+
import { CLI } from "./identity.js";
|
|
4
|
+
const CALLBACK_PATH = "/auth-connect-callback";
|
|
5
|
+
const CALLBACK_TIMEOUT_MS = 5 * 60_000;
|
|
6
|
+
const MAX_BODY_BYTES = 32 * 1_024;
|
|
7
|
+
const PACKAGE_TTL_SECONDS = 5 * 60;
|
|
8
|
+
export function buildAuthConnectSettingsUrl(hubUrl, port, state, encodedPublicKey, providers) {
|
|
9
|
+
return `${hubUrl}/settings?auth_connect=1&port=${port}&state=${encodeURIComponent(state)}&key=${encodeURIComponent(encodedPublicKey)}&providers=${encodeURIComponent(providers.join(","))}`;
|
|
10
|
+
}
|
|
11
|
+
export async function decryptAuthConnectEnvelope(inputEnvelope, privateKey, expected) {
|
|
12
|
+
const envelope = normalizeBrowserConnectionCode(inputEnvelope);
|
|
13
|
+
if (envelope.split(".").length !== 5)
|
|
14
|
+
throw new Error("Connection code is not a valid encrypted package.");
|
|
15
|
+
let decrypted;
|
|
16
|
+
try {
|
|
17
|
+
decrypted = await compactDecrypt(envelope, privateKey);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
throw new Error(`Connection code could not be decrypted by this ${CLI.command} process.`);
|
|
21
|
+
}
|
|
22
|
+
if (decrypted.protectedHeader.alg !== "ECDH-ES"
|
|
23
|
+
|| decrypted.protectedHeader.enc !== "A256GCM"
|
|
24
|
+
|| decrypted.protectedHeader.typ !== "tiller-auth-connect+jwe")
|
|
25
|
+
throw new Error("Connection code used an unsupported encryption format.");
|
|
26
|
+
let value;
|
|
27
|
+
try {
|
|
28
|
+
value = JSON.parse(new TextDecoder().decode(decrypted.plaintext));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
throw new Error("Authentication connection package is invalid.");
|
|
32
|
+
}
|
|
33
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
34
|
+
throw new Error("Authentication connection package is invalid.");
|
|
35
|
+
}
|
|
36
|
+
const payload = value;
|
|
37
|
+
const expectedKeys = ["exp", "grants", "hubUrl", "iat", "state", "version"];
|
|
38
|
+
const actualKeys = Object.keys(payload).sort();
|
|
39
|
+
if (actualKeys.length !== expectedKeys.length || actualKeys.some((key, index) => key !== expectedKeys[index])) {
|
|
40
|
+
throw new Error("Authentication connection package is invalid.");
|
|
41
|
+
}
|
|
42
|
+
if (!payload.grants || typeof payload.grants !== "object" || Array.isArray(payload.grants)) {
|
|
43
|
+
throw new Error("Authentication connection package is invalid.");
|
|
44
|
+
}
|
|
45
|
+
const grants = payload.grants;
|
|
46
|
+
const expectedProviders = [...expected.providers].sort();
|
|
47
|
+
const actualProviders = Object.keys(grants).sort();
|
|
48
|
+
if (actualProviders.length !== expectedProviders.length
|
|
49
|
+
|| actualProviders.some((provider, index) => provider !== expectedProviders[index]))
|
|
50
|
+
throw new Error("Authentication connection package providers did not match.");
|
|
51
|
+
const normalizedGrants = {};
|
|
52
|
+
for (const provider of expected.providers) {
|
|
53
|
+
const grant = grants[provider];
|
|
54
|
+
if (typeof grant !== "string" || !grant.trim() || grant.length > 256) {
|
|
55
|
+
throw new Error("Authentication connection package grant is invalid.");
|
|
56
|
+
}
|
|
57
|
+
normalizedGrants[provider] = grant.trim();
|
|
58
|
+
}
|
|
59
|
+
const now = expected.nowSeconds ?? Math.floor(Date.now() / 1_000);
|
|
60
|
+
const iat = payload.iat;
|
|
61
|
+
const exp = payload.exp;
|
|
62
|
+
if (payload.version !== 1
|
|
63
|
+
|| payload.state !== expected.state
|
|
64
|
+
|| exactBrowserHubOrigin(String(payload.hubUrl ?? "")) !== exactBrowserHubOrigin(expected.hubUrl)
|
|
65
|
+
|| !Number.isInteger(iat)
|
|
66
|
+
|| !Number.isInteger(exp)
|
|
67
|
+
|| iat > now + 30
|
|
68
|
+
|| iat < now - PACKAGE_TTL_SECONDS
|
|
69
|
+
|| exp <= now
|
|
70
|
+
|| exp <= iat
|
|
71
|
+
|| exp - iat > PACKAGE_TTL_SECONDS)
|
|
72
|
+
throw new Error("Authentication connection package has expired or does not match this request.");
|
|
73
|
+
return {
|
|
74
|
+
version: 1,
|
|
75
|
+
hubUrl: exactBrowserHubOrigin(expected.hubUrl),
|
|
76
|
+
grants: normalizedGrants,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
export async function runAuthConnectApproval(hubUrl, providers) {
|
|
80
|
+
const normalizedHubUrl = exactBrowserHubOrigin(hubUrl);
|
|
81
|
+
return runBrowserLoopback({
|
|
82
|
+
hubUrl: normalizedHubUrl,
|
|
83
|
+
callbackPath: CALLBACK_PATH,
|
|
84
|
+
callbackTimeoutMs: CALLBACK_TIMEOUT_MS,
|
|
85
|
+
maxBodyBytes: MAX_BODY_BYTES,
|
|
86
|
+
messages: {
|
|
87
|
+
bodyTooLarge: "Authentication callback body is too large.",
|
|
88
|
+
bodyEmpty: "Authentication callback body is empty.",
|
|
89
|
+
bodyInvalid: "Authentication callback body is invalid.",
|
|
90
|
+
alreadyConsumed: "Authentication callback was already consumed.",
|
|
91
|
+
callbackFailed: "Authentication callback failed.",
|
|
92
|
+
listenFailed: "Failed to start the local authentication callback server.",
|
|
93
|
+
timeout: "Browser approval timed out after 5 minutes.",
|
|
94
|
+
cancelled: "Subscription connection cancelled.",
|
|
95
|
+
opening: () => `${CLI.logTag} Opening your browser for one approval by the hub's owner.\n`,
|
|
96
|
+
browserFallback: `${CLI.logTag} Could not open a browser automatically. Open this URL manually:\n`,
|
|
97
|
+
manualPrompt: `${CLI.logTag} If the browser is on another machine, paste its connection code here.\n`,
|
|
98
|
+
manualRetry: (error) => `${CLI.logTag} ${error} Try the full code again.\n`,
|
|
99
|
+
},
|
|
100
|
+
buildBrowserUrl: ({ hubUrl: origin, port, state, encodedPublicKey }) => (buildAuthConnectSettingsUrl(origin, port, state, encodedPublicKey, providers)),
|
|
101
|
+
decodeCallbackBody: async (value, context) => {
|
|
102
|
+
if (!value
|
|
103
|
+
|| typeof value !== "object"
|
|
104
|
+
|| Array.isArray(value)
|
|
105
|
+
|| Object.keys(value).join(",") !== "envelope"
|
|
106
|
+
|| typeof value.envelope !== "string")
|
|
107
|
+
throw new Error("Authentication callback payload is invalid.");
|
|
108
|
+
return decryptAuthConnectEnvelope(value.envelope, context.privateKey, { state: context.state, hubUrl: context.hubUrl, providers });
|
|
109
|
+
},
|
|
110
|
+
decodeManualCode: (code, context) => decryptAuthConnectEnvelope(code, context.privateKey, { state: context.state, hubUrl: context.hubUrl, providers }),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { chmod, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { stdin } from "node:process";
|
|
6
|
+
import { runAuthConnectApproval, } from "./auth-connect-browser.js";
|
|
7
|
+
import { HUB_URL, hubControlHeaders, ensureAuth, } from "./config.js";
|
|
8
|
+
import { ensureInteractiveConfig } from "./init.js";
|
|
9
|
+
import { stopChildProcess } from "./shutdown.js";
|
|
10
|
+
import { CLI } from "./identity.js";
|
|
11
|
+
const MAX_CODEX_AUTH_BYTES = 64 * 1_024;
|
|
12
|
+
const MAX_CLAUDE_TOKEN_CHARS = 16 * 1_024;
|
|
13
|
+
const MAX_UPLOAD_BODY_BYTES = 64 * 1_024;
|
|
14
|
+
function safeChildEnvironment(overrides = {}) {
|
|
15
|
+
const allowed = [
|
|
16
|
+
"PATH", "HOME", "USER", "LOGNAME", "SHELL", "TMPDIR", "LANG", "LC_ALL",
|
|
17
|
+
"TERM", "COLORTERM", "DISPLAY", "WAYLAND_DISPLAY", "XDG_RUNTIME_DIR", "BROWSER",
|
|
18
|
+
"NODE_EXTRA_CA_CERTS", "SSL_CERT_FILE", "SSL_CERT_DIR",
|
|
19
|
+
];
|
|
20
|
+
const env = {};
|
|
21
|
+
for (const key of allowed) {
|
|
22
|
+
const value = process.env[key];
|
|
23
|
+
if (value !== undefined)
|
|
24
|
+
env[key] = value;
|
|
25
|
+
}
|
|
26
|
+
return { ...env, ...overrides };
|
|
27
|
+
}
|
|
28
|
+
export function buildSubscriptionLoginEnvironment(provider, codexHome) {
|
|
29
|
+
return safeChildEnvironment(provider === "codex" && codexHome ? { CODEX_HOME: codexHome } : {});
|
|
30
|
+
}
|
|
31
|
+
function defaultRunCommand(command, args, options) {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const child = spawn(command, args, options);
|
|
34
|
+
let settled = false;
|
|
35
|
+
let interrupted = false;
|
|
36
|
+
const cleanup = () => {
|
|
37
|
+
process.off("SIGINT", onSigint);
|
|
38
|
+
process.off("SIGTERM", onSigterm);
|
|
39
|
+
process.off("SIGHUP", onSighup);
|
|
40
|
+
};
|
|
41
|
+
const finish = (error) => {
|
|
42
|
+
if (settled)
|
|
43
|
+
return;
|
|
44
|
+
settled = true;
|
|
45
|
+
cleanup();
|
|
46
|
+
if (error)
|
|
47
|
+
reject(error);
|
|
48
|
+
else
|
|
49
|
+
resolve();
|
|
50
|
+
};
|
|
51
|
+
const onSignal = (signal) => {
|
|
52
|
+
if (settled || interrupted)
|
|
53
|
+
return;
|
|
54
|
+
interrupted = true;
|
|
55
|
+
const cancellation = new Error(`${command} authentication was cancelled.`);
|
|
56
|
+
const shutdown = stopChildProcess(child, { signal });
|
|
57
|
+
void shutdown.then(() => finish(cancellation), () => finish(cancellation));
|
|
58
|
+
};
|
|
59
|
+
const onSigint = () => onSignal("SIGINT");
|
|
60
|
+
const onSigterm = () => onSignal("SIGTERM");
|
|
61
|
+
const onSighup = () => onSignal("SIGHUP");
|
|
62
|
+
process.on("SIGINT", onSigint);
|
|
63
|
+
process.on("SIGTERM", onSigterm);
|
|
64
|
+
process.on("SIGHUP", onSighup);
|
|
65
|
+
child.once("error", (error) => {
|
|
66
|
+
finish(error.code === "ENOENT"
|
|
67
|
+
? new Error(`${command} is not installed or is not available on PATH.`)
|
|
68
|
+
: new Error(`${command} could not be started.`));
|
|
69
|
+
});
|
|
70
|
+
child.once("exit", (code, signal) => {
|
|
71
|
+
if (code === 0 && !interrupted)
|
|
72
|
+
finish();
|
|
73
|
+
else
|
|
74
|
+
finish(new Error(interrupted || signal || code === 130
|
|
75
|
+
? `${command} authentication was cancelled.`
|
|
76
|
+
: `${command} authentication exited with code ${code ?? "unknown"}.`));
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
export function readHiddenInput(prompt, dependencies = {}) {
|
|
81
|
+
const inputSource = dependencies.input ?? stdin;
|
|
82
|
+
const signalSource = dependencies.signalSource ?? process;
|
|
83
|
+
const writeStderr = dependencies.writeStderr ?? ((message) => process.stderr.write(message));
|
|
84
|
+
if (!inputSource.isTTY || typeof inputSource.setRawMode !== "function") {
|
|
85
|
+
return Promise.reject(new Error("A terminal is required to paste the Claude subscription token."));
|
|
86
|
+
}
|
|
87
|
+
writeStderr(prompt);
|
|
88
|
+
const wasRaw = inputSource.isRaw;
|
|
89
|
+
const wasPaused = inputSource.isPaused();
|
|
90
|
+
inputSource.setRawMode(true);
|
|
91
|
+
inputSource.resume();
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
|
+
let value = "";
|
|
94
|
+
let settled = false;
|
|
95
|
+
const cleanup = () => {
|
|
96
|
+
inputSource.off("data", onData);
|
|
97
|
+
inputSource.off("end", onEnd);
|
|
98
|
+
inputSource.off("close", onEnd);
|
|
99
|
+
inputSource.off("error", onError);
|
|
100
|
+
signalSource.off("SIGINT", onSigint);
|
|
101
|
+
signalSource.off("SIGTERM", onSigterm);
|
|
102
|
+
signalSource.off("SIGHUP", onSighup);
|
|
103
|
+
inputSource.setRawMode(Boolean(wasRaw));
|
|
104
|
+
if (wasPaused)
|
|
105
|
+
inputSource.pause();
|
|
106
|
+
writeStderr("\n");
|
|
107
|
+
};
|
|
108
|
+
const cancel = () => {
|
|
109
|
+
if (settled)
|
|
110
|
+
return;
|
|
111
|
+
settled = true;
|
|
112
|
+
cleanup();
|
|
113
|
+
reject(new Error("Claude authentication was cancelled."));
|
|
114
|
+
};
|
|
115
|
+
const finish = () => {
|
|
116
|
+
if (settled)
|
|
117
|
+
return;
|
|
118
|
+
settled = true;
|
|
119
|
+
cleanup();
|
|
120
|
+
resolve(value);
|
|
121
|
+
};
|
|
122
|
+
const fail = (error) => {
|
|
123
|
+
if (settled)
|
|
124
|
+
return;
|
|
125
|
+
settled = true;
|
|
126
|
+
cleanup();
|
|
127
|
+
reject(error);
|
|
128
|
+
};
|
|
129
|
+
const onSigint = () => cancel();
|
|
130
|
+
const onSigterm = () => cancel();
|
|
131
|
+
const onSighup = () => cancel();
|
|
132
|
+
const onEnd = () => fail(new Error("Claude authentication input closed."));
|
|
133
|
+
const onError = () => fail(new Error("Claude authentication input failed."));
|
|
134
|
+
const onData = (chunk) => {
|
|
135
|
+
for (const character of String(chunk)) {
|
|
136
|
+
if (character === "\u0003") {
|
|
137
|
+
cancel();
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (character === "\r" || character === "\n") {
|
|
141
|
+
finish();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (character === "\u007f" || character === "\b") {
|
|
145
|
+
value = value.slice(0, -1);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (character >= " ")
|
|
149
|
+
value += character;
|
|
150
|
+
if (value.length > MAX_CLAUDE_TOKEN_CHARS) {
|
|
151
|
+
fail(new Error("Claude subscription token is too large."));
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
signalSource.once("SIGINT", onSigint);
|
|
157
|
+
signalSource.once("SIGTERM", onSigterm);
|
|
158
|
+
signalSource.once("SIGHUP", onSighup);
|
|
159
|
+
inputSource.on("data", onData);
|
|
160
|
+
inputSource.on("end", onEnd);
|
|
161
|
+
inputSource.on("close", onEnd);
|
|
162
|
+
inputSource.on("error", onError);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
async function collectCodexAuth(runCommand) {
|
|
166
|
+
const temporaryRoot = process.platform === "darwin" ? "/private/tmp" : tmpdir();
|
|
167
|
+
const codexHome = await mkdtemp(join(temporaryRoot, "tiller-codex-login-"));
|
|
168
|
+
await chmod(codexHome, 0o700);
|
|
169
|
+
try {
|
|
170
|
+
await writeFile(join(codexHome, "config.toml"), [
|
|
171
|
+
'cli_auth_credentials_store = "file"',
|
|
172
|
+
'forced_login_method = "chatgpt"',
|
|
173
|
+
"",
|
|
174
|
+
].join("\n"), { mode: 0o600, flag: "wx" });
|
|
175
|
+
process.stderr.write(`${CLI.logTag} Starting an isolated Codex subscription login.\n`);
|
|
176
|
+
await runCommand("codex", ["login"], {
|
|
177
|
+
env: buildSubscriptionLoginEnvironment("codex", codexHome),
|
|
178
|
+
stdio: "inherit",
|
|
179
|
+
windowsHide: true,
|
|
180
|
+
});
|
|
181
|
+
const authPath = join(codexHome, "auth.json");
|
|
182
|
+
const metadata = await stat(authPath).catch(() => null);
|
|
183
|
+
if (!metadata?.isFile())
|
|
184
|
+
throw new Error("Codex login completed without creating auth.json.");
|
|
185
|
+
if (metadata.size < 1 || metadata.size > MAX_CODEX_AUTH_BYTES) {
|
|
186
|
+
throw new Error("Codex auth.json is empty or too large.");
|
|
187
|
+
}
|
|
188
|
+
const authJson = await readFile(authPath, "utf8");
|
|
189
|
+
let value;
|
|
190
|
+
try {
|
|
191
|
+
value = JSON.parse(authJson);
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
throw new Error("Codex produced an invalid auth.json file.");
|
|
195
|
+
}
|
|
196
|
+
if (!value || typeof value !== "object" || Array.isArray(value)
|
|
197
|
+
|| value.auth_mode !== "chatgpt") {
|
|
198
|
+
throw new Error("Codex login did not produce ChatGPT subscription credentials.");
|
|
199
|
+
}
|
|
200
|
+
return authJson;
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
await rm(codexHome, { recursive: true, force: true });
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
async function collectClaudeToken(runCommand, promptHidden) {
|
|
207
|
+
process.stderr.write(`${CLI.logTag} Starting Claude subscription token setup.\n`);
|
|
208
|
+
await runCommand("claude", ["setup-token"], {
|
|
209
|
+
env: buildSubscriptionLoginEnvironment("claude"),
|
|
210
|
+
stdio: "inherit",
|
|
211
|
+
windowsHide: true,
|
|
212
|
+
});
|
|
213
|
+
const token = (await promptHidden("Paste the Claude subscription token (input hidden): ")).trim();
|
|
214
|
+
if (!token)
|
|
215
|
+
throw new Error("Claude subscription token is required.");
|
|
216
|
+
if (token.length > MAX_CLAUDE_TOKEN_CHARS)
|
|
217
|
+
throw new Error("Claude subscription token is too large.");
|
|
218
|
+
// What `claude setup-token` prints. Anything else on the clipboard is refused here, not stored.
|
|
219
|
+
if (!/^sk-ant-oat\d+-[A-Za-z0-9_-]{20,}$/.test(token)) {
|
|
220
|
+
throw new Error("That is not a Claude subscription token. Copy the whole line that starts with sk-ant-oat and run this again.");
|
|
221
|
+
}
|
|
222
|
+
return token;
|
|
223
|
+
}
|
|
224
|
+
export async function collectSubscriptionCredentials(providers, dependencies = {}) {
|
|
225
|
+
const runCommand = dependencies.runCommand ?? defaultRunCommand;
|
|
226
|
+
const promptHidden = dependencies.promptHidden ?? readHiddenInput;
|
|
227
|
+
const result = {};
|
|
228
|
+
if (providers.includes("codex"))
|
|
229
|
+
result.codexAuthJson = await collectCodexAuth(runCommand);
|
|
230
|
+
if (providers.includes("claude"))
|
|
231
|
+
result.claudeOauthToken = await collectClaudeToken(runCommand, promptHidden);
|
|
232
|
+
return result;
|
|
233
|
+
}
|
|
234
|
+
export function parseAuthConnectProviders(args) {
|
|
235
|
+
if (args.length === 0)
|
|
236
|
+
return ["codex", "claude"];
|
|
237
|
+
if (args.length === 1 && (args[0] === "codex" || args[0] === "claude"))
|
|
238
|
+
return [args[0]];
|
|
239
|
+
throw new Error(`Usage: ${CLI.command} auth connect [codex|claude] [--hub-url <origin>]`);
|
|
240
|
+
}
|
|
241
|
+
async function uploadCredential(options) {
|
|
242
|
+
const serialized = JSON.stringify(options.body);
|
|
243
|
+
if (Buffer.byteLength(serialized, "utf8") > MAX_UPLOAD_BODY_BYTES) {
|
|
244
|
+
throw new Error(`${options.provider === "codex" ? "Codex" : "Claude"} authentication upload is too large.`);
|
|
245
|
+
}
|
|
246
|
+
const response = await options.fetchImpl(`${options.hubUrl}/api/auth/subscriptions/${options.provider}/connect`, {
|
|
247
|
+
method: "POST",
|
|
248
|
+
headers: {
|
|
249
|
+
"Content-Type": "application/json",
|
|
250
|
+
"X-Tiller-Auth-Grant": options.grant,
|
|
251
|
+
...options.accessHeaders,
|
|
252
|
+
},
|
|
253
|
+
body: serialized,
|
|
254
|
+
});
|
|
255
|
+
if (response.ok)
|
|
256
|
+
return;
|
|
257
|
+
const payload = await response.json().catch(() => null);
|
|
258
|
+
const message = typeof payload?.error === "string" && payload.error.trim()
|
|
259
|
+
? payload.error.trim()
|
|
260
|
+
: `Hub rejected the ${options.provider} authentication upload (HTTP ${response.status}).`;
|
|
261
|
+
throw new Error(message);
|
|
262
|
+
}
|
|
263
|
+
export async function runAuthConnectCommand(args, options = {}) {
|
|
264
|
+
const providers = parseAuthConnectProviders(args);
|
|
265
|
+
const dependencies = options.dependencies ?? {};
|
|
266
|
+
await (dependencies.initialize ?? ensureInteractiveConfig)({ hubUrlOverride: options.hubUrlOverride });
|
|
267
|
+
(dependencies.validateAuth ?? ensureAuth)();
|
|
268
|
+
const hubUrl = dependencies.hubUrl ?? HUB_URL;
|
|
269
|
+
const accessHeaders = dependencies.accessHeaders ?? hubControlHeaders;
|
|
270
|
+
const credentials = await collectSubscriptionCredentials(providers, dependencies);
|
|
271
|
+
const approval = await (dependencies.approve ?? runAuthConnectApproval)(hubUrl, providers);
|
|
272
|
+
if (new URL(approval.hubUrl).origin !== new URL(hubUrl).origin) {
|
|
273
|
+
throw new Error("Owner approval returned a different Hub origin.");
|
|
274
|
+
}
|
|
275
|
+
const fetchImpl = dependencies.fetch ?? fetch;
|
|
276
|
+
for (const provider of providers) {
|
|
277
|
+
const grant = approval.grants[provider];
|
|
278
|
+
if (!grant)
|
|
279
|
+
throw new Error(`Owner approval did not include a ${provider} connection grant.`);
|
|
280
|
+
if (provider === "codex") {
|
|
281
|
+
if (!credentials.codexAuthJson)
|
|
282
|
+
throw new Error("Codex authentication was not collected.");
|
|
283
|
+
await uploadCredential({
|
|
284
|
+
fetchImpl,
|
|
285
|
+
hubUrl,
|
|
286
|
+
accessHeaders,
|
|
287
|
+
provider,
|
|
288
|
+
grant,
|
|
289
|
+
body: { version: 1, auth_json: credentials.codexAuthJson },
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
if (!credentials.claudeOauthToken)
|
|
294
|
+
throw new Error("Claude authentication was not collected.");
|
|
295
|
+
await uploadCredential({
|
|
296
|
+
fetchImpl,
|
|
297
|
+
hubUrl,
|
|
298
|
+
accessHeaders,
|
|
299
|
+
provider,
|
|
300
|
+
grant,
|
|
301
|
+
body: { version: 1, oauth_token: credentials.claudeOauthToken },
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
process.stderr.write(`${CLI.logTag} ${provider === "codex" ? "Codex" : "Claude"} subscription connected and activated.\n`);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { hostname } from "node:os";
|
|
2
|
+
import { compactDecrypt } from "jose";
|
|
3
|
+
import { exactBrowserHubOrigin, normalizeBrowserConnectionCode, runBrowserLoopback, } from "./browser-loopback.js";
|
|
4
|
+
import { isMachineUuid } from "./config.js";
|
|
5
|
+
import { CLI } from "./identity.js";
|
|
6
|
+
const CALLBACK_PATH = "/bootstrap-callback";
|
|
7
|
+
const CALLBACK_TIMEOUT_MS = 5 * 60_000;
|
|
8
|
+
const MAX_BODY_BYTES = 16 * 1_024;
|
|
9
|
+
const CONNECT_ENVELOPE_TTL_SECONDS = 5 * 60;
|
|
10
|
+
function normalizeUrl(value) {
|
|
11
|
+
return value.trim().replace(/\/+$/, "");
|
|
12
|
+
}
|
|
13
|
+
function parseBootstrapPayload(value, expectedState) {
|
|
14
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
15
|
+
throw new Error("Bootstrap callback payload must be an object.");
|
|
16
|
+
}
|
|
17
|
+
const payload = value;
|
|
18
|
+
const expectedKeys = ["hubUrl", "protectionMode", "state"];
|
|
19
|
+
const actualKeys = Object.keys(payload).sort();
|
|
20
|
+
if (actualKeys.length !== expectedKeys.length || actualKeys.some((key, index) => key !== expectedKeys[index])) {
|
|
21
|
+
throw new Error("Bootstrap callback payload is invalid.");
|
|
22
|
+
}
|
|
23
|
+
const state = typeof payload.state === "string" ? payload.state.trim() : "";
|
|
24
|
+
if (!state || state !== expectedState)
|
|
25
|
+
throw new Error("Bootstrap callback state did not match.");
|
|
26
|
+
const hubUrl = typeof payload.hubUrl === "string" ? normalizeUrl(payload.hubUrl) : "";
|
|
27
|
+
if (!hubUrl)
|
|
28
|
+
throw new Error("Bootstrap callback did not include a hub URL.");
|
|
29
|
+
if (payload.protectionMode === "public")
|
|
30
|
+
return { hubUrl, protectionMode: "public" };
|
|
31
|
+
throw new Error("Bootstrap callback returned an unsupported protection mode.");
|
|
32
|
+
}
|
|
33
|
+
export function encodeBrowserBootstrapCode(payload) {
|
|
34
|
+
return Buffer.from(JSON.stringify(payload), "utf8").toString("base64url");
|
|
35
|
+
}
|
|
36
|
+
export function decodeBrowserBootstrapCode(value, expectedState) {
|
|
37
|
+
const code = normalizeBrowserConnectionCode(value);
|
|
38
|
+
if (!code)
|
|
39
|
+
throw new Error("Connection code is empty.");
|
|
40
|
+
let decoded;
|
|
41
|
+
try {
|
|
42
|
+
decoded = JSON.parse(Buffer.from(code, "base64url").toString("utf8"));
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
throw new Error("Connection code is not valid.");
|
|
46
|
+
}
|
|
47
|
+
return parseBootstrapPayload(decoded, expectedState);
|
|
48
|
+
}
|
|
49
|
+
export async function decryptBrowserConnectEnvelope(envelopeInput, privateKey, expected) {
|
|
50
|
+
const envelope = normalizeBrowserConnectionCode(envelopeInput);
|
|
51
|
+
if (envelope.split(".").length !== 5)
|
|
52
|
+
throw new Error("Connection code is not a valid encrypted package.");
|
|
53
|
+
let decrypted;
|
|
54
|
+
try {
|
|
55
|
+
decrypted = await compactDecrypt(envelope, privateKey);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
throw new Error(`Connection code could not be decrypted by this ${CLI.command} process.`);
|
|
59
|
+
}
|
|
60
|
+
if (decrypted.protectedHeader.alg !== "ECDH-ES"
|
|
61
|
+
|| decrypted.protectedHeader.enc !== "A256GCM"
|
|
62
|
+
|| decrypted.protectedHeader.typ !== "tiller-connect+jwe") {
|
|
63
|
+
throw new Error("Connection code used an unsupported encryption format.");
|
|
64
|
+
}
|
|
65
|
+
let value;
|
|
66
|
+
try {
|
|
67
|
+
value = JSON.parse(new TextDecoder().decode(decrypted.plaintext));
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
throw new Error("Connection package payload is invalid.");
|
|
71
|
+
}
|
|
72
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
73
|
+
throw new Error("Connection package payload is invalid.");
|
|
74
|
+
}
|
|
75
|
+
const payload = value;
|
|
76
|
+
const expectedKeys = ["clientId", "clientSecret", "controlSecret", "exp", "hubUrl", "iat", "state", "tokenExpiresAt"];
|
|
77
|
+
const actualKeys = Object.keys(payload).sort();
|
|
78
|
+
if (actualKeys.length !== expectedKeys.length || actualKeys.some((key, index) => key !== expectedKeys[index])) {
|
|
79
|
+
throw new Error("Connection package payload is invalid.");
|
|
80
|
+
}
|
|
81
|
+
const state = typeof payload.state === "string" ? payload.state : "";
|
|
82
|
+
const hubUrl = typeof payload.hubUrl === "string" ? normalizeUrl(payload.hubUrl) : "";
|
|
83
|
+
const clientId = typeof payload.clientId === "string" ? payload.clientId.trim() : "";
|
|
84
|
+
const clientSecret = typeof payload.clientSecret === "string" ? payload.clientSecret.trim() : "";
|
|
85
|
+
const controlSecret = typeof payload.controlSecret === "string" ? payload.controlSecret.trim() : "";
|
|
86
|
+
const tokenExpiresAt = typeof payload.tokenExpiresAt === "string" ? payload.tokenExpiresAt.trim() : "";
|
|
87
|
+
const iat = payload.iat;
|
|
88
|
+
const exp = payload.exp;
|
|
89
|
+
const now = expected.nowSeconds ?? Math.floor(Date.now() / 1_000);
|
|
90
|
+
if (state !== expected.state)
|
|
91
|
+
throw new Error("Connection package state did not match.");
|
|
92
|
+
if (hubUrl !== exactBrowserHubOrigin(expected.hubUrl))
|
|
93
|
+
throw new Error("Connection package Hub URL did not match.");
|
|
94
|
+
if (!clientId || !clientSecret || !/^[A-Za-z0-9_-]{43}$/.test(controlSecret) || !Number.isFinite(Date.parse(tokenExpiresAt))) {
|
|
95
|
+
throw new Error("Connection package credentials are incomplete.");
|
|
96
|
+
}
|
|
97
|
+
if (!Number.isInteger(iat)
|
|
98
|
+
|| !Number.isInteger(exp)
|
|
99
|
+
|| iat > now + 30
|
|
100
|
+
|| iat < now - CONNECT_ENVELOPE_TTL_SECONDS
|
|
101
|
+
|| exp <= now
|
|
102
|
+
|| exp <= iat
|
|
103
|
+
|| exp - iat > CONNECT_ENVELOPE_TTL_SECONDS) {
|
|
104
|
+
throw new Error("Connection package has expired or has invalid timestamps.");
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
hubUrl,
|
|
108
|
+
protectionMode: "sign-in",
|
|
109
|
+
clientId,
|
|
110
|
+
clientSecret,
|
|
111
|
+
controlSecret,
|
|
112
|
+
tokenExpiresAt,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/** This machine's name, shown on the hub's connect page and in its list of machines. */
|
|
116
|
+
export function connectMachineLabel(name = hostname()) {
|
|
117
|
+
return name.replace(/[\u0000-\u001f\u007f]/g, " ").trim().slice(0, 80) || "Machine";
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* `machineId`: this machine's id, when it already has one (it is connecting
|
|
121
|
+
* again). The connect page shows it, and the owner's approval there is what
|
|
122
|
+
* lets the new credential take back that id if the old credential was
|
|
123
|
+
* removed or ran out (the hub's `bindMachineCredential`).
|
|
124
|
+
*/
|
|
125
|
+
/**
|
|
126
|
+
* The hub's connect page for this connection. `cli` names which program is connecting, so the
|
|
127
|
+
* page can say it does not fit the hub's kind, and refuse, before anything is issued.
|
|
128
|
+
*/
|
|
129
|
+
export function connectPageUrl(input) {
|
|
130
|
+
return `${input.origin}/cli/bootstrap?port=${input.port}&state=${encodeURIComponent(input.state)}&key=${encodeURIComponent(input.encodedPublicKey)}`
|
|
131
|
+
+ `&label=${encodeURIComponent(input.label)}`
|
|
132
|
+
+ (input.machineId ? `&machine=${encodeURIComponent(input.machineId)}` : "")
|
|
133
|
+
+ `&cli=${encodeURIComponent((input.identity ?? CLI).name)}`;
|
|
134
|
+
}
|
|
135
|
+
export async function runBrowserBootstrap(hubUrl, options = {}) {
|
|
136
|
+
const machineId = options.machineId?.trim() && isMachineUuid(options.machineId) ? options.machineId.trim() : null;
|
|
137
|
+
const normalizedHubUrl = exactBrowserHubOrigin(normalizeUrl(hubUrl));
|
|
138
|
+
const decodeConnectionCode = async (codeInput, context) => {
|
|
139
|
+
const code = normalizeBrowserConnectionCode(codeInput);
|
|
140
|
+
if (code.split(".").length === 5) {
|
|
141
|
+
return decryptBrowserConnectEnvelope(code, context.privateKey, {
|
|
142
|
+
state: context.state,
|
|
143
|
+
hubUrl: context.hubUrl,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
const publicResult = decodeBrowserBootstrapCode(code, context.state);
|
|
147
|
+
if (exactBrowserHubOrigin(publicResult.hubUrl) !== context.hubUrl) {
|
|
148
|
+
throw new Error("Bootstrap callback Hub URL did not match.");
|
|
149
|
+
}
|
|
150
|
+
return publicResult;
|
|
151
|
+
};
|
|
152
|
+
return runBrowserLoopback({
|
|
153
|
+
hubUrl: normalizedHubUrl,
|
|
154
|
+
callbackPath: CALLBACK_PATH,
|
|
155
|
+
callbackTimeoutMs: CALLBACK_TIMEOUT_MS,
|
|
156
|
+
maxBodyBytes: MAX_BODY_BYTES,
|
|
157
|
+
messages: {
|
|
158
|
+
bodyTooLarge: "Bootstrap callback body is too large.",
|
|
159
|
+
bodyEmpty: "Bootstrap callback body is empty.",
|
|
160
|
+
bodyInvalid: "Bootstrap callback body is not valid JSON.",
|
|
161
|
+
alreadyConsumed: "Bootstrap callback was already consumed.",
|
|
162
|
+
callbackFailed: "Bootstrap callback failed.",
|
|
163
|
+
listenFailed: `${CLI.logTag} Failed to start the local browser callback server.`,
|
|
164
|
+
timeout: `${CLI.logTag} Browser sign-in timed out after 5 minutes. Keep the browser open, confirm the Hub is reachable, then run \`${CLI.command}\` again.`,
|
|
165
|
+
cancelled: `${CLI.logTag} Browser sign-in cancelled.`,
|
|
166
|
+
opening: (origin) => `${CLI.logTag} Opening your browser to connect ${CLI.command} to ${origin}\n`,
|
|
167
|
+
comparisonCode: (code) => (`${CLI.logTag} Comparison code: ${code}\n`
|
|
168
|
+
+ `${CLI.logTag} The connect page shows a comparison code. Approve there only if it is this one.\n`),
|
|
169
|
+
browserFallback: `${CLI.logTag} Could not open a browser automatically. Open this URL manually:\n`,
|
|
170
|
+
manualPrompt: `${CLI.logTag} If the browser is on another machine, paste the connection code shown there.\n`,
|
|
171
|
+
manualRetry: (error) => `${CLI.logTag} ${error} Paste the full connection code and try again.\n`,
|
|
172
|
+
},
|
|
173
|
+
buildBrowserUrl: ({ hubUrl: origin, port, state, encodedPublicKey }) => connectPageUrl({ origin, port, state, encodedPublicKey, label: connectMachineLabel(), machineId }),
|
|
174
|
+
decodeCallbackBody: async (value, context) => {
|
|
175
|
+
if (value
|
|
176
|
+
&& typeof value === "object"
|
|
177
|
+
&& !Array.isArray(value)
|
|
178
|
+
&& Object.keys(value).join(",") === "envelope"
|
|
179
|
+
&& typeof value.envelope === "string") {
|
|
180
|
+
return decryptBrowserConnectEnvelope(value.envelope, context.privateKey, { state: context.state, hubUrl: context.hubUrl });
|
|
181
|
+
}
|
|
182
|
+
const result = parseBootstrapPayload(value, context.state);
|
|
183
|
+
if (exactBrowserHubOrigin(result.hubUrl) !== context.hubUrl) {
|
|
184
|
+
throw new Error("Bootstrap callback Hub URL did not match.");
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
},
|
|
188
|
+
decodeManualCode: decodeConnectionCode,
|
|
189
|
+
});
|
|
190
|
+
}
|