@octen.ai/cli 0.8.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 +250 -4
- package/dist/api/errors.d.ts +15 -0
- package/dist/api/errors.js +15 -0
- 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/configureMcp.js +34 -18
- package/dist/commands/configureSkills.js +18 -4
- 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 +39 -3
- 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/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
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import { readCredentials, deleteCredentials } from "../auth/store.js";
|
|
3
|
+
// The 400 case ("the grant is already gone server-side", so logout's goal
|
|
4
|
+
// is already met) is identified by importing revoke.ts's own constant rather
|
|
5
|
+
// than re-typing its sentence: editing that copy now breaks the build here
|
|
6
|
+
// instead of silently downgrading this branch to the generic 401/403 message
|
|
7
|
+
// with no test failure.
|
|
8
|
+
import { revokeCliGrant, GRANT_NOT_RECOGNIZED_MESSAGE } from "../auth/revoke.js";
|
|
9
|
+
import { OctenNetworkError } from "../api/errors.js";
|
|
10
|
+
/**
|
|
11
|
+
* `octen logout` — revoke this device's authorization, then remove the
|
|
12
|
+
* local credential.
|
|
13
|
+
*
|
|
14
|
+
* The copy here is a hard requirement, not polish: this command clears a
|
|
15
|
+
* local file and revokes a *grant* (which only blocks the CLI from silently
|
|
16
|
+
* minting a new credential without a fresh consent screen). It never
|
|
17
|
+
* deactivates the underlying API key — that key is the user's own
|
|
18
|
+
* account-wide, long-lived key, and is very likely also sitting in other
|
|
19
|
+
* machines, production code, and the AI-client configs `octen
|
|
20
|
+
* configure-mcp` wrote. So the output must never claim to have revoked
|
|
21
|
+
* *access*, only "this authorization". Telling a user their access was
|
|
22
|
+
* revoked when the key is still live everywhere else would be the most
|
|
23
|
+
* damaging kind of wrong: they would stop looking for the exposure.
|
|
24
|
+
*
|
|
25
|
+
* Whether a failed revocation should keep or delete the file depends on
|
|
26
|
+
* whether the failure is retriable: `OctenNetworkError` is a transport
|
|
27
|
+
* fault, and a transport fault must never be read as "the credential is
|
|
28
|
+
* invalid" — deleting a working credential because the network was down
|
|
29
|
+
* would leave the user with nothing and no way back without re-consenting.
|
|
30
|
+
* So the file is kept and `--local` is suggested. Every
|
|
31
|
+
* other failure `revokeCliGrant` can throw is `OctenAuthError` from one of
|
|
32
|
+
* three deterministic causes (401/403/400 in revoke.ts) — the stored
|
|
33
|
+
* key is no longer valid, the grant isn't bound to it, or the grant id
|
|
34
|
+
* isn't recognized. All three fail identically on every retry of plain
|
|
35
|
+
* `octen logout`, so keeping the file there would strand the user with
|
|
36
|
+
* `--local` as the only escape hatch (and a lie for the 400 case, where the
|
|
37
|
+
* authorization was already gone). Those branches delete the file and say
|
|
38
|
+
* precisely why revocation didn't happen, printing the `grantId` so the
|
|
39
|
+
* user can still find and clean up the authorization in the dashboard if
|
|
40
|
+
* it's still listed as active there.
|
|
41
|
+
*/
|
|
42
|
+
export function registerLogout(program, internal = {}) {
|
|
43
|
+
program
|
|
44
|
+
.command("logout")
|
|
45
|
+
.description("Revoke this device's authorization and remove the locally stored credential")
|
|
46
|
+
.option("--local", "remove only the local credential file; skip revoking the authorization")
|
|
47
|
+
.action(async (opts) => {
|
|
48
|
+
const home = internal.home ?? os.homedir();
|
|
49
|
+
let creds;
|
|
50
|
+
try {
|
|
51
|
+
creds = readCredentials(home);
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
// Corrupt JSON or an unrecognized CREDENTIALS_VERSION: logout's job
|
|
55
|
+
// is to remove the file, not diagnose it — symmetric with
|
|
56
|
+
// src/auth/login.ts's tolerance of the same failure. There is no
|
|
57
|
+
// apiKey/grantId to extract from an unreadable file, so no
|
|
58
|
+
// revocation is attempted (or even possible) either way, regardless
|
|
59
|
+
// of --local.
|
|
60
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
61
|
+
process.stderr.write(`warning: ignoring an unreadable credentials file (removing it): ${msg}\n`);
|
|
62
|
+
deleteCredentials(home);
|
|
63
|
+
process.stdout.write("Cleared local credentials.\n");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!creds) {
|
|
67
|
+
process.stdout.write("Not logged in; nothing to do.\n");
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (creds.source === "api-key") {
|
|
71
|
+
// A pasted key has no grant behind it — nothing to revoke, and the
|
|
72
|
+
// copy must never claim otherwise, or the user is told an
|
|
73
|
+
// authorization was closed that never existed. Zero network requests
|
|
74
|
+
// on this branch.
|
|
75
|
+
deleteCredentials(home);
|
|
76
|
+
process.stdout.write("Cleared local credentials.\n");
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
// source === "login"
|
|
80
|
+
if (opts.local) {
|
|
81
|
+
const grantId = creds.grantId;
|
|
82
|
+
deleteCredentials(home);
|
|
83
|
+
process.stdout.write(`Cleared local credentials (--local: the authorization was not revoked). ` +
|
|
84
|
+
`Grant ${grantId} can still mint a new credential without a fresh consent screen — ` +
|
|
85
|
+
`revoke it from the dashboard if you want to close it out.\n`);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
await revokeCliGrant({
|
|
90
|
+
issuer: creds.issuer,
|
|
91
|
+
apiKey: creds.apiKey,
|
|
92
|
+
grantId: creds.grantId,
|
|
93
|
+
fetchImpl: internal.fetchImpl,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
98
|
+
if (err instanceof OctenNetworkError) {
|
|
99
|
+
// A failed revocation does not mean the credential is invalid —
|
|
100
|
+
// deleting it here would strand the user with no working key and
|
|
101
|
+
// no way to get a new one without re-consenting. Keep the file.
|
|
102
|
+
process.stderr.write(`Could not revoke this authorization: ${msg}\n` +
|
|
103
|
+
"Local credentials were kept. Retry, or run `octen logout --local` to remove them without revoking.\n");
|
|
104
|
+
process.exitCode = 1;
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
// Every other failure is deterministic (OctenAuthError from
|
|
108
|
+
// revoke.ts: 401/403/400) — retrying plain `octen logout` fails
|
|
109
|
+
// identically every time, so keeping the file only strands the
|
|
110
|
+
// user. Delete it, and say exactly why revocation didn't happen.
|
|
111
|
+
deleteCredentials(home);
|
|
112
|
+
if (msg === GRANT_NOT_RECOGNIZED_MESSAGE) {
|
|
113
|
+
// Do NOT say "already gone". The server's 400 is ambiguous by
|
|
114
|
+
// construction (see GRANT_NOT_RECOGNIZED_MESSAGE in auth/revoke.ts):
|
|
115
|
+
// it covers both "that id is unknown" and "the id is real and still
|
|
116
|
+
// active, but this key may no longer act on it" — the latter happens
|
|
117
|
+
// when the user has been removed from the organization that owns the
|
|
118
|
+
// key. Telling those users there is nothing left to revoke leaves a
|
|
119
|
+
// live grant behind that can still mint credentials, which is exactly
|
|
120
|
+
// what this command's doc comment above promises never to do.
|
|
121
|
+
process.stdout.write(`Cleared local credentials, but the server did not accept the revocation of ` +
|
|
122
|
+
`grant ${creds.grantId}.\n` +
|
|
123
|
+
` Most often the authorization no longer exists. It can also mean the ` +
|
|
124
|
+
`authorization is still active\n while this key may no longer act on it — for ` +
|
|
125
|
+
`example after you were removed from the\n organization that owns it. ` +
|
|
126
|
+
`Check that grant id in the dashboard if you need to be sure.\n`);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
process.stdout.write(`Cleared local credentials. This credential could not revoke its authorization ` +
|
|
130
|
+
`(${msg}) — if grant ${creds.grantId} still shows as active in the dashboard, ` +
|
|
131
|
+
`revoke it there.\n`);
|
|
132
|
+
}
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
deleteCredentials(home);
|
|
136
|
+
process.stdout.write("Cleared local credentials and revoked this authorization. " +
|
|
137
|
+
"This does not deactivate the API key itself: the same key still works on " +
|
|
138
|
+
"other machines, in production code, and in AI-client configs written by " +
|
|
139
|
+
"`octen configure-mcp`. If you suspect it was exposed, deactivate or rotate the " +
|
|
140
|
+
"key from the dashboard.\n");
|
|
141
|
+
});
|
|
142
|
+
}
|
package/dist/commands/reset.js
CHANGED
|
@@ -6,6 +6,7 @@ import { removeMcp } from "../mcp/install.js";
|
|
|
6
6
|
import { removeSkills } from "../skills/install.js";
|
|
7
7
|
import { quotePath } from "../util/quotePath.js";
|
|
8
8
|
import { parseScopeOpt } from "./utils.js";
|
|
9
|
+
import { deleteCredentials, readCredentials, credentialsPath } from "../auth/store.js";
|
|
9
10
|
// Maps CLI flag name → client id (shared across both registries)
|
|
10
11
|
const CLIENT_FLAG_MAP = {
|
|
11
12
|
claudeCode: "claude-code",
|
|
@@ -23,6 +24,7 @@ export function registerReset(program, internal = {}) {
|
|
|
23
24
|
.description("Remove Octen MCP server and/or skills from AI clients")
|
|
24
25
|
.option("--mcp", "remove MCP entries")
|
|
25
26
|
.option("--skills", "remove skills")
|
|
27
|
+
.option("--credentials", "remove the locally stored login credential (~/.octen/credentials.json); not included in --all")
|
|
26
28
|
.option("--all", "remove from both surfaces across all clients")
|
|
27
29
|
.option("--claude-code", "target Claude Code")
|
|
28
30
|
.option("--cursor", "target Cursor")
|
|
@@ -40,13 +42,47 @@ export function registerReset(program, internal = {}) {
|
|
|
40
42
|
// Parsed and validated by parseScopeOpt at option-parse time.
|
|
41
43
|
const scope = opts.scope;
|
|
42
44
|
const installOpts = { hasClaudeCli: internal.hasClaudeCli };
|
|
43
|
-
// 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.
|
|
44
49
|
const doMcp = opts.all ? true : !!opts.mcp;
|
|
45
50
|
const doSkills = opts.all ? true : !!opts.skills;
|
|
46
|
-
|
|
47
|
-
|
|
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");
|
|
48
55
|
return;
|
|
49
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
|
+
}
|
|
50
86
|
// Determine which client ids were explicitly requested via per-client flags
|
|
51
87
|
const requestedIds = new Set();
|
|
52
88
|
const anyPerClientFlag = Object.entries(CLIENT_FLAG_MAP).some(([flagName]) => !!opts[flagName]);
|
|
@@ -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/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 {};
|