pico-engine 1.3.0 → 1.5.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 +147 -0
- package/dist/RulesetRegistryLoaderFs.js +4 -7
- package/dist/auth/AuthService.js +537 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/webauthn.js +97 -0
- package/dist/cli.js +11 -0
- package/dist/index.js +83 -46
- package/dist/logging.js +36 -27
- package/dist/oauth/OAuthService.js +356 -0
- package/dist/oauth/acg.js +495 -0
- package/dist/oauth/channelEligibility.js +26 -0
- package/dist/oauth/errors.js +12 -0
- package/dist/oauth/index.js +21 -0
- package/dist/oauth/krlModule.js +27 -0
- package/dist/oauth/meshOAuth.js +43 -0
- package/dist/provisionRoot.js +80 -0
- package/dist/server.js +565 -17
- package/dist/utils/toFileUrl.js +1 -2
- package/krl/io.picolabs.did-o.krl +25 -5
- package/krl/io.picolabs.oauth.krl +48 -0
- package/krl/io.picolabs.pds.krl +344 -0
- package/krl/io.picolabs.pico-engine-ui.krl +43 -10
- package/krl/io.picolabs.wrangler.krl +21 -20
- package/package.json +29 -29
- package/public/pico-engine-ui.css +4 -4
- package/public/pico-engine-ui.js +50 -16
- package/public/pico-labs-logo.png +0 -0
- package/src/RulesetRegistryLoaderFs.ts +6 -10
- package/src/auth/AuthService.ts +755 -0
- package/src/auth/index.ts +17 -0
- package/src/auth/webauthn.ts +182 -0
- package/src/cli.ts +11 -0
- package/src/index.ts +123 -56
- package/src/logging.ts +36 -47
- package/src/oauth/OAuthService.ts +474 -0
- package/src/oauth/acg.ts +666 -0
- package/src/oauth/channelEligibility.ts +26 -0
- package/src/oauth/errors.ts +10 -0
- package/src/oauth/index.ts +14 -0
- package/src/oauth/krlModule.ts +31 -0
- package/src/oauth/meshOAuth.ts +50 -0
- package/src/provisionRoot.ts +97 -0
- package/src/server.ts +647 -17
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
import * as crypto from "crypto";
|
|
2
|
+
import { ChannelReadOnly, PicoDb, PicoDbKey, PicoFramework } from "pico-framework";
|
|
3
|
+
import * as acg from "./acg";
|
|
4
|
+
import { isOAuthEligibleChannel } from "./channelEligibility";
|
|
5
|
+
import { OAuthError } from "./errors";
|
|
6
|
+
import { isChannelUnderRoot, meshRequiresOAuth as meshRequiresOAuthForEci } from "./meshOAuth";
|
|
7
|
+
|
|
8
|
+
const OAUTH_CHANNEL_PREFIX = "oauth-channel";
|
|
9
|
+
const OAUTH_TOKEN_PREFIX = "oauth-token";
|
|
10
|
+
|
|
11
|
+
const DEFAULT_TOKEN_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
|
12
|
+
export const MIN_TOKEN_TTL_SEC = 5 * 60; // 5 minutes
|
|
13
|
+
export const MAX_TOKEN_TTL_SEC = 90 * 24 * 60 * 60; // 90 days
|
|
14
|
+
|
|
15
|
+
export { OAuthError } from "./errors";
|
|
16
|
+
|
|
17
|
+
interface StoredChannelOAuth {
|
|
18
|
+
channelEci: string;
|
|
19
|
+
rootPicoId: string;
|
|
20
|
+
secretSalt: string;
|
|
21
|
+
secretHash: string;
|
|
22
|
+
createdAt: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface StoredOAuthToken {
|
|
26
|
+
token: string;
|
|
27
|
+
grant?: OAuthGrantType;
|
|
28
|
+
channelEci?: string;
|
|
29
|
+
rootPicoId: string;
|
|
30
|
+
appId?: string;
|
|
31
|
+
accountId?: string;
|
|
32
|
+
scope?: string;
|
|
33
|
+
expiresAt: number;
|
|
34
|
+
createdAt: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type OAuthGrantType = "client_credentials" | "authorization_code";
|
|
38
|
+
|
|
39
|
+
export type {
|
|
40
|
+
AuthorizeParams,
|
|
41
|
+
OAuthAppCredentials,
|
|
42
|
+
OAuthAppSummary,
|
|
43
|
+
} from "./acg";
|
|
44
|
+
export { DEFAULT_ACG_SCOPE, parseAuthorizeQuery, parseApproveBody, renderConsentHtml } from "./acg";
|
|
45
|
+
|
|
46
|
+
export interface OAuthChannelStatus {
|
|
47
|
+
eligible: boolean;
|
|
48
|
+
enabled: boolean;
|
|
49
|
+
clientId?: string;
|
|
50
|
+
hasSecret: boolean;
|
|
51
|
+
createdAt?: string;
|
|
52
|
+
reason?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface OAuthChannelCredentials {
|
|
56
|
+
client_id: string;
|
|
57
|
+
client_secret: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface OAuthTokenResponse {
|
|
61
|
+
access_token: string;
|
|
62
|
+
token_type: "Bearer";
|
|
63
|
+
expires_in?: number;
|
|
64
|
+
refresh_token?: string;
|
|
65
|
+
scope?: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface OAuthServiceDeps {
|
|
69
|
+
db: PicoDb;
|
|
70
|
+
pf: PicoFramework;
|
|
71
|
+
tokenTtlMs?: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class OAuthService {
|
|
75
|
+
private deps: OAuthServiceDeps;
|
|
76
|
+
|
|
77
|
+
constructor(deps: OAuthServiceDeps) {
|
|
78
|
+
this.deps = deps;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get tokenTtlMs(): number {
|
|
82
|
+
return this.deps.tokenTtlMs ?? DEFAULT_TOKEN_TTL_MS;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
lookupChannelReadOnly(eci: string): ChannelReadOnly {
|
|
86
|
+
return this.deps.pf.lookupChannel(eci).toReadOnly();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
channelStatus(eci: string): OAuthChannelStatus {
|
|
90
|
+
let channel: ChannelReadOnly;
|
|
91
|
+
try {
|
|
92
|
+
channel = this.lookupChannelReadOnly(eci);
|
|
93
|
+
} catch (_e) {
|
|
94
|
+
return { eligible: false, enabled: false, hasSecret: false, reason: "Channel not found" };
|
|
95
|
+
}
|
|
96
|
+
if (!isOAuthEligibleChannel(channel)) {
|
|
97
|
+
return {
|
|
98
|
+
eligible: false,
|
|
99
|
+
enabled: false,
|
|
100
|
+
hasSecret: false,
|
|
101
|
+
reason: "Channel is not eligible for OAuth webhooks",
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
eligible: true,
|
|
106
|
+
enabled: true,
|
|
107
|
+
clientId: eci,
|
|
108
|
+
hasSecret: false,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async channelRequiresBearer(eci: string): Promise<boolean> {
|
|
113
|
+
try {
|
|
114
|
+
return isOAuthEligibleChannel(this.lookupChannelReadOnly(eci));
|
|
115
|
+
} catch (_e) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
meshRequiresOAuth(eci: string): boolean {
|
|
121
|
+
return meshRequiresOAuthForEci(this.deps.pf, eci);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** @deprecated use channelRequiresBearer */
|
|
125
|
+
async channelOAuthEnabled(eci: string): Promise<boolean> {
|
|
126
|
+
return this.channelRequiresBearer(eci);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async channelStatusAsync(eci: string): Promise<OAuthChannelStatus> {
|
|
130
|
+
let channel: ChannelReadOnly;
|
|
131
|
+
try {
|
|
132
|
+
channel = this.lookupChannelReadOnly(eci);
|
|
133
|
+
} catch (_e) {
|
|
134
|
+
return { eligible: false, enabled: false, hasSecret: false, reason: "Channel not found" };
|
|
135
|
+
}
|
|
136
|
+
if (!isOAuthEligibleChannel(channel)) {
|
|
137
|
+
return {
|
|
138
|
+
eligible: false,
|
|
139
|
+
enabled: false,
|
|
140
|
+
hasSecret: false,
|
|
141
|
+
reason: "Channel is not eligible for OAuth webhooks",
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
const stored = await this.getChannelOAuth(eci);
|
|
145
|
+
const hasSecret = !!stored;
|
|
146
|
+
return {
|
|
147
|
+
eligible: true,
|
|
148
|
+
enabled: true,
|
|
149
|
+
clientId: eci,
|
|
150
|
+
hasSecret,
|
|
151
|
+
createdAt: stored?.createdAt,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async createChannelSecret(eci: string): Promise<OAuthChannelCredentials> {
|
|
156
|
+
const channel = this.assertEligibleChannel(eci);
|
|
157
|
+
const rootPicoId = this.rootPicoIdForChannel(channel);
|
|
158
|
+
const client_secret = randomSecret();
|
|
159
|
+
const secretSalt = randomSalt();
|
|
160
|
+
const record: StoredChannelOAuth = {
|
|
161
|
+
channelEci: eci,
|
|
162
|
+
rootPicoId,
|
|
163
|
+
secretSalt,
|
|
164
|
+
secretHash: hashSecret(client_secret, secretSalt),
|
|
165
|
+
createdAt: new Date().toISOString(),
|
|
166
|
+
};
|
|
167
|
+
await this.deps.db.put([OAUTH_CHANNEL_PREFIX, eci], record);
|
|
168
|
+
await this.revokeTokens(eci);
|
|
169
|
+
return { client_id: eci, client_secret };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async revokeChannelSecret(eci: string): Promise<void> {
|
|
173
|
+
this.assertEligibleChannel(eci);
|
|
174
|
+
await this.deps.db.del([OAUTH_CHANNEL_PREFIX, eci]);
|
|
175
|
+
await this.revokeTokens(eci);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async revokeTokens(eci: string): Promise<void> {
|
|
179
|
+
const rows = await dbList<StoredOAuthToken>(this.deps.db, [OAUTH_TOKEN_PREFIX]);
|
|
180
|
+
for (const row of rows) {
|
|
181
|
+
if (row.value.channelEci === eci) {
|
|
182
|
+
await this.deps.db.del(row.key);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async clientCredentialsGrant(input: {
|
|
188
|
+
client_id?: string;
|
|
189
|
+
client_secret?: string;
|
|
190
|
+
expires_in?: number | string;
|
|
191
|
+
}): Promise<OAuthTokenResponse> {
|
|
192
|
+
const client_id = (input.client_id || "").trim();
|
|
193
|
+
const client_secret = (input.client_secret || "").trim();
|
|
194
|
+
if (client_id.startsWith("app_")) {
|
|
195
|
+
throw new OAuthError(
|
|
196
|
+
"OAuth apps require grant_type=authorization_code or refresh_token, not client_credentials",
|
|
197
|
+
"invalid_client",
|
|
198
|
+
401
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
if (!client_id || !client_secret) {
|
|
202
|
+
throw new OAuthError("Missing client credentials", "invalid_client", 401);
|
|
203
|
+
}
|
|
204
|
+
const channel = this.assertEligibleChannel(client_id);
|
|
205
|
+
const stored = await this.getChannelOAuth(client_id);
|
|
206
|
+
if (!stored) {
|
|
207
|
+
throw new OAuthError("Invalid client", "invalid_client", 401);
|
|
208
|
+
}
|
|
209
|
+
if (!verifySecret(client_secret, stored.secretSalt, stored.secretHash)) {
|
|
210
|
+
throw new OAuthError("Invalid client", "invalid_client", 401);
|
|
211
|
+
}
|
|
212
|
+
const tokenTtlMs = resolveTokenTtlMs(input.expires_in, this.tokenTtlMs);
|
|
213
|
+
const token = acg.mintAccessToken();
|
|
214
|
+
const expiresAt = tokenTtlMs === null ? 0 : Date.now() + tokenTtlMs;
|
|
215
|
+
const tokenRecord: StoredOAuthToken = {
|
|
216
|
+
token,
|
|
217
|
+
grant: "client_credentials",
|
|
218
|
+
channelEci: client_id,
|
|
219
|
+
rootPicoId: stored.rootPicoId,
|
|
220
|
+
expiresAt,
|
|
221
|
+
createdAt: new Date().toISOString(),
|
|
222
|
+
};
|
|
223
|
+
await this.deps.db.put([OAUTH_TOKEN_PREFIX, token], tokenRecord);
|
|
224
|
+
const response: OAuthTokenResponse = {
|
|
225
|
+
access_token: token,
|
|
226
|
+
token_type: "Bearer",
|
|
227
|
+
};
|
|
228
|
+
if (tokenTtlMs !== null) {
|
|
229
|
+
response.expires_in = Math.floor(tokenTtlMs / 1000);
|
|
230
|
+
}
|
|
231
|
+
return response;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async skyRequiresBearer(eci: string): Promise<boolean> {
|
|
235
|
+
if (await this.channelRequiresBearer(eci)) {
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
return this.meshRequiresOAuth(eci);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async validateBearerToken(token: string, channelEci: string): Promise<boolean> {
|
|
242
|
+
return this.validateSkyBearerToken(token, channelEci);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async validateSkyBearerToken(
|
|
246
|
+
token: string,
|
|
247
|
+
channelEci: string
|
|
248
|
+
): Promise<boolean> {
|
|
249
|
+
const trimmed = (token || "").trim();
|
|
250
|
+
if (!trimmed || !channelEci) {
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
const record = await this.getTokenRecord(trimmed);
|
|
254
|
+
if (!record) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
if (record.expiresAt !== 0 && record.expiresAt < Date.now()) {
|
|
258
|
+
await this.deps.db.del([OAUTH_TOKEN_PREFIX, trimmed]);
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
const grant = record.grant || "client_credentials";
|
|
262
|
+
if (grant === "authorization_code") {
|
|
263
|
+
return isChannelUnderRoot(this.deps.pf, channelEci, record.rootPicoId);
|
|
264
|
+
}
|
|
265
|
+
return record.channelEci === channelEci;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
listApps(rootPicoId: string): Promise<acg.OAuthAppSummary[]> {
|
|
269
|
+
return acg.listApps(this.acgContext(), rootPicoId);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
registerApp(
|
|
273
|
+
rootPicoId: string,
|
|
274
|
+
input: {
|
|
275
|
+
name?: string;
|
|
276
|
+
redirect_uris?: string[];
|
|
277
|
+
public_client?: boolean;
|
|
278
|
+
}
|
|
279
|
+
): Promise<acg.OAuthAppCredentials> {
|
|
280
|
+
return acg.registerApp(this.acgContext(), rootPicoId, input);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
revokeApp(rootPicoId: string, clientId: string): Promise<void> {
|
|
284
|
+
return acg.revokeApp(this.acgContext(), rootPicoId, clientId);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
getAppForAuthorize(params: acg.AuthorizeParams): Promise<acg.StoredOAuthApp> {
|
|
288
|
+
return acg.getAppForAuthorize(this.acgContext(), params);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
approveAuthorization(
|
|
292
|
+
session: { accountId: string; rootPicoId: string },
|
|
293
|
+
params: acg.ApproveParams
|
|
294
|
+
): Promise<string> {
|
|
295
|
+
return acg.approveAuthorization(this.acgContext(), session, params);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
authorizationCodeGrant(input: {
|
|
299
|
+
grant_type?: string;
|
|
300
|
+
code?: string;
|
|
301
|
+
redirect_uri?: string;
|
|
302
|
+
client_id?: string;
|
|
303
|
+
client_secret?: string;
|
|
304
|
+
code_verifier?: string;
|
|
305
|
+
}): Promise<OAuthTokenResponse> {
|
|
306
|
+
return acg.authorizationCodeGrant(this.acgContext(), input);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
refreshTokenGrant(input: {
|
|
310
|
+
grant_type?: string;
|
|
311
|
+
refresh_token?: string;
|
|
312
|
+
client_id?: string;
|
|
313
|
+
client_secret?: string;
|
|
314
|
+
}): Promise<OAuthTokenResponse> {
|
|
315
|
+
return acg.refreshTokenGrant(this.acgContext(), input);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
tokenGrant(body: Record<string, unknown>): Promise<OAuthTokenResponse> {
|
|
319
|
+
const grant_type = String(body.grant_type || "client_credentials").trim();
|
|
320
|
+
if (grant_type === "authorization_code") {
|
|
321
|
+
return this.authorizationCodeGrant(body as any);
|
|
322
|
+
}
|
|
323
|
+
if (grant_type === "refresh_token") {
|
|
324
|
+
return this.refreshTokenGrant(body as any);
|
|
325
|
+
}
|
|
326
|
+
return this.clientCredentialsGrant(body as any);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private acgContext(): acg.AcgContext {
|
|
330
|
+
return {
|
|
331
|
+
db: this.deps.db,
|
|
332
|
+
pf: this.deps.pf,
|
|
333
|
+
tokenTtlMs: this.tokenTtlMs,
|
|
334
|
+
putAccessToken: async (record) => {
|
|
335
|
+
const tokenRecord: StoredOAuthToken = {
|
|
336
|
+
token: record.token,
|
|
337
|
+
grant: "authorization_code",
|
|
338
|
+
rootPicoId: record.rootPicoId,
|
|
339
|
+
appId: record.appId,
|
|
340
|
+
accountId: record.accountId,
|
|
341
|
+
scope: record.scope,
|
|
342
|
+
expiresAt: record.expiresAt,
|
|
343
|
+
createdAt: new Date().toISOString(),
|
|
344
|
+
};
|
|
345
|
+
await this.deps.db.put([OAUTH_TOKEN_PREFIX, record.token], tokenRecord);
|
|
346
|
+
},
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private async getTokenRecord(token: string): Promise<StoredOAuthToken | null> {
|
|
351
|
+
return dbGet<StoredOAuthToken>(this.deps.db, [OAUTH_TOKEN_PREFIX, token]);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** Channel owner or an ancestor pico in the admin tree may manage webhook OAuth. */
|
|
355
|
+
assertChannelManagedBy(eci: string, callerPicoId: string): ChannelReadOnly {
|
|
356
|
+
const channel = this.lookupChannelReadOnly(eci);
|
|
357
|
+
let pico = this.deps.pf.lookupChannel(eci).pico;
|
|
358
|
+
while (true) {
|
|
359
|
+
if (pico.id === callerPicoId) {
|
|
360
|
+
return channel;
|
|
361
|
+
}
|
|
362
|
+
if (!pico.parent) {
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
pico = this.deps.pf.getPico(pico.parent);
|
|
366
|
+
}
|
|
367
|
+
throw new OAuthError(
|
|
368
|
+
"Channel is not in this pico's subtree",
|
|
369
|
+
"invalid_request",
|
|
370
|
+
403
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
private assertEligibleChannel(eci: string): ChannelReadOnly {
|
|
375
|
+
const channel = this.lookupChannelReadOnly(eci);
|
|
376
|
+
if (!isOAuthEligibleChannel(channel)) {
|
|
377
|
+
throw new OAuthError(
|
|
378
|
+
"Channel is not eligible for OAuth webhooks",
|
|
379
|
+
"invalid_client",
|
|
380
|
+
403
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
return channel;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
private rootPicoIdForChannel(channel: ChannelReadOnly): string {
|
|
387
|
+
const owner = this.deps.pf.lookupChannel(channel.id).pico;
|
|
388
|
+
let pico = owner;
|
|
389
|
+
while (pico.parent) {
|
|
390
|
+
pico = this.deps.pf.getPico(pico.parent);
|
|
391
|
+
}
|
|
392
|
+
return pico.id;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
private async getChannelOAuth(eci: string): Promise<StoredChannelOAuth | null> {
|
|
396
|
+
return dbGet<StoredChannelOAuth>(this.deps.db, [OAUTH_CHANNEL_PREFIX, eci]);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
async function dbGet<T>(db: PicoDb, key: PicoDbKey): Promise<T | null> {
|
|
401
|
+
try {
|
|
402
|
+
const value = await db.get(key);
|
|
403
|
+
return value === undefined ? null : (value as T);
|
|
404
|
+
} catch (err: any) {
|
|
405
|
+
if (err && (err.notFound || err.code === "LEVEL_NOT_FOUND")) {
|
|
406
|
+
return null;
|
|
407
|
+
}
|
|
408
|
+
throw err;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
async function dbList<T = any>(
|
|
413
|
+
db: PicoDb,
|
|
414
|
+
prefix: PicoDbKey
|
|
415
|
+
): Promise<{ key: PicoDbKey; value: T }[]> {
|
|
416
|
+
const out: { key: PicoDbKey; value: T }[] = [];
|
|
417
|
+
const iter = db.iterator({
|
|
418
|
+
gte: prefix,
|
|
419
|
+
lte: prefix.concat([undefined] as any),
|
|
420
|
+
});
|
|
421
|
+
for await (const [key, value] of iter) {
|
|
422
|
+
out.push({ key, value });
|
|
423
|
+
}
|
|
424
|
+
return out;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function resolveTokenTtlMs(
|
|
428
|
+
expires_in: number | string | undefined,
|
|
429
|
+
defaultMs: number
|
|
430
|
+
): number | null {
|
|
431
|
+
if (expires_in === undefined || expires_in === null || expires_in === "") {
|
|
432
|
+
return defaultMs;
|
|
433
|
+
}
|
|
434
|
+
if (typeof expires_in === "string" && expires_in.trim().toLowerCase() === "never") {
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
const seconds =
|
|
438
|
+
typeof expires_in === "string" ? Number.parseInt(expires_in, 10) : expires_in;
|
|
439
|
+
if (seconds === 0) {
|
|
440
|
+
return null;
|
|
441
|
+
}
|
|
442
|
+
if (
|
|
443
|
+
!Number.isFinite(seconds) ||
|
|
444
|
+
seconds < MIN_TOKEN_TTL_SEC ||
|
|
445
|
+
seconds > MAX_TOKEN_TTL_SEC
|
|
446
|
+
) {
|
|
447
|
+
throw new OAuthError(
|
|
448
|
+
`expires_in must be 0 (never), or between ${MIN_TOKEN_TTL_SEC} and ${MAX_TOKEN_TTL_SEC} seconds`,
|
|
449
|
+
"invalid_request",
|
|
450
|
+
400
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
return seconds * 1000;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function randomSecret(): string {
|
|
457
|
+
return crypto.randomBytes(32).toString("base64url");
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function randomSalt(): string {
|
|
461
|
+
return crypto.randomBytes(16).toString("base64url");
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function hashSecret(secret: string, salt: string): string {
|
|
465
|
+
return crypto.createHash("sha256").update(salt + secret).digest("base64url");
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function verifySecret(secret: string, salt: string, expected: string): boolean {
|
|
469
|
+
const actual = hashSecret(secret, salt);
|
|
470
|
+
if (actual.length !== expected.length) {
|
|
471
|
+
return false;
|
|
472
|
+
}
|
|
473
|
+
return crypto.timingSafeEqual(Buffer.from(actual), Buffer.from(expected));
|
|
474
|
+
}
|