@stigmer/cli 0.1.3 → 0.3.0
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/auth/browser.d.ts +7 -0
- package/auth/browser.d.ts.map +1 -0
- package/auth/browser.js +49 -0
- package/auth/browser.js.map +1 -0
- package/auth/callback-server.d.ts +8 -3
- package/auth/callback-server.d.ts.map +1 -1
- package/auth/callback-server.js +53 -19
- package/auth/callback-server.js.map +1 -1
- package/auth/device.d.ts +23 -0
- package/auth/device.d.ts.map +1 -0
- package/auth/device.js +119 -0
- package/auth/device.js.map +1 -0
- package/auth/index.d.ts +4 -3
- package/auth/index.d.ts.map +1 -1
- package/auth/index.js +4 -3
- package/auth/index.js.map +1 -1
- package/auth/login.d.ts +49 -1
- package/auth/login.d.ts.map +1 -1
- package/auth/login.js +161 -51
- package/auth/login.js.map +1 -1
- package/auth/oidc.d.ts +67 -0
- package/auth/oidc.d.ts.map +1 -0
- package/auth/oidc.js +213 -0
- package/auth/oidc.js.map +1 -0
- package/auth/token.d.ts +17 -23
- package/auth/token.d.ts.map +1 -1
- package/auth/token.js +51 -60
- package/auth/token.js.map +1 -1
- package/client/client.d.ts.map +1 -1
- package/client/client.js +4 -1
- package/client/client.js.map +1 -1
- package/client/current-organization.d.ts +5 -0
- package/client/current-organization.d.ts.map +1 -0
- package/client/current-organization.js +15 -0
- package/client/current-organization.js.map +1 -0
- package/commands/apikey/index.d.ts +16 -1
- package/commands/apikey/index.d.ts.map +1 -1
- package/commands/apikey/index.js +81 -0
- package/commands/apikey/index.js.map +1 -1
- package/commands/app-handover.d.ts +17 -0
- package/commands/app-handover.d.ts.map +1 -0
- package/commands/app-handover.js +168 -0
- package/commands/app-handover.js.map +1 -0
- package/commands/auth/index.d.ts.map +1 -1
- package/commands/auth/index.js +15 -9
- package/commands/auth/index.js.map +1 -1
- package/commands/auth/login.d.ts +7 -2
- package/commands/auth/login.d.ts.map +1 -1
- package/commands/auth/login.js +26 -23
- package/commands/auth/login.js.map +1 -1
- package/commands/auth/whoami.d.ts +15 -0
- package/commands/auth/whoami.d.ts.map +1 -1
- package/commands/auth/whoami.js +72 -2
- package/commands/auth/whoami.js.map +1 -1
- package/commands/coding-agent.d.ts +5 -0
- package/commands/coding-agent.d.ts.map +1 -0
- package/commands/coding-agent.js +30 -0
- package/commands/coding-agent.js.map +1 -0
- package/commands/config/backend.js +20 -6
- package/commands/config/backend.js.map +1 -1
- package/commands/docs/index.d.ts +9 -0
- package/commands/docs/index.d.ts.map +1 -0
- package/commands/docs/index.js +64 -0
- package/commands/docs/index.js.map +1 -0
- package/commands/embedding.d.ts +28 -0
- package/commands/embedding.d.ts.map +1 -0
- package/commands/embedding.js +170 -0
- package/commands/embedding.js.map +1 -0
- package/commands/example.d.ts +20 -0
- package/commands/example.d.ts.map +1 -0
- package/commands/example.js +136 -0
- package/commands/example.js.map +1 -0
- package/commands/help.d.ts.map +1 -1
- package/commands/help.js +19 -3
- package/commands/help.js.map +1 -1
- package/commands/list.d.ts.map +1 -1
- package/commands/list.js +13 -1
- package/commands/list.js.map +1 -1
- package/commands/load-test/burst.d.ts +64 -0
- package/commands/load-test/burst.d.ts.map +1 -0
- package/commands/load-test/burst.js +202 -0
- package/commands/load-test/burst.js.map +1 -0
- package/commands/load-test/index.d.ts +37 -0
- package/commands/load-test/index.d.ts.map +1 -0
- package/commands/load-test/index.js +154 -0
- package/commands/load-test/index.js.map +1 -0
- package/commands/load-test/report.d.ts +98 -0
- package/commands/load-test/report.d.ts.map +1 -0
- package/commands/load-test/report.js +210 -0
- package/commands/load-test/report.js.map +1 -0
- package/commands/mcp-server.d.ts +3 -3
- package/commands/mcp-server.d.ts.map +1 -1
- package/commands/mcp-server.js +16 -7
- package/commands/mcp-server.js.map +1 -1
- package/commands/provider-key/index.d.ts.map +1 -1
- package/commands/provider-key/index.js +2 -2
- package/commands/provider-key/index.js.map +1 -1
- package/commands/setup.d.ts +17 -0
- package/commands/setup.d.ts.map +1 -1
- package/commands/setup.js +71 -1
- package/commands/setup.js.map +1 -1
- package/commands/status.d.ts.map +1 -1
- package/commands/status.js +4 -3
- package/commands/status.js.map +1 -1
- package/commands/up.d.ts.map +1 -1
- package/commands/up.js +67 -9
- package/commands/up.js.map +1 -1
- package/commands/usage.d.ts +15 -0
- package/commands/usage.d.ts.map +1 -1
- package/commands/usage.js +52 -2
- package/commands/usage.js.map +1 -1
- package/config/config.d.ts +14 -7
- package/config/config.d.ts.map +1 -1
- package/config/config.js +5 -1
- package/config/config.js.map +1 -1
- package/config/index.d.ts +2 -2
- package/config/index.d.ts.map +1 -1
- package/config/index.js +2 -2
- package/config/index.js.map +1 -1
- package/config/resolve.d.ts +14 -3
- package/config/resolve.d.ts.map +1 -1
- package/config/resolve.js +30 -6
- package/config/resolve.js.map +1 -1
- package/errors/classify.d.ts.map +1 -1
- package/errors/classify.js +10 -1
- package/errors/classify.js.map +1 -1
- package/errors/cli-exit-error.d.ts +3 -1
- package/errors/cli-exit-error.d.ts.map +1 -1
- package/errors/cli-exit-error.js +6 -2
- package/errors/cli-exit-error.js.map +1 -1
- package/errors/handle.d.ts +32 -5
- package/errors/handle.d.ts.map +1 -1
- package/errors/handle.js +67 -8
- package/errors/handle.js.map +1 -1
- package/local/daemon/components.d.ts +28 -1
- package/local/daemon/components.d.ts.map +1 -1
- package/local/daemon/components.js +64 -2
- package/local/daemon/components.js.map +1 -1
- package/local/daemon/host.d.ts.map +1 -1
- package/local/daemon/host.js +6 -2
- package/local/daemon/host.js.map +1 -1
- package/local/daemon/launch.d.ts +12 -4
- package/local/daemon/launch.d.ts.map +1 -1
- package/local/daemon/launch.js +49 -16
- package/local/daemon/launch.js.map +1 -1
- package/local/daemon/types.d.ts +9 -1
- package/local/daemon/types.d.ts.map +1 -1
- package/local/key-check.d.ts +20 -0
- package/local/key-check.d.ts.map +1 -0
- package/local/key-check.js +45 -0
- package/local/key-check.js.map +1 -0
- package/local/model-key-step.d.ts +63 -0
- package/local/model-key-step.d.ts.map +1 -0
- package/local/model-key-step.js +133 -0
- package/local/model-key-step.js.map +1 -0
- package/local/runtime/node.d.ts +12 -4
- package/local/runtime/node.d.ts.map +1 -1
- package/local/runtime/node.js +73 -27
- package/local/runtime/node.js.map +1 -1
- package/local/setup/prompt.d.ts.map +1 -1
- package/local/setup/prompt.js +22 -18
- package/local/setup/prompt.js.map +1 -1
- package/local/setup/wizard.d.ts +10 -0
- package/local/setup/wizard.d.ts.map +1 -1
- package/local/setup/wizard.js +40 -17
- package/local/setup/wizard.js.map +1 -1
- package/local/state/lock.d.ts.map +1 -1
- package/local/state/lock.js +123 -36
- package/local/state/lock.js.map +1 -1
- package/local/state/pidfile.d.ts +4 -0
- package/local/state/pidfile.d.ts.map +1 -1
- package/local/state/pidfile.js +12 -3
- package/local/state/pidfile.js.map +1 -1
- package/local/status.d.ts.map +1 -1
- package/local/status.js +62 -4
- package/local/status.js.map +1 -1
- package/package.json +13 -10
- package/program.d.ts.map +1 -1
- package/program.js +12 -0
- package/program.js.map +1 -1
- package/resources/get.js +1 -1
- package/resources/get.js.map +1 -1
- package/resources/provider-key.d.ts +7 -2
- package/resources/provider-key.d.ts.map +1 -1
- package/resources/provider-key.js +18 -3
- package/resources/provider-key.js.map +1 -1
- package/resources/run/stream.d.ts.map +1 -1
- package/resources/run/stream.js +2 -1
- package/resources/run/stream.js.map +1 -1
- package/resources/runs.d.ts +10 -3
- package/resources/runs.d.ts.map +1 -1
- package/resources/runs.js +22 -4
- package/resources/runs.js.map +1 -1
- package/resources/stream/render-plaintext.d.ts +3 -0
- package/resources/stream/render-plaintext.d.ts.map +1 -1
- package/resources/stream/render-plaintext.js +20 -2
- package/resources/stream/render-plaintext.js.map +1 -1
- package/resources/usage.d.ts +8 -0
- package/resources/usage.d.ts.map +1 -1
- package/resources/usage.js +37 -0
- package/resources/usage.js.map +1 -1
- package/resources/views/agent.d.ts.map +1 -1
- package/resources/views/agent.js +18 -2
- package/resources/views/agent.js.map +1 -1
- package/runtime.d.ts +9 -0
- package/runtime.d.ts.map +1 -1
- package/runtime.js +12 -1
- package/runtime.js.map +1 -1
- package/src/auth/browser.ts +57 -0
- package/src/auth/callback-server.ts +58 -22
- package/src/auth/device.ts +151 -0
- package/src/auth/index.ts +4 -3
- package/src/auth/login.ts +243 -67
- package/src/auth/oidc.ts +270 -0
- package/src/auth/token.ts +53 -90
- package/src/client/client.ts +4 -1
- package/src/client/current-organization.ts +27 -0
- package/src/commands/apikey/index.ts +85 -0
- package/src/commands/app-handover.ts +202 -0
- package/src/commands/auth/index.ts +16 -10
- package/src/commands/auth/login.ts +30 -23
- package/src/commands/auth/whoami.ts +80 -1
- package/src/commands/coding-agent.ts +33 -0
- package/src/commands/config/backend.ts +20 -6
- package/src/commands/docs/index.ts +65 -0
- package/src/commands/embedding.ts +209 -0
- package/src/commands/example.ts +157 -0
- package/src/commands/help.ts +19 -3
- package/src/commands/list.ts +14 -1
- package/src/commands/load-test/burst.ts +291 -0
- package/src/commands/load-test/index.ts +183 -0
- package/src/commands/load-test/report.ts +302 -0
- package/src/commands/mcp-server.ts +16 -8
- package/src/commands/provider-key/index.ts +7 -1
- package/src/commands/setup.ts +76 -2
- package/src/commands/status.ts +4 -3
- package/src/commands/up.ts +76 -10
- package/src/commands/usage.ts +55 -2
- package/src/config/config.ts +18 -8
- package/src/config/index.ts +2 -0
- package/src/config/resolve.ts +31 -6
- package/src/errors/classify.ts +10 -1
- package/src/errors/cli-exit-error.ts +6 -2
- package/src/errors/handle.ts +74 -8
- package/src/local/daemon/components.ts +66 -2
- package/src/local/daemon/host.ts +6 -2
- package/src/local/daemon/launch.ts +51 -16
- package/src/local/daemon/types.ts +9 -1
- package/src/local/key-check.ts +61 -0
- package/src/local/model-key-step.ts +164 -0
- package/src/local/runtime/node.ts +95 -30
- package/src/local/setup/prompt.ts +22 -18
- package/src/local/setup/wizard.ts +45 -18
- package/src/local/state/lock.ts +116 -35
- package/src/local/state/pidfile.ts +14 -3
- package/src/local/status.ts +69 -4
- package/src/program.ts +12 -0
- package/src/resources/get.ts +1 -1
- package/src/resources/provider-key.ts +24 -3
- package/src/resources/run/stream.ts +2 -1
- package/src/resources/runs.ts +36 -4
- package/src/resources/stream/render-plaintext.ts +20 -2
- package/src/resources/usage.ts +42 -0
- package/src/resources/views/agent.ts +19 -2
- package/src/runtime.ts +17 -1
- package/auth/auth0.d.ts +0 -19
- package/auth/auth0.d.ts.map +0 -1
- package/auth/auth0.js +0 -39
- package/auth/auth0.js.map +0 -1
- package/src/auth/auth0.ts +0 -43
package/runtime.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/** Asks the dialled server where to get help (its `getServerInfo`'s `help`), sharing the client's one probe. */
|
|
2
|
+
export type HelpProbe = () => Promise<{
|
|
3
|
+
readonly url: string;
|
|
4
|
+
readonly label: string;
|
|
5
|
+
} | undefined>;
|
|
1
6
|
/**
|
|
2
7
|
* Where the address the backend client dials came from: the active
|
|
3
8
|
* backend's kind, or the STIGMER_SERVER_ADDRESS override.
|
|
@@ -24,4 +29,8 @@ export declare function dialedServerAddress(): ServerAddress | undefined;
|
|
|
24
29
|
export declare function setActionResourceType(type: string): void;
|
|
25
30
|
/** The resource type the running command resolved; "" when none was. */
|
|
26
31
|
export declare function actionResourceType(): string;
|
|
32
|
+
/** Record how to ask the dialled server for its help link; undefined forgets it (tests). */
|
|
33
|
+
export declare function setServerHelpProbe(probe: HelpProbe | undefined): void;
|
|
34
|
+
/** How to ask the dialled server for its help link; undefined when no client was made. */
|
|
35
|
+
export declare function serverHelpProbe(): HelpProbe | undefined;
|
|
27
36
|
//# sourceMappingURL=runtime.d.ts.map
|
package/runtime.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["src/runtime.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["src/runtime.ts"],"names":[],"mappings":"AAkBA,gHAAgH;AAChH,MAAM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
// Two records are written later, by the command itself, so an error can
|
|
6
6
|
// name what was actually tried: the server address the backend client
|
|
7
7
|
// dials (and where that address came from), recorded when the client is
|
|
8
|
-
// made, and the resource type a get, delete or download resolved.
|
|
8
|
+
// made, and the resource type a get, delete or download resolved. The
|
|
9
|
+
// client also records how to ask its server where to get help, so a
|
|
10
|
+
// failure the server answered can end with the server's help line.
|
|
9
11
|
let debug = false;
|
|
10
12
|
let standalone = false;
|
|
11
13
|
let actionCommand = "";
|
|
12
14
|
let serverAddress;
|
|
13
15
|
let resourceType = "";
|
|
16
|
+
let helpProbe;
|
|
14
17
|
export function setDebug(value) {
|
|
15
18
|
debug = value;
|
|
16
19
|
}
|
|
@@ -47,4 +50,12 @@ export function setActionResourceType(type) {
|
|
|
47
50
|
export function actionResourceType() {
|
|
48
51
|
return resourceType;
|
|
49
52
|
}
|
|
53
|
+
/** Record how to ask the dialled server for its help link; undefined forgets it (tests). */
|
|
54
|
+
export function setServerHelpProbe(probe) {
|
|
55
|
+
helpProbe = probe;
|
|
56
|
+
}
|
|
57
|
+
/** How to ask the dialled server for its help link; undefined when no client was made. */
|
|
58
|
+
export function serverHelpProbe() {
|
|
59
|
+
return helpProbe;
|
|
60
|
+
}
|
|
50
61
|
//# sourceMappingURL=runtime.js.map
|
package/runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.js","sourceRoot":"","sources":["src/runtime.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,uEAAuE;AACvE,2EAA2E;AAC3E,EAAE;AACF,wEAAwE;AACxE,sEAAsE;AACtE,wEAAwE;AACxE,
|
|
1
|
+
{"version":3,"file":"runtime.js","sourceRoot":"","sources":["src/runtime.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,uEAAuE;AACvE,2EAA2E;AAC3E,EAAE;AACF,wEAAwE;AACxE,sEAAsE;AACtE,wEAAwE;AACxE,sEAAsE;AACtE,oEAAoE;AACpE,mEAAmE;AAEnE,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
|
2
|
+
//
|
|
3
|
+
// `BROWSER` names the command that opens an address, as `git` and `gh` read
|
|
4
|
+
// it: the CLI runs it through the shell with the address as its one argument
|
|
5
|
+
// (`$1`, never pasted into the command line), so `BROWSER="firefox
|
|
6
|
+
// --new-tab"` works and an address can never become shell syntax. Without
|
|
7
|
+
// it, the `open` package picks the system's opener.
|
|
8
|
+
//
|
|
9
|
+
// `canOpenBrowser` says whether a browser can open here at all: `BROWSER`
|
|
10
|
+
// set, or a desktop session (macOS and Windows outside an SSH session, Linux
|
|
11
|
+
// with a display). Where none can, `stigmer auth login` signs in with a code
|
|
12
|
+
// typed on any device instead.
|
|
13
|
+
|
|
14
|
+
import { spawn } from "node:child_process";
|
|
15
|
+
|
|
16
|
+
/** Opens an address in a browser; rejects when it cannot. */
|
|
17
|
+
export type BrowserOpener = (url: string) => Promise<void>;
|
|
18
|
+
|
|
19
|
+
/** Whether a browser can open on this machine. */
|
|
20
|
+
export function canOpenBrowser(
|
|
21
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
22
|
+
platform: NodeJS.Platform = process.platform,
|
|
23
|
+
): boolean {
|
|
24
|
+
if (isSet(env.BROWSER)) return true;
|
|
25
|
+
const overSsh = isSet(env.SSH_CONNECTION) || isSet(env.SSH_TTY);
|
|
26
|
+
if (platform === "darwin" || platform === "win32") return !overSsh;
|
|
27
|
+
return isSet(env.DISPLAY) || isSet(env.WAYLAND_DISPLAY);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Open `url` with `BROWSER` when it is set, else with the system's opener. */
|
|
31
|
+
export async function openBrowser(url: string, env: NodeJS.ProcessEnv = process.env): Promise<void> {
|
|
32
|
+
const command = env.BROWSER;
|
|
33
|
+
if (isSet(command) && process.platform !== "win32") {
|
|
34
|
+
await runBrowserCommand(command as string, url);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const { default: open } = await import("open");
|
|
38
|
+
await open(url);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function runBrowserCommand(command: string, url: string): Promise<void> {
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const child = spawn("/bin/sh", ["-c", `${command} "$1"`, "stigmer-browser", url], {
|
|
44
|
+
stdio: "ignore",
|
|
45
|
+
detached: true,
|
|
46
|
+
});
|
|
47
|
+
child.once("error", reject);
|
|
48
|
+
child.once("spawn", () => {
|
|
49
|
+
child.unref();
|
|
50
|
+
resolve();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isSet(value: string | undefined): boolean {
|
|
56
|
+
return value !== undefined && value !== "";
|
|
57
|
+
}
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
// A short-lived loopback HTTP server that receives
|
|
2
|
-
// the authorization code. Lifecycle: new -> start ->
|
|
1
|
+
// A short-lived loopback HTTP server that receives the identity provider's
|
|
2
|
+
// redirect carrying the authorization code. Lifecycle: new -> start ->
|
|
3
|
+
// waitForCallback -> shutdown.
|
|
4
|
+
//
|
|
5
|
+
// It binds 127.0.0.1 on the first free port of the list it is given: the
|
|
6
|
+
// loopback callbacks an operator registers for the native client
|
|
7
|
+
// (`http://127.0.0.1:17234-17236/auth/callback`, the same three the desktop
|
|
8
|
+
// app's debug build returns to), so a second sign-in running elsewhere on the
|
|
9
|
+
// machine takes the next port instead of failing.
|
|
3
10
|
|
|
4
11
|
import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
|
|
5
12
|
import { CliExitError, ExitCode } from "../errors/index.js";
|
|
@@ -18,36 +25,53 @@ type Settler = {
|
|
|
18
25
|
export class CallbackServer {
|
|
19
26
|
private server?: Server;
|
|
20
27
|
private settler?: Settler;
|
|
28
|
+
private boundPort?: number;
|
|
21
29
|
|
|
22
30
|
constructor(
|
|
23
|
-
private readonly
|
|
31
|
+
private readonly ports: readonly number[],
|
|
24
32
|
private readonly path: string,
|
|
25
33
|
) {}
|
|
26
34
|
|
|
27
|
-
/**
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
35
|
+
/** The port bound by {@link start}. */
|
|
36
|
+
get port(): number {
|
|
37
|
+
if (this.boundPort === undefined) throw new Error("callback server not started");
|
|
38
|
+
return this.boundPort;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The redirect address the provider calls back on. */
|
|
42
|
+
get redirectUri(): string {
|
|
43
|
+
return `http://127.0.0.1:${this.port}${this.path}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Bind 127.0.0.1 on the first free port, reporting a clear error when none is. */
|
|
47
|
+
async start(): Promise<void> {
|
|
48
|
+
const failures: string[] = [];
|
|
49
|
+
for (const port of this.ports) {
|
|
50
|
+
try {
|
|
51
|
+
this.server = await listen(port, (req, res) => this.handle(req, res));
|
|
52
|
+
this.boundPort = port;
|
|
53
|
+
return;
|
|
54
|
+
} catch (err) {
|
|
55
|
+
failures.push(`${port}: ${(err as Error).message}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
throw new CliExitError(
|
|
59
|
+
`failed to start the sign-in callback on 127.0.0.1 (ports ${this.ports.join(", ")} are in use)`,
|
|
60
|
+
ExitCode.General,
|
|
61
|
+
["Finish or stop the other sign-in, then try again.", ...failures.map((f) => ` ${f}`)],
|
|
62
|
+
);
|
|
44
63
|
}
|
|
45
64
|
|
|
46
65
|
/** Resolve when the OAuth callback arrives; reject on error or timeout. */
|
|
47
66
|
waitForCallback(timeoutMs: number): Promise<CallbackResult> {
|
|
48
67
|
return new Promise<CallbackResult>((resolve, reject) => {
|
|
49
68
|
const timer = setTimeout(() => {
|
|
50
|
-
reject(
|
|
69
|
+
reject(
|
|
70
|
+
new CliExitError("the sign-in wasn't finished in the browser in time, so nothing was saved", ExitCode.General, [
|
|
71
|
+
"Run 'stigmer auth login' again and finish signing in on the page it opens.",
|
|
72
|
+
"Where no browser can open here, run 'stigmer auth login --device' instead.",
|
|
73
|
+
]),
|
|
74
|
+
);
|
|
51
75
|
}, timeoutMs);
|
|
52
76
|
this.settler = {
|
|
53
77
|
resolve: (result) => {
|
|
@@ -65,11 +89,12 @@ export class CallbackServer {
|
|
|
65
89
|
async shutdown(): Promise<void> {
|
|
66
90
|
const server = this.server;
|
|
67
91
|
if (server === undefined) return;
|
|
92
|
+
server.closeAllConnections();
|
|
68
93
|
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
69
94
|
}
|
|
70
95
|
|
|
71
96
|
private handle(req: IncomingMessage, res: ServerResponse): void {
|
|
72
|
-
const url = new URL(req.url ?? "/", `http://
|
|
97
|
+
const url = new URL(req.url ?? "/", `http://127.0.0.1:${this.boundPort ?? 0}`);
|
|
73
98
|
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
74
99
|
|
|
75
100
|
if (url.pathname !== this.path) {
|
|
@@ -102,3 +127,14 @@ export class CallbackServer {
|
|
|
102
127
|
this.settler?.resolve({ code, state: url.searchParams.get("state") ?? "" });
|
|
103
128
|
}
|
|
104
129
|
}
|
|
130
|
+
|
|
131
|
+
function listen(port: number, handler: (req: IncomingMessage, res: ServerResponse) => void): Promise<Server> {
|
|
132
|
+
return new Promise((resolve, reject) => {
|
|
133
|
+
const server = createServer(handler);
|
|
134
|
+
server.once("error", reject);
|
|
135
|
+
server.listen(port, "127.0.0.1", () => {
|
|
136
|
+
server.removeListener("error", reject);
|
|
137
|
+
resolve(server);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Signing in with a code typed on any device (OAuth 2.0 Device Authorization
|
|
2
|
+
// Grant, RFC 8628), for a machine where no browser can open: an SSH session,
|
|
3
|
+
// a remote sandbox, a coding agent's container.
|
|
4
|
+
//
|
|
5
|
+
// 1. POST the issuer's `device_authorization_endpoint` (from its discovery
|
|
6
|
+
// document) with the native client, the scopes and the audience; the
|
|
7
|
+
// answer is a device code, a short user code and the address to type it at.
|
|
8
|
+
// 2. Tell the person: the address, the code, and the address with the code
|
|
9
|
+
// filled in when the issuer gives one.
|
|
10
|
+
// 3. Poll the token endpoint with the device grant at the issuer's interval:
|
|
11
|
+
// `authorization_pending` waits, `slow_down` adds five seconds (section
|
|
12
|
+
// 3.5), `access_denied` and `expired_token` end it in plain words.
|
|
13
|
+
|
|
14
|
+
import { CliExitError, ExitCode } from "../errors/index.js";
|
|
15
|
+
import {
|
|
16
|
+
DEVICE_CODE_GRANT,
|
|
17
|
+
type Discovery,
|
|
18
|
+
type NativeSignIn,
|
|
19
|
+
postForm,
|
|
20
|
+
ProviderError,
|
|
21
|
+
SCOPES,
|
|
22
|
+
type TokenSet,
|
|
23
|
+
tokenSetOf,
|
|
24
|
+
} from "./oidc.js";
|
|
25
|
+
|
|
26
|
+
/** What the issuer answered for a device sign-in, as the person sees it. */
|
|
27
|
+
export interface DeviceCode {
|
|
28
|
+
readonly deviceCode: string;
|
|
29
|
+
readonly userCode: string;
|
|
30
|
+
readonly verificationUri: string;
|
|
31
|
+
readonly verificationUriComplete?: string;
|
|
32
|
+
readonly expiresInSeconds: number;
|
|
33
|
+
readonly intervalSeconds: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The waits and clock a device sign-in uses; tests pass their own. */
|
|
37
|
+
export interface DeviceTiming {
|
|
38
|
+
readonly sleep: (ms: number) => Promise<void>;
|
|
39
|
+
readonly now: () => number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const DEFAULT_INTERVAL_SECONDS = 5;
|
|
43
|
+
const SLOW_DOWN_SECONDS = 5;
|
|
44
|
+
|
|
45
|
+
export const realTiming: DeviceTiming = {
|
|
46
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
47
|
+
now: () => Date.now(),
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** Ask the issuer for a device code. */
|
|
51
|
+
export async function requestDeviceCode(discovery: Discovery, signIn: NativeSignIn): Promise<DeviceCode> {
|
|
52
|
+
const endpoint = discovery.deviceAuthorizationEndpoint;
|
|
53
|
+
if (endpoint === undefined) {
|
|
54
|
+
throw new CliExitError("this server's sign-in provider doesn't offer sign-in with a code", ExitCode.Auth, [
|
|
55
|
+
"Sign in from a machine with a browser (stigmer auth login), or use an API key:",
|
|
56
|
+
" stigmer apikey create --name cli (on a machine that is signed in)",
|
|
57
|
+
" stigmer config backend add <name> --endpoint <host:port> --api-key <key>",
|
|
58
|
+
]);
|
|
59
|
+
}
|
|
60
|
+
let body: Record<string, unknown>;
|
|
61
|
+
try {
|
|
62
|
+
body = await postForm(endpoint, {
|
|
63
|
+
client_id: signIn.clientId,
|
|
64
|
+
scope: SCOPES,
|
|
65
|
+
audience: signIn.audience,
|
|
66
|
+
});
|
|
67
|
+
} catch (err) {
|
|
68
|
+
if (err instanceof ProviderError) {
|
|
69
|
+
throw new CliExitError(`the sign-in provider refused a sign-in with a code: ${err.message}`, ExitCode.Auth, [
|
|
70
|
+
err.code === "unauthorized_client"
|
|
71
|
+
? "The server's operator has to allow the device grant for its native client."
|
|
72
|
+
: "Sign in from a machine with a browser instead: stigmer auth login",
|
|
73
|
+
]);
|
|
74
|
+
}
|
|
75
|
+
throw err;
|
|
76
|
+
}
|
|
77
|
+
const deviceCode = body.device_code;
|
|
78
|
+
const userCode = body.user_code;
|
|
79
|
+
const verificationUri = body.verification_uri ?? body.verification_url;
|
|
80
|
+
if (typeof deviceCode !== "string" || typeof userCode !== "string" || typeof verificationUri !== "string") {
|
|
81
|
+
throw new CliExitError("the sign-in provider answered no code to sign in with", ExitCode.Auth);
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
deviceCode,
|
|
85
|
+
userCode,
|
|
86
|
+
verificationUri,
|
|
87
|
+
...(typeof body.verification_uri_complete === "string"
|
|
88
|
+
? { verificationUriComplete: body.verification_uri_complete }
|
|
89
|
+
: {}),
|
|
90
|
+
expiresInSeconds: typeof body.expires_in === "number" ? body.expires_in : 600,
|
|
91
|
+
intervalSeconds: typeof body.interval === "number" && body.interval > 0 ? body.interval : DEFAULT_INTERVAL_SECONDS,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The lines that tell the person where to type the code. */
|
|
96
|
+
export function deviceInstructions(code: DeviceCode): string {
|
|
97
|
+
const lines = [`To sign in, open ${code.verificationUri} and enter ${code.userCode}`];
|
|
98
|
+
if (code.verificationUriComplete !== undefined) {
|
|
99
|
+
lines.push(`or open ${code.verificationUriComplete}`);
|
|
100
|
+
}
|
|
101
|
+
lines.push(`The code works for ${Math.round(code.expiresInSeconds / 60)} minutes. Waiting...`);
|
|
102
|
+
return `${lines.join("\n")}\n`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Poll the token endpoint until the person approves, refuses, or the code expires. */
|
|
106
|
+
export async function pollDeviceToken(
|
|
107
|
+
discovery: Discovery,
|
|
108
|
+
signIn: NativeSignIn,
|
|
109
|
+
code: DeviceCode,
|
|
110
|
+
timing: DeviceTiming = realTiming,
|
|
111
|
+
): Promise<TokenSet> {
|
|
112
|
+
const deadline = timing.now() + code.expiresInSeconds * 1000;
|
|
113
|
+
let interval = code.intervalSeconds;
|
|
114
|
+
for (;;) {
|
|
115
|
+
await timing.sleep(interval * 1000);
|
|
116
|
+
if (timing.now() > deadline) throw expired();
|
|
117
|
+
try {
|
|
118
|
+
const body = await postForm(discovery.tokenEndpoint, {
|
|
119
|
+
grant_type: DEVICE_CODE_GRANT,
|
|
120
|
+
device_code: code.deviceCode,
|
|
121
|
+
client_id: signIn.clientId,
|
|
122
|
+
});
|
|
123
|
+
return tokenSetOf(body);
|
|
124
|
+
} catch (err) {
|
|
125
|
+
if (!(err instanceof ProviderError)) throw err;
|
|
126
|
+
switch (err.code) {
|
|
127
|
+
case "authorization_pending":
|
|
128
|
+
continue;
|
|
129
|
+
case "slow_down":
|
|
130
|
+
interval += SLOW_DOWN_SECONDS;
|
|
131
|
+
continue;
|
|
132
|
+
case "expired_token":
|
|
133
|
+
throw expired();
|
|
134
|
+
case "access_denied":
|
|
135
|
+
throw new CliExitError("the sign-in was refused on the other device", ExitCode.Auth, [
|
|
136
|
+
"Run 'stigmer auth login --device' to try again.",
|
|
137
|
+
]);
|
|
138
|
+
default:
|
|
139
|
+
throw new CliExitError(`the sign-in provider ended the sign-in: ${err.message}`, ExitCode.Auth, [
|
|
140
|
+
"Run 'stigmer auth login --device' to try again.",
|
|
141
|
+
]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function expired(): CliExitError {
|
|
148
|
+
return new CliExitError("the sign-in code expired before it was entered", ExitCode.Auth, [
|
|
149
|
+
"Run 'stigmer auth login --device' for a new code.",
|
|
150
|
+
]);
|
|
151
|
+
}
|
package/src/auth/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Public surface of the auth module.
|
|
2
2
|
|
|
3
|
-
export {
|
|
4
|
-
export { login } from "./login.js";
|
|
3
|
+
export { canOpenBrowser, openBrowser } from "./browser.js";
|
|
4
|
+
export { login, LOOPBACK_PORTS, type LoginOptions, type LoginOutcome } from "./login.js";
|
|
5
|
+
export { authorizeUrl, discover, type NativeSignIn, providerOnServersSite, type TokenSet } from "./oidc.js";
|
|
5
6
|
export { challengeS256, generateState, generateVerifier } from "./pkce.js";
|
|
6
|
-
export { createRefreshingTokenProvider,
|
|
7
|
+
export { createRefreshingTokenProvider, refreshAccessToken } from "./token.js";
|