@antasphere/cli-core 0.5.0 → 0.7.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 +324 -85
- package/dist/auth.d.ts +1 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/config.d.ts +120 -62
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +207 -91
- package/dist/config.js.map +1 -1
- package/dist/connect-on-demand.d.ts +196 -99
- package/dist/connect-on-demand.d.ts.map +1 -1
- package/dist/connect-on-demand.js +565 -117
- package/dist/connect-on-demand.js.map +1 -1
- package/dist/connect.d.ts +8 -8
- package/dist/connect.d.ts.map +1 -1
- package/dist/index.d.ts +25 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -15
- package/dist/index.js.map +1 -1
- package/dist/kept-keys.d.ts +208 -0
- package/dist/kept-keys.d.ts.map +1 -0
- package/dist/kept-keys.js +484 -0
- package/dist/kept-keys.js.map +1 -0
- package/dist/lock.d.ts +65 -0
- package/dist/lock.d.ts.map +1 -0
- package/dist/lock.js +408 -0
- package/dist/lock.js.map +1 -0
- package/dist/migrate.d.ts +9 -3
- package/dist/migrate.d.ts.map +1 -1
- package/dist/migrate.js +32 -11
- package/dist/migrate.js.map +1 -1
- package/dist/resolve.d.ts +12 -4
- package/dist/resolve.d.ts.map +1 -1
- package/dist/resolve.js +12 -8
- package/dist/resolve.js.map +1 -1
- package/dist/sign-in.d.ts +266 -0
- package/dist/sign-in.d.ts.map +1 -0
- package/dist/sign-in.js +398 -0
- package/dist/sign-in.js.map +1 -0
- package/package.json +1 -1
- package/src/auth.ts +1 -1
- package/src/config.ts +303 -136
- package/src/connect-on-demand.ts +801 -223
- package/src/connect.ts +8 -8
- package/src/index.ts +42 -26
- package/src/kept-keys.ts +561 -0
- package/src/lock.ts +479 -0
- package/src/migrate.ts +33 -11
- package/src/resolve.ts +20 -15
- package/src/sign-in.ts +601 -0
package/src/kept-keys.ts
ADDED
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
import { loadConfig, updateConfig } from './config.js';
|
|
2
|
+
import type { CliConfig, CliConnectKey, CliEnv, CliHubKey, CliProfile, CliSiteFacts } from './config.js';
|
|
3
|
+
import { firstSet, profileUrl, sameInstance } from './resolve.js';
|
|
4
|
+
import type { CliLogin } from './sign-in.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The keys a tool profile keeps (PRDCT-5192). A KEPT KEY is filed by the
|
|
8
|
+
* hub's address and the account's e-mail (`hubKeys`), never by a login's
|
|
9
|
+
* name, so renaming a login, or signing out and in again with the same
|
|
10
|
+
* account, strands nothing.
|
|
11
|
+
*
|
|
12
|
+
* - One key per `(hub, e-mail)`: the newest `mintedAt` is kept; another one
|
|
13
|
+
* for the same pair is STALE (an undated one is the oldest).
|
|
14
|
+
* - A kept key whose login is GONE (no login at its hub carrying its e-mail
|
|
15
|
+
* nor carrying none: nobody is signed in there, or only other accounts
|
|
16
|
+
* are) is STALE; so is one whose hub is not the hub the site names, when
|
|
17
|
+
* the site names one. A login that comes back mints a new key.
|
|
18
|
+
* - The OLD shape (`connectKeys[<login name>]`) is moved once, by the first
|
|
19
|
+
* command that resolves a credential on the profile: when the login of
|
|
20
|
+
* that name is in place and carries the entry's e-mail, the entry moves to
|
|
21
|
+
* `hubKeys` under that login's hub; otherwise it is STALE.
|
|
22
|
+
* - Unknown is not gone: while the hub file holds an entry with a key whose
|
|
23
|
+
* hub cannot be told (`undecidedLogins`), no key is judged gone for want of
|
|
24
|
+
* a login, an old slot no login matches included (`judgeGone: false`); a
|
|
25
|
+
* key of another hub than the site's and a duplicate stay stale. Nor is a
|
|
26
|
+
* key that carries an e-mail while a login at its hub carries none: that
|
|
27
|
+
* login may be its account. It is never served to that login (the exchange
|
|
28
|
+
* runs and tells the address, and from then on the rules above settle the
|
|
29
|
+
* key), and an old slot whose login of that name carries no e-mail stays
|
|
30
|
+
* where it is, neither moved nor stale, until the address is known. A key
|
|
31
|
+
* that carries no e-mail is matched by no login.
|
|
32
|
+
*
|
|
33
|
+
* A stale key is revoked on the profile's own site, then forgotten; until its
|
|
34
|
+
* revoke succeeds it stays where it is in the file, never served. A stale key
|
|
35
|
+
* another profile at that same site still holds is never revoked there: it is
|
|
36
|
+
* forgotten on this profile only (`keysHeldElsewhere`). A key the profile
|
|
37
|
+
* keeps, or its own key, is never stale whatever else names it.
|
|
38
|
+
* Everything here that writes is one `updateConfig`; nothing here touches the
|
|
39
|
+
* network.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/** A value of the file as a trimmed string, or undefined when it is not a non-blank string. */
|
|
43
|
+
export function text(value: unknown): string | undefined {
|
|
44
|
+
return typeof value === 'string' ? firstSet(value) : undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** An e-mail in its comparable form: trimmed and case-folded; undefined when blank or absent. */
|
|
48
|
+
export function foldEmail(email: unknown): string | undefined {
|
|
49
|
+
return text(email)?.toLowerCase();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Whether two e-mails are one address: both present, equal once trimmed and case-folded. */
|
|
53
|
+
export function sameEmail(a: unknown, b: unknown): boolean {
|
|
54
|
+
const x = foldEmail(a);
|
|
55
|
+
return x !== undefined && x === foldEmail(b);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A mint's time for comparisons: its epoch ms, or -Infinity when absent or unreadable (the oldest). */
|
|
59
|
+
export function mintTime(mintedAt: unknown): number {
|
|
60
|
+
const t = typeof mintedAt === 'string' ? Date.parse(mintedAt) : Number.NaN;
|
|
61
|
+
return Number.isNaN(t) ? -Infinity : t;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The newest of some kept keys (ties go to the first), or undefined. */
|
|
65
|
+
export function newestKey(keys: CliHubKey[]): CliHubKey | undefined {
|
|
66
|
+
let best: CliHubKey | undefined;
|
|
67
|
+
for (const key of keys) if (!best || mintTime(key.mintedAt) > mintTime(best.mintedAt)) best = key;
|
|
68
|
+
return best;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Whether two kept keys fill one slot: one hub (`sameInstance`), one e-mail (case-folded). */
|
|
72
|
+
export function sameSlot(a: { hub: string; email: string }, b: { hub: string; email: string }): boolean {
|
|
73
|
+
return sameInstance(a.hub, b.hub) && sameEmail(a.email, b.email);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Whether the machine cannot tell that a login is not the account of a key's e-mail: the login
|
|
78
|
+
* carries that e-mail, or it carries none while the key carries one (« cannot tell »). A key that
|
|
79
|
+
* carries no e-mail is no login's.
|
|
80
|
+
*/
|
|
81
|
+
function mayBeItsLogin(login: CliLogin, email: unknown): boolean {
|
|
82
|
+
return (
|
|
83
|
+
sameEmail(login.email, email) || (foldEmail(login.email) === undefined && foldEmail(email) !== undefined)
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
88
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The profile's kept keys that read as keys, in the file's order (`mintedAt` '' when absent). */
|
|
92
|
+
export function hubKeysOf(profile: CliProfile | undefined): CliHubKey[] {
|
|
93
|
+
const raw: unknown = profile?.hubKeys;
|
|
94
|
+
if (!Array.isArray(raw)) return [];
|
|
95
|
+
const keys: CliHubKey[] = [];
|
|
96
|
+
for (const k of raw as unknown[]) {
|
|
97
|
+
if (!isObject(k) || !text(k.apiKey) || typeof k.hub !== 'string' || typeof k.email !== 'string') continue;
|
|
98
|
+
keys.push({
|
|
99
|
+
hub: k.hub,
|
|
100
|
+
email: k.email,
|
|
101
|
+
apiKey: k.apiKey as string,
|
|
102
|
+
mintedAt: typeof k.mintedAt === 'string' ? k.mintedAt : ''
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return keys;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The profile's old slots that hold a key, in the file's order. */
|
|
109
|
+
function oldSlotsOf(profile: CliProfile | undefined): Array<[string, CliConnectKey]> {
|
|
110
|
+
const raw: unknown = profile?.connectKeys;
|
|
111
|
+
if (!isObject(raw)) return [];
|
|
112
|
+
const slots: Array<[string, CliConnectKey]> = [];
|
|
113
|
+
for (const [slot, entry] of Object.entries(raw)) {
|
|
114
|
+
if (!isObject(entry) || !text(entry.apiKey)) continue;
|
|
115
|
+
slots.push([
|
|
116
|
+
slot,
|
|
117
|
+
{
|
|
118
|
+
apiKey: entry.apiKey as string,
|
|
119
|
+
...(typeof entry.email === 'string' ? { email: entry.email } : {}),
|
|
120
|
+
...(typeof entry.mintedAt === 'string' ? { mintedAt: entry.mintedAt } : {})
|
|
121
|
+
}
|
|
122
|
+
]);
|
|
123
|
+
}
|
|
124
|
+
return slots;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** What the one move of the old slots does to a profile. */
|
|
128
|
+
interface MovePlan {
|
|
129
|
+
/** The old slots that move to `hubKeys`, with the kept key each becomes. */
|
|
130
|
+
moved: Array<{ slot: string; key: CliHubKey }>;
|
|
131
|
+
/** The old slots that hold a key `hubKeys` already keeps: removed, never revoked. */
|
|
132
|
+
dropped: string[];
|
|
133
|
+
/** The old slots left in place, stale: no login of that name with that address, or an older key
|
|
134
|
+
* for a slot that has a newer one (hub '' when they had none). */
|
|
135
|
+
stays: CliHubKey[];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The one move of the old slots, decided without writing: an entry whose
|
|
140
|
+
* login (by name) is in place and carries its e-mail becomes `{ hub: <that
|
|
141
|
+
* login's hub>, email, apiKey, mintedAt: entry.mintedAt ?? now }`, unless the
|
|
142
|
+
* slot already holds a key at least as new (an undated entry is the oldest);
|
|
143
|
+
* an entry with an e-mail whose login of that name carries none stays where
|
|
144
|
+
* it is, neither moved nor stale (« cannot tell »); every other entry stays,
|
|
145
|
+
* stale, except one no login matches while `judgeGone` is false, which
|
|
146
|
+
* stays, not stale.
|
|
147
|
+
*/
|
|
148
|
+
function planMove(profile: CliProfile, logins: CliLogin[], now: string, judgeGone = true): MovePlan {
|
|
149
|
+
const plan: MovePlan = { moved: [], dropped: [], stays: [] };
|
|
150
|
+
const current: Array<{ key: CliHubKey; time: number }> = hubKeysOf(profile).map((key) => ({
|
|
151
|
+
key,
|
|
152
|
+
time: mintTime(key.mintedAt)
|
|
153
|
+
}));
|
|
154
|
+
for (const [slot, entry] of oldSlotsOf(profile)) {
|
|
155
|
+
if (current.some((c) => c.key.apiKey === entry.apiKey)) {
|
|
156
|
+
plan.dropped.push(slot);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const login = logins.find((l) => l.name === slot);
|
|
160
|
+
const email = text(entry.email) ?? '';
|
|
161
|
+
// Its login carries no address: it may be this slot's account. It waits for the address.
|
|
162
|
+
if (login && foldEmail(login.email) === undefined && email) continue;
|
|
163
|
+
if (!login || !sameEmail(login.email, email)) {
|
|
164
|
+
// No login of that name carrying its address: gone, when the machine can tell.
|
|
165
|
+
if (judgeGone) {
|
|
166
|
+
plan.stays.push({ hub: '', email, apiKey: entry.apiKey, mintedAt: entry.mintedAt ?? '' });
|
|
167
|
+
}
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const key: CliHubKey = { hub: login.hub, email, apiKey: entry.apiKey, mintedAt: entry.mintedAt ?? now };
|
|
171
|
+
let rival: { key: CliHubKey; time: number } | undefined;
|
|
172
|
+
for (const c of current) if (sameSlot(c.key, key) && (!rival || c.time > rival.time)) rival = c;
|
|
173
|
+
if (rival && !(mintTime(entry.mintedAt) > rival.time)) {
|
|
174
|
+
plan.stays.push({ ...key, mintedAt: entry.mintedAt ?? '' });
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
current.push({ key, time: mintTime(key.mintedAt) });
|
|
178
|
+
plan.moved.push({ slot, key });
|
|
179
|
+
}
|
|
180
|
+
return plan;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** A profile's kept keys sorted out: what may serve or wait, and what the next command revokes. */
|
|
184
|
+
export interface KeySort {
|
|
185
|
+
/** One per (hub, e-mail), none stale: each servable to its account while its login is in place
|
|
186
|
+
* (with `judgeGone` false, one whose login is not in place too, and one whose hub has a login
|
|
187
|
+
* that carries no e-mail: never served, never revoked). */
|
|
188
|
+
kept: CliHubKey[];
|
|
189
|
+
/** What the next command on the profile revokes on its site, then forgets (old slots included). */
|
|
190
|
+
stale: CliHubKey[];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Sort a profile's keys out against the logins in place and the hub the site
|
|
195
|
+
* names (`siteHub`: a string when it names one, null or undefined when it
|
|
196
|
+
* names none or was never reached), as they will stand once the old slots
|
|
197
|
+
* are moved. `judgeGone: false` (the hub file holds an entry whose hub cannot
|
|
198
|
+
* be told) judges no key gone for want of a login. Pure: nothing is written.
|
|
199
|
+
*/
|
|
200
|
+
export function sortKeys(
|
|
201
|
+
profile: CliProfile | undefined,
|
|
202
|
+
logins: CliLogin[],
|
|
203
|
+
siteHub: string | null | undefined,
|
|
204
|
+
now: string,
|
|
205
|
+
opts: { judgeGone?: boolean } = {}
|
|
206
|
+
): KeySort {
|
|
207
|
+
if (!profile) return { kept: [], stale: [] };
|
|
208
|
+
const judgeGone = opts.judgeGone !== false;
|
|
209
|
+
const move = planMove(profile, logins, now, judgeGone);
|
|
210
|
+
const winners: CliHubKey[] = [];
|
|
211
|
+
const stale: CliHubKey[] = [...move.stays];
|
|
212
|
+
for (const key of [...hubKeysOf(profile), ...move.moved.map((m) => m.key)]) {
|
|
213
|
+
const i = winners.findIndex((w) => sameSlot(w, key));
|
|
214
|
+
if (i < 0) winners.push(key);
|
|
215
|
+
else if (mintTime(key.mintedAt) > mintTime(winners[i]!.mintedAt)) {
|
|
216
|
+
stale.push(winners[i]!);
|
|
217
|
+
winners[i] = key;
|
|
218
|
+
} else stale.push(key);
|
|
219
|
+
}
|
|
220
|
+
const kept: CliHubKey[] = [];
|
|
221
|
+
for (const key of winners) {
|
|
222
|
+
const otherHub = typeof siteHub === 'string' && !sameInstance(key.hub, siteHub);
|
|
223
|
+
// Gone: no login at its hub that may be its account (one carrying its address, or carrying none),
|
|
224
|
+
// whether nobody is signed in there or only other accounts are; judged only when every keyed
|
|
225
|
+
// entry of the hub file has a hub to tell.
|
|
226
|
+
const gone =
|
|
227
|
+
judgeGone && !logins.some((l) => sameInstance(l.hub, key.hub) && mayBeItsLogin(l, key.email));
|
|
228
|
+
if (otherHub || gone) stale.push(key);
|
|
229
|
+
else kept.push(key);
|
|
230
|
+
}
|
|
231
|
+
// A key the profile keeps, or its own key, is never revoked, whatever else names it.
|
|
232
|
+
const keep = new Set(kept.map((k) => k.apiKey));
|
|
233
|
+
const own = text(profile.apiKey);
|
|
234
|
+
if (own) keep.add(own);
|
|
235
|
+
const seen = new Set<string>();
|
|
236
|
+
return {
|
|
237
|
+
kept,
|
|
238
|
+
stale: stale.filter((k) => {
|
|
239
|
+
const key = k.apiKey.trim();
|
|
240
|
+
if (keep.has(k.apiKey) || keep.has(key) || seen.has(key)) return false;
|
|
241
|
+
seen.add(key);
|
|
242
|
+
return true;
|
|
243
|
+
})
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** Whether a profile holds old slots (or an empty old map) the move would touch. */
|
|
248
|
+
export function hasOldSlots(profile: CliProfile | undefined): boolean {
|
|
249
|
+
return profile?.connectKeys !== undefined;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The one move of the old slots of a profile, under the file's lock (the
|
|
254
|
+
* logins as `listLogins` read them): the matched slots move to `hubKeys`, a
|
|
255
|
+
* slot that duplicates a kept key is dropped, the stale ones stay until their
|
|
256
|
+
* revoke, and `connectKeys` goes once empty. A second run changes nothing and
|
|
257
|
+
* writes nothing. The caller has checked the profile is at its site.
|
|
258
|
+
*/
|
|
259
|
+
export function moveOldSlots(
|
|
260
|
+
env: CliEnv,
|
|
261
|
+
tool: string,
|
|
262
|
+
profileName: string,
|
|
263
|
+
logins: CliLogin[],
|
|
264
|
+
now: string
|
|
265
|
+
): boolean {
|
|
266
|
+
return updateConfig(env, tool, (config) => {
|
|
267
|
+
const profile = config.profiles[profileName];
|
|
268
|
+
const raw: unknown = profile?.connectKeys;
|
|
269
|
+
if (!profile || !isObject(raw)) return;
|
|
270
|
+
const move = planMove(profile, logins, now);
|
|
271
|
+
const gone = new Set([...move.moved.map((m) => m.slot), ...move.dropped]);
|
|
272
|
+
if (move.moved.length > 0) {
|
|
273
|
+
const keys: unknown = profile.hubKeys;
|
|
274
|
+
profile.hubKeys = [
|
|
275
|
+
...(Array.isArray(keys) ? (keys as CliHubKey[]) : []),
|
|
276
|
+
...move.moved.map((m) => m.key)
|
|
277
|
+
];
|
|
278
|
+
}
|
|
279
|
+
const rest = Object.entries(raw).filter(([slot]) => !gone.has(slot));
|
|
280
|
+
if (rest.length === 0) delete profile.connectKeys;
|
|
281
|
+
else if (gone.size > 0) profile.connectKeys = Object.fromEntries(rest) as Record<string, CliConnectKey>;
|
|
282
|
+
}).changed;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** What storing a freshly minted kept key came to. */
|
|
286
|
+
export type StoreOutcome =
|
|
287
|
+
/** `ours`: this key is kept now (it was stored, or it was already there). */
|
|
288
|
+
| { kept: CliHubKey; ours: true }
|
|
289
|
+
/** First stored wins: another command stored a key for the slot first; that one is kept and
|
|
290
|
+
* the key just minted is the caller's to revoke on its site. */
|
|
291
|
+
| { kept: CliHubKey; ours: false }
|
|
292
|
+
/** The profile is now at another site than the key's: nothing was written. */
|
|
293
|
+
| { elsewhere: string }
|
|
294
|
+
/** The profile has no site of its own (no `baseUrl`, no `cloudUrl`) and already holds keys, of
|
|
295
|
+
* no known site: they are never adopted, and nothing was written. */
|
|
296
|
+
| { unknown_site: true };
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Store a key the exchange just minted on the profile of its site, in one
|
|
300
|
+
* `updateConfig`. FIRST STORED WINS: when the profile keeps a key for that
|
|
301
|
+
* `(hub, e-mail)` that ANOTHER command stored after this command's last read
|
|
302
|
+
* before its exchange (the re-read under the mint lock), that key is kept
|
|
303
|
+
* (the caller revokes its own). `held` lists the keys the profile held at
|
|
304
|
+
* that read: none of them is « first stored », so the entry is stored beside
|
|
305
|
+
* it, and the older of the two is a stale duplicate the next command revokes
|
|
306
|
+
* on its site (the same account signed in again: its newer key serves).
|
|
307
|
+
* Otherwise the entry is added; a profile not on disk is created with
|
|
308
|
+
* `baseUrl` and `site`, and made the default only when the tool had no
|
|
309
|
+
* profile at all. Either way an existing profile with no `baseUrl` takes this
|
|
310
|
+
* one, with `site` (a profile is never repointed), and one with a `baseUrl`
|
|
311
|
+
* takes `site` only when it holds none; nothing else is written. A profile
|
|
312
|
+
* with no site of its own (no `baseUrl`, and no `cloudUrl` to be at) that
|
|
313
|
+
* already holds a key (its own, a kept key, an old slot) answers
|
|
314
|
+
* `unknown_site` and is left as it is: keys of no known site are never handed
|
|
315
|
+
* to this one. `avoid` is a key never kept (the one a site just refused): it
|
|
316
|
+
* is dropped from the slot when it is still there. `lastHub`, the address of
|
|
317
|
+
* the hub the exchange went through in its normal form (`normalizeUrl`), is
|
|
318
|
+
* written on the profile of this site whichever key is kept (the exchange
|
|
319
|
+
* succeeded either way), and on no other.
|
|
320
|
+
*/
|
|
321
|
+
export function storeHubKey(
|
|
322
|
+
env: CliEnv,
|
|
323
|
+
tool: string,
|
|
324
|
+
profileName: string,
|
|
325
|
+
entry: CliHubKey,
|
|
326
|
+
opts: {
|
|
327
|
+
baseUrl: string;
|
|
328
|
+
cloudUrl?: string | undefined;
|
|
329
|
+
site?: CliSiteFacts | undefined;
|
|
330
|
+
avoid?: string | undefined;
|
|
331
|
+
lastHub?: string | undefined;
|
|
332
|
+
/** The kept keys the profile held at this command's last read before its exchange. */
|
|
333
|
+
held?: readonly string[] | undefined;
|
|
334
|
+
}
|
|
335
|
+
): StoreOutcome {
|
|
336
|
+
let outcome: StoreOutcome = { kept: entry, ours: true };
|
|
337
|
+
const held = new Set((opts.held ?? []).map((k) => k.trim()));
|
|
338
|
+
updateConfig(env, tool, (config) => {
|
|
339
|
+
const first = Object.keys(config.profiles).length === 0;
|
|
340
|
+
const profile = config.profiles[profileName];
|
|
341
|
+
if (!profile) {
|
|
342
|
+
config.profiles[profileName] = {
|
|
343
|
+
baseUrl: opts.baseUrl,
|
|
344
|
+
...(opts.site ? { site: opts.site } : {}),
|
|
345
|
+
hubKeys: [entry],
|
|
346
|
+
...(opts.lastHub ? { lastHub: opts.lastHub } : {})
|
|
347
|
+
};
|
|
348
|
+
if (first) config.activeProfile = profileName;
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
const at = profileUrl(profile, opts.cloudUrl);
|
|
352
|
+
if (at !== undefined && !sameInstance(at, opts.baseUrl)) {
|
|
353
|
+
outcome = { elsewhere: at };
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
if (at === undefined && keysOfProfile(profile).length > 0) {
|
|
357
|
+
outcome = { unknown_site: true };
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
// First stored: another command's key, stored in this slot since this command's last read.
|
|
361
|
+
const there = newestKey(
|
|
362
|
+
hubKeysOf(profile).filter(
|
|
363
|
+
(k) => k.apiKey !== opts.avoid && !held.has(k.apiKey.trim()) && sameSlot(k, entry)
|
|
364
|
+
)
|
|
365
|
+
);
|
|
366
|
+
if (there) {
|
|
367
|
+
outcome = { kept: there, ours: there.apiKey === entry.apiKey };
|
|
368
|
+
} else {
|
|
369
|
+
const keys: unknown = profile.hubKeys;
|
|
370
|
+
profile.hubKeys = [
|
|
371
|
+
...(Array.isArray(keys) ? (keys as unknown[]) : []).filter(
|
|
372
|
+
(k) => !(isObject(k) && opts.avoid !== undefined && k.apiKey === opts.avoid)
|
|
373
|
+
),
|
|
374
|
+
entry
|
|
375
|
+
] as CliHubKey[];
|
|
376
|
+
}
|
|
377
|
+
// A profile with no site of its own (and no key) takes this one: the next command then finds
|
|
378
|
+
// it at its site (and what a site-less profile remembered is no site's).
|
|
379
|
+
const founding = !text(profile.baseUrl);
|
|
380
|
+
if (founding) profile.baseUrl = opts.baseUrl;
|
|
381
|
+
if (opts.site && (founding || profile.site === undefined)) profile.site = opts.site;
|
|
382
|
+
if (opts.lastHub) profile.lastHub = opts.lastHub;
|
|
383
|
+
});
|
|
384
|
+
return outcome;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* File a key the exchange minted, lost at the store (first stored wins), and
|
|
389
|
+
* could not revoke on its site: in its slot, UNDATED (`mintedAt: ''`, the
|
|
390
|
+
* oldest of the slot), so it is a stale duplicate that the next command on the
|
|
391
|
+
* profile revokes on its site, then forgets. One `updateConfig`, on a profile
|
|
392
|
+
* at that site only (`baseUrl`; one with no `baseUrl` is at `cloudUrl`).
|
|
393
|
+
* Answers whether the profile holds it now.
|
|
394
|
+
*/
|
|
395
|
+
export function storeUnrevokedKey(
|
|
396
|
+
env: CliEnv,
|
|
397
|
+
tool: string,
|
|
398
|
+
profileName: string,
|
|
399
|
+
entry: CliHubKey,
|
|
400
|
+
opts: { baseUrl: string; cloudUrl?: string | undefined }
|
|
401
|
+
): boolean {
|
|
402
|
+
let filed = false;
|
|
403
|
+
updateConfig(env, tool, (config) => {
|
|
404
|
+
const profile = config.profiles[profileName];
|
|
405
|
+
const at = profile ? profileUrl(profile, opts.cloudUrl) : undefined;
|
|
406
|
+
if (!profile || at === undefined || !sameInstance(at, opts.baseUrl)) return;
|
|
407
|
+
filed = true;
|
|
408
|
+
if (hubKeysOf(profile).some((k) => k.apiKey === entry.apiKey)) return;
|
|
409
|
+
const keys: unknown = profile.hubKeys;
|
|
410
|
+
profile.hubKeys = [
|
|
411
|
+
...(Array.isArray(keys) ? (keys as unknown[]) : []),
|
|
412
|
+
{ ...entry, mintedAt: '' }
|
|
413
|
+
] as CliHubKey[];
|
|
414
|
+
});
|
|
415
|
+
return filed;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** Remove from a profile every kept key (and old slot, and with `own` its own key) whose key is listed. */
|
|
419
|
+
function removeKeys(profile: CliProfile, apiKeys: Set<string>, own: boolean): number {
|
|
420
|
+
const listed = (k: unknown): boolean => {
|
|
421
|
+
const key = isObject(k) ? k.apiKey : undefined;
|
|
422
|
+
return typeof key === 'string' && (apiKeys.has(key) || apiKeys.has(key.trim()));
|
|
423
|
+
};
|
|
424
|
+
let removed = 0;
|
|
425
|
+
const keys: unknown = profile.hubKeys;
|
|
426
|
+
if (Array.isArray(keys)) {
|
|
427
|
+
const rest = (keys as unknown[]).filter((k) => !listed(k));
|
|
428
|
+
removed += keys.length - rest.length;
|
|
429
|
+
if (rest.length === 0) delete profile.hubKeys;
|
|
430
|
+
else if (rest.length !== keys.length) profile.hubKeys = rest as CliHubKey[];
|
|
431
|
+
}
|
|
432
|
+
const slots: unknown = profile.connectKeys;
|
|
433
|
+
if (isObject(slots)) {
|
|
434
|
+
const entries = Object.entries(slots);
|
|
435
|
+
const rest = entries.filter(([, e]) => !listed(e));
|
|
436
|
+
removed += entries.length - rest.length;
|
|
437
|
+
if (rest.length === 0) delete profile.connectKeys;
|
|
438
|
+
else if (rest.length !== entries.length) {
|
|
439
|
+
profile.connectKeys = Object.fromEntries(rest) as Record<string, CliConnectKey>;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
if (own && listed({ apiKey: profile.apiKey })) {
|
|
443
|
+
delete profile.apiKey;
|
|
444
|
+
removed += 1;
|
|
445
|
+
}
|
|
446
|
+
return removed;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* COMPARE-AND-EVICT, after a site refused a kept key: removes the kept entry
|
|
451
|
+
* (or old slot) whose key IS `apiKey`, in one `updateConfig`; another key in
|
|
452
|
+
* the same slot (another command already replaced it) is left in place. The
|
|
453
|
+
* profile's own key is never touched. With `remember` (the refused key's hub,
|
|
454
|
+
* its address in its normal form, `normalizeUrl`), the same write gives the
|
|
455
|
+
* profile that hub as its `lastHub`
|
|
456
|
+
* when it is at `remember.site` (one with no `baseUrl` is at `cloudUrl`) and
|
|
457
|
+
* holds none of its own (a profile from before the field): the kept key that
|
|
458
|
+
* said which hub goes, the hub stays remembered. Answers whether a key was
|
|
459
|
+
* removed.
|
|
460
|
+
*/
|
|
461
|
+
export function evictKey(
|
|
462
|
+
env: CliEnv,
|
|
463
|
+
tool: string,
|
|
464
|
+
profileName: string,
|
|
465
|
+
apiKey: string,
|
|
466
|
+
remember?: { lastHub: string; site: string; cloudUrl?: string | undefined }
|
|
467
|
+
): boolean {
|
|
468
|
+
if (!loadConfig(env, tool).profiles[profileName]) return false;
|
|
469
|
+
let removed = 0;
|
|
470
|
+
updateConfig(env, tool, (config) => {
|
|
471
|
+
const profile = config.profiles[profileName];
|
|
472
|
+
if (!profile) return;
|
|
473
|
+
removed = removeKeys(profile, new Set([apiKey]), false);
|
|
474
|
+
const at = remember ? profileUrl(profile, remember.cloudUrl) : undefined;
|
|
475
|
+
if (remember && at !== undefined && sameInstance(at, remember.site) && !text(profile.lastHub)) {
|
|
476
|
+
profile.lastHub = remember.lastHub;
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
return removed > 0;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** One key a profile holds, for the command that revokes it (a logout, a removal). Never printed. */
|
|
483
|
+
export interface ProfileKey {
|
|
484
|
+
apiKey: string;
|
|
485
|
+
/** `own`: the tool's own login; `kept`: a kept key; `old`: a slot of the old shape. */
|
|
486
|
+
kind: 'own' | 'kept' | 'old';
|
|
487
|
+
hub?: string;
|
|
488
|
+
email?: string;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Every key a profile holds, each once: its own key, its kept keys, its old
|
|
493
|
+
* slots. The list a logout or a profile removal revokes on the profile's own
|
|
494
|
+
* site; never printed, redacted or not.
|
|
495
|
+
*/
|
|
496
|
+
export function keysOfProfile(profile: CliProfile | undefined): ProfileKey[] {
|
|
497
|
+
if (!profile) return [];
|
|
498
|
+
const out: ProfileKey[] = [];
|
|
499
|
+
const seen = new Set<string>();
|
|
500
|
+
const add = (key: ProfileKey): void => {
|
|
501
|
+
const apiKey = key.apiKey.trim();
|
|
502
|
+
if (seen.has(apiKey)) return;
|
|
503
|
+
seen.add(apiKey);
|
|
504
|
+
out.push({ ...key, apiKey });
|
|
505
|
+
};
|
|
506
|
+
const own = text(profile.apiKey);
|
|
507
|
+
if (own) add({ apiKey: own, kind: 'own' });
|
|
508
|
+
for (const k of hubKeysOf(profile)) add({ apiKey: k.apiKey, kind: 'kept', hub: k.hub, email: k.email });
|
|
509
|
+
for (const [, entry] of oldSlotsOf(profile)) {
|
|
510
|
+
const email = text(entry.email);
|
|
511
|
+
add({ apiKey: entry.apiKey, kind: 'old', ...(email ? { email } : {}) });
|
|
512
|
+
}
|
|
513
|
+
return out;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* The one rule of every revoke (R2): a key is revoked on its site only when
|
|
518
|
+
* no other profile AT THAT SAME SITE still holds it; otherwise it is
|
|
519
|
+
* forgotten on this profile only, and a line says so. This answers which
|
|
520
|
+
* other profile holds what: each key (trimmed) that another profile of
|
|
521
|
+
* `config` holds (`keysOfProfile`: its own key, its kept keys, its old slots)
|
|
522
|
+
* mapped to the first such profile's name in the file's order, counting only
|
|
523
|
+
* the profiles whose site is the same instance as `site`, the two compared in
|
|
524
|
+
* `normalizeUrl`'s one form (a profile with no `baseUrl` is at `cloudUrl`; one
|
|
525
|
+
* with no site at all counts for no site). A profile at another site never
|
|
526
|
+
* counts: it can never use the key, its commands go to its own site, so
|
|
527
|
+
* counting it would leave the key live where no profile uses it (verifier
|
|
528
|
+
* round 4, finding 3). A key matches only exactly. Pure: nothing is read or
|
|
529
|
+
* written.
|
|
530
|
+
*/
|
|
531
|
+
export function keysHeldElsewhere(
|
|
532
|
+
config: CliConfig,
|
|
533
|
+
profileName: string,
|
|
534
|
+
site: string,
|
|
535
|
+
cloudUrl?: string
|
|
536
|
+
): Map<string, string> {
|
|
537
|
+
const held = new Map<string, string>();
|
|
538
|
+
for (const [name, profile] of Object.entries(config.profiles)) {
|
|
539
|
+
const at = profileUrl(profile, cloudUrl);
|
|
540
|
+
if (name === profileName || at === undefined || !sameInstance(at, site)) continue;
|
|
541
|
+
for (const key of keysOfProfile(profile)) if (!held.has(key.apiKey)) held.set(key.apiKey, name);
|
|
542
|
+
}
|
|
543
|
+
return held;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* Forget keys of a profile, COMPARE-AND-REMOVE, in one `updateConfig`: each
|
|
548
|
+
* kept key, old slot, and (when listed) the profile's own key whose key is
|
|
549
|
+
* one of `apiKeys` is removed; whatever another command added meanwhile
|
|
550
|
+
* stays. Revoke first, with no lock held, then forget. Answers how many
|
|
551
|
+
* entries were removed; nothing is written when none was.
|
|
552
|
+
*/
|
|
553
|
+
export function forgetKeys(env: CliEnv, tool: string, profileName: string, apiKeys: string[]): number {
|
|
554
|
+
if (apiKeys.length === 0 || !loadConfig(env, tool).profiles[profileName]) return 0;
|
|
555
|
+
let removed = 0;
|
|
556
|
+
updateConfig(env, tool, (config) => {
|
|
557
|
+
const profile = config.profiles[profileName];
|
|
558
|
+
if (profile) removed = removeKeys(profile, new Set(apiKeys.map((k) => k.trim())), true);
|
|
559
|
+
});
|
|
560
|
+
return removed;
|
|
561
|
+
}
|