@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/sign-in.ts
ADDED
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
import { HUB_TOOL, loadConfig } from './config.js';
|
|
2
|
+
import type { CliEnv, CliHubKey, CliProfile, CliSiteFacts } from './config.js';
|
|
3
|
+
import { keysOfProfile, newestKey, sameEmail, sortKeys, text } from './kept-keys.js';
|
|
4
|
+
import { normalizeUrl, profileUrl, sameInstance } from './resolve.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Which Antasphere login signs a tool command in (R2, PRDCT-3521): the login
|
|
8
|
+
* at the hub the command's SITE trusts, never the machine's active hub login.
|
|
9
|
+
*
|
|
10
|
+
* - A LOGIN is an entry of the hub file (`HUB_TOOL`, `config.json`) that
|
|
11
|
+
* holds a key: its name, its hub (`baseUrl`, else the hub's cloud URL), its
|
|
12
|
+
* e-mail. Several may share a hub. An entry that holds a key and whose hub
|
|
13
|
+
* cannot be told (a `baseUrl` that does not parse as an `http:` or `https:`
|
|
14
|
+
* URL) is no login but UNDECIDED (`undecidedLogins`): unknown is not gone,
|
|
15
|
+
* so while one is there no kept key is judged gone for want of a login.
|
|
16
|
+
* Every function whose answer decides a stale key requires `hubCloudUrl`,
|
|
17
|
+
* so an entry with no `baseUrl` always has a hub.
|
|
18
|
+
* - Rule R2, the hub (`chooseHub`): a site that names its hub (discovery
|
|
19
|
+
* `auth.hub`) decides it, and `--hub` or `ANTASPHERE_URL` naming ANOTHER
|
|
20
|
+
* hub is refused before any exchange, both named. A site that names none (a
|
|
21
|
+
* server from before), or one never reached: `--hub`, else
|
|
22
|
+
* `ANTASPHERE_URL`, else the hub of the profile's newest kept key whose
|
|
23
|
+
* login is still in place, else the hub its last exchange went through
|
|
24
|
+
* (`lastHub`) while a login is in place there, else an error naming the
|
|
25
|
+
* machine's logins and the one command that picks one. Never a guess, even
|
|
26
|
+
* with one login. A recovery after a 401 (`decide`'s internal `hubHint`)
|
|
27
|
+
* continues the command whose key was refused: for a site that names none,
|
|
28
|
+
* right after `ANTASPHERE_URL`, the refused key's hub while a login is in
|
|
29
|
+
* place there.
|
|
30
|
+
* - Rule 3, the login at that hub (`chooseLogin`): `--email` when given,
|
|
31
|
+
* else the only one, else the one whose address the profile's newest kept
|
|
32
|
+
* key there carries, else an error naming them; none there, an error
|
|
33
|
+
* naming the hub and `login`.
|
|
34
|
+
*
|
|
35
|
+
* `planSignIn` is the one decision the command (`connectOnDemand`) and a
|
|
36
|
+
* listing share: pure, it reads the hub file, writes nothing and asks
|
|
37
|
+
* nothing of the network.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A hub address this CLI can read: the URL a non-blank string parses as, when
|
|
42
|
+
* its scheme is `http:` or `https:`; undefined otherwise (a value that is not
|
|
43
|
+
* a string included).
|
|
44
|
+
*/
|
|
45
|
+
export function httpUrl(value: unknown): URL | undefined {
|
|
46
|
+
const raw = text(value);
|
|
47
|
+
if (!raw) return undefined;
|
|
48
|
+
try {
|
|
49
|
+
const url = new URL(raw);
|
|
50
|
+
return url.protocol === 'http:' || url.protocol === 'https:' ? url : undefined;
|
|
51
|
+
} catch {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The `hubCloudUrl` of a function whose answer decides a stale key, trimmed.
|
|
58
|
+
* Missing, blank, or not an `http:` or `https:` URL, it is a `TypeError`
|
|
59
|
+
* naming the option: with it, a hub entry with no `baseUrl` always has a hub
|
|
60
|
+
* (its default address, the rule the selection applies).
|
|
61
|
+
*/
|
|
62
|
+
export function requireHubCloudUrl(fn: string, value: unknown): string {
|
|
63
|
+
const raw = text(value);
|
|
64
|
+
if (raw && httpUrl(raw)) return raw;
|
|
65
|
+
throw new TypeError(
|
|
66
|
+
`${fn}: the hubCloudUrl option is required (the hub's cloud URL, the hub of a login with no baseUrl); ` +
|
|
67
|
+
(raw ? `"${raw}" is not an http: or https: URL.` : 'it is missing or blank.')
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A login of this machine: an entry of the hub file that holds a key, at a hub this CLI can read. */
|
|
72
|
+
export interface CliLogin {
|
|
73
|
+
/** The entry's name in the hub file's `profiles`. */
|
|
74
|
+
name: string;
|
|
75
|
+
/** Its hub: `normalizeUrl(firstSet(baseUrl, hubCloudUrl))`. */
|
|
76
|
+
hub: string;
|
|
77
|
+
/** Its account's e-mail, when it carries one. */
|
|
78
|
+
email?: string;
|
|
79
|
+
/** The entry itself (its `apiKey` is the hub key: never printed). */
|
|
80
|
+
profile: CliProfile;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** What one read of the hub file says of its entries that hold a key. */
|
|
84
|
+
export interface LoginsRead {
|
|
85
|
+
/** The logins, in the file's order. */
|
|
86
|
+
logins: CliLogin[];
|
|
87
|
+
/** The names of the entries whose hub cannot be told, in the file's order. */
|
|
88
|
+
undecided: string[];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A hub entry's hub, normalized: its `baseUrl`, else (none, or a blank one)
|
|
93
|
+
* `hubCloudUrl`; undefined when that is not an `http:` or `https:` URL (a
|
|
94
|
+
* `baseUrl` that is not a string included).
|
|
95
|
+
*/
|
|
96
|
+
function entryHub(baseUrl: unknown, hubCloudUrl: string | undefined): string | undefined {
|
|
97
|
+
const none = baseUrl === undefined || baseUrl === null || (typeof baseUrl === 'string' && !baseUrl.trim());
|
|
98
|
+
const url = httpUrl(none ? hubCloudUrl : baseUrl);
|
|
99
|
+
return url ? normalizeUrl(url.href) : undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* One read of the hub file: each entry that holds a key is a login when its
|
|
104
|
+
* hub can be told (`entryHub`), and UNDECIDED otherwise. An entry with no key
|
|
105
|
+
* is neither. Strict like `loadConfig`: a hub file that does not parse throws.
|
|
106
|
+
*/
|
|
107
|
+
export function readLogins(env: CliEnv, hubCloudUrl: string | undefined): LoginsRead {
|
|
108
|
+
const read: LoginsRead = { logins: [], undecided: [] };
|
|
109
|
+
for (const [name, profile] of Object.entries(loadConfig(env, HUB_TOOL).profiles)) {
|
|
110
|
+
if (!text(profile.apiKey)) continue;
|
|
111
|
+
const hub = entryHub(profile.baseUrl, hubCloudUrl);
|
|
112
|
+
if (hub === undefined) {
|
|
113
|
+
read.undecided.push(name);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const email = text(profile.email);
|
|
117
|
+
read.logins.push({ name, hub, ...(email ? { email } : {}), profile });
|
|
118
|
+
}
|
|
119
|
+
return read;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The logins of this machine, in the hub file's order: every entry that holds
|
|
124
|
+
* a key and whose hub can be told: its `baseUrl` when that is an `http:` or
|
|
125
|
+
* `https:` URL, else (no `baseUrl`, or a blank one) `hubCloudUrl`. An entry
|
|
126
|
+
* with no key is no login, nor one whose hub cannot be told: a `baseUrl` that
|
|
127
|
+
* does not parse as such a URL (`undecidedLogins` names those), or none when
|
|
128
|
+
* no `hubCloudUrl` is given. Strict like `loadConfig`: a hub file that does
|
|
129
|
+
* not parse throws.
|
|
130
|
+
*/
|
|
131
|
+
export function listLogins(env: CliEnv, opts: { hubCloudUrl?: string | undefined } = {}): CliLogin[] {
|
|
132
|
+
return readLogins(env, text(opts.hubCloudUrl)).logins;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The names of the hub file's entries that hold a key and whose hub cannot be
|
|
137
|
+
* told (a `baseUrl` that does not parse as an `http:` or `https:` URL), in the
|
|
138
|
+
* file's order, for a listing to say. Such an entry is no login, and while
|
|
139
|
+
* one is there no kept key is judged gone for want of a login: nothing is
|
|
140
|
+
* revoked on that ground. `hubCloudUrl` is required (a `TypeError` naming it):
|
|
141
|
+
* an entry with no `baseUrl` is at it.
|
|
142
|
+
*/
|
|
143
|
+
export function undecidedLogins(env: CliEnv, opts: { hubCloudUrl: string }): string[] {
|
|
144
|
+
return readLogins(env, requireHubCloudUrl('undecidedLogins', opts?.hubCloudUrl)).undecided;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** How a login is named in a sentence: its address, or that it carries none. */
|
|
148
|
+
function addressOf(login: CliLogin): string {
|
|
149
|
+
return login.email ?? '(no address)';
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* How every sentence names the profile to sign in on at `site`: `--profile <name>` when that
|
|
154
|
+
* profile is on disk there (`onDisk`: its name), else `--api-url <site>`. `--profile` names only a
|
|
155
|
+
* profile on disk (`resolveProfile` throws `Unknown profile` otherwise); `--api-url` selects the
|
|
156
|
+
* site's profile, or makes it.
|
|
157
|
+
*/
|
|
158
|
+
export function profileFlag(site: string, onDisk: string | undefined): string {
|
|
159
|
+
return onDisk ? `--profile ${onDisk}` : `--api-url ${normalizeUrl(site)}`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The flag of `chooseHub`'s and `chooseLogin`'s sentences: their `profile` is the one on disk at the site. */
|
|
163
|
+
function flagOf(input: { siteUrl: string; profile?: CliProfile | undefined; profileName: string }): string {
|
|
164
|
+
return profileFlag(input.siteUrl, input.profile ? input.profileName : undefined);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Where rule R2 found its hub (`remembered`: the profile's `lastHub`). */
|
|
168
|
+
export type HubSource = 'site' | 'option' | 'env' | 'kept_key' | 'remembered';
|
|
169
|
+
|
|
170
|
+
/** Rule R2's answer: the hub, a refusal, or nothing found (the person picks one). */
|
|
171
|
+
export type HubChoice =
|
|
172
|
+
| { kind: 'hub'; hub: string; source: HubSource }
|
|
173
|
+
| { kind: 'refused'; sentence: string }
|
|
174
|
+
| { kind: 'pick_hub'; sentence: string; candidates: CliLogin[] };
|
|
175
|
+
|
|
176
|
+
export interface ChooseHubInput {
|
|
177
|
+
/** The command's site (its origin). */
|
|
178
|
+
siteUrl: string;
|
|
179
|
+
/** The hub the site names (`auth.hub`); null when it names none; undefined when never reached. */
|
|
180
|
+
siteHub?: string | null | undefined;
|
|
181
|
+
/** The profile AT this site, on disk, whose kept keys (old slots read by name), then its
|
|
182
|
+
* `lastHub`, hint the hub of a site that names none; the sentence names it by `--profile`. Omit
|
|
183
|
+
* for a profile not on disk, or at another site: the sentence then names `--api-url <site>`. */
|
|
184
|
+
profile?: CliProfile | undefined;
|
|
185
|
+
/** The machine's logins (`listLogins`). */
|
|
186
|
+
logins: CliLogin[];
|
|
187
|
+
/** `login --hub`. */
|
|
188
|
+
hubUrl?: string | undefined;
|
|
189
|
+
/** `ANTASPHERE_URL` (the listing leaves it out). */
|
|
190
|
+
envHub?: string | undefined;
|
|
191
|
+
/** The profile, for the sentences. */
|
|
192
|
+
profileName: string;
|
|
193
|
+
/** The binary named in the sentences. */
|
|
194
|
+
bin: string;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Rule R2: the hub that signs a command on this site in. */
|
|
198
|
+
export function chooseHub(input: ChooseHubInput): HubChoice {
|
|
199
|
+
const kept = sortKeys(input.profile, input.logins, input.siteHub, new Date().toISOString()).kept;
|
|
200
|
+
return givenHub(input) ?? profileHub(input, kept, lastHubOf(input.profile), flagOf(input));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Whether a kept key's login is in place: a login at its hub carrying its address. */
|
|
204
|
+
function inPlace(key: CliHubKey, logins: CliLogin[]): boolean {
|
|
205
|
+
return logins.some((l) => sameInstance(l.hub, key.hub) && sameEmail(l.email, key.email));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** The hub a profile's last exchange went through, normalized; undefined when it holds none, or not a string. */
|
|
209
|
+
function lastHubOf(profile: CliProfile | undefined): string | undefined {
|
|
210
|
+
const hub = text(profile?.lastHub);
|
|
211
|
+
return hub ? normalizeUrl(hub) : undefined;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Rule R2's first half: the hub the site names (`--hub` or `ANTASPHERE_URL` naming another one
|
|
216
|
+
* refused, both named), else `--hub`, else `ANTASPHERE_URL`; undefined when none of them names one.
|
|
217
|
+
*/
|
|
218
|
+
function givenHub(input: ChooseHubInput): HubChoice | undefined {
|
|
219
|
+
const site = normalizeUrl(input.siteUrl);
|
|
220
|
+
const named = text(input.siteHub);
|
|
221
|
+
const option = text(input.hubUrl);
|
|
222
|
+
const env = text(input.envHub);
|
|
223
|
+
if (named) {
|
|
224
|
+
const hub = normalizeUrl(named);
|
|
225
|
+
if (option && !sameInstance(option, hub)) {
|
|
226
|
+
return {
|
|
227
|
+
kind: 'refused',
|
|
228
|
+
sentence:
|
|
229
|
+
`${site} signs in through the Antasphere hub ${hub}, and --hub names ${normalizeUrl(option)}. ` +
|
|
230
|
+
'Drop --hub: a site that names its hub decides it.'
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
if (env && !sameInstance(env, hub)) {
|
|
234
|
+
return {
|
|
235
|
+
kind: 'refused',
|
|
236
|
+
sentence:
|
|
237
|
+
`${site} signs in through the Antasphere hub ${hub}, and ANTASPHERE_URL names ${normalizeUrl(env)}. ` +
|
|
238
|
+
'Unset ANTASPHERE_URL: a site that names its hub decides it.'
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
return { kind: 'hub', hub, source: 'site' };
|
|
242
|
+
}
|
|
243
|
+
if (option) return { kind: 'hub', hub: normalizeUrl(option), source: 'option' };
|
|
244
|
+
if (env) return { kind: 'hub', hub: normalizeUrl(env), source: 'env' };
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Where `decide` found its hub: rule R2's sources, or a recovery's hint (`refused_key`, internal). */
|
|
249
|
+
export type DecidedSource = HubSource | 'refused_key';
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* A recovery's hint, for a site that names no hub and no hub given (internal, never an option and
|
|
253
|
+
* never refused): the hub of the key the site just refused, while a login is in place there.
|
|
254
|
+
*/
|
|
255
|
+
function hintedHub(
|
|
256
|
+
hint: string | undefined,
|
|
257
|
+
logins: CliLogin[]
|
|
258
|
+
): { kind: 'hub'; hub: string; source: 'refused_key' } | undefined {
|
|
259
|
+
const hub = text(hint);
|
|
260
|
+
if (!hub || !logins.some((l) => sameInstance(l.hub, hub))) return undefined;
|
|
261
|
+
return { kind: 'hub', hub: normalizeUrl(hub), source: 'refused_key' };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Rule R2's second half, for a site that names no hub and no hub given: the hub of the newest kept
|
|
266
|
+
* key whose login is in place, else the profile's `lastHub` while a login is in place there, else
|
|
267
|
+
* the pick-a-hub sentence, naming the profile by `flag` (`profileFlag`).
|
|
268
|
+
*/
|
|
269
|
+
function profileHub(
|
|
270
|
+
input: ChooseHubInput,
|
|
271
|
+
kept: CliHubKey[],
|
|
272
|
+
remembered: string | undefined,
|
|
273
|
+
flag: string
|
|
274
|
+
): HubChoice {
|
|
275
|
+
const site = normalizeUrl(input.siteUrl);
|
|
276
|
+
const last = newestKey(kept.filter((k) => inPlace(k, input.logins)));
|
|
277
|
+
if (last) return { kind: 'hub', hub: normalizeUrl(last.hub), source: 'kept_key' };
|
|
278
|
+
// The hub of the profile's last sign-in, while a login is in place there.
|
|
279
|
+
if (remembered && input.logins.some((l) => sameInstance(l.hub, remembered))) {
|
|
280
|
+
return { kind: 'hub', hub: remembered, source: 'remembered' };
|
|
281
|
+
}
|
|
282
|
+
const n = input.logins.length;
|
|
283
|
+
const holds =
|
|
284
|
+
n === 0
|
|
285
|
+
? 'This machine holds no Antasphere login.'
|
|
286
|
+
: `This machine holds ${n} Antasphere login${n === 1 ? '' : 's'}: ` +
|
|
287
|
+
`${input.logins.map((l) => `${addressOf(l)} at ${l.hub}`).join(', ')}.`;
|
|
288
|
+
const history = remembered
|
|
289
|
+
? `last signed in through ${remembered}, where this machine holds no login now`
|
|
290
|
+
: 'has never signed in there';
|
|
291
|
+
return {
|
|
292
|
+
kind: 'pick_hub',
|
|
293
|
+
candidates: input.logins,
|
|
294
|
+
sentence:
|
|
295
|
+
`${site} names no Antasphere hub (it runs a version from before sites named theirs), and profile ` +
|
|
296
|
+
`"${input.profileName}" ${history}. ${holds} Pick the hub this site trusts: ` +
|
|
297
|
+
`${input.bin} ${flag} login --hub <hub>.`
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Rule 3's answer: the login, none at that hub, or several and none used before. */
|
|
302
|
+
export type LoginChoice =
|
|
303
|
+
| { kind: 'login'; login: CliLogin }
|
|
304
|
+
| { kind: 'no_login_at_hub'; sentence: string }
|
|
305
|
+
| { kind: 'pick_login'; sentence: string; candidates: CliLogin[] };
|
|
306
|
+
|
|
307
|
+
export interface ChooseLoginInput {
|
|
308
|
+
/** The hub rule R2 picked. */
|
|
309
|
+
hub: string;
|
|
310
|
+
/** The command's site (its origin), for the sentences. */
|
|
311
|
+
siteUrl: string;
|
|
312
|
+
/** The profile AT this site, on disk, whose newest kept key at that hub names the login last
|
|
313
|
+
* used; the sentences name it by `--profile`. Omit for a profile not on disk, or at another
|
|
314
|
+
* site: the sentences then name `--api-url <site>`. */
|
|
315
|
+
profile?: CliProfile | undefined;
|
|
316
|
+
logins: CliLogin[];
|
|
317
|
+
/** `login --email`: that account's login at that hub. */
|
|
318
|
+
email?: string | undefined;
|
|
319
|
+
profileName: string;
|
|
320
|
+
bin: string;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Rule 3: the login at that hub. */
|
|
324
|
+
export function chooseLogin(input: ChooseLoginInput): LoginChoice {
|
|
325
|
+
// Rule 3 reads only the kept keys at this hub: what the site names elsewhere does not matter here.
|
|
326
|
+
const kept = sortKeys(input.profile, input.logins, undefined, new Date().toISOString()).kept;
|
|
327
|
+
return pickLogin(input, kept, flagOf(input));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Rule 3, its sentences naming the profile by `flag` (`profileFlag`). */
|
|
331
|
+
function pickLogin(input: ChooseLoginInput, kept: CliHubKey[], flag: string): LoginChoice {
|
|
332
|
+
const site = normalizeUrl(input.siteUrl);
|
|
333
|
+
const hub = normalizeUrl(input.hub);
|
|
334
|
+
const atHub = input.logins.filter((l) => sameInstance(l.hub, hub));
|
|
335
|
+
const none = (fix: string, forAddress = ''): LoginChoice => ({
|
|
336
|
+
kind: 'no_login_at_hub',
|
|
337
|
+
sentence:
|
|
338
|
+
`${site} signs in through the Antasphere hub ${hub}, and this machine holds no Antasphere login there` +
|
|
339
|
+
`${forAddress}. Run ${input.bin} ${flag} ${fix} to sign in.`
|
|
340
|
+
});
|
|
341
|
+
const wanted = text(input.email);
|
|
342
|
+
if (wanted) {
|
|
343
|
+
const match = atHub.find((l) => sameEmail(l.email, wanted));
|
|
344
|
+
return match ? { kind: 'login', login: match } : none(`login --email ${wanted}`, ` for ${wanted}`);
|
|
345
|
+
}
|
|
346
|
+
if (atHub.length === 0) return none('login');
|
|
347
|
+
// One account may sit in several entries (one address at one hub): they are one login.
|
|
348
|
+
const accounts: CliLogin[] = [];
|
|
349
|
+
for (const l of atHub) {
|
|
350
|
+
if (!accounts.some((a) => a === l || (a.email !== undefined && sameEmail(a.email, l.email)))) {
|
|
351
|
+
accounts.push(l);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (accounts.length === 1) return { kind: 'login', login: accounts[0]! };
|
|
355
|
+
const last = newestKey(
|
|
356
|
+
kept.filter((k) => sameInstance(k.hub, hub) && atHub.some((l) => sameEmail(l.email, k.email)))
|
|
357
|
+
);
|
|
358
|
+
const used = last ? atHub.find((l) => sameEmail(l.email, last.email)) : undefined;
|
|
359
|
+
if (used) return { kind: 'login', login: used };
|
|
360
|
+
return {
|
|
361
|
+
kind: 'pick_login',
|
|
362
|
+
candidates: atHub,
|
|
363
|
+
sentence:
|
|
364
|
+
`This machine holds ${accounts.length} Antasphere logins at ${hub} ` +
|
|
365
|
+
`(${accounts.map(addressOf).join(', ')}), and profile "${input.profileName}" has used none of them. ` +
|
|
366
|
+
`Pick one: ${input.bin} ${flag} login --email <address>.`
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** Where a profile stands with its sign-in: one state, and why. */
|
|
371
|
+
export type SignInState = 'signed_in' | 'will_sign_in' | 'needs_login' | 'unknown';
|
|
372
|
+
|
|
373
|
+
export type SignInReason =
|
|
374
|
+
| 'own_key'
|
|
375
|
+
| 'kept_key'
|
|
376
|
+
| 'login_ready'
|
|
377
|
+
| 'no_login_at_hub'
|
|
378
|
+
| 'pick_hub'
|
|
379
|
+
| 'pick_login'
|
|
380
|
+
| 'own_sign_in'
|
|
381
|
+
| 'never_reached'
|
|
382
|
+
| 'refused';
|
|
383
|
+
|
|
384
|
+
/** The one decision of who signs a profile in on its site, from the two files alone. */
|
|
385
|
+
export interface SignInPlan {
|
|
386
|
+
state: SignInState;
|
|
387
|
+
/** `key`: the profile's own key; `login`: a login at a hub; null when nothing signs in yet. */
|
|
388
|
+
via: 'key' | 'login' | null;
|
|
389
|
+
/** The hub rule R2 picks, when one is picked. */
|
|
390
|
+
hub: string | null;
|
|
391
|
+
/** The login rule 3 picks. */
|
|
392
|
+
login: CliLogin | null;
|
|
393
|
+
/** The servable kept key (`signed_in` via `login`). */
|
|
394
|
+
keptKey: CliHubKey | null;
|
|
395
|
+
reason: SignInReason;
|
|
396
|
+
/** The sentence, when `reason` is `refused`. */
|
|
397
|
+
refusal: string | null;
|
|
398
|
+
/** `pick_hub`: every login; `pick_login`: the logins at the hub; otherwise empty. */
|
|
399
|
+
candidates: CliLogin[];
|
|
400
|
+
/** What the next command on this profile revokes on its site, before it serves a kept key or
|
|
401
|
+
* mints, then forgets (old slots included, with hub '' when they had none). A key whose login
|
|
402
|
+
* is gone is not in it while the hub file holds an entry whose hub cannot be told
|
|
403
|
+
* (`undecidedLogins`), nor while a login at its hub carries no e-mail (it may be its account). */
|
|
404
|
+
stale: CliHubKey[];
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export interface PlanSignInInput {
|
|
408
|
+
env: CliEnv;
|
|
409
|
+
/** The tool's namespace; also the binary named in sentences. */
|
|
410
|
+
tool: string;
|
|
411
|
+
profileName: string;
|
|
412
|
+
/** The profile as the tool file holds it (undefined: not on disk). */
|
|
413
|
+
profile: CliProfile | undefined;
|
|
414
|
+
/** The site the command targets (a listing: the profile's own). */
|
|
415
|
+
siteUrl: string;
|
|
416
|
+
/** What a profile with no `baseUrl` is at. */
|
|
417
|
+
cloudUrl?: string | undefined;
|
|
418
|
+
/** REQUIRED: the hub's cloud URL, what a login with no `baseUrl` is at (a `TypeError` naming it
|
|
419
|
+
* when missing, blank or not an `http:` or `https:` URL). */
|
|
420
|
+
hubCloudUrl: string;
|
|
421
|
+
/** `ANTASPHERE_URL`: a command passes `firstSet(env.ANTASPHERE_URL)`, a listing nothing. */
|
|
422
|
+
envHub?: string | undefined;
|
|
423
|
+
/** `login --hub`. */
|
|
424
|
+
hubUrl?: string | undefined;
|
|
425
|
+
/** `login --email`. */
|
|
426
|
+
email?: string | undefined;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** A profile's remembered site facts, when they read as such. */
|
|
430
|
+
export function siteFactsOf(profile: CliProfile | undefined): CliSiteFacts | undefined {
|
|
431
|
+
const s: unknown = profile?.site;
|
|
432
|
+
if (typeof s !== 'object' || s === null) return undefined;
|
|
433
|
+
const f = s as Record<string, unknown>;
|
|
434
|
+
const nullableText = (v: unknown): boolean => v === null || typeof v === 'string';
|
|
435
|
+
if (!nullableText(f.hub) || typeof f.federated !== 'boolean' || !nullableText(f.name)) return undefined;
|
|
436
|
+
if (!nullableText(f.cliVersion) || typeof f.seenAt !== 'string') return undefined;
|
|
437
|
+
return s as CliSiteFacts;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* The plan of who signs a profile in on a site, in this order: the profile's
|
|
442
|
+
* own key (`signed_in`, `own_key`); a site that signs in by itself
|
|
443
|
+
* (`site.federated` false: `needs_login`, `own_sign_in`); rule R2 (a refusal:
|
|
444
|
+
* `needs_login`, `refused`, with its sentence); rule 3; then the kept key:
|
|
445
|
+
* servable (`signed_in`, `kept_key`), else a login ready to sign in at the
|
|
446
|
+
* next command (`will_sign_in`, `login_ready`), no login at the hub
|
|
447
|
+
* (`no_login_at_hub`), several (`pick_login`), nothing found for a site that
|
|
448
|
+
* names no hub (`pick_hub`), nothing known at all (`unknown`,
|
|
449
|
+
* `never_reached`). Pure: it reads the hub file once (`readLogins`), writes
|
|
450
|
+
* nothing and asks nothing of the network. A kept key is servable only on a
|
|
451
|
+
* profile at `siteUrl`, at the hub R2 picks, to the login rule 3 picks.
|
|
452
|
+
* `hubCloudUrl` is required: a `TypeError` naming it otherwise.
|
|
453
|
+
*/
|
|
454
|
+
export function planSignIn(input: PlanSignInInput): SignInPlan {
|
|
455
|
+
const hubCloudUrl = requireHubCloudUrl('planSignIn', input.hubCloudUrl);
|
|
456
|
+
const { logins, undecided } = readLogins(input.env, hubCloudUrl);
|
|
457
|
+
return decide({
|
|
458
|
+
siteUrl: input.siteUrl,
|
|
459
|
+
profileName: input.profileName,
|
|
460
|
+
profile: input.profile,
|
|
461
|
+
cloudUrl: input.cloudUrl,
|
|
462
|
+
site: undefined,
|
|
463
|
+
logins,
|
|
464
|
+
undecided,
|
|
465
|
+
envHub: input.envHub,
|
|
466
|
+
hubUrl: input.hubUrl,
|
|
467
|
+
email: input.email,
|
|
468
|
+
bin: input.tool,
|
|
469
|
+
ownKeyFirst: true,
|
|
470
|
+
now: new Date().toISOString()
|
|
471
|
+
}).plan;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
export interface DecideInput {
|
|
475
|
+
siteUrl: string;
|
|
476
|
+
profileName: string;
|
|
477
|
+
profile: CliProfile | undefined;
|
|
478
|
+
cloudUrl?: string | undefined;
|
|
479
|
+
/** The site's facts; undefined = the ones the profile remembers (when it is at the site). */
|
|
480
|
+
site: CliSiteFacts | undefined;
|
|
481
|
+
logins: CliLogin[];
|
|
482
|
+
/** The entries whose hub cannot be told, from the same read (`readLogins`): while there is one,
|
|
483
|
+
* no kept key is judged gone for want of a login. */
|
|
484
|
+
undecided: string[];
|
|
485
|
+
envHub?: string | undefined;
|
|
486
|
+
hubUrl?: string | undefined;
|
|
487
|
+
email?: string | undefined;
|
|
488
|
+
bin: string;
|
|
489
|
+
/** Whether the profile's own key comes first (the plan of a listing); a hub sign-in skips it. */
|
|
490
|
+
ownKeyFirst: boolean;
|
|
491
|
+
/** A key never served (the one a site just refused). */
|
|
492
|
+
avoid?: string | undefined;
|
|
493
|
+
/** A recovery's hint: the hub of the key a site just refused. For a site that names no hub, it
|
|
494
|
+
* comes right after `envHub`, while a login is in place there; never an option, never refused. */
|
|
495
|
+
hubHint?: string | undefined;
|
|
496
|
+
now: string;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** A plan, with the sentence a command throws when it cannot sign in. */
|
|
500
|
+
export interface Decision {
|
|
501
|
+
plan: SignInPlan;
|
|
502
|
+
sentence: string | null;
|
|
503
|
+
/** Where the plan's hub was found, when one was. */
|
|
504
|
+
hubSource?: DecidedSource;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/** The plan, and the sentence of its refusal or of what the person must pick (internal). */
|
|
508
|
+
export function decide(input: DecideInput): Decision {
|
|
509
|
+
const { profile, logins } = input;
|
|
510
|
+
const siteUrl = normalizeUrl(input.siteUrl);
|
|
511
|
+
const at = profile ? profileUrl(profile, input.cloudUrl) : undefined;
|
|
512
|
+
const here = profile !== undefined && at !== undefined && sameInstance(at, siteUrl);
|
|
513
|
+
const site = input.site ?? (here ? siteFactsOf(profile) : undefined);
|
|
514
|
+
// Unknown is not gone: an entry whose hub cannot be told may be any key's login.
|
|
515
|
+
const judgeGone = input.undecided.length === 0;
|
|
516
|
+
const sort = here
|
|
517
|
+
? sortKeys(profile, logins, site?.hub, input.now, { judgeGone })
|
|
518
|
+
: { kept: [], stale: [] };
|
|
519
|
+
const plan = (
|
|
520
|
+
state: SignInState,
|
|
521
|
+
via: SignInPlan['via'],
|
|
522
|
+
reason: SignInReason,
|
|
523
|
+
more: Partial<SignInPlan> = {}
|
|
524
|
+
): SignInPlan => ({
|
|
525
|
+
state,
|
|
526
|
+
via,
|
|
527
|
+
hub: null,
|
|
528
|
+
login: null,
|
|
529
|
+
keptKey: null,
|
|
530
|
+
reason,
|
|
531
|
+
refusal: null,
|
|
532
|
+
candidates: [],
|
|
533
|
+
stale: sort.stale,
|
|
534
|
+
...more
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
if (input.ownKeyFirst && here && text(profile.apiKey)) {
|
|
538
|
+
return { plan: plan('signed_in', 'key', 'own_key'), sentence: null };
|
|
539
|
+
}
|
|
540
|
+
if (site?.federated === false) return { plan: plan('needs_login', null, 'own_sign_in'), sentence: null };
|
|
541
|
+
|
|
542
|
+
const ruleInput = { siteUrl, logins, profileName: input.profileName, bin: input.bin };
|
|
543
|
+
const remembered = here ? lastHubOf(profile) : undefined;
|
|
544
|
+
const hubInput = { ...ruleInput, siteHub: site?.hub, hubUrl: input.hubUrl, envHub: input.envHub };
|
|
545
|
+
// The sentences name the profile by --profile only when it is on disk at this site.
|
|
546
|
+
const flag = profileFlag(siteUrl, here ? input.profileName : undefined);
|
|
547
|
+
// A recovery continues the command whose key was refused: that key's hub comes before the profile's.
|
|
548
|
+
const hub =
|
|
549
|
+
givenHub(hubInput) ??
|
|
550
|
+
hintedHub(input.hubHint, logins) ??
|
|
551
|
+
profileHub(hubInput, sort.kept, remembered, flag);
|
|
552
|
+
if (hub.kind === 'refused') {
|
|
553
|
+
return { plan: plan('needs_login', null, 'refused', { refusal: hub.sentence }), sentence: hub.sentence };
|
|
554
|
+
}
|
|
555
|
+
if (hub.kind === 'pick_hub') {
|
|
556
|
+
// Nothing known: the site never reached, and the profile holds no key and no hub of its last sign-in.
|
|
557
|
+
const known =
|
|
558
|
+
site !== undefined || (here && (keysOfProfile(profile).length > 0 || remembered !== undefined));
|
|
559
|
+
return known
|
|
560
|
+
? {
|
|
561
|
+
plan: plan('needs_login', null, 'pick_hub', { candidates: hub.candidates }),
|
|
562
|
+
sentence: hub.sentence
|
|
563
|
+
}
|
|
564
|
+
: { plan: plan('unknown', null, 'never_reached'), sentence: hub.sentence };
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const hubSource = hub.source;
|
|
568
|
+
const choice = pickLogin({ ...ruleInput, hub: hub.hub, email: input.email }, sort.kept, flag);
|
|
569
|
+
if (choice.kind === 'no_login_at_hub') {
|
|
570
|
+
return {
|
|
571
|
+
plan: plan('needs_login', null, 'no_login_at_hub', { hub: hub.hub }),
|
|
572
|
+
sentence: choice.sentence,
|
|
573
|
+
hubSource
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
if (choice.kind === 'pick_login') {
|
|
577
|
+
return {
|
|
578
|
+
plan: plan('needs_login', null, 'pick_login', { hub: hub.hub, candidates: choice.candidates }),
|
|
579
|
+
sentence: choice.sentence,
|
|
580
|
+
hubSource
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
const { login } = choice;
|
|
584
|
+
const keptKey = login.email
|
|
585
|
+
? sort.kept.find(
|
|
586
|
+
(k) => sameInstance(k.hub, hub.hub) && sameEmail(k.email, login.email) && k.apiKey !== input.avoid
|
|
587
|
+
)
|
|
588
|
+
: undefined;
|
|
589
|
+
if (keptKey) {
|
|
590
|
+
return {
|
|
591
|
+
plan: plan('signed_in', 'login', 'kept_key', { hub: hub.hub, login, keptKey }),
|
|
592
|
+
sentence: null,
|
|
593
|
+
hubSource
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
return {
|
|
597
|
+
plan: plan('will_sign_in', 'login', 'login_ready', { hub: hub.hub, login }),
|
|
598
|
+
sentence: null,
|
|
599
|
+
hubSource
|
|
600
|
+
};
|
|
601
|
+
}
|