@octen.ai/cli 0.7.0 → 0.9.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/README.md +325 -4
- package/dist/api/chat.d.ts +4 -1
- package/dist/api/client.d.ts +25 -1
- package/dist/api/client.js +144 -17
- package/dist/api/errors.d.ts +27 -0
- package/dist/api/errors.js +52 -0
- package/dist/api/search.js +5 -2
- package/dist/api/sse.d.ts +15 -3
- package/dist/api/sse.js +131 -36
- package/dist/auth/constants.d.ts +47 -0
- package/dist/auth/constants.js +89 -0
- package/dist/auth/exchange.d.ts +74 -0
- package/dist/auth/exchange.js +124 -0
- package/dist/auth/login.d.ts +64 -0
- package/dist/auth/login.js +163 -0
- package/dist/auth/loopback.d.ts +32 -0
- package/dist/auth/loopback.js +95 -0
- package/dist/auth/oauthClient.d.ts +70 -0
- package/dist/auth/oauthClient.js +136 -0
- package/dist/auth/pkce.d.ts +6 -0
- package/dist/auth/pkce.js +13 -0
- package/dist/auth/revoke.d.ts +58 -0
- package/dist/auth/revoke.js +91 -0
- package/dist/auth/store.d.ts +34 -0
- package/dist/auth/store.js +149 -0
- package/dist/cli.js +6 -0
- package/dist/commands/broadSearch.js +6 -6
- package/dist/commands/chat.js +12 -8
- package/dist/commands/configureMcp.js +38 -20
- package/dist/commands/configureSkills.js +25 -10
- package/dist/commands/imageSearch.js +3 -3
- package/dist/commands/login.d.ts +49 -0
- package/dist/commands/login.js +161 -0
- package/dist/commands/logout.d.ts +46 -0
- package/dist/commands/logout.js +142 -0
- package/dist/commands/reset.js +43 -5
- package/dist/commands/search.js +6 -6
- package/dist/commands/utils.d.ts +21 -2
- package/dist/commands/utils.js +43 -6
- package/dist/commands/whoami.d.ts +27 -0
- package/dist/commands/whoami.js +109 -0
- package/dist/config/resolve.d.ts +44 -1
- package/dist/config/resolve.js +80 -5
- package/dist/mcp/clients.js +7 -3
- package/dist/mcp/install.js +15 -2
- package/dist/mcp/write.js +24 -3
- package/dist/output/pretty/whoami.d.ts +59 -0
- package/dist/output/pretty/whoami.js +103 -0
- package/dist/skills/setkey.js +5 -4
- package/dist/util/secretFile.d.ts +24 -0
- package/dist/util/secretFile.js +35 -0
- package/package.json +1 -1
package/dist/commands/reset.js
CHANGED
|
@@ -5,6 +5,8 @@ import { SKILL_CLIENTS } from "../skills/clients.js";
|
|
|
5
5
|
import { removeMcp } from "../mcp/install.js";
|
|
6
6
|
import { removeSkills } from "../skills/install.js";
|
|
7
7
|
import { quotePath } from "../util/quotePath.js";
|
|
8
|
+
import { parseScopeOpt } from "./utils.js";
|
|
9
|
+
import { deleteCredentials, readCredentials, credentialsPath } from "../auth/store.js";
|
|
8
10
|
// Maps CLI flag name → client id (shared across both registries)
|
|
9
11
|
const CLIENT_FLAG_MAP = {
|
|
10
12
|
claudeCode: "claude-code",
|
|
@@ -22,6 +24,7 @@ export function registerReset(program, internal = {}) {
|
|
|
22
24
|
.description("Remove Octen MCP server and/or skills from AI clients")
|
|
23
25
|
.option("--mcp", "remove MCP entries")
|
|
24
26
|
.option("--skills", "remove skills")
|
|
27
|
+
.option("--credentials", "remove the locally stored login credential (~/.octen/credentials.json); not included in --all")
|
|
25
28
|
.option("--all", "remove from both surfaces across all clients")
|
|
26
29
|
.option("--claude-code", "target Claude Code")
|
|
27
30
|
.option("--cursor", "target Cursor")
|
|
@@ -31,20 +34,55 @@ export function registerReset(program, internal = {}) {
|
|
|
31
34
|
.option("--codex", "target Codex")
|
|
32
35
|
.option("--openclaw", "target OpenClaw")
|
|
33
36
|
.option("--hermes", "target Hermes")
|
|
34
|
-
.option("--scope <s>", "config scope: user | project (default: user)", "user")
|
|
37
|
+
.option("--scope <s>", "config scope: user | project (default: user)", parseScopeOpt("--scope"), "user")
|
|
35
38
|
.action((_opts, command) => {
|
|
36
39
|
const opts = command.opts();
|
|
37
40
|
const home = internal.home ?? os.homedir();
|
|
38
41
|
const cwd = internal.cwd ?? process.cwd();
|
|
39
|
-
|
|
42
|
+
// Parsed and validated by parseScopeOpt at option-parse time.
|
|
43
|
+
const scope = opts.scope;
|
|
40
44
|
const installOpts = { hasClaudeCli: internal.hasClaudeCli };
|
|
41
|
-
// Surface selection
|
|
45
|
+
// Surface selection. `--credentials` is deliberately NOT folded into
|
|
46
|
+
// `--all` — `--all`'s existing meaning is "both surfaces x all
|
|
47
|
+
// clients" (MCP + skills), and silently logging the user out as a
|
|
48
|
+
// side effect of that would be an unwelcome surprise.
|
|
42
49
|
const doMcp = opts.all ? true : !!opts.mcp;
|
|
43
50
|
const doSkills = opts.all ? true : !!opts.skills;
|
|
44
|
-
|
|
45
|
-
|
|
51
|
+
const doCredentials = !!opts.credentials;
|
|
52
|
+
if (!doMcp && !doSkills && !doCredentials) {
|
|
53
|
+
process.stdout.write("specify --mcp, --skills, --credentials, or --all to select what to remove\n" +
|
|
54
|
+
"(--all covers --mcp and --skills across all clients; --credentials is separate)\n");
|
|
46
55
|
return;
|
|
47
56
|
}
|
|
57
|
+
// --- Credentials surface (independent of MCP/skills and of --all) ---
|
|
58
|
+
if (doCredentials) {
|
|
59
|
+
// Read before deleting: a `source: "login"` credential is the only
|
|
60
|
+
// place this machine records its grantId, and destroying it without
|
|
61
|
+
// printing that id strands the user — the grant stays listed in the
|
|
62
|
+
// dashboard and nothing here can name it any more — the same
|
|
63
|
+
// stranding `octen logout --local` prints the id to avoid. A
|
|
64
|
+
// corrupt/unreadable file is tolerated, exactly as `octen logout`
|
|
65
|
+
// tolerates it: reset's job is to remove the file, not diagnose it.
|
|
66
|
+
let grantId;
|
|
67
|
+
try {
|
|
68
|
+
const existing = readCredentials(home);
|
|
69
|
+
if (existing?.source === "login")
|
|
70
|
+
grantId = existing.grantId;
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
74
|
+
process.stderr.write(`warning: ignoring an unreadable credentials file (removing it): ${msg}\n`);
|
|
75
|
+
}
|
|
76
|
+
const removed = deleteCredentials(home);
|
|
77
|
+
process.stdout.write(removed
|
|
78
|
+
? `removed local credentials (${quotePath(credentialsPath(home))})\n`
|
|
79
|
+
: "no local credentials found\n");
|
|
80
|
+
if (removed && grantId) {
|
|
81
|
+
process.stdout.write(`Grant ${grantId} was not revoked — \`reset --credentials\` makes no network ` +
|
|
82
|
+
`request. It can still mint a new credential without a fresh consent screen; ` +
|
|
83
|
+
`revoke it from the dashboard's authorization list if you want to close it out.\n`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
48
86
|
// Determine which client ids were explicitly requested via per-client flags
|
|
49
87
|
const requestedIds = new Set();
|
|
50
88
|
const anyPerClientFlag = Object.entries(CLIENT_FLAG_MAP).some(([flagName]) => !!opts[flagName]);
|
package/dist/commands/search.js
CHANGED
|
@@ -2,21 +2,21 @@ import { ENDPOINTS } from "../api/constants.js";
|
|
|
2
2
|
import { buildSearchRequest } from "../api/search.js";
|
|
3
3
|
import { chooseMode, emit } from "../output/render.js";
|
|
4
4
|
import { renderSearch } from "../output/pretty/search.js";
|
|
5
|
-
import { makeClient, parseIntOpt } from "./utils.js";
|
|
5
|
+
import { makeClient, parseCsvOpt, parseIntOpt } from "./utils.js";
|
|
6
6
|
export function registerSearch(program, fixedTopic) {
|
|
7
7
|
const cmd = program.command(fixedTopic ?? "search")
|
|
8
8
|
.argument("<query...>", "search query")
|
|
9
9
|
.description(fixedTopic ? "News-focused web search" : "Search the live web")
|
|
10
10
|
.option("-n, --count <n>", "results 1-100", parseIntOpt("--count"))
|
|
11
|
-
.option("--include-domains <list>", "comma list", (
|
|
12
|
-
.option("--exclude-domains <list>", "comma list", (
|
|
13
|
-
.option("--include-text <list>", "comma list", (
|
|
14
|
-
.option("--exclude-text <list>", "comma list", (
|
|
11
|
+
.option("--include-domains <list>", "comma list", parseCsvOpt("--include-domains"))
|
|
12
|
+
.option("--exclude-domains <list>", "comma list", parseCsvOpt("--exclude-domains"))
|
|
13
|
+
.option("--include-text <list>", "comma list", parseCsvOpt("--include-text"))
|
|
14
|
+
.option("--exclude-text <list>", "comma list", parseCsvOpt("--exclude-text"))
|
|
15
15
|
.option("--time-basis <b>", "auto|published|crawled").option("--time-range <r>", "day|week|month|year (or d|w|m|y)")
|
|
16
16
|
.option("--start-time <when>", "YYYY-MM-DD or ISO datetime (e.g. 2024-01-01T00:00:00Z)")
|
|
17
17
|
.option("--end-time <when>", "YYYY-MM-DD or ISO datetime (e.g. 2024-12-31T23:59:59Z)")
|
|
18
18
|
.option("--format <f>", "text|markdown").option("--safesearch <s>", "off|strict")
|
|
19
|
-
.option("--language <list>", "ISO 639-1 codes, comma-separated, e.g. en,ja", (
|
|
19
|
+
.option("--language <list>", "ISO 639-1 codes, comma-separated, e.g. en,ja", parseCsvOpt("--language"))
|
|
20
20
|
.option("--highlight").option("--highlight-max-tokens <n>", "max tokens per highlight", parseIntOpt("--highlight-max-tokens"))
|
|
21
21
|
.option("--full-content").option("--full-content-max-tokens <n>", "max tokens per result", parseIntOpt("--full-content-max-tokens"))
|
|
22
22
|
.option("--images");
|
package/dist/commands/utils.d.ts
CHANGED
|
@@ -4,7 +4,26 @@ export declare function makeClient(g: {
|
|
|
4
4
|
apiKey?: string;
|
|
5
5
|
baseUrl?: string;
|
|
6
6
|
}): OctenClient;
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Commander option parser for integer flags. Lexical only: a value that is not
|
|
9
|
+
* exactly an integer is rejected rather than truncated, so `1.5`, `2junk` and
|
|
10
|
+
* `1e2` name themselves instead of silently becoming `1`, `2` and `1`. Range
|
|
11
|
+
* checks stay in the request builders, which own the published limits.
|
|
12
|
+
*/
|
|
8
13
|
export declare const parseIntOpt: (name: string) => (v: string) => number;
|
|
9
|
-
/** Commander option parser for float flags
|
|
14
|
+
/** Commander option parser for float flags. Whole-string match, finite result only. */
|
|
10
15
|
export declare const parseFloatOpt: (name: string) => (v: string) => number;
|
|
16
|
+
/**
|
|
17
|
+
* Commander option parser for comma-separated list flags. Each item is trimmed
|
|
18
|
+
* and empty items are dropped; a list with nothing left is an error rather than
|
|
19
|
+
* a `[""]` or `[]` that would reach the API as a filter nobody asked for.
|
|
20
|
+
*/
|
|
21
|
+
export declare const parseCsvOpt: (name: string) => (v: string) => string[];
|
|
22
|
+
export declare const SCOPE_OPTIONS: readonly ["user", "project"];
|
|
23
|
+
export type ConfigScope = (typeof SCOPE_OPTIONS)[number];
|
|
24
|
+
/**
|
|
25
|
+
* Commander option parser for `--scope`. An unknown scope is an error: coercing
|
|
26
|
+
* it to `user` would write a config file to a location the caller did not ask
|
|
27
|
+
* for, and `reset` would then delete from the wrong one.
|
|
28
|
+
*/
|
|
29
|
+
export declare const parseScopeOpt: (name: string) => (v: string) => ConfigScope;
|
package/dist/commands/utils.js
CHANGED
|
@@ -8,17 +8,54 @@ export function makeClient(g) {
|
|
|
8
8
|
baseUrl: resolveBaseUrl(g.baseUrl, process.env),
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
|
-
/**
|
|
11
|
+
/** Optional sign followed by decimal digits — nothing else. */
|
|
12
|
+
const INTEGER = /^-?\d+$/;
|
|
13
|
+
/** Decimal, fractional or exponent notation, whole-string. Rejects hex and trailing garbage. */
|
|
14
|
+
const DECIMAL = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/;
|
|
15
|
+
/**
|
|
16
|
+
* Commander option parser for integer flags. Lexical only: a value that is not
|
|
17
|
+
* exactly an integer is rejected rather than truncated, so `1.5`, `2junk` and
|
|
18
|
+
* `1e2` name themselves instead of silently becoming `1`, `2` and `1`. Range
|
|
19
|
+
* checks stay in the request builders, which own the published limits.
|
|
20
|
+
*/
|
|
12
21
|
export const parseIntOpt = (name) => (v) => {
|
|
13
|
-
|
|
14
|
-
if (Number.isNaN(n))
|
|
22
|
+
if (!INTEGER.test(v))
|
|
15
23
|
throw new OctenValidationError(`${name} must be an integer`);
|
|
24
|
+
const n = Number(v);
|
|
25
|
+
if (!Number.isSafeInteger(n))
|
|
26
|
+
throw new OctenValidationError(`${name} must be a safe integer`);
|
|
16
27
|
return n;
|
|
17
28
|
};
|
|
18
|
-
/** Commander option parser for float flags
|
|
29
|
+
/** Commander option parser for float flags. Whole-string match, finite result only. */
|
|
19
30
|
export const parseFloatOpt = (name) => (v) => {
|
|
20
|
-
|
|
21
|
-
|
|
31
|
+
if (!DECIMAL.test(v))
|
|
32
|
+
throw new OctenValidationError(`${name} must be a number`);
|
|
33
|
+
const n = Number(v);
|
|
34
|
+
if (!Number.isFinite(n))
|
|
22
35
|
throw new OctenValidationError(`${name} must be a number`);
|
|
23
36
|
return n;
|
|
24
37
|
};
|
|
38
|
+
/**
|
|
39
|
+
* Commander option parser for comma-separated list flags. Each item is trimmed
|
|
40
|
+
* and empty items are dropped; a list with nothing left is an error rather than
|
|
41
|
+
* a `[""]` or `[]` that would reach the API as a filter nobody asked for.
|
|
42
|
+
*/
|
|
43
|
+
export const parseCsvOpt = (name) => (v) => {
|
|
44
|
+
const values = v.split(",").map((item) => item.trim()).filter(Boolean);
|
|
45
|
+
if (values.length === 0) {
|
|
46
|
+
throw new OctenValidationError(`${name} must contain at least one non-empty value`);
|
|
47
|
+
}
|
|
48
|
+
return values;
|
|
49
|
+
};
|
|
50
|
+
export const SCOPE_OPTIONS = ["user", "project"];
|
|
51
|
+
/**
|
|
52
|
+
* Commander option parser for `--scope`. An unknown scope is an error: coercing
|
|
53
|
+
* it to `user` would write a config file to a location the caller did not ask
|
|
54
|
+
* for, and `reset` would then delete from the wrong one.
|
|
55
|
+
*/
|
|
56
|
+
export const parseScopeOpt = (name) => (v) => {
|
|
57
|
+
if (!SCOPE_OPTIONS.includes(v)) {
|
|
58
|
+
throw new OctenValidationError(`${name} must be one of: ${SCOPE_OPTIONS.join(", ")}`);
|
|
59
|
+
}
|
|
60
|
+
return v;
|
|
61
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Command } from "commander";
|
|
2
|
+
export interface WhoamiInternalOpts {
|
|
3
|
+
/** Injected home dir (for testing); defaults to os.homedir(). */
|
|
4
|
+
home?: string;
|
|
5
|
+
/** Injected env (for testing); defaults to process.env. */
|
|
6
|
+
env?: NodeJS.ProcessEnv;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* `octen whoami` — show the locally stored credential. This reads only the
|
|
10
|
+
* local file and makes zero network requests.
|
|
11
|
+
*
|
|
12
|
+
* There is deliberately no `--verify`. The stored credential is a long-lived
|
|
13
|
+
* API key with no short-lived state attached, so there is nothing local that
|
|
14
|
+
* could have gone stale and needs checking against the server; a request
|
|
15
|
+
* would only prove the key worked at that instant, which running any actual
|
|
16
|
+
* command already demonstrates. A `--verify` flag would also turn the one
|
|
17
|
+
* command a user reaches for while debugging broken connectivity into
|
|
18
|
+
* another command that needs working connectivity.
|
|
19
|
+
*
|
|
20
|
+
* It also reports which of the three sources is *actually* in effect. The
|
|
21
|
+
* two filters `resolveApiKey` applies — the flag/env short-circuit and the
|
|
22
|
+
* issuer/resource comparison — are applied here too (the second by calling
|
|
23
|
+
* `credentialIgnoredReason`, the very function `resolveApiKey` calls), so
|
|
24
|
+
* `whoami` can never describe a credential that `octen search` would refuse
|
|
25
|
+
* to use.
|
|
26
|
+
*/
|
|
27
|
+
export declare function registerWhoami(program: Command, internal?: WhoamiInternalOpts): void;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import { readCredentials } from "../auth/store.js";
|
|
3
|
+
import { authIssuer, authResource } from "../auth/constants.js";
|
|
4
|
+
import { credentialIgnoredReason } from "../config/resolve.js";
|
|
5
|
+
import { chooseMode, emit } from "../output/render.js";
|
|
6
|
+
import { renderWhoami, } from "../output/pretty/whoami.js";
|
|
7
|
+
/**
|
|
8
|
+
* `octen whoami` — show the locally stored credential. This reads only the
|
|
9
|
+
* local file and makes zero network requests.
|
|
10
|
+
*
|
|
11
|
+
* There is deliberately no `--verify`. The stored credential is a long-lived
|
|
12
|
+
* API key with no short-lived state attached, so there is nothing local that
|
|
13
|
+
* could have gone stale and needs checking against the server; a request
|
|
14
|
+
* would only prove the key worked at that instant, which running any actual
|
|
15
|
+
* command already demonstrates. A `--verify` flag would also turn the one
|
|
16
|
+
* command a user reaches for while debugging broken connectivity into
|
|
17
|
+
* another command that needs working connectivity.
|
|
18
|
+
*
|
|
19
|
+
* It also reports which of the three sources is *actually* in effect. The
|
|
20
|
+
* two filters `resolveApiKey` applies — the flag/env short-circuit and the
|
|
21
|
+
* issuer/resource comparison — are applied here too (the second by calling
|
|
22
|
+
* `credentialIgnoredReason`, the very function `resolveApiKey` calls), so
|
|
23
|
+
* `whoami` can never describe a credential that `octen search` would refuse
|
|
24
|
+
* to use.
|
|
25
|
+
*/
|
|
26
|
+
export function registerWhoami(program, internal = {}) {
|
|
27
|
+
program
|
|
28
|
+
.command("whoami")
|
|
29
|
+
.description("Show the locally stored credential (reads only the local file, no network request)")
|
|
30
|
+
.action(async (_opts, command) => {
|
|
31
|
+
const g = command.optsWithGlobals();
|
|
32
|
+
const home = internal.home ?? os.homedir();
|
|
33
|
+
const env = internal.env ?? process.env;
|
|
34
|
+
const mode = chooseMode(g, process.stdout.isTTY ?? false);
|
|
35
|
+
// Step 1/2 of resolveApiKey: an explicit flag or env var wins before
|
|
36
|
+
// the file is even read, so a stored credential behind either of them
|
|
37
|
+
// is not the one any command would use.
|
|
38
|
+
const shadow = g.apiKey
|
|
39
|
+
? "flag-shadowed"
|
|
40
|
+
: env.OCTEN_API_KEY
|
|
41
|
+
? "env-shadowed"
|
|
42
|
+
: undefined;
|
|
43
|
+
const shadowSource = g.apiKey
|
|
44
|
+
? "--api-key"
|
|
45
|
+
: env.OCTEN_API_KEY
|
|
46
|
+
? "OCTEN_API_KEY"
|
|
47
|
+
: undefined;
|
|
48
|
+
// The comment above is the whole point, so honour it: when a flag or env
|
|
49
|
+
// var already decided the outcome, the file is irrelevant and must not be
|
|
50
|
+
// able to fail this command. `resolveApiKey` returns on that path without
|
|
51
|
+
// touching disk, so `octen search` works with a corrupt credentials file
|
|
52
|
+
// while `octen whoami` used to be the one command that exited 2 — and in
|
|
53
|
+
// --json mode emitted nothing at all, which is worse than useless to the
|
|
54
|
+
// scripts that read it.
|
|
55
|
+
let creds;
|
|
56
|
+
try {
|
|
57
|
+
creds = readCredentials(home);
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
if (!shadowSource)
|
|
61
|
+
throw err;
|
|
62
|
+
creds = undefined;
|
|
63
|
+
}
|
|
64
|
+
if (!creds) {
|
|
65
|
+
const data = { loggedIn: false, effectiveSource: shadowSource ?? "none" };
|
|
66
|
+
emit(data, mode, renderWhoami);
|
|
67
|
+
// Exit code tracks "is a key in effect", not "does a file exist".
|
|
68
|
+
// It used to track the latter and was wrong in both directions: it
|
|
69
|
+
// exited 0 while reporting effectiveSource "none" (every command would
|
|
70
|
+
// fail), and exited 2 when an env key was working fine. That made
|
|
71
|
+
// `octen whoami && octen search …` unreliable in both directions.
|
|
72
|
+
if (!shadowSource)
|
|
73
|
+
process.exitCode = 2;
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
// Only reached when nothing shadows the file — mirroring resolveApiKey,
|
|
77
|
+
// which never parses OCTEN_AUTH_* on the flag/env path either.
|
|
78
|
+
const mismatch = shadow ? undefined : credentialIgnoredReason(creds, env);
|
|
79
|
+
const ignoredReason = shadow ?? mismatch;
|
|
80
|
+
const inEffect = ignoredReason === undefined;
|
|
81
|
+
const effectiveSource = shadowSource ?? (inEffect ? "credentials-file" : "none");
|
|
82
|
+
const effect = {
|
|
83
|
+
loggedIn: true,
|
|
84
|
+
effectiveSource,
|
|
85
|
+
inEffect,
|
|
86
|
+
...(ignoredReason !== undefined ? { ignoredReason } : {}),
|
|
87
|
+
...(mismatch === "issuer-mismatch" ? { expectedIssuer: authIssuer(env) } : {}),
|
|
88
|
+
...(mismatch === "resource-mismatch" ? { expectedResource: authResource(env) } : {}),
|
|
89
|
+
};
|
|
90
|
+
const data = creds.source === "api-key"
|
|
91
|
+
? { ...effect, source: "api-key" }
|
|
92
|
+
: {
|
|
93
|
+
...effect,
|
|
94
|
+
source: "login",
|
|
95
|
+
issuer: creds.issuer,
|
|
96
|
+
resource: creds.resource,
|
|
97
|
+
grantId: creds.grantId,
|
|
98
|
+
...(creds.accountId !== undefined ? { accountId: creds.accountId } : {}),
|
|
99
|
+
...(creds.accountType !== undefined ? { accountType: creds.accountType } : {}),
|
|
100
|
+
apiKeyExpiresAt: creds.apiKeyExpiresAt,
|
|
101
|
+
};
|
|
102
|
+
emit(data, mode, renderWhoami);
|
|
103
|
+
// Same rule as the no-credential branch: "none" means every command would
|
|
104
|
+
// fail, so it must not exit 0. A credential that exists but is shadowed
|
|
105
|
+
// or belongs to another environment is exactly that case.
|
|
106
|
+
if (effectiveSource === "none")
|
|
107
|
+
process.exitCode = 2;
|
|
108
|
+
});
|
|
109
|
+
}
|
package/dist/config/resolve.d.ts
CHANGED
|
@@ -1,2 +1,45 @@
|
|
|
1
|
-
|
|
1
|
+
import { type Credentials } from "../auth/store.js";
|
|
2
|
+
/**
|
|
3
|
+
* Why a stored credential is not the one commands will use. `undefined` means
|
|
4
|
+
* it is usable. Deliberately does NOT cover flag/env shadowing: those are
|
|
5
|
+
* decided before the file is even read (`resolveApiKey` steps 1-2), and
|
|
6
|
+
* `octen whoami` labels them separately.
|
|
7
|
+
*/
|
|
8
|
+
export type CredentialIgnoredReason = "issuer-mismatch" | "resource-mismatch" | "expired";
|
|
9
|
+
/**
|
|
10
|
+
* The single place that decides whether a stored credential is usable.
|
|
11
|
+
* `resolveApiKey` calls it below, and so does `octen whoami`
|
|
12
|
+
* (`src/commands/whoami.ts` calls it via this export) — sharing the one
|
|
13
|
+
* function is what stops `whoami` from reporting a credential that no other
|
|
14
|
+
* command would touch.
|
|
15
|
+
*
|
|
16
|
+
* Synchronous and network-free, like everything on this path.
|
|
17
|
+
*/
|
|
18
|
+
export declare function credentialIgnoredReason(creds: Credentials, env: NodeJS.ProcessEnv, nowSeconds?: number): CredentialIgnoredReason | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* The message for a credential that exists on disk but was minted for another
|
|
21
|
+
* environment. It deliberately does NOT reuse NO_CREDENTIAL_MESSAGE: telling
|
|
22
|
+
* the user to run `octen login` when an OCTEN_AUTH_* override is the cause
|
|
23
|
+
* sends them through a browser consent flow that cannot fix anything.
|
|
24
|
+
*/
|
|
25
|
+
export declare function mismatchMessage(reason: "issuer-mismatch" | "resource-mismatch", stored: string, expected: string): string;
|
|
26
|
+
export interface ResolveApiKeyOpts {
|
|
27
|
+
/** Injected home dir (for testing); defaults to os.homedir(). */
|
|
28
|
+
home?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Resolve the API key to use: --api-key flag > OCTEN_API_KEY env >
|
|
32
|
+
* ~/.octen/credentials.json (written by `octen login`) > throw.
|
|
33
|
+
*
|
|
34
|
+
* Stays synchronous and does no network work: the stored key never expires
|
|
35
|
+
* on its own (the not-yet-reachable short-lived-key path below is the only
|
|
36
|
+
* exception), so a plain, lock-free file read is enough. This is the property
|
|
37
|
+
* everything else depends on: the credential is written once, by `octen
|
|
38
|
+
* login`, and afterwards only read — so there is no refresh to serialize, no
|
|
39
|
+
* lock to hold, and no reason for this function to become async. Making it
|
|
40
|
+
* async would ripple into every command's client construction. Steps 1 and 2 return before
|
|
41
|
+
* touching the disk at all, so an explicit flag or env var never pays for a
|
|
42
|
+
* file read and always wins over a stale or foreign login.
|
|
43
|
+
*/
|
|
44
|
+
export declare function resolveApiKey(flag: string | undefined, env: NodeJS.ProcessEnv, opts?: ResolveApiKeyOpts): string;
|
|
2
45
|
export declare function resolveBaseUrl(flag: string | undefined, env: NodeJS.ProcessEnv): string;
|
package/dist/config/resolve.js
CHANGED
|
@@ -1,11 +1,86 @@
|
|
|
1
|
+
import os from "node:os";
|
|
1
2
|
import { DEFAULT_BASE_URL } from "../api/constants.js";
|
|
2
|
-
import { OctenAuthError } from "../api/errors.js";
|
|
3
|
-
|
|
3
|
+
import { OctenAuthError, OctenNoCredentialError } from "../api/errors.js";
|
|
4
|
+
import { readCredentials } from "../auth/store.js";
|
|
5
|
+
import { authIssuer, authResource } from "../auth/constants.js";
|
|
6
|
+
/** Names both ways out: interactive login and the environment variable. */
|
|
7
|
+
const NO_CREDENTIAL_MESSAGE = "No API key. Run `octen login`, pass --api-key, or set OCTEN_API_KEY.";
|
|
8
|
+
/**
|
|
9
|
+
* The single place that decides whether a stored credential is usable.
|
|
10
|
+
* `resolveApiKey` calls it below, and so does `octen whoami`
|
|
11
|
+
* (`src/commands/whoami.ts` calls it via this export) — sharing the one
|
|
12
|
+
* function is what stops `whoami` from reporting a credential that no other
|
|
13
|
+
* command would touch.
|
|
14
|
+
*
|
|
15
|
+
* Synchronous and network-free, like everything on this path.
|
|
16
|
+
*/
|
|
17
|
+
export function credentialIgnoredReason(creds, env, nowSeconds = Math.floor(Date.now() / 1000)) {
|
|
18
|
+
// A pasted key carries no issuer/resource/expiry — nothing to disqualify it.
|
|
19
|
+
if (creds.source !== "login")
|
|
20
|
+
return undefined;
|
|
21
|
+
// A credential minted for a different issuer/resource belongs to another
|
|
22
|
+
// environment, not to this resolution. Treat it as no credential at all —
|
|
23
|
+
// never use it, and never delete the file, since it isn't ours to manage.
|
|
24
|
+
if (creds.issuer !== authIssuer(env))
|
|
25
|
+
return "issuer-mismatch";
|
|
26
|
+
if (creds.resource !== authResource(env))
|
|
27
|
+
return "resource-mismatch";
|
|
28
|
+
// Forward-compatibility only: the server returns apiKeyExpiresAt: null for
|
|
29
|
+
// every credential it mints today, so this branch never fires yet. It is
|
|
30
|
+
// here so that if short-lived keys ever ship, an expired credential names
|
|
31
|
+
// itself instead of surfacing as an opaque 401 from the first API call.
|
|
32
|
+
if (creds.apiKeyExpiresAt !== null && creds.apiKeyExpiresAt <= nowSeconds)
|
|
33
|
+
return "expired";
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The message for a credential that exists on disk but was minted for another
|
|
38
|
+
* environment. It deliberately does NOT reuse NO_CREDENTIAL_MESSAGE: telling
|
|
39
|
+
* the user to run `octen login` when an OCTEN_AUTH_* override is the cause
|
|
40
|
+
* sends them through a browser consent flow that cannot fix anything.
|
|
41
|
+
*/
|
|
42
|
+
export function mismatchMessage(reason, stored, expected) {
|
|
43
|
+
const varName = reason === "issuer-mismatch" ? "OCTEN_AUTH_ISSUER" : "OCTEN_AUTH_RESOURCE";
|
|
44
|
+
return (`A stored credential exists, but it was issued for ${stored} while ${varName} selects ` +
|
|
45
|
+
`${expected}, so it is being ignored. Unset ${varName} to use the stored credential, or ` +
|
|
46
|
+
`run \`octen login\` again to get one for ${expected}. \`octen whoami\` shows which ` +
|
|
47
|
+
`credential is in effect.`);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Resolve the API key to use: --api-key flag > OCTEN_API_KEY env >
|
|
51
|
+
* ~/.octen/credentials.json (written by `octen login`) > throw.
|
|
52
|
+
*
|
|
53
|
+
* Stays synchronous and does no network work: the stored key never expires
|
|
54
|
+
* on its own (the not-yet-reachable short-lived-key path below is the only
|
|
55
|
+
* exception), so a plain, lock-free file read is enough. This is the property
|
|
56
|
+
* everything else depends on: the credential is written once, by `octen
|
|
57
|
+
* login`, and afterwards only read — so there is no refresh to serialize, no
|
|
58
|
+
* lock to hold, and no reason for this function to become async. Making it
|
|
59
|
+
* async would ripple into every command's client construction. Steps 1 and 2 return before
|
|
60
|
+
* touching the disk at all, so an explicit flag or env var never pays for a
|
|
61
|
+
* file read and always wins over a stale or foreign login.
|
|
62
|
+
*/
|
|
63
|
+
export function resolveApiKey(flag, env, opts = {}) {
|
|
4
64
|
const key = flag || env.OCTEN_API_KEY;
|
|
5
|
-
if (
|
|
6
|
-
|
|
65
|
+
if (key)
|
|
66
|
+
return key;
|
|
67
|
+
const home = opts.home ?? os.homedir();
|
|
68
|
+
const creds = readCredentials(home);
|
|
69
|
+
if (!creds) {
|
|
70
|
+
throw new OctenNoCredentialError(NO_CREDENTIAL_MESSAGE);
|
|
71
|
+
}
|
|
72
|
+
if (creds.source === "login") {
|
|
73
|
+
const reason = credentialIgnoredReason(creds, env);
|
|
74
|
+
if (reason === "expired") {
|
|
75
|
+
throw new OctenAuthError("Credential expired; run `octen login` again.");
|
|
76
|
+
}
|
|
77
|
+
if (reason !== undefined) {
|
|
78
|
+
const stored = reason === "issuer-mismatch" ? creds.issuer : creds.resource;
|
|
79
|
+
const expected = reason === "issuer-mismatch" ? authIssuer(env) : authResource(env);
|
|
80
|
+
throw new OctenAuthError(mismatchMessage(reason, stored, expected));
|
|
81
|
+
}
|
|
7
82
|
}
|
|
8
|
-
return
|
|
83
|
+
return creds.apiKey;
|
|
9
84
|
}
|
|
10
85
|
export function resolveBaseUrl(flag, env) {
|
|
11
86
|
return flag || env.OCTEN_API_URL || DEFAULT_BASE_URL;
|
package/dist/mcp/clients.js
CHANGED
|
@@ -50,9 +50,13 @@ export const MCP_CLIENTS = [
|
|
|
50
50
|
id: "codex",
|
|
51
51
|
label: "Codex",
|
|
52
52
|
format: "toml",
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
// Codex reads a project-level .codex/config.toml (MCP servers included) in a
|
|
54
|
+
// repository it has been told to trust, so project scope has a real target.
|
|
55
|
+
supportsProject: true,
|
|
56
|
+
pathFor(scope, home, cwd) {
|
|
57
|
+
return scope === "project"
|
|
58
|
+
? join(cwd, ".codex/config.toml")
|
|
59
|
+
: join(home, ".codex/config.toml");
|
|
56
60
|
},
|
|
57
61
|
},
|
|
58
62
|
];
|
package/dist/mcp/install.js
CHANGED
|
@@ -13,9 +13,22 @@ function claudeAvailable(override) {
|
|
|
13
13
|
}
|
|
14
14
|
export function installMcp(client, scope, entry, home, cwd, opts = {}) {
|
|
15
15
|
if (client.id === "claude-code") {
|
|
16
|
-
|
|
16
|
+
const apiKey = entry.env["OCTEN_API_KEY"] ?? "";
|
|
17
|
+
// A real key must never reach another process's argv: `ps -ww` and
|
|
18
|
+
// /proc/<pid>/cmdline expose it to every local user for the child's
|
|
19
|
+
// lifetime. That used to be harmless here because the only values that
|
|
20
|
+
// could arrive were ones the user had already put in argv (`--api-key`) or
|
|
21
|
+
// the environment (`OCTEN_API_KEY`); since credential resolution learned to
|
|
22
|
+
// read ~/.octen/credentials.json, a key that exists nowhere but a 0600 file
|
|
23
|
+
// can land here, and shelling out would be the one thing that leaks it.
|
|
24
|
+
//
|
|
25
|
+
// So: when the value is a literal secret, take the file path — the same
|
|
26
|
+
// path this function already uses when the `claude` CLI is absent, writing
|
|
27
|
+
// the same `mcpServers` entry to the same file. The CLI is still used for
|
|
28
|
+
// the `${OCTEN_API_KEY}` placeholder, which is not a secret.
|
|
29
|
+
const carriesLiteralSecret = apiKey !== "" && !apiKey.startsWith("${");
|
|
30
|
+
if (claudeAvailable(opts.hasClaudeCli) && !carriesLiteralSecret) {
|
|
17
31
|
// Use claude CLI: build args safely, no string concatenation
|
|
18
|
-
const apiKey = entry.env["OCTEN_API_KEY"] ?? "";
|
|
19
32
|
execFileSync("claude", [
|
|
20
33
|
"mcp",
|
|
21
34
|
"add",
|
package/dist/mcp/write.js
CHANGED
|
@@ -2,6 +2,17 @@ import { mkdirSync, readFileSync, writeFileSync, existsSync } from "node:fs";
|
|
|
2
2
|
import { dirname } from "node:path";
|
|
3
3
|
import { parse as tomlParse, stringify as tomlStringify } from "smol-toml";
|
|
4
4
|
import { OctenValidationError } from "../api/errors.js";
|
|
5
|
+
import { writeSecretFile } from "../util/secretFile.js";
|
|
6
|
+
/**
|
|
7
|
+
* True when this entry embeds an actual key rather than the
|
|
8
|
+
* `${OCTEN_API_KEY}` placeholder, i.e. when the file we are about to write
|
|
9
|
+
* becomes a secret-bearing file.
|
|
10
|
+
*/
|
|
11
|
+
function carriesLiteralSecret(entry) {
|
|
12
|
+
const env = entry.env;
|
|
13
|
+
const value = env?.["OCTEN_API_KEY"];
|
|
14
|
+
return typeof value === "string" && value !== "" && !value.startsWith("${");
|
|
15
|
+
}
|
|
5
16
|
function assertNever(x) {
|
|
6
17
|
throw new Error(`unhandled McpFormat: ${String(x)}`);
|
|
7
18
|
}
|
|
@@ -33,6 +44,7 @@ function readTomlFile(filePath) {
|
|
|
33
44
|
}
|
|
34
45
|
export function upsertMcpServer(filePath, format, entry) {
|
|
35
46
|
mkdirSync(dirname(filePath), { recursive: true });
|
|
47
|
+
const restrict = carriesLiteralSecret(entry);
|
|
36
48
|
switch (format) {
|
|
37
49
|
case "json-mcpServers":
|
|
38
50
|
case "claude-code": {
|
|
@@ -41,7 +53,10 @@ export function upsertMcpServer(filePath, format, entry) {
|
|
|
41
53
|
obj.mcpServers = {};
|
|
42
54
|
}
|
|
43
55
|
obj.mcpServers["octen"] = entry;
|
|
44
|
-
|
|
56
|
+
if (restrict)
|
|
57
|
+
writeSecretFile(filePath, JSON.stringify(obj, null, 2));
|
|
58
|
+
else
|
|
59
|
+
writeFileSync(filePath, JSON.stringify(obj, null, 2), "utf8");
|
|
45
60
|
return;
|
|
46
61
|
}
|
|
47
62
|
case "json-servers": {
|
|
@@ -50,7 +65,10 @@ export function upsertMcpServer(filePath, format, entry) {
|
|
|
50
65
|
obj.servers = {};
|
|
51
66
|
}
|
|
52
67
|
obj.servers["octen"] = entry;
|
|
53
|
-
|
|
68
|
+
if (restrict)
|
|
69
|
+
writeSecretFile(filePath, JSON.stringify(obj, null, 2));
|
|
70
|
+
else
|
|
71
|
+
writeFileSync(filePath, JSON.stringify(obj, null, 2), "utf8");
|
|
54
72
|
return;
|
|
55
73
|
}
|
|
56
74
|
case "toml": {
|
|
@@ -59,7 +77,10 @@ export function upsertMcpServer(filePath, format, entry) {
|
|
|
59
77
|
obj.mcp_servers = {};
|
|
60
78
|
}
|
|
61
79
|
obj.mcp_servers["octen"] = entry;
|
|
62
|
-
|
|
80
|
+
if (restrict)
|
|
81
|
+
writeSecretFile(filePath, tomlStringify(obj));
|
|
82
|
+
else
|
|
83
|
+
writeFileSync(filePath, tomlStringify(obj), "utf8");
|
|
63
84
|
return;
|
|
64
85
|
}
|
|
65
86
|
default:
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which of the three credential sources (`--api-key` > `OCTEN_API_KEY` >
|
|
3
|
+
* the stored file) `resolveApiKey` would actually pick right now. `"none"`
|
|
4
|
+
* means every command would fail with "No API key" — including when a
|
|
5
|
+
* credentials file exists but is being ignored.
|
|
6
|
+
*/
|
|
7
|
+
export type EffectiveKeySource = "--api-key" | "OCTEN_API_KEY" | "credentials-file" | "none";
|
|
8
|
+
/**
|
|
9
|
+
* The one wording for "there is no local credential file". Two paths render
|
|
10
|
+
* it: pretty mode throws it as an OctenAuthError from src/commands/whoami.ts
|
|
11
|
+
* (to get exit code 2), json mode emits the structured payload. Spelling it
|
|
12
|
+
* twice is how the two drift, so both go through `notLoggedInMessage`.
|
|
13
|
+
*/
|
|
14
|
+
export declare const NOT_LOGGED_IN_MESSAGE = "Not logged in (no local credential file). Run `octen login`, or `octen login --api-key <key>`.";
|
|
15
|
+
/**
|
|
16
|
+
* The not-logged-in message, plus — when a flag or env var is supplying a key
|
|
17
|
+
* regardless — the note that commands still work. Saying only "not logged in"
|
|
18
|
+
* to someone whose `OCTEN_API_KEY` is set describes a state they are not in.
|
|
19
|
+
*/
|
|
20
|
+
export declare function notLoggedInMessage(effectiveSource?: EffectiveKeySource): string;
|
|
21
|
+
/**
|
|
22
|
+
* Why the stored credential is not the one in effect. The first two are
|
|
23
|
+
* decided before the file is read at all; the last three mirror
|
|
24
|
+
* `credentialIgnoredReason` in src/config/resolve.ts.
|
|
25
|
+
*/
|
|
26
|
+
export type WhoamiIgnoredReason = "flag-shadowed" | "env-shadowed" | "issuer-mismatch" | "resource-mismatch" | "expired";
|
|
27
|
+
/** Fields common to every "a credentials file exists" shape. */
|
|
28
|
+
interface WhoamiEffect {
|
|
29
|
+
loggedIn: true;
|
|
30
|
+
effectiveSource: EffectiveKeySource;
|
|
31
|
+
/** Would `resolveApiKey` return THIS credential's key? */
|
|
32
|
+
inEffect: boolean;
|
|
33
|
+
ignoredReason?: WhoamiIgnoredReason;
|
|
34
|
+
/** Only present for a mismatch, and only for the field that mismatched. */
|
|
35
|
+
expectedIssuer?: string;
|
|
36
|
+
expectedResource?: string;
|
|
37
|
+
}
|
|
38
|
+
export type WhoamiData = {
|
|
39
|
+
loggedIn: false;
|
|
40
|
+
effectiveSource: EffectiveKeySource;
|
|
41
|
+
} | (WhoamiEffect & {
|
|
42
|
+
source: "api-key";
|
|
43
|
+
}) | (WhoamiEffect & {
|
|
44
|
+
source: "login";
|
|
45
|
+
issuer: string;
|
|
46
|
+
resource: string;
|
|
47
|
+
grantId: string;
|
|
48
|
+
accountId?: string;
|
|
49
|
+
accountType?: string;
|
|
50
|
+
apiKeyExpiresAt: number | null;
|
|
51
|
+
});
|
|
52
|
+
/**
|
|
53
|
+
* Pretty-print `octen whoami` output. Reflects only the local credentials
|
|
54
|
+
* file — every line here is derived from disk, never from a request, and the
|
|
55
|
+
* trailing note says so explicitly so a reader never mistakes this for
|
|
56
|
+
* confirmation that the key still works server-side.
|
|
57
|
+
*/
|
|
58
|
+
export declare function renderWhoami(data: WhoamiData): string;
|
|
59
|
+
export {};
|