@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.
- package/README.md +95 -12
- package/dist/B6PCore.js +61 -3
- package/dist/B6PCore.js.map +1 -1
- package/dist/Err.d.ts +42 -3
- package/dist/Err.js +51 -1
- package/dist/Err.js.map +1 -1
- package/dist/auth/BearerAuthProvider.d.ts +43 -13
- package/dist/auth/BearerAuthProvider.js +151 -22
- package/dist/auth/BearerAuthProvider.js.map +1 -1
- package/dist/cache/OrgCache.js +5 -4
- package/dist/cache/OrgCache.js.map +1 -1
- package/dist/constants/Gateway.d.ts +78 -1
- package/dist/constants/Gateway.js +79 -1
- package/dist/constants/Gateway.js.map +1 -1
- package/dist/gateway/GatewayClient.d.ts +72 -0
- package/dist/gateway/GatewayClient.js +132 -15
- package/dist/gateway/GatewayClient.js.map +1 -1
- package/dist/gateway/GatewayContext.d.ts +10 -3
- package/dist/gateway/GatewayKb.d.ts +2 -2
- package/dist/gateway/GatewayKb.js +6 -6
- package/dist/gateway/GatewayKb.js.map +1 -1
- package/dist/gateway/GatewayService.d.ts +25 -5
- package/dist/gateway/GatewayService.js +42 -8
- package/dist/gateway/GatewayService.js.map +1 -1
- package/dist/gateway/GatewaySignIn.d.ts +372 -0
- package/dist/gateway/GatewaySignIn.js +825 -0
- package/dist/gateway/GatewaySignIn.js.map +1 -0
- package/dist/gateway/GatewayToken.d.ts +19 -4
- package/dist/gateway/GatewayToken.js +51 -10
- package/dist/gateway/GatewayToken.js.map +1 -1
- package/dist/gateway/GatewayTokenService.d.ts +2 -2
- package/dist/gateway/GatewayTokenService.js +20 -9
- package/dist/gateway/GatewayTokenService.js.map +1 -1
- package/dist/index.d.ts +7 -5
- package/dist/index.js +8 -3
- package/dist/index.js.map +1 -1
- package/dist/persistence/PersistenceKeys.d.ts +6 -1
- package/dist/persistence/PersistenceKeys.js +5 -0
- package/dist/persistence/PersistenceKeys.js.map +1 -1
- package/dist/persistence/PublicPersistanceMap.js +17 -9
- package/dist/persistence/PublicPersistanceMap.js.map +1 -1
- package/dist/providers.d.ts +76 -3
- package/dist/testing/FakeGateway.d.ts +135 -1
- package/dist/testing/FakeGateway.js +197 -1
- package/dist/testing/FakeGateway.js.map +1 -1
- package/dist/types.d.ts +78 -0
- package/package.json +2 -2
package/dist/providers.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ScriptRepoOptions } from "@bluestep-systems/b6p-core-bsjs-git";
|
|
2
2
|
import type { B6PUri } from "./B6PUri";
|
|
3
3
|
import type { OrgCacheSettings } from "./cache/OrgCache";
|
|
4
|
-
import type { AuthParams } from "./types";
|
|
4
|
+
import type { AuthParams, GatewayMintedToken, GatewaySignInRecord } from "./types";
|
|
5
5
|
export interface FileStat {
|
|
6
6
|
type: "file" | "directory";
|
|
7
7
|
mtime: number;
|
|
@@ -175,13 +175,76 @@ export interface GatewayTokenSource {
|
|
|
175
175
|
*/
|
|
176
176
|
export interface GatewayTokenStore {
|
|
177
177
|
/**
|
|
178
|
-
* Store `next` in place of `expected`.
|
|
178
|
+
* Store `next` in place of `expected`. `minted` is what the gateway said about `next`, when its answer was on
|
|
179
|
+
* contract: a device sign-in's record then follows the token (its new id, expiry and scopes); without it the
|
|
180
|
+
* record keeps all but the id, which becomes unknown. Rejects, leaving the stored token as it was, when the
|
|
179
181
|
* stored token is no longer `expected` (something else replaced it meanwhile) or cannot be
|
|
180
182
|
* written. The check is a compare-then-write, not atomic across processes. Neither value is
|
|
181
183
|
* ever logged or put in an error message.
|
|
182
184
|
* @lastreviewed null
|
|
183
185
|
*/
|
|
184
|
-
replace(expected: string, next: string): Promise<void>;
|
|
186
|
+
replace(expected: string, next: string, minted?: GatewayMintedToken): Promise<void>;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* A stored token and, when a device sign-in made it, its record.
|
|
190
|
+
* @lastreviewed null
|
|
191
|
+
*/
|
|
192
|
+
export interface GatewayStoredToken {
|
|
193
|
+
/**
|
|
194
|
+
* The secret. Never log it or put it in a message.
|
|
195
|
+
* @lastreviewed null
|
|
196
|
+
*/
|
|
197
|
+
token: string;
|
|
198
|
+
/**
|
|
199
|
+
* The sign-in's record, `null` for a pasted token.
|
|
200
|
+
* @lastreviewed null
|
|
201
|
+
*/
|
|
202
|
+
signIn: GatewaySignInRecord | null;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* The store behind `core.gateway.signIn` (device sign-in, sign-out, renewal): the same ONE stored token the rest of
|
|
206
|
+
* the gateway reads, with the record a device sign-in keeps beside it, plus the lock and the timestamp renewal
|
|
207
|
+
* needs. Nothing here prompts.
|
|
208
|
+
*
|
|
209
|
+
* `B6PCore` backs it with the bearer auth provider's secret (`BearerAuthProvider.storeSignIn` and `forget`) and the
|
|
210
|
+
* persistence's lock ({@link Persistence.withLock}, shared across processes when the persistence has it).
|
|
211
|
+
* @lastreviewed null
|
|
212
|
+
*/
|
|
213
|
+
export interface GatewaySignInStore {
|
|
214
|
+
/**
|
|
215
|
+
* The stored token and its record, `undefined` when none (or only a malformed one) is stored.
|
|
216
|
+
* @lastreviewed null
|
|
217
|
+
*/
|
|
218
|
+
read(): Promise<GatewayStoredToken | undefined>;
|
|
219
|
+
/**
|
|
220
|
+
* Store `token` with `signIn` in place of whatever is stored, under the store's lock, and read it back.
|
|
221
|
+
* Resolves to what it replaced (read inside the lock), `undefined` when nothing was stored.
|
|
222
|
+
* @throws {Error} the write failed or the read-back found something else; neither value is in the message
|
|
223
|
+
* @lastreviewed null
|
|
224
|
+
*/
|
|
225
|
+
store(token: string, signIn: GatewaySignInRecord): Promise<GatewayStoredToken | undefined>;
|
|
226
|
+
/**
|
|
227
|
+
* Forget the stored token when it is still `expected`, under the store's lock. Resolves `true` when it was
|
|
228
|
+
* forgotten, `false` when something else is stored now (left as it is).
|
|
229
|
+
* @lastreviewed null
|
|
230
|
+
*/
|
|
231
|
+
forget(expected: string): Promise<boolean>;
|
|
232
|
+
/**
|
|
233
|
+
* Run `work` holding the sign-in lifecycle lock: one device sign-in's store, renewal or sign-out at a time
|
|
234
|
+
* (across processes when the persistence can lock). Not re-entrant.
|
|
235
|
+
* @lastreviewed null
|
|
236
|
+
*/
|
|
237
|
+
exclusive<T>(work: () => Promise<T>): Promise<T>;
|
|
238
|
+
/**
|
|
239
|
+
* When a renewal was last attempted on this computer (ISO 8601), `undefined` when never.
|
|
240
|
+
* @lastreviewed null
|
|
241
|
+
*/
|
|
242
|
+
lastRenewAttempt(): Promise<string | undefined>;
|
|
243
|
+
/**
|
|
244
|
+
* Record a renewal attempt at `at` (ISO 8601).
|
|
245
|
+
* @lastreviewed null
|
|
246
|
+
*/
|
|
247
|
+
recordRenewAttempt(at: string): Promise<void>;
|
|
185
248
|
}
|
|
186
249
|
export interface Logger {
|
|
187
250
|
info(...args: unknown[]): void;
|
|
@@ -239,6 +302,16 @@ export interface B6PProviders {
|
|
|
239
302
|
progress: Progress;
|
|
240
303
|
/** Optional debug-mode flag callback. Defaults to `() => false` if not provided. */
|
|
241
304
|
isDebugMode?: () => boolean;
|
|
305
|
+
/**
|
|
306
|
+
* `true` for a session that must write nothing of its own: core then uses a logger and a progress provider that
|
|
307
|
+
* output nothing (progress still runs every task, in order) in place of `logger` and `progress`, for itself and
|
|
308
|
+
* everything it builds (the default auth provider, caches, subsystems). For a caller whose output is consumed by
|
|
309
|
+
* a program and whose stderr must carry only its own fixed lines, such as an MCP client's header helper
|
|
310
|
+
* (`gateway.mcpHeaders`): a warning about a malformed cache entry or stored credential then never reaches it.
|
|
311
|
+
* `prompt` is unchanged. Default `false`.
|
|
312
|
+
* @lastreviewed null
|
|
313
|
+
*/
|
|
314
|
+
silent?: boolean;
|
|
242
315
|
/**
|
|
243
316
|
* Optional authentication scheme. Defaults to `BearerAuthProvider` (this file
|
|
244
317
|
* deliberately imports nothing from `auth/`, so that is a plain reference, not a
|
|
@@ -104,6 +104,11 @@ export interface FakeGatewayFailure {
|
|
|
104
104
|
* @lastreviewed null
|
|
105
105
|
*/
|
|
106
106
|
body?: GatewayErrorBody | string;
|
|
107
|
+
/**
|
|
108
|
+
* Extra response headers, such as `Retry-After`.
|
|
109
|
+
* @lastreviewed null
|
|
110
|
+
*/
|
|
111
|
+
headers?: Readonly<Record<string, string>>;
|
|
107
112
|
/**
|
|
108
113
|
* Serve this failure to the next request with this HTTP method rather than the very next
|
|
109
114
|
* request, leaving requests of other methods alone. Lets a spec fail a `POST` that a `GET`
|
|
@@ -112,6 +117,83 @@ export interface FakeGatewayFailure {
|
|
|
112
117
|
*/
|
|
113
118
|
method?: string;
|
|
114
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* One device sign-in the fake has started (`POST /api/v1/device-logins`), for a spec to steer and inspect.
|
|
122
|
+
* @lastreviewed null
|
|
123
|
+
*/
|
|
124
|
+
export interface FakeGatewayDeviceLogin {
|
|
125
|
+
/**
|
|
126
|
+
* b6p's secret half; a spec checks it never reaches a log line or an error.
|
|
127
|
+
* @lastreviewed null
|
|
128
|
+
*/
|
|
129
|
+
readonly deviceCode: string;
|
|
130
|
+
/**
|
|
131
|
+
* The code shown to the person, `XXXX-XXXX`.
|
|
132
|
+
* @lastreviewed null
|
|
133
|
+
*/
|
|
134
|
+
readonly userCode: string;
|
|
135
|
+
/**
|
|
136
|
+
* The start's body as sent (`client`, `clientVersion`, `host`, `platform`).
|
|
137
|
+
* @lastreviewed null
|
|
138
|
+
*/
|
|
139
|
+
readonly request: Record<string, unknown>;
|
|
140
|
+
/**
|
|
141
|
+
* `pending` until a spec allows or denies it; `delivered` once a poll collected the token.
|
|
142
|
+
* @lastreviewed null
|
|
143
|
+
*/
|
|
144
|
+
state: "pending" | "approved" | "denied" | "delivered" | "expired";
|
|
145
|
+
/**
|
|
146
|
+
* The interval the fake currently asks for, in seconds.
|
|
147
|
+
* @lastreviewed null
|
|
148
|
+
*/
|
|
149
|
+
interval: number;
|
|
150
|
+
/**
|
|
151
|
+
* How many polls it has answered.
|
|
152
|
+
* @lastreviewed null
|
|
153
|
+
*/
|
|
154
|
+
polls: number;
|
|
155
|
+
/**
|
|
156
|
+
* The approved answer, once allowed (secret included).
|
|
157
|
+
* @lastreviewed null
|
|
158
|
+
*/
|
|
159
|
+
answer?: Record<string, unknown>;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* How {@link FakeGateway.approveDeviceLogin} allows a sign-in. Every member is optional; the defaults are the real
|
|
163
|
+
* gateway's grant.
|
|
164
|
+
* @lastreviewed null
|
|
165
|
+
*/
|
|
166
|
+
export interface FakeGatewayDeviceApproval {
|
|
167
|
+
/**
|
|
168
|
+
* The claiming account; `null` sends `account: null` (an older replica). Default `{ name: "Ada Lovelace", login:
|
|
169
|
+
* "ada@example.test" }`.
|
|
170
|
+
* @lastreviewed null
|
|
171
|
+
*/
|
|
172
|
+
account?: {
|
|
173
|
+
name: string;
|
|
174
|
+
login: string;
|
|
175
|
+
} | null;
|
|
176
|
+
/**
|
|
177
|
+
* The owner key the token is filed under (tokens:self lists by owner). Default `"user"`.
|
|
178
|
+
* @lastreviewed null
|
|
179
|
+
*/
|
|
180
|
+
owner?: string;
|
|
181
|
+
/**
|
|
182
|
+
* The minted token's prefix. Default `b6p_g_`.
|
|
183
|
+
* @lastreviewed null
|
|
184
|
+
*/
|
|
185
|
+
prefix?: string;
|
|
186
|
+
/**
|
|
187
|
+
* When it expires. Default 90 days from now.
|
|
188
|
+
* @lastreviewed null
|
|
189
|
+
*/
|
|
190
|
+
expiresAt?: string | null;
|
|
191
|
+
/**
|
|
192
|
+
* Change the approved answer before it is served (an off-contract answer, for a spec).
|
|
193
|
+
* @lastreviewed null
|
|
194
|
+
*/
|
|
195
|
+
edit?: (answer: Record<string, unknown>) => void;
|
|
196
|
+
}
|
|
115
197
|
/**
|
|
116
198
|
* An extra route, checked before the built-in REST routes. The git layer's specs mount the smart
|
|
117
199
|
* HTTP endpoints here. The token is checked first (Bearer, or Basic with the token as password, as
|
|
@@ -150,7 +232,9 @@ export interface FakeGatewayRoute {
|
|
|
150
232
|
* (`GET`/`POST /api/v1/tokens`, `DELETE /api/v1/tokens/{id}`, `POST /api/v1/tokens/{id}/rotate`,
|
|
151
233
|
* scope `tokens:self`, with `scope-escalation` and `alias-taken`; refreshable tokens, as web-global
|
|
152
234
|
* proposed them under CU-1k5g91btqvr: `PATCH /api/v1/tokens/{id}` `{refreshable}`, and a rotation's
|
|
153
|
-
* `expiresAt`; see {@link refreshableServed}),
|
|
235
|
+
* `expiresAt`; see {@link refreshableServed}), device sign-in (`POST /api/v1/device-logins` and `.../poll`, no token,
|
|
236
|
+
* steered with {@link approveDeviceLogin}, {@link denyDeviceLogin}, {@link expireDeviceLogin}, {@link slowDownPolls};
|
|
237
|
+
* 404 when {@link deviceLoginEnabled} is off) and `GET /api/v1/auth/options`, and `404 not-found` for anything else.
|
|
154
238
|
* A rotation honours `Idempotency-Key`: a retry with the same presenting token and key within 10
|
|
155
239
|
* minutes gets the first attempt's answer and rotates nothing (see {@link loseNextRotateAnswer}).
|
|
156
240
|
* Tokens are granted explicitly with their scopes; a request without one is `401 token-missing`,
|
|
@@ -204,6 +288,32 @@ export declare class FakeGateway {
|
|
|
204
288
|
* @lastreviewed null
|
|
205
289
|
*/
|
|
206
290
|
internalAccessUnsupported: boolean;
|
|
291
|
+
/**
|
|
292
|
+
* When `false`, device sign-in is switched off (`gateway.device-login.enabled`): its routes answer 404 and the
|
|
293
|
+
* sign-in options say `deviceLogin: false`. Default `true`.
|
|
294
|
+
* @lastreviewed null
|
|
295
|
+
*/
|
|
296
|
+
deviceLoginEnabled: boolean;
|
|
297
|
+
/**
|
|
298
|
+
* The interval, in seconds, a started sign-in asks b6p to poll at. Default 5, as the gateway.
|
|
299
|
+
* @lastreviewed null
|
|
300
|
+
*/
|
|
301
|
+
deviceLoginInterval: number;
|
|
302
|
+
/**
|
|
303
|
+
* Every device sign-in started, in order.
|
|
304
|
+
* @lastreviewed null
|
|
305
|
+
*/
|
|
306
|
+
readonly deviceLogins: FakeGatewayDeviceLogin[];
|
|
307
|
+
/**
|
|
308
|
+
* Milliseconds each poll waits before it is answered (to stop a sign-in while a poll is under way).
|
|
309
|
+
* @lastreviewed null
|
|
310
|
+
*/
|
|
311
|
+
deviceLoginDelayMs: number;
|
|
312
|
+
/**
|
|
313
|
+
* When positive, that many of the next polls of a pending sign-in answer `slow-down` (+5 s each).
|
|
314
|
+
* @lastreviewed null
|
|
315
|
+
*/
|
|
316
|
+
slowDownPolls: number;
|
|
207
317
|
/**
|
|
208
318
|
* How many rotations actually happened (a replay does not count).
|
|
209
319
|
* @lastreviewed null
|
|
@@ -304,6 +414,24 @@ export declare class FakeGateway {
|
|
|
304
414
|
* @lastreviewed null
|
|
305
415
|
*/
|
|
306
416
|
loseNextRotateAnswer(lost?: FakeGatewayLostAnswer): void;
|
|
417
|
+
/**
|
|
418
|
+
* Allow the latest pending device sign-in, as the browser's Allow does: mint its token (the gateway's grant: the
|
|
419
|
+
* five b6p scopes, 90 days, refreshable, named `b6p on <host> <yyyy-MM-dd HH:mm>`) and hold it for the next poll.
|
|
420
|
+
* @throws {Error} no sign-in is pending
|
|
421
|
+
* @lastreviewed null
|
|
422
|
+
*/
|
|
423
|
+
approveDeviceLogin(options?: FakeGatewayDeviceApproval): FakeGatewayToken;
|
|
424
|
+
/**
|
|
425
|
+
* Deny the latest pending device sign-in, as the browser's Deny does.
|
|
426
|
+
* @lastreviewed null
|
|
427
|
+
*/
|
|
428
|
+
denyDeviceLogin(): void;
|
|
429
|
+
/**
|
|
430
|
+
* Expire the latest pending device sign-in (its ten minutes are up).
|
|
431
|
+
* @lastreviewed null
|
|
432
|
+
*/
|
|
433
|
+
expireDeviceLogin(): void;
|
|
434
|
+
private pendingDeviceLogin;
|
|
307
435
|
/**
|
|
308
436
|
* Mount an extra route; see {@link FakeGatewayRoute}.
|
|
309
437
|
* @lastreviewed null
|
|
@@ -320,6 +448,12 @@ export declare class FakeGateway {
|
|
|
320
448
|
*/
|
|
321
449
|
stop(): Promise<void>;
|
|
322
450
|
private handle;
|
|
451
|
+
/**
|
|
452
|
+
* The anonymous device sign-in routes and the sign-in options, answered as web-global's gateway does (its route
|
|
453
|
+
* contract). They read no token. Returns `false`, having sent nothing, for any other request.
|
|
454
|
+
* @lastreviewed null
|
|
455
|
+
*/
|
|
456
|
+
private handleDeviceLogin;
|
|
323
457
|
/**
|
|
324
458
|
* `POST /api/v1/registry/repos` for a request already authorised with `registry:push`: the contract's
|
|
325
459
|
* `201`/`200`/`409 repo-exists`/`400`, with the request's metadata echoed. Name checks are the contract's.
|
|
@@ -70,6 +70,16 @@ const IDEMPOTENCY_KEY = /^[\x21-\x7e]{16,128}$/;
|
|
|
70
70
|
function isExpired(expiresAt) {
|
|
71
71
|
return expiresAt !== null && Date.parse(expiresAt) <= Date.now();
|
|
72
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* The scopes a device sign-in's token gets: the gateway's fixed grant.
|
|
75
|
+
* @lastreviewed null
|
|
76
|
+
*/
|
|
77
|
+
const DEVICE_LOGIN_SCOPES = ["mcp", "kb:read", "kb:push", "registry:read", "tokens:self"];
|
|
78
|
+
/**
|
|
79
|
+
* The letters of a user code (RFC 8628 §6.1: no vowels, no look-alikes).
|
|
80
|
+
* @lastreviewed null
|
|
81
|
+
*/
|
|
82
|
+
const USER_CODE_LETTERS = "BCDFGHJKLMNPQRSTVWXZ";
|
|
73
83
|
/**
|
|
74
84
|
* An in-process gateway on `127.0.0.1` (an ephemeral port) answering the b6p gateway contract's
|
|
75
85
|
* REST shapes and error codes, for specs in this package and its consumers. It is a real HTTP
|
|
@@ -91,7 +101,9 @@ function isExpired(expiresAt) {
|
|
|
91
101
|
* (`GET`/`POST /api/v1/tokens`, `DELETE /api/v1/tokens/{id}`, `POST /api/v1/tokens/{id}/rotate`,
|
|
92
102
|
* scope `tokens:self`, with `scope-escalation` and `alias-taken`; refreshable tokens, as web-global
|
|
93
103
|
* proposed them under CU-1k5g91btqvr: `PATCH /api/v1/tokens/{id}` `{refreshable}`, and a rotation's
|
|
94
|
-
* `expiresAt`; see {@link refreshableServed}),
|
|
104
|
+
* `expiresAt`; see {@link refreshableServed}), device sign-in (`POST /api/v1/device-logins` and `.../poll`, no token,
|
|
105
|
+
* steered with {@link approveDeviceLogin}, {@link denyDeviceLogin}, {@link expireDeviceLogin}, {@link slowDownPolls};
|
|
106
|
+
* 404 when {@link deviceLoginEnabled} is off) and `GET /api/v1/auth/options`, and `404 not-found` for anything else.
|
|
95
107
|
* A rotation honours `Idempotency-Key`: a retry with the same presenting token and key within 10
|
|
96
108
|
* minutes gets the first attempt's answer and rotates nothing (see {@link loseNextRotateAnswer}).
|
|
97
109
|
* Tokens are granted explicitly with their scopes; a request without one is `401 token-missing`,
|
|
@@ -141,6 +153,32 @@ class FakeGateway {
|
|
|
141
153
|
* @lastreviewed null
|
|
142
154
|
*/
|
|
143
155
|
internalAccessUnsupported = false;
|
|
156
|
+
/**
|
|
157
|
+
* When `false`, device sign-in is switched off (`gateway.device-login.enabled`): its routes answer 404 and the
|
|
158
|
+
* sign-in options say `deviceLogin: false`. Default `true`.
|
|
159
|
+
* @lastreviewed null
|
|
160
|
+
*/
|
|
161
|
+
deviceLoginEnabled = true;
|
|
162
|
+
/**
|
|
163
|
+
* The interval, in seconds, a started sign-in asks b6p to poll at. Default 5, as the gateway.
|
|
164
|
+
* @lastreviewed null
|
|
165
|
+
*/
|
|
166
|
+
deviceLoginInterval = 5;
|
|
167
|
+
/**
|
|
168
|
+
* Every device sign-in started, in order.
|
|
169
|
+
* @lastreviewed null
|
|
170
|
+
*/
|
|
171
|
+
deviceLogins = [];
|
|
172
|
+
/**
|
|
173
|
+
* Milliseconds each poll waits before it is answered (to stop a sign-in while a poll is under way).
|
|
174
|
+
* @lastreviewed null
|
|
175
|
+
*/
|
|
176
|
+
deviceLoginDelayMs = 0;
|
|
177
|
+
/**
|
|
178
|
+
* When positive, that many of the next polls of a pending sign-in answer `slow-down` (+5 s each).
|
|
179
|
+
* @lastreviewed null
|
|
180
|
+
*/
|
|
181
|
+
slowDownPolls = 0;
|
|
144
182
|
/**
|
|
145
183
|
* How many rotations actually happened (a replay does not count).
|
|
146
184
|
* @lastreviewed null
|
|
@@ -296,6 +334,70 @@ class FakeGateway {
|
|
|
296
334
|
loseNextRotateAnswer(lost = { status: 503 }) {
|
|
297
335
|
this.lostAnswers.push(lost);
|
|
298
336
|
}
|
|
337
|
+
/**
|
|
338
|
+
* Allow the latest pending device sign-in, as the browser's Allow does: mint its token (the gateway's grant: the
|
|
339
|
+
* five b6p scopes, 90 days, refreshable, named `b6p on <host> <yyyy-MM-dd HH:mm>`) and hold it for the next poll.
|
|
340
|
+
* @throws {Error} no sign-in is pending
|
|
341
|
+
* @lastreviewed null
|
|
342
|
+
*/
|
|
343
|
+
approveDeviceLogin(options = {}) {
|
|
344
|
+
const login = this.pendingDeviceLogin();
|
|
345
|
+
const n = ++this.tokenSerial;
|
|
346
|
+
const secret = `${options.prefix ?? "b6p_g_"}${(0, node_crypto_1.randomBytes)(24).toString("hex")}`;
|
|
347
|
+
const started = new Date();
|
|
348
|
+
const stamp = started.toISOString().slice(0, 16).replace("T", " ");
|
|
349
|
+
const host = typeof login.request.host === "string" ? login.request.host : "computer";
|
|
350
|
+
const record = {
|
|
351
|
+
secret,
|
|
352
|
+
id: `tok_${n}`,
|
|
353
|
+
alias: `b6p on ${host} ${stamp}`,
|
|
354
|
+
owner: options.owner ?? "user",
|
|
355
|
+
scopes: [...DEVICE_LOGIN_SCOPES],
|
|
356
|
+
createdAt: started.toISOString(),
|
|
357
|
+
expiresAt: options.expiresAt === undefined
|
|
358
|
+
? new Date(started.getTime() + DEFAULT_TOKEN_LIFETIME_MS).toISOString()
|
|
359
|
+
: options.expiresAt,
|
|
360
|
+
lastUsedAt: null,
|
|
361
|
+
refreshable: true,
|
|
362
|
+
};
|
|
363
|
+
this.issuedSecrets.push(secret);
|
|
364
|
+
this.tokens.set(secret, record);
|
|
365
|
+
const answer = {
|
|
366
|
+
status: "approved",
|
|
367
|
+
token: secret,
|
|
368
|
+
tokenId: record.id,
|
|
369
|
+
alias: record.alias,
|
|
370
|
+
scopes: [...record.scopes],
|
|
371
|
+
expiresAt: record.expiresAt,
|
|
372
|
+
refreshable: true,
|
|
373
|
+
account: options.account === undefined ? { name: "Ada Lovelace", login: "ada@example.test" } : options.account,
|
|
374
|
+
};
|
|
375
|
+
options.edit?.(answer);
|
|
376
|
+
login.answer = answer;
|
|
377
|
+
login.state = "approved";
|
|
378
|
+
return { ...record, scopes: [...record.scopes] };
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Deny the latest pending device sign-in, as the browser's Deny does.
|
|
382
|
+
* @lastreviewed null
|
|
383
|
+
*/
|
|
384
|
+
denyDeviceLogin() {
|
|
385
|
+
this.pendingDeviceLogin().state = "denied";
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Expire the latest pending device sign-in (its ten minutes are up).
|
|
389
|
+
* @lastreviewed null
|
|
390
|
+
*/
|
|
391
|
+
expireDeviceLogin() {
|
|
392
|
+
this.pendingDeviceLogin().state = "expired";
|
|
393
|
+
}
|
|
394
|
+
pendingDeviceLogin() {
|
|
395
|
+
const login = [...this.deviceLogins].reverse().find((l) => l.state === "pending");
|
|
396
|
+
if (!login) {
|
|
397
|
+
throw new Error("No device sign-in is pending.");
|
|
398
|
+
}
|
|
399
|
+
return login;
|
|
400
|
+
}
|
|
299
401
|
/**
|
|
300
402
|
* Mount an extra route; see {@link FakeGatewayRoute}.
|
|
301
403
|
* @lastreviewed null
|
|
@@ -341,6 +443,9 @@ class FakeGateway {
|
|
|
341
443
|
sendFailure(res, failure);
|
|
342
444
|
return;
|
|
343
445
|
}
|
|
446
|
+
if (await this.handleDeviceLogin(method, url.pathname, body, res)) {
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
344
449
|
if (custom) {
|
|
345
450
|
if (this.authorize(req, res, custom.scope)) {
|
|
346
451
|
await custom.handler(req, res);
|
|
@@ -408,6 +513,94 @@ class FakeGateway {
|
|
|
408
513
|
this.inFlight--;
|
|
409
514
|
}
|
|
410
515
|
}
|
|
516
|
+
/**
|
|
517
|
+
* The anonymous device sign-in routes and the sign-in options, answered as web-global's gateway does (its route
|
|
518
|
+
* contract). They read no token. Returns `false`, having sent nothing, for any other request.
|
|
519
|
+
* @lastreviewed null
|
|
520
|
+
*/
|
|
521
|
+
async handleDeviceLogin(method, pathname, body, res) {
|
|
522
|
+
if (method === "GET" && pathname === Gateway_1.Gateway.Paths.AUTH_OPTIONS) {
|
|
523
|
+
sendJson(res, 200, {
|
|
524
|
+
password: false,
|
|
525
|
+
factors: [],
|
|
526
|
+
sso: [{ id: "microsoft" }],
|
|
527
|
+
deviceLogin: this.deviceLoginEnabled,
|
|
528
|
+
});
|
|
529
|
+
return true;
|
|
530
|
+
}
|
|
531
|
+
if (pathname !== Gateway_1.Gateway.Paths.DEVICE_LOGINS && pathname !== Gateway_1.Gateway.Paths.DEVICE_LOGIN_POLL) {
|
|
532
|
+
return false;
|
|
533
|
+
}
|
|
534
|
+
if (!this.deviceLoginEnabled) {
|
|
535
|
+
res.writeHead(404).end();
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
if (method !== "POST") {
|
|
539
|
+
sendJson(res, 404, { error: "not-found", message: "Not found." });
|
|
540
|
+
return true;
|
|
541
|
+
}
|
|
542
|
+
const request = parseObject(body);
|
|
543
|
+
if (pathname === Gateway_1.Gateway.Paths.DEVICE_LOGINS) {
|
|
544
|
+
const host = request?.host;
|
|
545
|
+
if (request === null ||
|
|
546
|
+
typeof request.client !== "string" ||
|
|
547
|
+
request.client.length === 0 ||
|
|
548
|
+
typeof host !== "string" ||
|
|
549
|
+
host.trim().length === 0 ||
|
|
550
|
+
[...host.trim()].length > 40) {
|
|
551
|
+
sendJson(res, 400, { error: "invalid-request", message: "Expected client and host." });
|
|
552
|
+
return true;
|
|
553
|
+
}
|
|
554
|
+
const letters = Array.from((0, node_crypto_1.randomBytes)(8), (b) => USER_CODE_LETTERS[b % USER_CODE_LETTERS.length]).join("");
|
|
555
|
+
const login = {
|
|
556
|
+
deviceCode: (0, node_crypto_1.randomBytes)(32).toString("base64url"),
|
|
557
|
+
userCode: `${letters.slice(0, 4)}-${letters.slice(4)}`,
|
|
558
|
+
request,
|
|
559
|
+
state: "pending",
|
|
560
|
+
interval: this.deviceLoginInterval,
|
|
561
|
+
polls: 0,
|
|
562
|
+
};
|
|
563
|
+
this.deviceLogins.push(login);
|
|
564
|
+
sendJson(res, 201, {
|
|
565
|
+
deviceCode: login.deviceCode,
|
|
566
|
+
userCode: login.userCode,
|
|
567
|
+
verificationUri: `${this.url}/device`,
|
|
568
|
+
expiresIn: 600,
|
|
569
|
+
interval: login.interval,
|
|
570
|
+
});
|
|
571
|
+
return true;
|
|
572
|
+
}
|
|
573
|
+
if (request === null || typeof request.deviceCode !== "string") {
|
|
574
|
+
sendJson(res, 400, { error: "invalid-request", message: "Expected a JSON object: deviceCode (a string)" });
|
|
575
|
+
return true;
|
|
576
|
+
}
|
|
577
|
+
if (this.deviceLoginDelayMs > 0) {
|
|
578
|
+
await new Promise((resolve) => setTimeout(resolve, this.deviceLoginDelayMs));
|
|
579
|
+
}
|
|
580
|
+
const login = this.deviceLogins.find((l) => l.deviceCode === request.deviceCode);
|
|
581
|
+
if (login) {
|
|
582
|
+
login.polls++;
|
|
583
|
+
}
|
|
584
|
+
if (!login || login.state === "expired" || login.state === "delivered") {
|
|
585
|
+
sendJson(res, 410, { error: "login-expired", message: "This code has expired or was used." });
|
|
586
|
+
}
|
|
587
|
+
else if (login.state === "denied") {
|
|
588
|
+
sendJson(res, 403, { error: "login-denied", message: "The sign-in was denied in the browser" });
|
|
589
|
+
}
|
|
590
|
+
else if (login.state === "approved") {
|
|
591
|
+
login.state = "delivered";
|
|
592
|
+
sendJson(res, 200, login.answer ?? {});
|
|
593
|
+
}
|
|
594
|
+
else if (this.slowDownPolls > 0) {
|
|
595
|
+
this.slowDownPolls--;
|
|
596
|
+
login.interval += 5;
|
|
597
|
+
sendJson(res, 200, { status: "slow-down", interval: login.interval });
|
|
598
|
+
}
|
|
599
|
+
else {
|
|
600
|
+
sendJson(res, 200, { status: "pending", interval: login.interval });
|
|
601
|
+
}
|
|
602
|
+
return true;
|
|
603
|
+
}
|
|
411
604
|
/**
|
|
412
605
|
* `POST /api/v1/registry/repos` for a request already authorised with `registry:push`: the contract's
|
|
413
606
|
* `201`/`200`/`409 repo-exists`/`400`, with the request's metadata echoed. Name checks are the contract's.
|
|
@@ -1154,6 +1347,9 @@ function sendJson(res, status, body) {
|
|
|
1154
1347
|
res.end(JSON.stringify(body));
|
|
1155
1348
|
}
|
|
1156
1349
|
function sendFailure(res, failure) {
|
|
1350
|
+
for (const [name, value] of Object.entries(failure.headers ?? {})) {
|
|
1351
|
+
res.setHeader(name, value);
|
|
1352
|
+
}
|
|
1157
1353
|
if (failure.body === undefined) {
|
|
1158
1354
|
res.writeHead(failure.status).end();
|
|
1159
1355
|
}
|