@antasphere/cli-core 0.4.0 → 0.6.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 +217 -111
- package/dist/config.d.ts +63 -24
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +49 -9
- package/dist/config.js.map +1 -1
- package/dist/connect-on-demand.d.ts +101 -26
- package/dist/connect-on-demand.d.ts.map +1 -1
- package/dist/connect-on-demand.js +125 -53
- package/dist/connect-on-demand.js.map +1 -1
- package/dist/connect.d.ts +21 -6
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +17 -1
- package/dist/connect.js.map +1 -1
- package/dist/index.d.ts +15 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -10
- package/dist/index.js.map +1 -1
- package/dist/resolve.d.ts +125 -10
- package/dist/resolve.d.ts.map +1 -1
- package/dist/resolve.js +167 -8
- package/dist/resolve.js.map +1 -1
- package/package.json +2 -2
- package/src/config.ts +92 -25
- package/src/connect-on-demand.ts +206 -73
- package/src/connect.ts +27 -6
- package/src/index.ts +51 -13
- package/src/resolve.ts +234 -13
- package/dist/org.d.ts +0 -42
- package/dist/org.d.ts.map +0 -1
- package/dist/org.js +0 -39
- package/dist/org.js.map +0 -1
- package/src/org.ts +0 -73
package/src/config.ts
CHANGED
|
@@ -16,9 +16,16 @@ import { dirname, join } from 'node:path';
|
|
|
16
16
|
* └── <tool>.json 0600 every other tool's profiles
|
|
17
17
|
* (e.g. tools/slideless.json)
|
|
18
18
|
*
|
|
19
|
-
* Each file holds one tool's `{ activeProfile?, profiles }
|
|
20
|
-
*
|
|
21
|
-
*
|
|
19
|
+
* Each file holds one tool's `{ activeProfile?, profiles }`. A PROFILE IS AN
|
|
20
|
+
* INSTANCE: it names the tool instance it talks to (`baseUrl`; absent = the
|
|
21
|
+
* tool's cloud URL) and carries the credentials for that instance, either
|
|
22
|
+
* the tool's own key (`apiKey`, the tool's login) or the keys the hub
|
|
23
|
+
* exchange minted (`connectKeys`, one per hub profile). Profiles are named
|
|
24
|
+
* for their host by default (`cloud` for the cloud instance,
|
|
25
|
+
* `share.antasphere.com`, `localhost:3400`); the person may rename them and
|
|
26
|
+
* chooses the active one with `<tool> use`. The hub profile additionally
|
|
27
|
+
* carries `workspaceId`, the default organization at login (informational;
|
|
28
|
+
* the hub's server-side default is what applies).
|
|
22
29
|
*
|
|
23
30
|
* Files store API keys, so directories are created 0700 and files kept 0600
|
|
24
31
|
* on every write. All I/O is synchronous and env-injected (no process.env
|
|
@@ -30,11 +37,11 @@ export type CliEnv = Record<string, string | undefined>;
|
|
|
30
37
|
|
|
31
38
|
/**
|
|
32
39
|
* A tool-local key minted by the cross-tool connect exchange (binding
|
|
33
|
-
* patterns §7), cached
|
|
34
|
-
* `antasphere login` credential minted it. The
|
|
35
|
-
* ADR 014): one key serves every org — the org is a
|
|
36
|
-
* never part of the credential or of this cache's
|
|
37
|
-
* the key itself is informational.
|
|
40
|
+
* patterns §7), cached on the profile of the instance it was minted for,
|
|
41
|
+
* under the HUB PROFILE whose `antasphere login` credential minted it. The
|
|
42
|
+
* key is USER-scoped (hub ADR 014): one key serves every org — the org is a
|
|
43
|
+
* per-request parameter, never part of the credential or of this cache's
|
|
44
|
+
* slot. Every field beyond the key itself is informational.
|
|
38
45
|
*/
|
|
39
46
|
export interface CliConnectKey {
|
|
40
47
|
/** Full tool-local API key (`slk_`-style) — shown once at mint, cached here. */
|
|
@@ -46,20 +53,20 @@ export interface CliConnectKey {
|
|
|
46
53
|
}
|
|
47
54
|
|
|
48
55
|
export interface CliProfile {
|
|
49
|
-
/** `<prefix>_...` API key for this profile. */
|
|
56
|
+
/** `<prefix>_...` API key for this profile (the tool's own login). */
|
|
50
57
|
apiKey?: string;
|
|
51
|
-
/**
|
|
58
|
+
/** The instance this profile IS: its origin. Absent = the tool's cloud URL. */
|
|
52
59
|
baseUrl?: string;
|
|
53
60
|
/** The signed-in account's email (informational, set by login). */
|
|
54
61
|
email?: string;
|
|
55
|
-
/**
|
|
62
|
+
/** Hub profile: the default organization at login (informational; the
|
|
63
|
+
* hub's server-side default is what applies, `--org` overrides per
|
|
64
|
+
* command). Tool profiles from a tool's own login may carry the
|
|
65
|
+
* workspace that login bound. */
|
|
56
66
|
workspaceId?: string;
|
|
57
|
-
/** Hub profiles only: the org selected via `antasphere org use` (the
|
|
58
|
-
* default per-request org context; never part of any credential). */
|
|
59
|
-
activeWorkspaceId?: string;
|
|
60
67
|
/** Tool profiles only: exchange-minted USER-scoped keys, ONE per hub
|
|
61
|
-
* profile — the (tool, hub profile) cache of the cross-tool
|
|
62
|
-
* flow. Org-independent: a cached key serves every org
|
|
68
|
+
* profile — the (tool profile, hub profile) cache of the cross-tool
|
|
69
|
+
* connect flow. Org-independent: a cached key serves every org. */
|
|
63
70
|
connectKeys?: Record<string, CliConnectKey>;
|
|
64
71
|
}
|
|
65
72
|
|
|
@@ -162,32 +169,92 @@ export function lookupConnectKey(
|
|
|
162
169
|
}
|
|
163
170
|
|
|
164
171
|
/**
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
172
|
+
* The cached connect key the connect-on-demand flow may SERVE: the slot's
|
|
173
|
+
* key, only when the hub profile carries an e-mail address, the cached
|
|
174
|
+
* entry carries one too, and the two are the same address (trimmed,
|
|
175
|
+
* case-insensitive). A key is the credential of the person who minted it
|
|
176
|
+
* (PRDCT-3032), so the cache serves only that person; a hub profile with no
|
|
177
|
+
* address is someone unknown, and an entry with no address was minted by
|
|
178
|
+
* someone unknown: neither is served (PRDCT-3046 F7). `lookupConnectKey`
|
|
179
|
+
* stays the plain slot read.
|
|
180
|
+
*/
|
|
181
|
+
export function cachedConnectKey(
|
|
182
|
+
profile: CliProfile | undefined,
|
|
183
|
+
hubProfileName: string,
|
|
184
|
+
hubEmail: string | undefined
|
|
185
|
+
): string | undefined {
|
|
186
|
+
const entry = profile?.connectKeys?.[hubProfileName];
|
|
187
|
+
const hub = hubEmail?.trim().toLowerCase();
|
|
188
|
+
const minted = entry?.email?.trim().toLowerCase();
|
|
189
|
+
if (!hub || !minted || hub !== minted) return undefined;
|
|
190
|
+
return entry?.apiKey;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** How a profile signs in, for a `profiles`-style listing. */
|
|
194
|
+
export type ProfileCredential = 'key' | 'hub' | 'none';
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* What a profile signs in with, in the order the tool resolves it: its own
|
|
198
|
+
* `apiKey` (`key`, the tool's login), else a key the hub exchange minted
|
|
199
|
+
* (`hub`: under the named hub profile, or under any when none is named),
|
|
200
|
+
* else `none`. A listing can then say which profiles ride the hub login.
|
|
201
|
+
*/
|
|
202
|
+
export function profileCredential(
|
|
203
|
+
profile: CliProfile | undefined,
|
|
204
|
+
hubProfileName?: string
|
|
205
|
+
): ProfileCredential {
|
|
206
|
+
if (!profile) return 'none';
|
|
207
|
+
if (profile.apiKey) return 'key';
|
|
208
|
+
const slots = profile.connectKeys ?? {};
|
|
209
|
+
const connected = hubProfileName
|
|
210
|
+
? Boolean(slots[hubProfileName]?.apiKey)
|
|
211
|
+
: Object.values(slots).some((k) => k.apiKey);
|
|
212
|
+
return connected ? 'hub' : 'none';
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export interface StoreConnectKeyOptions {
|
|
216
|
+
/**
|
|
217
|
+
* The instance the key was minted for. Written on the profile when it is
|
|
218
|
+
* created here or has no `baseUrl` of its own (a blank one counts as
|
|
219
|
+
* none); a `baseUrl` that is there is never overwritten (a profile is
|
|
220
|
+
* never repointed).
|
|
221
|
+
*/
|
|
222
|
+
baseUrl?: string;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Cache an exchange-minted tool key on the SELECTED profile under the hub
|
|
227
|
+
* profile's name. A profile not on disk yet is created (with `baseUrl` when
|
|
228
|
+
* given) and made active only when the tool had NO profile at all: the
|
|
229
|
+
* first cloud connect on a fresh machine. An existing profile is never
|
|
230
|
+
* renamed, repointed or made active; every other field is preserved.
|
|
231
|
+
* Returns the file written.
|
|
169
232
|
*/
|
|
170
233
|
export function storeConnectKey(
|
|
171
234
|
env: CliEnv,
|
|
172
235
|
tool: string,
|
|
173
236
|
profileName: string,
|
|
174
237
|
hubProfileName: string,
|
|
175
|
-
entry: CliConnectKey
|
|
238
|
+
entry: CliConnectKey,
|
|
239
|
+
opts: StoreConnectKeyOptions = {}
|
|
176
240
|
): string {
|
|
177
241
|
const config = loadConfig(env, tool);
|
|
242
|
+
const first = Object.keys(config.profiles).length === 0;
|
|
178
243
|
const profile = config.profiles[profileName] ?? {};
|
|
179
244
|
config.profiles[profileName] = {
|
|
180
245
|
...profile,
|
|
246
|
+
...(!profile.baseUrl?.trim() && opts.baseUrl ? { baseUrl: opts.baseUrl } : {}),
|
|
181
247
|
connectKeys: { ...profile.connectKeys, [hubProfileName]: entry }
|
|
182
248
|
};
|
|
183
|
-
if (
|
|
249
|
+
if (first) config.activeProfile = profileName;
|
|
184
250
|
return saveConfig(env, tool, config);
|
|
185
251
|
}
|
|
186
252
|
|
|
187
253
|
/**
|
|
188
|
-
* Drop one hub profile's cached connect key (disconnect this
|
|
189
|
-
* hub account
|
|
190
|
-
*
|
|
254
|
+
* Drop one hub profile's cached connect key from a profile (disconnect this
|
|
255
|
+
* tool from the hub account, or evict a key the hub revoked). Returns
|
|
256
|
+
* whether anything was removed; the tool's own `apiKey` is never touched —
|
|
257
|
+
* it belongs to the tool's own login flow.
|
|
191
258
|
*/
|
|
192
259
|
export function removeConnectKey(
|
|
193
260
|
env: CliEnv,
|
package/src/connect-on-demand.ts
CHANGED
|
@@ -1,27 +1,43 @@
|
|
|
1
1
|
import {
|
|
2
2
|
assertToolNamespace,
|
|
3
|
+
cachedConnectKey,
|
|
3
4
|
HUB_TOOL,
|
|
4
5
|
loadConfig,
|
|
5
|
-
|
|
6
|
-
saveConfig,
|
|
6
|
+
removeConnectKey,
|
|
7
7
|
storeConnectKey
|
|
8
8
|
} from './config.js';
|
|
9
|
-
import type { CliEnv } from './config.js';
|
|
9
|
+
import type { CliEnv, CliProfile } from './config.js';
|
|
10
10
|
import { CliConnectClient, CliConnectError } from './connect.js';
|
|
11
11
|
import type { CliConnectResult } from './connect.js';
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
CliUsageError,
|
|
14
|
+
firstSet,
|
|
15
|
+
normalizeUrl,
|
|
16
|
+
profileUrl,
|
|
17
|
+
resolveProfile,
|
|
18
|
+
sameInstance,
|
|
19
|
+
selectProfile
|
|
20
|
+
} from './resolve.js';
|
|
13
21
|
import type { ResolvedProfile } from './resolve.js';
|
|
14
22
|
|
|
15
23
|
/**
|
|
16
24
|
* Connect-on-demand (binding patterns §7, gcloud model), extracted from the
|
|
17
25
|
* Slideless CLI so every Antasphere tool CLI inherits "one `antasphere login`
|
|
18
26
|
* serves every federated tool". When a tool CLI resolves NO credential of its
|
|
19
|
-
* own, it calls `connectOnDemand
|
|
27
|
+
* own, it calls `connectOnDemand` with the profile it selected
|
|
28
|
+
* (`selectProfile`: the profile IS the instance):
|
|
20
29
|
*
|
|
21
30
|
* 0. The cache is consulted FIRST: a key minted earlier for THIS instance
|
|
22
|
-
* under the
|
|
23
|
-
*
|
|
24
|
-
*
|
|
31
|
+
* under the hub profile (the one `ANTASPHERE_URL` names, else the
|
|
32
|
+
* active one: `selectHubProfile`) is USER-scoped (hub ADR 014) — it
|
|
33
|
+
* serves every subsequent run and every org, with zero network (no
|
|
34
|
+
* probe, no hub round-trip, no fresh mint). It is read on the selected
|
|
35
|
+
* profile only when that profile is at the resolved instance (its
|
|
36
|
+
* `baseUrl`, else the tool's `cloudUrl` when the profile has none), so
|
|
37
|
+
* a cached key never travels to another origin, and it is served only
|
|
38
|
+
* to the address that minted it: the hub profile's `email` and the
|
|
39
|
+
* cached entry's `email` must both be there and match
|
|
40
|
+
* (`cachedConnectKey`). `cached: false` skips this step.
|
|
25
41
|
* 1. On a miss, discovery gates the flow: only an Antasphere-CLOUD
|
|
26
42
|
* instance can mint keys through the hub (unauthenticated GET
|
|
27
43
|
* /instance — never guess). Unreachable, foreign, or self-hosted
|
|
@@ -29,16 +45,29 @@ import type { ResolvedProfile } from './resolve.js';
|
|
|
29
45
|
* error unchanged.
|
|
30
46
|
* 2. The hub credential is what `antasphere login` stored on the hub
|
|
31
47
|
* profile (the config home's HUB_TOOL namespace) — hub URL included,
|
|
32
|
-
* never a hardcoded constant here.
|
|
48
|
+
* never a hardcoded constant here. The hub profile is the one
|
|
49
|
+
* `ANTASPHERE_URL` names (the profile at that instance, the way the
|
|
50
|
+
* account CLI picks it), else the active one; a URL no hub profile is
|
|
51
|
+
* at has no credential, and the guided `antasphere login` error says
|
|
52
|
+
* so.
|
|
33
53
|
* 3. `CliConnectClient.exchange` trades it for a tool-local key (one
|
|
34
54
|
* re-exchange retry when the 120 s JWT dies in transit; a rejected hub
|
|
35
|
-
* credential maps to guided `antasphere login` advice
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
55
|
+
* credential maps to guided `antasphere login` advice on either
|
|
56
|
+
* attempt; a hub REFUSAL, a 403 that is not the scope case, propagates
|
|
57
|
+
* as its `CliConnectError` with the hub's own sentence and `exitCode`
|
|
58
|
+
* 3).
|
|
59
|
+
* 4. The minted key is cached on the SELECTED profile under the hub
|
|
60
|
+
* profile's name. A profile not on disk yet is created with this
|
|
61
|
+
* instance's URL, and made active only when the tool had no profile at
|
|
62
|
+
* all; an existing profile is never renamed or repointed, and one that
|
|
63
|
+
* is at another instance never receives the key.
|
|
64
|
+
*
|
|
65
|
+
* A stranded key recovers: on a 401 with a key that came from the cache,
|
|
66
|
+
* the tool CLI calls `refreshConnectKey` once (evict, exchange again with
|
|
67
|
+
* `cached: false`) and retries its request once; a second 401 fails.
|
|
39
68
|
*
|
|
40
|
-
* A new tool needs only its namespace,
|
|
41
|
-
* is tool-agnostic.
|
|
69
|
+
* A new tool needs only its namespace, its cloud URL, and copy — the flow
|
|
70
|
+
* itself is tool-agnostic.
|
|
42
71
|
*/
|
|
43
72
|
|
|
44
73
|
/** What the unauthenticated discovery probe learned about a tool instance. */
|
|
@@ -85,13 +114,42 @@ export async function probeToolInstance(
|
|
|
85
114
|
}
|
|
86
115
|
}
|
|
87
116
|
|
|
117
|
+
/** The hub's URL env var: the instance the account CLI talks to. */
|
|
118
|
+
export const HUB_URL_ENV = 'ANTASPHERE_URL';
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The hub profile the exchange uses, selected the way the account CLI
|
|
122
|
+
* selects it: when `ANTASPHERE_URL` is set, the profile at that instance
|
|
123
|
+
* (`selectProfile`'s URL rule: the active one first when several name it,
|
|
124
|
+
* then `cloud`, then the first declared); a URL nothing on disk names
|
|
125
|
+
* yields the in-memory profile `selectProfile` makes (named for its host,
|
|
126
|
+
* `cloud` for `cloudUrl`, a `baseUrl` and no key), so the caller's
|
|
127
|
+
* missing-login error fires. Without `ANTASPHERE_URL`, the active profile,
|
|
128
|
+
* and none when no profile is active (here the account CLI's rule differs:
|
|
129
|
+
* it would fall back to its cloud profile; a tool CLI asks for a login
|
|
130
|
+
* instead) (PRDCT-3392: following the active profile whatever
|
|
131
|
+
* `ANTASPHERE_URL` said asked the wrong hub). Its NAME is the connect cache's slot (config.ts
|
|
132
|
+
* `connectKeys`): stable per hub account on this machine, independent of
|
|
133
|
+
* any org selection.
|
|
134
|
+
*/
|
|
135
|
+
export function selectHubProfile(env: CliEnv, opts: { cloudUrl?: string | undefined } = {}): ResolvedProfile {
|
|
136
|
+
const config = loadConfig(env, HUB_TOOL);
|
|
137
|
+
const apiUrl = firstSet(env[HUB_URL_ENV]);
|
|
138
|
+
if (apiUrl) {
|
|
139
|
+
const { name, profile } = selectProfile({ config, apiUrl, cloudUrl: opts.cloudUrl });
|
|
140
|
+
return { name, profile };
|
|
141
|
+
}
|
|
142
|
+
return resolveProfile(config, undefined);
|
|
143
|
+
}
|
|
144
|
+
|
|
88
145
|
/**
|
|
89
|
-
* The
|
|
90
|
-
*
|
|
91
|
-
*
|
|
146
|
+
* The hub profile `ANTASPHERE_URL` names, else the active one — what
|
|
147
|
+
* `antasphere login` stored. Kept for the callers of 0.5.
|
|
148
|
+
*
|
|
149
|
+
* @deprecated Use `selectHubProfile`, which also takes the hub's cloud URL.
|
|
92
150
|
*/
|
|
93
151
|
export function activeHubProfile(env: CliEnv): ResolvedProfile {
|
|
94
|
-
return
|
|
152
|
+
return selectHubProfile(env);
|
|
95
153
|
}
|
|
96
154
|
|
|
97
155
|
/** The facts the connected notice is rendered from (fresh exchanges only —
|
|
@@ -101,12 +159,13 @@ export interface ConnectNoticeFacts {
|
|
|
101
159
|
toolBaseUrl: string;
|
|
102
160
|
/** The signed-in account's email at mint. */
|
|
103
161
|
email: string;
|
|
104
|
-
/** Whether the minted key was persisted for the next run (
|
|
105
|
-
*
|
|
162
|
+
/** Whether the minted key was persisted for the next run (on the
|
|
163
|
+
* selected profile, under the hub profile's name); false = the profile
|
|
164
|
+
* is at another instance. */
|
|
106
165
|
stored: boolean;
|
|
107
166
|
/** The tool profile the cache targeted. */
|
|
108
167
|
profileName: string;
|
|
109
|
-
/** When NOT stored: the other instance the profile
|
|
168
|
+
/** When NOT stored: the other instance the profile is at. */
|
|
110
169
|
profileBaseUrl?: string;
|
|
111
170
|
}
|
|
112
171
|
|
|
@@ -151,17 +210,34 @@ export interface ConnectOnDemandOptions {
|
|
|
151
210
|
tool: string;
|
|
152
211
|
/** The resolved tool instance origin the CLI is talking to. */
|
|
153
212
|
toolBaseUrl: string;
|
|
213
|
+
/** The tool's cloud URL, the same value `selectProfile` got: what a
|
|
214
|
+
* profile with no `baseUrl` of its own is at. Omit only for a tool with
|
|
215
|
+
* no cloud instance. */
|
|
216
|
+
cloudUrl?: string | undefined;
|
|
217
|
+
/** The hub's cloud URL, three roles: what the implicit `cloud` hub profile
|
|
218
|
+
* is at, so an `ANTASPHERE_URL` equal to it names the `cloud` slot (omit
|
|
219
|
+
* and a fresh hub profile is named for its host); where a hub profile
|
|
220
|
+
* with no `baseUrl` is placed for the selection; and the hub URL such a
|
|
221
|
+
* profile's exchange goes to. A profile's own `baseUrl` always wins. */
|
|
222
|
+
hubCloudUrl?: string | undefined;
|
|
154
223
|
/** The exchange resource URL; default = this instance's MCP endpoint,
|
|
155
224
|
* `${toolBaseUrl}/mcp` (the tool's registered resource URL). */
|
|
156
225
|
resource?: string;
|
|
157
|
-
/** The tool profile the
|
|
158
|
-
|
|
226
|
+
/** The tool profile the CLI selected for this run (`selectProfile`), the
|
|
227
|
+
* one the minted key is cached on. Not on disk yet → created with
|
|
228
|
+
* `toolBaseUrl` on first contact; at another instance → the key is
|
|
229
|
+
* returned but never stored. cli-core never guesses it. */
|
|
230
|
+
profileName: string;
|
|
159
231
|
env: CliEnv;
|
|
160
232
|
fetch?: typeof globalThis.fetch;
|
|
161
233
|
/** Receives the human "Connected to …" notice (tools wire their stderr);
|
|
162
234
|
* omitted = silent. Cache hits are always silent either way. */
|
|
163
235
|
notify?: (line: string) => void;
|
|
164
236
|
messages?: ConnectOnDemandMessages;
|
|
237
|
+
/** false = do not read the cache: run the exchange even when a key is
|
|
238
|
+
* cached (the one re-exchange after a 401 on a cached key;
|
|
239
|
+
* `refreshConnectKey` sets it). Default true. */
|
|
240
|
+
cached?: boolean;
|
|
165
241
|
}
|
|
166
242
|
|
|
167
243
|
export type ConnectOnDemandOutcome =
|
|
@@ -169,13 +245,14 @@ export type ConnectOnDemandOutcome =
|
|
|
169
245
|
outcome: 'connected';
|
|
170
246
|
/** The tool-local key — this invocation's credential. */
|
|
171
247
|
key: string;
|
|
172
|
-
/** true = served from the (tool, hub profile) config cache:
|
|
173
|
-
* network, nothing minted. false = a fresh exchange ran.
|
|
248
|
+
/** true = served from the (tool profile, hub profile) config cache:
|
|
249
|
+
* zero network, nothing minted. false = a fresh exchange ran. A 401
|
|
250
|
+
* on a `cached: true` key is what warrants `refreshConnectKey`. */
|
|
174
251
|
cached: boolean;
|
|
175
252
|
/** Fresh exchanges only: the full result (user, key meta). */
|
|
176
253
|
result?: CliConnectResult;
|
|
177
254
|
/** Fresh exchanges only: whether the minted key was persisted for
|
|
178
|
-
* the next run (false = the profile
|
|
255
|
+
* the next run (false = the profile is at another instance). */
|
|
179
256
|
stored?: boolean;
|
|
180
257
|
}
|
|
181
258
|
| {
|
|
@@ -184,30 +261,53 @@ export type ConnectOnDemandOutcome =
|
|
|
184
261
|
outcome: 'not_cloud';
|
|
185
262
|
};
|
|
186
263
|
|
|
264
|
+
/**
|
|
265
|
+
* Where the selected profile stands with respect to the resolved instance:
|
|
266
|
+
* `here` (its `baseUrl`, else the tool's cloud URL, is this instance),
|
|
267
|
+
* `elsewhere` (it is at another instance), or `free` (absent, or with no
|
|
268
|
+
* instance at all: no `baseUrl` and no cloud URL known). The cache is read
|
|
269
|
+
* only when `here`; a key is stored unless `elsewhere`; a key is evicted
|
|
270
|
+
* only when `here`.
|
|
271
|
+
*/
|
|
272
|
+
function placeOf(
|
|
273
|
+
existing: CliProfile | undefined,
|
|
274
|
+
toolBaseUrl: string,
|
|
275
|
+
cloudUrl: string | undefined
|
|
276
|
+
): { place: 'here' | 'elsewhere' | 'free'; at?: string } {
|
|
277
|
+
const at = existing ? profileUrl(existing, cloudUrl) : undefined;
|
|
278
|
+
if (!existing || at === undefined) return { place: 'free' };
|
|
279
|
+
return sameInstance(at, toolBaseUrl) ? { place: 'here', at } : { place: 'elsewhere', at };
|
|
280
|
+
}
|
|
281
|
+
|
|
187
282
|
/**
|
|
188
283
|
* The hub → tool exchange fallback a tool CLI runs when no direct credential
|
|
189
284
|
* resolved. Serves the cached user-scoped key when one exists for this
|
|
190
|
-
* (instance, hub profile)
|
|
191
|
-
* is not an Antasphere-cloud one (or
|
|
192
|
-
* `CliUsageError` when the instance IS cloud
|
|
193
|
-
*
|
|
285
|
+
* (instance, hub profile) on the selected profile; otherwise returns
|
|
286
|
+
* `not_cloud` when the instance is not an Antasphere-cloud one (or
|
|
287
|
+
* unreachable). Throws a guided `CliUsageError` when the instance IS cloud
|
|
288
|
+
* but the hub login is missing or rejected; a refusal and every other
|
|
289
|
+
* exchange failure propagate as their `CliConnectError` (`exitCode` says 3
|
|
290
|
+
* for the refusal, `message` is the server's own sentence).
|
|
194
291
|
*/
|
|
195
292
|
export async function connectOnDemand(opts: ConnectOnDemandOptions): Promise<ConnectOnDemandOutcome> {
|
|
196
293
|
assertToolNamespace(opts.tool);
|
|
197
|
-
const toolBaseUrl = opts.toolBaseUrl
|
|
294
|
+
const toolBaseUrl = normalizeUrl(opts.toolBaseUrl);
|
|
198
295
|
const messages = opts.messages ?? {};
|
|
199
|
-
const profileName = opts
|
|
296
|
+
const { profileName } = opts;
|
|
200
297
|
|
|
201
|
-
// 0. The cache: a key minted earlier by the
|
|
202
|
-
//
|
|
203
|
-
//
|
|
204
|
-
//
|
|
205
|
-
//
|
|
206
|
-
|
|
298
|
+
// 0. The cache: a key minted earlier by the hub profile (the one
|
|
299
|
+
// ANTASPHERE_URL names, else the active one) is user-scoped — it serves
|
|
300
|
+
// every run and every org (hub ADR 014; the org is a per-request
|
|
301
|
+
// parameter, never part of the credential). Only the selected profile
|
|
302
|
+
// is read, and only when it is at THIS instance: a cached key never
|
|
303
|
+
// travels to a different instance. It is served only to the address
|
|
304
|
+
// that minted it (both e-mails there and equal). A hit costs zero
|
|
305
|
+
// network. `cached: false` (the re-exchange after a 401) skips it.
|
|
306
|
+
const hub = selectHubProfile(opts.env, { cloudUrl: opts.hubCloudUrl });
|
|
207
307
|
const existing = loadConfig(opts.env, opts.tool).profiles[profileName];
|
|
208
|
-
const
|
|
209
|
-
if (
|
|
210
|
-
const cachedKey =
|
|
308
|
+
const { place, at } = placeOf(existing, toolBaseUrl, opts.cloudUrl);
|
|
309
|
+
if (opts.cached !== false && place === 'here' && hub.name) {
|
|
310
|
+
const cachedKey = cachedConnectKey(existing, hub.name, hub.profile?.email);
|
|
211
311
|
if (cachedKey) return { outcome: 'connected', key: cachedKey, cached: true };
|
|
212
312
|
}
|
|
213
313
|
|
|
@@ -218,9 +318,16 @@ export async function connectOnDemand(opts: ConnectOnDemandOptions): Promise<Con
|
|
|
218
318
|
|
|
219
319
|
// 2. The hub credential is what `antasphere login` stored on the hub
|
|
220
320
|
// profile (HUB_TOOL namespace) — one login for the whole tool family.
|
|
221
|
-
// The hub URL comes from that same profile, never from a constant here
|
|
222
|
-
|
|
223
|
-
|
|
321
|
+
// The hub URL comes from that same profile, never from a constant here;
|
|
322
|
+
// an in-memory profile (ANTASPHERE_URL naming no profile) has no key.
|
|
323
|
+
// A blank field is no field (the package's one blank rule, firstSet).
|
|
324
|
+
const hubKey = firstSet(hub.profile?.apiKey);
|
|
325
|
+
// A hub profile with no baseUrl (a hand edit, another writer: every
|
|
326
|
+
// Antasphere login writes one) is at the hub's cloud URL when the caller
|
|
327
|
+
// names one, the same rule a tool profile has with `cloudUrl`, and the
|
|
328
|
+
// rule `selectProfile` already applies to place it; the exchange half
|
|
329
|
+
// agrees with the selection half (verifier round 1, F4; round 3, F2).
|
|
330
|
+
const hubBaseUrl = firstSet(hub.profile?.baseUrl, opts.hubCloudUrl);
|
|
224
331
|
if (!hubKey || !hubBaseUrl || !hub.name) {
|
|
225
332
|
throw new CliUsageError(messages.missingHubLogin ?? defaultMissingHubLogin(opts.tool));
|
|
226
333
|
}
|
|
@@ -235,38 +342,44 @@ export async function connectOnDemand(opts: ConnectOnDemandOptions): Promise<Con
|
|
|
235
342
|
...(opts.fetch ? { fetch: opts.fetch } : {})
|
|
236
343
|
});
|
|
237
344
|
const body = { resource };
|
|
345
|
+
// One attempt, with the hub's verdict on the KEY mapped to the guided
|
|
346
|
+
// login error whichever attempt it comes on. A refusal (403 that is not
|
|
347
|
+
// the scope case) and every wire error propagate as they are: the error
|
|
348
|
+
// carries the server's own sentence and its exit code.
|
|
349
|
+
const attempt = async (): Promise<CliConnectResult> => {
|
|
350
|
+
try {
|
|
351
|
+
return await connect.exchange(hubKey, body);
|
|
352
|
+
} catch (e) {
|
|
353
|
+
if (e instanceof CliConnectError && e.needsHubLogin) {
|
|
354
|
+
throw new CliUsageError(messages.hubLoginRejected ?? DEFAULT_HUB_LOGIN_REJECTED);
|
|
355
|
+
}
|
|
356
|
+
throw e;
|
|
357
|
+
}
|
|
358
|
+
};
|
|
238
359
|
let result: CliConnectResult;
|
|
239
360
|
try {
|
|
240
|
-
result = await
|
|
361
|
+
result = await attempt();
|
|
241
362
|
} catch (e) {
|
|
242
|
-
if (e instanceof CliConnectError && e.needsReExchange)
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
} else if (e instanceof CliConnectError && e.needsHubLogin) {
|
|
247
|
-
throw new CliUsageError(messages.hubLoginRejected ?? DEFAULT_HUB_LOGIN_REJECTED);
|
|
248
|
-
} else {
|
|
249
|
-
throw e;
|
|
250
|
-
}
|
|
363
|
+
if (!(e instanceof CliConnectError && e.needsReExchange)) throw e;
|
|
364
|
+
// The 120 s JWT died in transit (expired/replayed) — one fresh
|
|
365
|
+
// exchange; a second rejection propagates.
|
|
366
|
+
result = await attempt();
|
|
251
367
|
}
|
|
252
368
|
|
|
253
|
-
// 4. Cache the minted key
|
|
254
|
-
// profile
|
|
255
|
-
//
|
|
256
|
-
|
|
369
|
+
// 4. Cache the minted key on the SELECTED profile under the hub profile's
|
|
370
|
+
// name. A profile not on disk yet, or one with no instance of its own,
|
|
371
|
+
// takes this instance's URL; a profile at another instance never
|
|
372
|
+
// receives the key and is never repointed.
|
|
373
|
+
const stored = place !== 'elsewhere';
|
|
257
374
|
if (stored) {
|
|
258
|
-
storeConnectKey(
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
const cfg = loadConfig(opts.env, opts.tool);
|
|
267
|
-
cfg.profiles[profileName] = { ...cfg.profiles[profileName], baseUrl: toolBaseUrl };
|
|
268
|
-
saveConfig(opts.env, opts.tool, cfg);
|
|
269
|
-
}
|
|
375
|
+
storeConnectKey(
|
|
376
|
+
opts.env,
|
|
377
|
+
opts.tool,
|
|
378
|
+
profileName,
|
|
379
|
+
hub.name,
|
|
380
|
+
{ apiKey: result.key, email: result.user.email, mintedAt: new Date().toISOString() },
|
|
381
|
+
{ baseUrl: toolBaseUrl }
|
|
382
|
+
);
|
|
270
383
|
}
|
|
271
384
|
|
|
272
385
|
const facts: ConnectNoticeFacts = {
|
|
@@ -274,10 +387,30 @@ export async function connectOnDemand(opts: ConnectOnDemandOptions): Promise<Con
|
|
|
274
387
|
email: result.user.email,
|
|
275
388
|
stored,
|
|
276
389
|
profileName,
|
|
277
|
-
// `stored` is false only when the profile
|
|
278
|
-
...(stored ? {} : { profileBaseUrl:
|
|
390
|
+
// `stored` is false only when the profile is at another instance.
|
|
391
|
+
...(stored ? {} : { profileBaseUrl: at })
|
|
279
392
|
};
|
|
280
393
|
opts.notify?.((messages.connected ?? defaultConnected)(facts));
|
|
281
394
|
|
|
282
395
|
return { outcome: 'connected', key: result.key, result, cached: false, stored };
|
|
283
396
|
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* The stranded-key recovery: what a tool CLI calls once when a request
|
|
400
|
+
* answered 401 with a key that `connectOnDemand` served from the cache
|
|
401
|
+
* (`cached: true`). Evicts that key from the selected profile (the hub
|
|
402
|
+
* revoked it, or a membership was swept), only when that profile is at
|
|
403
|
+
* this instance (a key of another instance is never touched), and runs the
|
|
404
|
+
* exchange again with the cache skipped. The caller retries its request
|
|
405
|
+
* once with the new key and fails on a second 401; `not_cloud` means there
|
|
406
|
+
* is nothing to recover with, and the caller fails with its 401 as it
|
|
407
|
+
* stands.
|
|
408
|
+
*/
|
|
409
|
+
export async function refreshConnectKey(opts: ConnectOnDemandOptions): Promise<ConnectOnDemandOutcome> {
|
|
410
|
+
assertToolNamespace(opts.tool);
|
|
411
|
+
const hub = selectHubProfile(opts.env, { cloudUrl: opts.hubCloudUrl });
|
|
412
|
+
const existing = loadConfig(opts.env, opts.tool).profiles[opts.profileName];
|
|
413
|
+
const { place } = placeOf(existing, normalizeUrl(opts.toolBaseUrl), opts.cloudUrl);
|
|
414
|
+
if (hub.name && place === 'here') removeConnectKey(opts.env, opts.tool, opts.profileName, hub.name);
|
|
415
|
+
return connectOnDemand({ ...opts, cached: false });
|
|
416
|
+
}
|
package/src/connect.ts
CHANGED
|
@@ -11,7 +11,9 @@ import { CliAuthError, type CliAuthMintedKey } from './auth.js';
|
|
|
11
11
|
* offline grant for live as-the-user hub reads). Both are OPAQUE to
|
|
12
12
|
* this client: relayed hub → tool, never inspected or stored. The
|
|
13
13
|
* exchange names NO organization (hub ADR 014): the credential
|
|
14
|
-
* identifies the user;
|
|
14
|
+
* identifies the user; the organization is a per-request parameter
|
|
15
|
+
* tool-side, the hub's server-side default unless `--org` says
|
|
16
|
+
* otherwise.
|
|
15
17
|
* 2. POST {tool}/api/v1/sso/cli-connect — NO auth header (the JWT is
|
|
16
18
|
* the credential), body `{ token, hubRefreshToken }` → the tool
|
|
17
19
|
* verifies, JIT-provisions, stores the grant, and mints an ordinary
|
|
@@ -20,9 +22,10 @@ import { CliAuthError, type CliAuthMintedKey } from './auth.js';
|
|
|
20
22
|
* The hub key is sent to the HUB only — it never reaches the tool. The
|
|
21
23
|
* hubRefreshToken is sent to the TOOL only (it is that tool's own grant;
|
|
22
24
|
* redeeming it needs the tool client's credentials) and never persisted by
|
|
23
|
-
* the CLI. The minted key is cached per (tool, hub profile) in the
|
|
24
|
-
* config namespace (config.ts `connectKeys`)
|
|
25
|
-
*
|
|
25
|
+
* the CLI. The minted key is cached per (tool profile, hub profile) in the
|
|
26
|
+
* tool's config namespace (config.ts `connectKeys`), on the profile of the
|
|
27
|
+
* instance it was minted for; day-to-day tool CLI calls then take the
|
|
28
|
+
* ordinary local API-key path with the hub out of the loop.
|
|
26
29
|
*/
|
|
27
30
|
|
|
28
31
|
export type CliConnectStage = 'hub' | 'tool';
|
|
@@ -34,6 +37,8 @@ export type CliConnectStage = 'hub' | 'tool';
|
|
|
34
37
|
* - hub 400 `unknown_resource` — the resource URL is not in the registry.
|
|
35
38
|
* - `needsHubLogin` — the hub rejected the KEY itself; re-run
|
|
36
39
|
* `antasphere login`.
|
|
40
|
+
* - `refusal` — the account was refused (403), not the key: print the
|
|
41
|
+
* server's own sentence and exit 3. No login fixes it.
|
|
37
42
|
* - `needsReExchange` — the tool rejected the 120 s JWT (expired/replayed
|
|
38
43
|
* in transit); run the exchange again.
|
|
39
44
|
* - tool 429 `rate_limited` — back off and retry.
|
|
@@ -66,6 +71,22 @@ export class CliConnectError extends CliAuthError {
|
|
|
66
71
|
get needsReExchange(): boolean {
|
|
67
72
|
return this.stage === 'tool' && this.status === 401;
|
|
68
73
|
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The person was refused, not the credential: a 403 that is not the
|
|
77
|
+
* scope case, from either side (`tool_access_denied` on a restricted
|
|
78
|
+
* tool, `unverified_email`, a grant the tool will not take). `message`
|
|
79
|
+
* is the server's own sentence; a runner prints it as is and exits
|
|
80
|
+
* `exitCode`, 3, the code the tool docs promise for a refusal.
|
|
81
|
+
*/
|
|
82
|
+
get refusal(): boolean {
|
|
83
|
+
return this.status === 403 && !this.needsHubLogin;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The exit code a runner uses for this error: 3 for a refusal, 1 otherwise. */
|
|
87
|
+
get exitCode(): number {
|
|
88
|
+
return this.refusal ? 3 : 1;
|
|
89
|
+
}
|
|
69
90
|
}
|
|
70
91
|
|
|
71
92
|
export interface CliToolTokenBody {
|
|
@@ -101,7 +122,7 @@ export interface CliConnected {
|
|
|
101
122
|
|
|
102
123
|
/** The flattened outcome of a full exchange. */
|
|
103
124
|
export interface CliConnectResult {
|
|
104
|
-
/** Full tool-local key — user-scoped: ONE per (tool, hub profile),
|
|
125
|
+
/** Full tool-local key — user-scoped: ONE per (tool profile, hub profile),
|
|
105
126
|
* valid for every org the user can select per request (config.ts). */
|
|
106
127
|
key: string;
|
|
107
128
|
apiKey: CliAuthMintedKey;
|
|
@@ -130,7 +151,7 @@ export class CliConnectClient {
|
|
|
130
151
|
this.fetchImpl = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
131
152
|
}
|
|
132
153
|
|
|
133
|
-
/** Step 1 — the hub mints the 120 s
|
|
154
|
+
/** Step 1 — the hub mints the 120 s user-scoped JWT for the tool's resource. */
|
|
134
155
|
toolToken(hubApiKey: string, body: CliToolTokenBody): Promise<CliToolTokenMinted> {
|
|
135
156
|
return this.call('hub', `${this.hubBaseUrl}/api/v1/sso/tool-token`, body, hubApiKey);
|
|
136
157
|
}
|