@antasphere/cli-core 0.2.0 → 0.4.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 +49 -22
- package/dist/config.d.ts +26 -24
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +20 -27
- package/dist/config.js.map +1 -1
- package/dist/connect-on-demand.d.ts +127 -0
- package/dist/connect-on-demand.d.ts.map +1 -0
- package/dist/connect-on-demand.js +149 -0
- package/dist/connect-on-demand.js.map +1 -0
- package/dist/connect.d.ts +43 -28
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +14 -8
- package/dist/connect.js.map +1 -1
- package/dist/index.d.ts +10 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -5
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/config.ts +38 -37
- package/src/connect-on-demand.ts +283 -0
- package/src/connect.ts +48 -31
- package/src/index.ts +19 -8
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertToolNamespace,
|
|
3
|
+
HUB_TOOL,
|
|
4
|
+
loadConfig,
|
|
5
|
+
lookupConnectKey,
|
|
6
|
+
saveConfig,
|
|
7
|
+
storeConnectKey
|
|
8
|
+
} from './config.js';
|
|
9
|
+
import type { CliEnv } from './config.js';
|
|
10
|
+
import { CliConnectClient, CliConnectError } from './connect.js';
|
|
11
|
+
import type { CliConnectResult } from './connect.js';
|
|
12
|
+
import { CliUsageError, DEFAULT_PROFILE, resolveProfile } from './resolve.js';
|
|
13
|
+
import type { ResolvedProfile } from './resolve.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Connect-on-demand (binding patterns §7, gcloud model), extracted from the
|
|
17
|
+
* Slideless CLI so every Antasphere tool CLI inherits "one `antasphere login`
|
|
18
|
+
* serves every federated tool". When a tool CLI resolves NO credential of its
|
|
19
|
+
* own, it calls `connectOnDemand`:
|
|
20
|
+
*
|
|
21
|
+
* 0. The cache is consulted FIRST: a key minted earlier for THIS instance
|
|
22
|
+
* under the active hub profile is USER-scoped (hub ADR 014) — it serves
|
|
23
|
+
* every subsequent run and every org, with zero network (no probe, no
|
|
24
|
+
* hub round-trip, no fresh mint).
|
|
25
|
+
* 1. On a miss, discovery gates the flow: only an Antasphere-CLOUD
|
|
26
|
+
* instance can mint keys through the hub (unauthenticated GET
|
|
27
|
+
* /instance — never guess). Unreachable, foreign, or self-hosted
|
|
28
|
+
* instances answer `not_cloud` and the tool raises its classic no-key
|
|
29
|
+
* error unchanged.
|
|
30
|
+
* 2. The hub credential is what `antasphere login` stored on the hub
|
|
31
|
+
* profile (the config home's HUB_TOOL namespace) — hub URL included,
|
|
32
|
+
* never a hardcoded constant here.
|
|
33
|
+
* 3. `CliConnectClient.exchange` trades it for a tool-local key (one
|
|
34
|
+
* re-exchange retry when the 120 s JWT dies in transit; a rejected hub
|
|
35
|
+
* credential maps to guided `antasphere login` advice).
|
|
36
|
+
* 4. The minted key is cached per (tool, HUB PROFILE) — but only onto a
|
|
37
|
+
* profile that names THIS instance (a fresh profile gets pinned to it);
|
|
38
|
+
* a profile pointing elsewhere never receives it.
|
|
39
|
+
*
|
|
40
|
+
* A new tool needs only its namespace, base URL, and copy — the flow itself
|
|
41
|
+
* is tool-agnostic.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/** What the unauthenticated discovery probe learned about a tool instance. */
|
|
45
|
+
export interface ToolInstanceProbe {
|
|
46
|
+
/** Whether the instance signs in through the Antasphere hub (advertises
|
|
47
|
+
* the `antasphere` auth method, or calls itself the cloud edition). */
|
|
48
|
+
cloud: boolean;
|
|
49
|
+
/** The instance's self-reported edition (`cloud`, `oss`, …). */
|
|
50
|
+
edition: string | undefined;
|
|
51
|
+
/** The advertised auth methods (open enum — unknown entries kept). */
|
|
52
|
+
authMethods: string[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Probe a tool instance's unauthenticated discovery endpoint
|
|
57
|
+
* (`GET {toolBaseUrl}/api/v1/instance`). Returns what it advertises, or
|
|
58
|
+
* `null` when the instance is unreachable or answers a foreign shape —
|
|
59
|
+
* callers treat both exactly like "not cloud".
|
|
60
|
+
*/
|
|
61
|
+
export async function probeToolInstance(
|
|
62
|
+
toolBaseUrl: string,
|
|
63
|
+
fetchImpl?: typeof globalThis.fetch
|
|
64
|
+
): Promise<ToolInstanceProbe | null> {
|
|
65
|
+
const f = fetchImpl ?? globalThis.fetch.bind(globalThis);
|
|
66
|
+
try {
|
|
67
|
+
const res = await f(`${toolBaseUrl.replace(/\/+$/, '')}/api/v1/instance`, {
|
|
68
|
+
method: 'GET',
|
|
69
|
+
// Discovery is served cacheable for CLI/MCP clients, but the probe
|
|
70
|
+
// must see live state (mirrors the tool SDKs' discovery call).
|
|
71
|
+
cache: 'no-store'
|
|
72
|
+
});
|
|
73
|
+
if (!res.ok) return null;
|
|
74
|
+
const info = (await res.json().catch(() => null)) as {
|
|
75
|
+
edition?: unknown;
|
|
76
|
+
auth?: { methods?: unknown };
|
|
77
|
+
} | null;
|
|
78
|
+
const methods = info?.auth?.methods;
|
|
79
|
+
if (!Array.isArray(methods)) return null;
|
|
80
|
+
const authMethods = methods.filter((m): m is string => typeof m === 'string');
|
|
81
|
+
const edition = typeof info?.edition === 'string' ? info.edition : undefined;
|
|
82
|
+
return { cloud: authMethods.includes('antasphere') || edition === 'cloud', edition, authMethods };
|
|
83
|
+
} catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The ACTIVE hub profile — what `antasphere login` stored. Its NAME is the
|
|
90
|
+
* connect cache's slot (config.ts `connectKeys`): stable per hub account on
|
|
91
|
+
* this machine, independent of any org selection.
|
|
92
|
+
*/
|
|
93
|
+
export function activeHubProfile(env: CliEnv): ResolvedProfile {
|
|
94
|
+
return resolveProfile(loadConfig(env, HUB_TOOL), undefined);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The facts the connected notice is rendered from (fresh exchanges only —
|
|
98
|
+
* cache hits are silent). */
|
|
99
|
+
export interface ConnectNoticeFacts {
|
|
100
|
+
/** The tool instance the exchange connected to (origin-normalized). */
|
|
101
|
+
toolBaseUrl: string;
|
|
102
|
+
/** The signed-in account's email at mint. */
|
|
103
|
+
email: string;
|
|
104
|
+
/** Whether the minted key was persisted for the next run (per
|
|
105
|
+
* (tool, hub profile)); false = the profile names another instance. */
|
|
106
|
+
stored: boolean;
|
|
107
|
+
/** The tool profile the cache targeted. */
|
|
108
|
+
profileName: string;
|
|
109
|
+
/** When NOT stored: the other instance the profile points at. */
|
|
110
|
+
profileBaseUrl?: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Branded copy seams. Defaults reproduce the Slideless CLI's strings modulo
|
|
115
|
+
* the tool name (the tool namespace stands in for the display name, and the
|
|
116
|
+
* key-env-var name is derived from it: `slideless` → `SLIDELESS_API_KEY`).
|
|
117
|
+
*/
|
|
118
|
+
export interface ConnectOnDemandMessages {
|
|
119
|
+
/** `CliUsageError` text when the instance is cloud but no `antasphere
|
|
120
|
+
* login` credential is stored on the hub profile. */
|
|
121
|
+
missingHubLogin?: string;
|
|
122
|
+
/** `CliUsageError` text when the hub rejected the stored credential. */
|
|
123
|
+
hubLoginRejected?: string;
|
|
124
|
+
/** Renders the success notice handed to `notify` (newline included). */
|
|
125
|
+
connected?: (facts: ConnectNoticeFacts) => string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function defaultMissingHubLogin(tool: string): string {
|
|
129
|
+
const envVar = `${tool.replace(/-/g, '_').toUpperCase()}_API_KEY`;
|
|
130
|
+
return (
|
|
131
|
+
`This ${tool} instance signs in through the Antasphere hub. Run \`antasphere login\` once, ` +
|
|
132
|
+
`then retry — or pass --api-key / set ${envVar}.`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const DEFAULT_HUB_LOGIN_REJECTED =
|
|
137
|
+
'The stored Antasphere hub credential was rejected — run `antasphere login` again, then retry.';
|
|
138
|
+
|
|
139
|
+
function defaultConnected(facts: ConnectNoticeFacts): string {
|
|
140
|
+
return (
|
|
141
|
+
`Connected to ${facts.toolBaseUrl} as ${facts.email} via Antasphere` +
|
|
142
|
+
(facts.stored
|
|
143
|
+
? ''
|
|
144
|
+
: ` — key NOT cached: profile '${facts.profileName}' points at ${facts.profileBaseUrl}`) +
|
|
145
|
+
'.\n'
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface ConnectOnDemandOptions {
|
|
150
|
+
/** The tool's config namespace (lowercase slug — it names the cache file). */
|
|
151
|
+
tool: string;
|
|
152
|
+
/** The resolved tool instance origin the CLI is talking to. */
|
|
153
|
+
toolBaseUrl: string;
|
|
154
|
+
/** The exchange resource URL; default = this instance's MCP endpoint,
|
|
155
|
+
* `${toolBaseUrl}/mcp` (the tool's registered resource URL). */
|
|
156
|
+
resource?: string;
|
|
157
|
+
/** The tool profile the minted key is cached on; default `DEFAULT_PROFILE`. */
|
|
158
|
+
profileName?: string;
|
|
159
|
+
env: CliEnv;
|
|
160
|
+
fetch?: typeof globalThis.fetch;
|
|
161
|
+
/** Receives the human "Connected to …" notice (tools wire their stderr);
|
|
162
|
+
* omitted = silent. Cache hits are always silent either way. */
|
|
163
|
+
notify?: (line: string) => void;
|
|
164
|
+
messages?: ConnectOnDemandMessages;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export type ConnectOnDemandOutcome =
|
|
168
|
+
| {
|
|
169
|
+
outcome: 'connected';
|
|
170
|
+
/** The tool-local key — this invocation's credential. */
|
|
171
|
+
key: string;
|
|
172
|
+
/** true = served from the (tool, hub profile) config cache: zero
|
|
173
|
+
* network, nothing minted. false = a fresh exchange ran. */
|
|
174
|
+
cached: boolean;
|
|
175
|
+
/** Fresh exchanges only: the full result (user, key meta). */
|
|
176
|
+
result?: CliConnectResult;
|
|
177
|
+
/** Fresh exchanges only: whether the minted key was persisted for
|
|
178
|
+
* the next run (false = the profile names another instance). */
|
|
179
|
+
stored?: boolean;
|
|
180
|
+
}
|
|
181
|
+
| {
|
|
182
|
+
/** Not an Antasphere-cloud instance (or unreachable): the caller
|
|
183
|
+
* raises its classic no-key error unchanged. */
|
|
184
|
+
outcome: 'not_cloud';
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The hub → tool exchange fallback a tool CLI runs when no direct credential
|
|
189
|
+
* resolved. Serves the cached user-scoped key when one exists for this
|
|
190
|
+
* (instance, hub profile); otherwise returns `not_cloud` when the instance
|
|
191
|
+
* is not an Antasphere-cloud one (or unreachable). Throws a guided
|
|
192
|
+
* `CliUsageError` when the instance IS cloud but the hub login is missing or
|
|
193
|
+
* rejected; every other exchange failure propagates as its `CliConnectError`.
|
|
194
|
+
*/
|
|
195
|
+
export async function connectOnDemand(opts: ConnectOnDemandOptions): Promise<ConnectOnDemandOutcome> {
|
|
196
|
+
assertToolNamespace(opts.tool);
|
|
197
|
+
const toolBaseUrl = opts.toolBaseUrl.replace(/\/+$/, '');
|
|
198
|
+
const messages = opts.messages ?? {};
|
|
199
|
+
const profileName = opts.profileName ?? DEFAULT_PROFILE;
|
|
200
|
+
|
|
201
|
+
// 0. The cache: a key minted earlier by the active hub profile is
|
|
202
|
+
// user-scoped — it serves every run and every org (hub ADR 014; the
|
|
203
|
+
// org is a per-request parameter, never part of the credential). Only
|
|
204
|
+
// a profile that names THIS instance is trusted: a cached key never
|
|
205
|
+
// travels to a different instance. A hit costs zero network.
|
|
206
|
+
const hub = activeHubProfile(opts.env);
|
|
207
|
+
const existing = loadConfig(opts.env, opts.tool).profiles[profileName];
|
|
208
|
+
const pinnedHere = existing?.baseUrl?.replace(/\/+$/, '') === toolBaseUrl;
|
|
209
|
+
if (pinnedHere && hub.name) {
|
|
210
|
+
const cachedKey = lookupConnectKey(existing, hub.name);
|
|
211
|
+
if (cachedKey) return { outcome: 'connected', key: cachedKey, cached: true };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// 1. Only a CLOUD instance can mint keys through the hub — ask discovery,
|
|
215
|
+
// never guess. An unreachable or foreign instance is `not_cloud`.
|
|
216
|
+
const probe = await probeToolInstance(toolBaseUrl, opts.fetch);
|
|
217
|
+
if (!probe?.cloud) return { outcome: 'not_cloud' };
|
|
218
|
+
|
|
219
|
+
// 2. The hub credential is what `antasphere login` stored on the hub
|
|
220
|
+
// 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
|
+
const hubKey = hub.profile?.apiKey;
|
|
223
|
+
const hubBaseUrl = hub.profile?.baseUrl;
|
|
224
|
+
if (!hubKey || !hubBaseUrl || !hub.name) {
|
|
225
|
+
throw new CliUsageError(messages.missingHubLogin ?? defaultMissingHubLogin(opts.tool));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// 3. The exchange resource defaults to THIS instance's registered resource
|
|
229
|
+
// URL — its MCP endpoint, derived from the resolved instance origin.
|
|
230
|
+
// The body names NO org: the exchange is user-scoped end to end.
|
|
231
|
+
const resource = opts.resource ?? `${toolBaseUrl}/mcp`;
|
|
232
|
+
const connect = new CliConnectClient({
|
|
233
|
+
hubBaseUrl,
|
|
234
|
+
toolBaseUrl,
|
|
235
|
+
...(opts.fetch ? { fetch: opts.fetch } : {})
|
|
236
|
+
});
|
|
237
|
+
const body = { resource };
|
|
238
|
+
let result: CliConnectResult;
|
|
239
|
+
try {
|
|
240
|
+
result = await connect.exchange(hubKey, body);
|
|
241
|
+
} catch (e) {
|
|
242
|
+
if (e instanceof CliConnectError && e.needsReExchange) {
|
|
243
|
+
// The 120 s JWT died in transit (expired/replayed) — one fresh
|
|
244
|
+
// exchange; a second rejection propagates.
|
|
245
|
+
result = await connect.exchange(hubKey, body);
|
|
246
|
+
} else if (e instanceof CliConnectError && e.needsHubLogin) {
|
|
247
|
+
throw new CliUsageError(messages.hubLoginRejected ?? DEFAULT_HUB_LOGIN_REJECTED);
|
|
248
|
+
} else {
|
|
249
|
+
throw e;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// 4. Cache the minted key per (tool, hub profile) — but only onto a
|
|
254
|
+
// profile that names THIS instance (or a fresh one, which gets pinned
|
|
255
|
+
// to it), never onto a profile pointing somewhere else.
|
|
256
|
+
const stored = !existing?.baseUrl || pinnedHere;
|
|
257
|
+
if (stored) {
|
|
258
|
+
storeConnectKey(opts.env, opts.tool, profileName, hub.name, {
|
|
259
|
+
apiKey: result.key,
|
|
260
|
+
email: result.user.email,
|
|
261
|
+
mintedAt: new Date().toISOString()
|
|
262
|
+
});
|
|
263
|
+
if (!existing?.baseUrl) {
|
|
264
|
+
// First contact: pin the profile to this instance so the next
|
|
265
|
+
// invocation resolves the URL and the cached key with no flags.
|
|
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
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const facts: ConnectNoticeFacts = {
|
|
273
|
+
toolBaseUrl,
|
|
274
|
+
email: result.user.email,
|
|
275
|
+
stored,
|
|
276
|
+
profileName,
|
|
277
|
+
// `stored` is false only when the profile names another instance.
|
|
278
|
+
...(stored ? {} : { profileBaseUrl: existing?.baseUrl })
|
|
279
|
+
};
|
|
280
|
+
opts.notify?.((messages.connected ?? defaultConnected)(facts));
|
|
281
|
+
|
|
282
|
+
return { outcome: 'connected', key: result.key, result, cached: false, stored };
|
|
283
|
+
}
|
package/src/connect.ts
CHANGED
|
@@ -6,17 +6,23 @@ import { CliAuthError, type CliAuthMintedKey } from './auth.js';
|
|
|
6
6
|
* API key is traded for a tool-local key in two calls:
|
|
7
7
|
*
|
|
8
8
|
* 1. POST {hub}/api/v1/sso/tool-token — auth = hub API key (Bearer),
|
|
9
|
-
* body `{ resource
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* body `{ resource }` → a USER-scoped, 120-second JWT for the tool's
|
|
10
|
+
* resource URL plus a one-time `hubRefreshToken` (the tool's standing
|
|
11
|
+
* offline grant for live as-the-user hub reads). Both are OPAQUE to
|
|
12
|
+
* this client: relayed hub → tool, never inspected or stored. The
|
|
13
|
+
* exchange names NO organization (hub ADR 014): the credential
|
|
14
|
+
* identifies the user; any org is a per-request selection tool-side.
|
|
12
15
|
* 2. POST {tool}/api/v1/sso/cli-connect — NO auth header (the JWT is
|
|
13
|
-
* the credential), body `{ token }` → the tool
|
|
14
|
-
*
|
|
16
|
+
* the credential), body `{ token, hubRefreshToken }` → the tool
|
|
17
|
+
* verifies, JIT-provisions, stores the grant, and mints an ordinary
|
|
18
|
+
* USER-scoped tool-local API key, returned exactly once.
|
|
15
19
|
*
|
|
16
20
|
* The hub key is sent to the HUB only — it never reaches the tool. The
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
21
|
+
* hubRefreshToken is sent to the TOOL only (it is that tool's own grant;
|
|
22
|
+
* 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 tool's
|
|
24
|
+
* config namespace (config.ts `connectKeys`); day-to-day tool CLI calls
|
|
25
|
+
* then take the ordinary local API-key path with the hub out of the loop.
|
|
20
26
|
*/
|
|
21
27
|
|
|
22
28
|
export type CliConnectStage = 'hub' | 'tool';
|
|
@@ -26,8 +32,6 @@ export type CliConnectStage = 'hub' | 'tool';
|
|
|
26
32
|
* pick the remedy without string-matching:
|
|
27
33
|
*
|
|
28
34
|
* - hub 400 `unknown_resource` — the resource URL is not in the registry.
|
|
29
|
-
* - hub 403 `no_membership` / `workspace_suspended` — the target org is
|
|
30
|
-
* not exchangeable for this user; pick another org.
|
|
31
35
|
* - `needsHubLogin` — the hub rejected the KEY itself; re-run
|
|
32
36
|
* `antasphere login`.
|
|
33
37
|
* - `needsReExchange` — the tool rejected the 120 s JWT (expired/replayed
|
|
@@ -65,20 +69,24 @@ export class CliConnectError extends CliAuthError {
|
|
|
65
69
|
}
|
|
66
70
|
|
|
67
71
|
export interface CliToolTokenBody {
|
|
68
|
-
/** The tool's registry resource URL (e.g. `https://app.slideless.ai/mcp`).
|
|
72
|
+
/** The tool's registry resource URL (e.g. `https://app.slideless.ai/mcp`).
|
|
73
|
+
* The ONLY field — the exchange is user-scoped and names no org
|
|
74
|
+
* (hub contract `ssoToolTokenRequestSchema`, ADR 014). */
|
|
69
75
|
resource: string;
|
|
70
|
-
/** Target hub org — any live ACTIVE membership of the user (gcloud
|
|
71
|
-
* model); omit = the hub key's bound workspace. */
|
|
72
|
-
workspaceId?: string;
|
|
73
76
|
}
|
|
74
77
|
|
|
78
|
+
/** The hub's tool-token response (hub contract `ssoToolTokenSchema`). */
|
|
75
79
|
export interface CliToolTokenMinted {
|
|
76
|
-
/** The
|
|
80
|
+
/** The user-scoped exchange JWT — opaque, ~120 s; relay it, never keep it. */
|
|
77
81
|
token: string;
|
|
78
82
|
/** ISO expiry of the JWT (informational; the minted key outlives it). */
|
|
79
83
|
expiresAt: string;
|
|
80
|
-
/** The
|
|
81
|
-
|
|
84
|
+
/** The raw one-time offline-grant refresh token for the TARGET TOOL —
|
|
85
|
+
* relayed to that tool's /sso/cli-connect exactly once (it stores it as
|
|
86
|
+
* the standing grant for live as-the-user hub reads) and NEVER persisted
|
|
87
|
+
* or logged by the CLI. Without it, a fresh CLI user's connect is
|
|
88
|
+
* refused tool-side (`hub_grant_missing` — no key is minted born-dead). */
|
|
89
|
+
hubRefreshToken: string;
|
|
82
90
|
}
|
|
83
91
|
|
|
84
92
|
export interface CliConnected {
|
|
@@ -86,20 +94,21 @@ export interface CliConnected {
|
|
|
86
94
|
key: string;
|
|
87
95
|
apiKey: CliAuthMintedKey;
|
|
88
96
|
user: { id: string; email: string; name: string };
|
|
89
|
-
/** The tool
|
|
90
|
-
|
|
97
|
+
/** The tool workspace the key is PINNED to — null on the live contract
|
|
98
|
+
* (user-scoped, unpinned; the org is a per-request parameter). */
|
|
99
|
+
workspaceId: string | null;
|
|
91
100
|
}
|
|
92
101
|
|
|
93
|
-
/** The flattened outcome of a full exchange
|
|
102
|
+
/** The flattened outcome of a full exchange. */
|
|
94
103
|
export interface CliConnectResult {
|
|
95
|
-
/** Full tool-local key —
|
|
104
|
+
/** Full tool-local key — user-scoped: ONE per (tool, hub profile),
|
|
105
|
+
* valid for every org the user can select per request (config.ts). */
|
|
96
106
|
key: string;
|
|
97
107
|
apiKey: CliAuthMintedKey;
|
|
98
108
|
user: { id: string; email: string; name: string };
|
|
99
|
-
/** The
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
toolWorkspaceId: string;
|
|
109
|
+
/** The tool workspace the key is pinned to — null = unpinned (the live
|
|
110
|
+
* contract always answers null; informational). */
|
|
111
|
+
toolWorkspaceId: string | null;
|
|
103
112
|
}
|
|
104
113
|
|
|
105
114
|
export interface CliConnectClientOptions {
|
|
@@ -126,20 +135,28 @@ export class CliConnectClient {
|
|
|
126
135
|
return this.call('hub', `${this.hubBaseUrl}/api/v1/sso/tool-token`, body, hubApiKey);
|
|
127
136
|
}
|
|
128
137
|
|
|
129
|
-
/**
|
|
130
|
-
|
|
131
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Step 2 — the tool verifies the JWT (its only credential) and mints its
|
|
140
|
+
* key. `hubRefreshToken` is the SAME response's offline grant, relayed so
|
|
141
|
+
* the tool can store it (a grant-less connect is refused for users with
|
|
142
|
+
* none already stored — `hub_grant_missing`).
|
|
143
|
+
*/
|
|
144
|
+
cliConnect(token: string, hubRefreshToken?: string): Promise<CliConnected> {
|
|
145
|
+
return this.call('tool', `${this.toolBaseUrl}/api/v1/sso/cli-connect`, {
|
|
146
|
+
token,
|
|
147
|
+
...(hubRefreshToken ? { hubRefreshToken } : {})
|
|
148
|
+
});
|
|
132
149
|
}
|
|
133
150
|
|
|
134
|
-
/** The full two-call exchange. The hub key goes to the hub ONLY
|
|
151
|
+
/** The full two-call exchange. The hub key goes to the hub ONLY; the
|
|
152
|
+
* offline grant goes to the TOOL only — neither is ever persisted here. */
|
|
135
153
|
async exchange(hubApiKey: string, body: CliToolTokenBody): Promise<CliConnectResult> {
|
|
136
154
|
const minted = await this.toolToken(hubApiKey, body);
|
|
137
|
-
const connected = await this.cliConnect(minted.token);
|
|
155
|
+
const connected = await this.cliConnect(minted.token, minted.hubRefreshToken);
|
|
138
156
|
return {
|
|
139
157
|
key: connected.key,
|
|
140
158
|
apiKey: connected.apiKey,
|
|
141
159
|
user: connected.user,
|
|
142
|
-
hubWorkspaceId: minted.workspaceId,
|
|
143
160
|
toolWorkspaceId: connected.workspaceId
|
|
144
161
|
};
|
|
145
162
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
* builds on (hub-architecture §8): the `~/.config/antasphere/` config home
|
|
4
4
|
* with tool-namespaced profiles, the flag → env → profile → default URL
|
|
5
5
|
* resolution order, the instance email-OTP auth client, the hub→tool
|
|
6
|
-
* cross-tool connect exchange (+ its per-(tool,
|
|
7
|
-
* hub-profile org context
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* cross-tool connect exchange (+ its per-(tool, hub profile) user-scoped
|
|
7
|
+
* key cache, the hub-profile org context, and the connect-on-demand seam a
|
|
8
|
+
* tool CLI runs when no direct credential resolved), and the CliIo prompt
|
|
9
|
+
* seam. Tool
|
|
10
|
+
* commands, wire types, and API surfaces live in the tools; this package
|
|
11
|
+
* carries only config/login plumbing, never secrets.
|
|
10
12
|
*/
|
|
11
13
|
|
|
12
14
|
export {
|
|
@@ -15,14 +17,14 @@ export {
|
|
|
15
17
|
configPath,
|
|
16
18
|
HUB_TOOL,
|
|
17
19
|
loadConfig,
|
|
18
|
-
|
|
20
|
+
lookupConnectKey,
|
|
19
21
|
parseConfig,
|
|
20
22
|
redactKey,
|
|
21
|
-
|
|
23
|
+
removeConnectKey,
|
|
22
24
|
saveConfig,
|
|
23
|
-
|
|
25
|
+
storeConnectKey
|
|
24
26
|
} from './config.js';
|
|
25
|
-
export type { CliConfig, CliEnv, CliProfile
|
|
27
|
+
export type { CliConfig, CliConnectKey, CliEnv, CliProfile } from './config.js';
|
|
26
28
|
|
|
27
29
|
export { ask, printJson } from './io.js';
|
|
28
30
|
export type { CliIo } from './io.js';
|
|
@@ -51,6 +53,15 @@ export type {
|
|
|
51
53
|
CliToolTokenMinted
|
|
52
54
|
} from './connect.js';
|
|
53
55
|
|
|
56
|
+
export { activeHubProfile, connectOnDemand, probeToolInstance } from './connect-on-demand.js';
|
|
57
|
+
export type {
|
|
58
|
+
ConnectNoticeFacts,
|
|
59
|
+
ConnectOnDemandMessages,
|
|
60
|
+
ConnectOnDemandOptions,
|
|
61
|
+
ConnectOnDemandOutcome,
|
|
62
|
+
ToolInstanceProbe
|
|
63
|
+
} from './connect-on-demand.js';
|
|
64
|
+
|
|
54
65
|
export { getActiveWorkspace, resolveTargetWorkspace, setActiveWorkspace } from './org.js';
|
|
55
66
|
export type { OrgContextOptions, ResolveTargetWorkspaceOptions } from './org.js';
|
|
56
67
|
|