@bluestep-systems/b6p-core 1.8.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +95 -12
  2. package/dist/B6PCore.js +61 -3
  3. package/dist/B6PCore.js.map +1 -1
  4. package/dist/Err.d.ts +42 -3
  5. package/dist/Err.js +51 -1
  6. package/dist/Err.js.map +1 -1
  7. package/dist/auth/BearerAuthProvider.d.ts +43 -13
  8. package/dist/auth/BearerAuthProvider.js +151 -22
  9. package/dist/auth/BearerAuthProvider.js.map +1 -1
  10. package/dist/cache/OrgCache.js +5 -4
  11. package/dist/cache/OrgCache.js.map +1 -1
  12. package/dist/constants/Gateway.d.ts +78 -1
  13. package/dist/constants/Gateway.js +79 -1
  14. package/dist/constants/Gateway.js.map +1 -1
  15. package/dist/gateway/GatewayClient.d.ts +72 -0
  16. package/dist/gateway/GatewayClient.js +132 -15
  17. package/dist/gateway/GatewayClient.js.map +1 -1
  18. package/dist/gateway/GatewayContext.d.ts +10 -3
  19. package/dist/gateway/GatewayKb.d.ts +2 -2
  20. package/dist/gateway/GatewayKb.js +6 -6
  21. package/dist/gateway/GatewayKb.js.map +1 -1
  22. package/dist/gateway/GatewayService.d.ts +25 -5
  23. package/dist/gateway/GatewayService.js +42 -8
  24. package/dist/gateway/GatewayService.js.map +1 -1
  25. package/dist/gateway/GatewaySignIn.d.ts +372 -0
  26. package/dist/gateway/GatewaySignIn.js +825 -0
  27. package/dist/gateway/GatewaySignIn.js.map +1 -0
  28. package/dist/gateway/GatewayToken.d.ts +19 -4
  29. package/dist/gateway/GatewayToken.js +51 -10
  30. package/dist/gateway/GatewayToken.js.map +1 -1
  31. package/dist/gateway/GatewayTokenService.d.ts +2 -2
  32. package/dist/gateway/GatewayTokenService.js +20 -9
  33. package/dist/gateway/GatewayTokenService.js.map +1 -1
  34. package/dist/index.d.ts +7 -5
  35. package/dist/index.js +8 -3
  36. package/dist/index.js.map +1 -1
  37. package/dist/persistence/PersistenceKeys.d.ts +6 -1
  38. package/dist/persistence/PersistenceKeys.js +5 -0
  39. package/dist/persistence/PersistenceKeys.js.map +1 -1
  40. package/dist/persistence/PublicPersistanceMap.js +17 -9
  41. package/dist/persistence/PublicPersistanceMap.js.map +1 -1
  42. package/dist/providers.d.ts +76 -3
  43. package/dist/testing/FakeGateway.d.ts +135 -1
  44. package/dist/testing/FakeGateway.js +197 -1
  45. package/dist/testing/FakeGateway.js.map +1 -1
  46. package/dist/types.d.ts +78 -0
  47. package/package.json +2 -2
