@vibes.diy/identity 14.3.395 → 14.3.397
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/dash-api/token.js +3 -2
- package/package.json +2 -2
- package/runtime/settled-once.d.ts +1 -0
- package/runtime/settled-once.js +10 -0
- package/sts/index.js +47 -50
package/dash-api/token.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ResolveOnce, Result, param, exception2Result, isArrayBuffer, isUint8Array } from "@adviser/cement";
|
|
2
2
|
import { ensureSuperThis } from "../runtime/superthis.js";
|
|
3
|
+
import { settledOnce } from "../runtime/settled-once.js";
|
|
3
4
|
import * as sts from "../sts/index.js";
|
|
4
5
|
import { decodeProtectedHeader, exportJWK } from "jose";
|
|
5
6
|
import { ClerkClaimSchema } from "../clerk-claim.js";
|
|
@@ -59,7 +60,7 @@ export class DeviceIdApiToken {
|
|
|
59
60
|
return Result.Err(res.error);
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
|
-
export const deviceIdCAFromEnv =
|
|
63
|
+
export const deviceIdCAFromEnv = settledOnce(async (sthis) => {
|
|
63
64
|
const rEnv = sthis.env.gets({ DEVICE_ID_CA_PRIV_KEY: param.REQUIRED, DEVICE_ID_CA_CERT: param.REQUIRED });
|
|
64
65
|
if (rEnv.isErr()) {
|
|
65
66
|
throw rEnv.Err();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vibes.diy/identity",
|
|
3
|
-
"version": "14.3.
|
|
3
|
+
"version": "14.3.397",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"@adviser/ts-xxhash": "~1.0.2",
|
|
20
20
|
"@clerk/shared": "~4.44.1",
|
|
21
21
|
"@hono/node-server": "~2.1.5",
|
|
22
|
-
"@vibes.diy/cmd-tools": "14.3.
|
|
22
|
+
"@vibes.diy/cmd-tools": "14.3.397",
|
|
23
23
|
"arktype": "~2.2.9",
|
|
24
24
|
"hono": "~4.13.16",
|
|
25
25
|
"jose": "~6.2.17",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function settledOnce<A extends unknown[], T>(load: (...args: A) => Promise<T>): (...args: A) => Promise<T>;
|
package/sts/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BuildURI,
|
|
1
|
+
import { BuildURI, Option, Result, exception2Result, timeouted } from "@adviser/cement";
|
|
2
2
|
import { exportJWK, importJWK as joseImportJWK, jwtVerify, importSPKI } from "jose";
|
|
3
3
|
import { base58btc } from "multiformats/bases/base58";
|
|
4
4
|
import { z } from "zod";
|
|
@@ -236,9 +236,40 @@ export function isFetchWellKnownJwksResultErr(r) {
|
|
|
236
236
|
export function isFetchWellKnownJwksResultTimeout(r) {
|
|
237
237
|
return r.type === "timeout";
|
|
238
238
|
}
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
239
|
+
const WELL_KNOWN_JWKS_TTL_MS = 30 * 60 * 1000;
|
|
240
|
+
const keysFromWellKnownJwksCache = new Map();
|
|
241
|
+
async function loadWellKnownJwks(url, opts) {
|
|
242
|
+
const timeout = await timeouted(opts
|
|
243
|
+
.fetch(url, {
|
|
244
|
+
method: "GET",
|
|
245
|
+
})
|
|
246
|
+
.then((res) => {
|
|
247
|
+
if (!res.ok) {
|
|
248
|
+
throw new Error(`Failed to fetch well-known JWKS from ${url}: ${res.status} ${res.statusText}`);
|
|
249
|
+
}
|
|
250
|
+
return res.json();
|
|
251
|
+
}), {
|
|
252
|
+
timeout: opts.fetchTimeoutMs || 1000,
|
|
253
|
+
});
|
|
254
|
+
switch (timeout.state) {
|
|
255
|
+
case "timeout":
|
|
256
|
+
return { result: { type: "timeout", url }, keep: false };
|
|
257
|
+
case "error":
|
|
258
|
+
return { result: { type: "error", error: timeout.error, url }, keep: false };
|
|
259
|
+
case "success": {
|
|
260
|
+
const parsed = z.object({ keys: JWKPublicSchema.array() }).safeParse(timeout.value);
|
|
261
|
+
if (!parsed.success) {
|
|
262
|
+
return {
|
|
263
|
+
result: { type: "error", error: new Error(`Invalid JWKS format from ${url}: ${parsed.error.message}`), url },
|
|
264
|
+
keep: true,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
return { result: { type: "ok", keys: parsed.data.keys, url }, keep: true };
|
|
268
|
+
}
|
|
269
|
+
default:
|
|
270
|
+
throw new Error("unreachable");
|
|
271
|
+
}
|
|
272
|
+
}
|
|
242
273
|
export async function fetchWellKnownJwks(urls, iopts = {}) {
|
|
243
274
|
const opts = {
|
|
244
275
|
fetchTimeoutMs: 1000,
|
|
@@ -260,52 +291,18 @@ export async function fetchWellKnownJwks(urls, iopts = {}) {
|
|
|
260
291
|
})
|
|
261
292
|
.filter((u) => !!u)
|
|
262
293
|
.map(async (url) => {
|
|
263
|
-
const
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
}
|
|
273
|
-
return res.json();
|
|
274
|
-
}), {
|
|
275
|
-
timeout: opts.fetchTimeoutMs || 1000,
|
|
276
|
-
});
|
|
277
|
-
switch (timeout.state) {
|
|
278
|
-
case "timeout":
|
|
279
|
-
onceFn.reset();
|
|
280
|
-
return {
|
|
281
|
-
type: "timeout",
|
|
282
|
-
url,
|
|
283
|
-
};
|
|
284
|
-
case "error":
|
|
285
|
-
onceFn.reset();
|
|
286
|
-
return {
|
|
287
|
-
type: "error",
|
|
288
|
-
error: timeout.error,
|
|
289
|
-
url,
|
|
290
|
-
};
|
|
291
|
-
case "success": {
|
|
292
|
-
const parsed = z.object({ keys: JWKPublicSchema.array() }).safeParse(timeout.value);
|
|
293
|
-
if (!parsed.success) {
|
|
294
|
-
return {
|
|
295
|
-
type: "error",
|
|
296
|
-
error: new Error(`Invalid JWKS format from ${url}: ${parsed.error.message}`),
|
|
297
|
-
url,
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
return {
|
|
301
|
-
type: "ok",
|
|
302
|
-
keys: parsed.data.keys,
|
|
303
|
-
url,
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
default:
|
|
307
|
-
throw new Error("unreachable");
|
|
294
|
+
const kept = keysFromWellKnownJwksCache.get(url);
|
|
295
|
+
if (kept !== undefined && kept.expiresAtMs > Date.now()) {
|
|
296
|
+
return kept.result;
|
|
297
|
+
}
|
|
298
|
+
const loaded = await loadWellKnownJwks(url, opts);
|
|
299
|
+
if (loaded.keep) {
|
|
300
|
+
const settledMeanwhile = keysFromWellKnownJwksCache.get(url);
|
|
301
|
+
if (settledMeanwhile !== undefined && settledMeanwhile.expiresAtMs > Date.now()) {
|
|
302
|
+
return settledMeanwhile.result;
|
|
308
303
|
}
|
|
309
|
-
|
|
304
|
+
keysFromWellKnownJwksCache.set(url, { result: loaded.result, expiresAtMs: Date.now() + WELL_KNOWN_JWKS_TTL_MS });
|
|
305
|
+
}
|
|
306
|
+
return loaded.result;
|
|
310
307
|
}));
|
|
311
308
|
}
|