@@ -0,0 +1,372 @@
1
+ import type { GatewaySignInAccount } from "../types";
2
+ import type { GatewayClient } from "./GatewayClient";
3
+ import type { GatewayContext } from "./GatewayContext";
4
+ import type { GatewayTokenService } from "./GatewayTokenService";
5
+ /**
6
+ * What a started sign-in hands the consumer to show: the code the person types on the gateway's `/device` page, and
7
+ * where that page is. The device code (b6p's secret half) is never handed out.
8
+ * @lastreviewed null
9
+ */
10
+ export interface GatewaySignInCode {
11
+ /**
12
+ * The code to type, shown `XXXX-XXXX`. Show it to the person; never log it.
13
+ * @lastreviewed null
14
+ */
15
+ userCode: string;
16
+ /**
17
+ * The page to type it on (`https://gateway.bluestep.net/device`). The code is never put in it.
18
+ * @lastreviewed null
19
+ */
20
+ verificationUri: string;
21
+ /**
22
+ * When the code expires (ISO 8601), by this computer's clock: ten minutes from the start.
23
+ * @lastreviewed null
24
+ */
25
+ expiresAt: string;
26
+ }
27
+ /**
28
+ * Options for {@link GatewaySignInService.login}.
29
+ * @lastreviewed null
30
+ */
31
+ export interface GatewayLoginOptions {
32
+ /**
33
+ * Called once, when the gateway has given a code, before b6p starts waiting: print the code and the page (and
34
+ * offer to open the page). b6p polls only after it returns.
35
+ * @lastreviewed null
36
+ */
37
+ onCode(code: GatewaySignInCode): void | Promise<void>;
38
+ /**
39
+ * The consumer's version, shown on the confirm page (`b6p 1.8.0 on …`). Left out when not given or not printable
40
+ * (at most 32 characters).
41
+ * @lastreviewed null
42
+ */
43
+ clientVersion?: string;
44
+ /**
45
+ * The computer's name, shown on the confirm page as its own name for itself. Defaults to `os.hostname()`.
46
+ * Control and direction characters are dropped and it is cut to 40 characters.
47
+ * @lastreviewed null
48
+ */
49
+ host?: string;
50
+ /**
51
+ * The platform, shown on the confirm page. Defaults to `process.platform`.
52
+ * @lastreviewed null
53
+ */
54
+ platform?: string;
55
+ /**
56
+ * Stops the wait (Ctrl-C): the login then rejects with {@link Err.GatewayDeviceLoginError} `aborted`, and the code
57
+ * expires at the gateway.
58
+ * @lastreviewed null
59
+ */
60
+ signal?: AbortSignal;
61
+ /**
62
+ * How to wait between polls; defaults to a timer that ends early when `signal` aborts. A consumer may supply its
63
+ * own (to drive a spinner, or in tests).
64
+ * @lastreviewed null
65
+ */
66
+ sleep?: (ms: number, signal?: AbortSignal) => Promise<void>;
67
+ }
68
+ /**
69
+ * Why a token b6p meant to revoke was not revoked:
70
+ * - `pasted`: it was pasted (`b6p auth set`), so it may be in use elsewhere; only revoked when asked;
71
+ * - `other-gateway`: a sign-in on another gateway made it; b6p never sends a token to a gateway that did not make it;
72
+ * - `unreachable`: the gateway did not answer (offline, timeout, 502/503/504);
73
+ * - `already-gone`: the gateway no longer takes it, or does not list it (expired, or revoked elsewhere);
74
+ * - `scope-missing`: it lacks `tokens:self`, so it may not revoke itself;
75
+ * - `failed`: any other refusal.
76
+ * @lastreviewed null
77
+ */
78
+ export type GatewayRevokeProblem = "pasted" | "other-gateway" | "unreachable" | "already-gone" | "scope-missing" | "failed";
79
+ /**
80
+ * The stored token a new sign-in replaced, and what became of it.
81
+ * @lastreviewed null
82
+ */
83
+ export interface GatewayReplacedToken {
84
+ /**
85
+ * `login` when a device sign-in made it, else `pasted`.
86
+ * @lastreviewed null
87
+ */
88
+ method: "login" | "pasted";
89
+ /**
90
+ * Its name and id, when a sign-in made it; else `null`.
91
+ * @lastreviewed null
92
+ */
93
+ alias: string | null;
94
+ tokenId: string | null;
95
+ /**
96
+ * Who allowed it, when a sign-in made it and the gateway said; else `null`.
97
+ * @lastreviewed null
98
+ */
99
+ account: GatewaySignInAccount | null;
100
+ /**
101
+ * `true` when the gateway revoked it.
102
+ * @lastreviewed null
103
+ */
104
+ revoked: boolean;
105
+ /**
106
+ * Why not, when `revoked` is `false`; else `null`.
107
+ * @lastreviewed null
108
+ */
109
+ problem: GatewayRevokeProblem | null;
110
+ }
111
+ /**
112
+ * What {@link GatewaySignInService.login} did: the token it stored (described, never the secret) and the one it
113
+ * replaced.
114
+ * @lastreviewed null
115
+ */
116
+ export interface GatewayLoginResult {
117
+ /**
118
+ * The gateway signed in to.
119
+ * @lastreviewed null
120
+ */
121
+ gateway: string;
122
+ account: GatewaySignInAccount | null;
123
+ alias: string;
124
+ tokenId: string;
125
+ scopes: string[];
126
+ expiresAt: string | null;
127
+ refreshable: boolean;
128
+ /**
129
+ * The token this sign-in took the place of, `null` when none was stored.
130
+ * @lastreviewed null
131
+ */
132
+ replaced: GatewayReplacedToken | null;
133
+ }
134
+ /**
135
+ * What {@link GatewaySignInService.logout} did.
136
+ * @lastreviewed null
137
+ */
138
+ export interface GatewayLogoutResult {
139
+ /**
140
+ * `false` when no token was stored (nothing was done).
141
+ * @lastreviewed null
142
+ */
143
+ wasSignedIn: boolean;
144
+ method: "login" | "pasted" | null;
145
+ alias: string | null;
146
+ account: GatewaySignInAccount | null;
147
+ /**
148
+ * `true` when the gateway revoked the token.
149
+ * @lastreviewed null
150
+ */
151
+ revoked: boolean;
152
+ /**
153
+ * Why it was not revoked, when it was not and b6p had a token; else `null`.
154
+ * @lastreviewed null
155
+ */
156
+ problem: GatewayRevokeProblem | null;
157
+ /**
158
+ * `true` when the token is no longer stored. `false` only when another token was stored meanwhile (by another
159
+ * b6p), which is left as it is.
160
+ * @lastreviewed null
161
+ */
162
+ forgotten: boolean;
163
+ }
164
+ /**
165
+ * Why {@link GatewaySignInService.renewIfDue} did or did not renew:
166
+ * - `renewed`: rotated to a fresh lifetime and stored;
167
+ * - `not-signed-in`, `pasted`: no token, or one b6p did not make (rotate a pasted one with `core.gateway.tokens`);
168
+ * - `not-refreshable`, `never-expires`: nothing to renew;
169
+ * - `other-gateway`: a sign-in on another gateway made it; it is renewed only there;
170
+ * - `not-due`: more than 30 days are left;
171
+ * - `checked-recently`: a renewal was tried on this computer less than a day ago;
172
+ * - `expired`: too late to renew; sign in again;
173
+ * - `failed`: the renewal was tried and did not work (`error` says why).
174
+ * @lastreviewed null
175
+ */
176
+ export type GatewayRenewReason = "renewed" | "not-signed-in" | "pasted" | "not-refreshable" | "never-expires" | "other-gateway" | "not-due" | "checked-recently" | "expired" | "failed";
177
+ /**
178
+ * What {@link GatewaySignInService.renewIfDue} did.
179
+ * @lastreviewed null
180
+ */
181
+ export interface GatewayRenewResult {
182
+ renewed: boolean;
183
+ reason: GatewayRenewReason;
184
+ /**
185
+ * The stored token's expiry after the call (the new one when renewed), `null` for never or no token.
186
+ * @lastreviewed null
187
+ */
188
+ expiresAt: string | null;
189
+ /**
190
+ * For `failed`, what went wrong; else `null`. A {@link Err.GatewayTokenStoreError} here means the token WAS
191
+ * rotated (the old one is revoked) and its replacement, carried in the error, could not be stored: show it.
192
+ * @lastreviewed null
193
+ */
194
+ error: Error | null;
195
+ }
196
+ /**
197
+ * The stored sign-in, described offline ({@link GatewaySignInService.status}). Never the secret.
198
+ * @lastreviewed null
199
+ */
200
+ export interface GatewaySignInStatus {
201
+ signedIn: boolean;
202
+ /**
203
+ * `login` (a device sign-in made it), `pasted`, or `null` when signed out.
204
+ * @lastreviewed null
205
+ */
206
+ method: "login" | "pasted" | null;
207
+ /**
208
+ * The token's prefix (`b6p_g_`, `b6p_t_` or `b6pt_`), for a masked display; `null` when signed out or not a
209
+ * platform token.
210
+ * @lastreviewed null
211
+ */
212
+ tokenPrefix: string | null;
213
+ /**
214
+ * The configured gateway, `null` when its setting is unusable.
215
+ * @lastreviewed null
216
+ */
217
+ gateway: string | null;
218
+ /**
219
+ * For a sign-in: the gateway that made it, and whether it is the configured one. Otherwise `null`.
220
+ * @lastreviewed null
221
+ */
222
+ signedInGateway: string | null;
223
+ onConfiguredGateway: boolean | null;
224
+ /**
225
+ * For a sign-in, what the gateway said about the token; otherwise `null`.
226
+ * @lastreviewed null
227
+ */
228
+ alias: string | null;
229
+ tokenId: string | null;
230
+ account: GatewaySignInAccount | null;
231
+ scopes: string[] | null;
232
+ expiresAt: string | null;
233
+ refreshable: boolean | null;
234
+ /**
235
+ * For a sign-in that expires: whole days left (negative once expired), whether it has expired, and whether a
236
+ * renewal is due (30 days or fewer left, refreshable, not expired). Otherwise `null`.
237
+ * @lastreviewed null
238
+ */
239
+ daysLeft: number | null;
240
+ expired: boolean | null;
241
+ renewDue: boolean | null;
242
+ }
243
+ /**
244
+ * `b6p auth login`'s core (`core.gateway.signIn`): the gateway's device sign-in, modelled on `gh auth login`, plus
245
+ * sign-out, renewal and an offline status of the ONE stored token.
246
+ *
247
+ * - **Sign-in**: start (`POST /api/v1/device-logins`, no token) → the consumer shows the code and page → poll
248
+ * (`POST /api/v1/device-logins/poll`) at the gateway's interval, slower when it says `slow-down` → the approved
249
+ * answer's token is stored with its record (id, name, account, scopes, expiry, refreshable) → the token it
250
+ * replaced is revoked when a sign-in made it, presenting that token's own `tokens:self`. The gateway fixes the
251
+ * scopes, lifetime and name; b6p asks for nothing. 404 on the start is "this gateway does not offer it"
252
+ * ({@link Err.GatewayDeviceLoginError} `unavailable`).
253
+ * - **The token arrives in the poll's answer, over TLS.** The gateway seals it at rest (X25519 + AES-256-GCM, keyed
254
+ * by the device code it holds only as a hash) and opens it itself on the collecting poll; b6p has no key and
255
+ * nothing to unseal.
256
+ * - **Sign-out** revokes a token a sign-in made (a pasted one only when asked), then forgets it. Offline, it forgets
257
+ * it and says the token stays live.
258
+ * - **Renewal** ({@link renewIfDue}): for a consumer's ordinary commands, never for the MCP header helper (a rotation
259
+ * revokes the old secret at once, which would cut the connection the helper is opening).
260
+ *
261
+ * Never logged and never in a message: the device code, the user code and any token. A token is sent only to the
262
+ * gateway that made it.
263
+ * @lastreviewed null
264
+ */
265
+ export declare class GatewaySignInService {
266
+ private readonly client;
267
+ private readonly tokens;
268
+ private readonly ctx;
269
+ constructor(client: GatewayClient, tokens: GatewayTokenService, ctx: GatewayContext);
270
+ /**
271
+ * Whether the configured gateway offers device sign-in, from its anonymous sign-in options (`deviceLogin: true`).
272
+ * `false` for a gateway that predates it, has it switched off, or has no sign-in options at all (404).
273
+ *
274
+ * @throws {Err.GatewayError} the gateway could not be asked (network, timeout, a redirect, a 5xx)
275
+ * @lastreviewed null
276
+ */
277
+ available(): Promise<boolean>;
278
+ /**
279
+ * Sign b6p in through the browser: start a device sign-in, hand the code to `options.onCode`, wait while the
280
+ * person allows it on the gateway's `/device` page, then store the token and revoke the one it replaced (when a
281
+ * sign-in made that one, on this gateway). See {@link GatewaySignInService} for the whole flow.
282
+ *
283
+ * Waiting: polls at the gateway's interval (5 s), plus a little jitter, slower after `slow-down` or a `429`; a
284
+ * gateway that does not answer one poll (offline, 502/503/504) is polled again until the code expires.
285
+ *
286
+ * @throws {Err.GatewayDeviceLoginError} `unavailable` (404: the gateway does not offer it), `denied`, `expired`,
287
+ * `rate-limited` (on the start), `aborted` (`options.signal`)
288
+ * @throws {Err.GatewayUnavailableError} the start went unanswered, or the gateway said `503 unavailable`
289
+ * @throws {Err.GatewayInvalidRequestError} the gateway refused the start or a poll as malformed
290
+ * @throws {Err.GatewayUnexpectedResponseError} an answer the contract does not describe; no token was received
291
+ * @throws {Err.GatewayOffContractTokenError} the approved answer carried a token but was malformed (or the token
292
+ * is not a platform token): the error carries the secret, unstored; it is live until it expires or is revoked
293
+ * @throws {Err.GatewayTokenStoreError} the token was received but could not be stored; the error carries it
294
+ * @throws {Err.GatewayConfigError} the configured gateway URL is unusable
295
+ * @lastreviewed null
296
+ */
297
+ login(options: GatewayLoginOptions): Promise<GatewayLoginResult>;
298
+ /**
299
+ * Sign out: revoke the stored token when a device sign-in made it (a pasted one only with `revokePasted`), then
300
+ * forget it. The revoke presents the token itself (its own `tokens:self`), and only to the gateway that made it;
301
+ * whatever the gateway says, or when it cannot be reached, the token is forgotten and the result says whether it
302
+ * was revoked and why not. Runs under the sign-in lock (one login, renewal or sign-out at a time), on one gateway
303
+ * origin throughout.
304
+ *
305
+ * @throws {Err.GatewayConfigError} the configured gateway URL is unusable, or changed during the sign-out (nothing
306
+ * was sent to the new one, and nothing was forgotten)
307
+ * @lastreviewed null
308
+ */
309
+ logout(options?: {
310
+ revokePasted?: boolean;
311
+ }): Promise<GatewayLogoutResult>;
312
+ /**
313
+ * Renew the stored token when a device sign-in made it, it is refreshable, and 30 days or fewer are left: rotate
314
+ * it (`core.gateway.tokens.rotate`) to 90 days from now, and store the replacement with its record. At most one
315
+ * attempt a day on one computer, and one login, renewal or sign-out at a time (the sign-in lock, shared by every
316
+ * b6p process that can share it); the state is read again inside the lock, and the rotation presents exactly the
317
+ * token read there and refuses unsent if the store has changed, so a token another b6p just stored is never
318
+ * rotated in its place.
319
+ *
320
+ * For a consumer's ordinary commands, before their first gateway request. Never call it from an MCP header
321
+ * helper: a rotation revokes the old secret at once.
322
+ *
323
+ * Never throws for the gateway's answer or the lock: a failure is `reason: "failed"` with the `error`, so it never
324
+ * fails the command it runs before. A replacement that was received but not stored is in that error
325
+ * ({@link Err.GatewayTokenStoreError}).
326
+ *
327
+ * @param options.force renew now whatever is left, and whatever the day's attempt (`b6p auth refresh`)
328
+ * @param options.now the time to judge by (ms since the epoch); defaults to now
329
+ * @lastreviewed null
330
+ */
331
+ renewIfDue(options?: {
332
+ force?: boolean;
333
+ now?: number;
334
+ }): Promise<GatewayRenewResult>;
335
+ /**
336
+ * The stored sign-in, described from what b6p stored: offline, instant, never the secret. `b6p auth status`.
337
+ * @lastreviewed null
338
+ */
339
+ status(options?: {
340
+ now?: number;
341
+ }): Promise<GatewaySignInStatus>;
342
+ /**
343
+ * One anonymous request of a sign-in: on the pinned origin, stopped by its signal (as `aborted`).
344
+ * @lastreviewed null
345
+ */
346
+ private ask;
347
+ /**
348
+ * An approval's token: stored and the replaced token retired; or, when the caller stopped the sign-in while the
349
+ * approval was on its way, stored without retiring anything, and the stop reported plainly.
350
+ * @lastreviewed null
351
+ */
352
+ private collect;
353
+ /**
354
+ * Store the approved answer's token with its record and, when `retire`, revoke the token it replaced; under the
355
+ * sign-in lock. Every received token is stored, or handed back in the error.
356
+ * @lastreviewed null
357
+ */
358
+ private finish;
359
+ /**
360
+ * Revoke the token a new sign-in replaced, when a sign-in on this gateway made it; never a pasted one.
361
+ * @lastreviewed null
362
+ */
363
+ private retire;
364
+ /**
365
+ * Revoke `token` at the pinned origin, presenting `token` itself: by `tokenId`, or, when that is not known, the
366
+ * list's `current` entry. `null` when revoked, else why not. Never throws for the gateway's answer.
367
+ *
368
+ * @throws {Err.GatewayConfigError} the configured gateway is no longer the pinned origin; nothing was sent
369
+ * @lastreviewed null
370
+ */
371
+ private revokeWith;
372
+ }