@wix/zero-config-implementation 1.63.0 → 1.65.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.
@@ -0,0 +1,708 @@
1
+ import { Buffer as F } from "node:buffer";
2
+ import * as K from "node:crypto";
3
+ import { KeyObject as v, createPublicKey as G, createPrivateKey as oe, constants as D, createSecretKey as L } from "node:crypto";
4
+ import * as B from "node:util";
5
+ import { promisify as k } from "node:util";
6
+ const P = new TextEncoder(), I = new TextDecoder();
7
+ function ie(...e) {
8
+ const t = e.reduce((o, { length: a }) => o + a, 0), r = new Uint8Array(t);
9
+ let n = 0;
10
+ for (const o of e)
11
+ r.set(o, n), n += o.length;
12
+ return r;
13
+ }
14
+ function ae(e) {
15
+ let t = e;
16
+ return t instanceof Uint8Array && (t = I.decode(t)), t;
17
+ }
18
+ const R = (e) => new Uint8Array(F.from(ae(e), "base64url"));
19
+ class S extends Error {
20
+ static code = "ERR_JOSE_GENERIC";
21
+ code = "ERR_JOSE_GENERIC";
22
+ constructor(t, r) {
23
+ super(t, r), this.name = this.constructor.name, Error.captureStackTrace?.(this, this.constructor);
24
+ }
25
+ }
26
+ class h extends S {
27
+ static code = "ERR_JWT_CLAIM_VALIDATION_FAILED";
28
+ code = "ERR_JWT_CLAIM_VALIDATION_FAILED";
29
+ claim;
30
+ reason;
31
+ payload;
32
+ constructor(t, r, n = "unspecified", o = "unspecified") {
33
+ super(t, { cause: { claim: n, reason: o, payload: r } }), this.claim = n, this.reason = o, this.payload = r;
34
+ }
35
+ }
36
+ class N extends S {
37
+ static code = "ERR_JWT_EXPIRED";
38
+ code = "ERR_JWT_EXPIRED";
39
+ claim;
40
+ reason;
41
+ payload;
42
+ constructor(t, r, n = "unspecified", o = "unspecified") {
43
+ super(t, { cause: { claim: n, reason: o, payload: r } }), this.claim = n, this.reason = o, this.payload = r;
44
+ }
45
+ }
46
+ class se extends S {
47
+ static code = "ERR_JOSE_ALG_NOT_ALLOWED";
48
+ code = "ERR_JOSE_ALG_NOT_ALLOWED";
49
+ }
50
+ class w extends S {
51
+ static code = "ERR_JOSE_NOT_SUPPORTED";
52
+ code = "ERR_JOSE_NOT_SUPPORTED";
53
+ }
54
+ class c extends S {
55
+ static code = "ERR_JWS_INVALID";
56
+ code = "ERR_JWS_INVALID";
57
+ }
58
+ class q extends S {
59
+ static code = "ERR_JWT_INVALID";
60
+ code = "ERR_JWT_INVALID";
61
+ }
62
+ class ce extends S {
63
+ static code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
64
+ code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
65
+ constructor(t = "signature verification failed", r) {
66
+ super(t, r);
67
+ }
68
+ }
69
+ const z = (e) => B.types.isKeyObject(e), fe = K.webcrypto, C = (e) => B.types.isCryptoKey(e);
70
+ function l(e, t = "algorithm.name") {
71
+ return new TypeError(`CryptoKey does not support this operation, its ${t} must be ${e}`);
72
+ }
73
+ function T(e, t) {
74
+ return e.name === t;
75
+ }
76
+ function J(e) {
77
+ return parseInt(e.name.slice(4), 10);
78
+ }
79
+ function de(e) {
80
+ switch (e) {
81
+ case "ES256":
82
+ return "P-256";
83
+ case "ES384":
84
+ return "P-384";
85
+ case "ES512":
86
+ return "P-521";
87
+ default:
88
+ throw new Error("unreachable");
89
+ }
90
+ }
91
+ function ue(e, t) {
92
+ if (t.length && !t.some((r) => e.usages.includes(r))) {
93
+ let r = "CryptoKey does not support this operation, its usages must include ";
94
+ if (t.length > 2) {
95
+ const n = t.pop();
96
+ r += `one of ${t.join(", ")}, or ${n}.`;
97
+ } else t.length === 2 ? r += `one of ${t[0]} or ${t[1]}.` : r += `${t[0]}.`;
98
+ throw new TypeError(r);
99
+ }
100
+ }
101
+ function pe(e, t, ...r) {
102
+ switch (t) {
103
+ case "HS256":
104
+ case "HS384":
105
+ case "HS512": {
106
+ if (!T(e.algorithm, "HMAC"))
107
+ throw l("HMAC");
108
+ const n = parseInt(t.slice(2), 10);
109
+ if (J(e.algorithm.hash) !== n)
110
+ throw l(`SHA-${n}`, "algorithm.hash");
111
+ break;
112
+ }
113
+ case "RS256":
114
+ case "RS384":
115
+ case "RS512": {
116
+ if (!T(e.algorithm, "RSASSA-PKCS1-v1_5"))
117
+ throw l("RSASSA-PKCS1-v1_5");
118
+ const n = parseInt(t.slice(2), 10);
119
+ if (J(e.algorithm.hash) !== n)
120
+ throw l(`SHA-${n}`, "algorithm.hash");
121
+ break;
122
+ }
123
+ case "PS256":
124
+ case "PS384":
125
+ case "PS512": {
126
+ if (!T(e.algorithm, "RSA-PSS"))
127
+ throw l("RSA-PSS");
128
+ const n = parseInt(t.slice(2), 10);
129
+ if (J(e.algorithm.hash) !== n)
130
+ throw l(`SHA-${n}`, "algorithm.hash");
131
+ break;
132
+ }
133
+ case "EdDSA": {
134
+ if (e.algorithm.name !== "Ed25519" && e.algorithm.name !== "Ed448")
135
+ throw l("Ed25519 or Ed448");
136
+ break;
137
+ }
138
+ case "Ed25519": {
139
+ if (!T(e.algorithm, "Ed25519"))
140
+ throw l("Ed25519");
141
+ break;
142
+ }
143
+ case "ES256":
144
+ case "ES384":
145
+ case "ES512": {
146
+ if (!T(e.algorithm, "ECDSA"))
147
+ throw l("ECDSA");
148
+ const n = de(t);
149
+ if (e.algorithm.namedCurve !== n)
150
+ throw l(n, "algorithm.namedCurve");
151
+ break;
152
+ }
153
+ default:
154
+ throw new TypeError("CryptoKey does not support this operation");
155
+ }
156
+ ue(e, r);
157
+ }
158
+ function X(e, t, ...r) {
159
+ if (r = r.filter(Boolean), r.length > 2) {
160
+ const n = r.pop();
161
+ e += `one of type ${r.join(", ")}, or ${n}.`;
162
+ } else r.length === 2 ? e += `one of type ${r[0]} or ${r[1]}.` : e += `of type ${r[0]}.`;
163
+ return t == null ? e += ` Received ${t}` : typeof t == "function" && t.name ? e += ` Received function ${t.name}` : typeof t == "object" && t != null && t.constructor?.name && (e += ` Received an instance of ${t.constructor.name}`), e;
164
+ }
165
+ const W = (e, ...t) => X("Key must be ", e, ...t);
166
+ function Y(e, t, ...r) {
167
+ return X(`Key for the ${e} algorithm must be `, t, ...r);
168
+ }
169
+ const Q = (e) => z(e) || C(e), E = ["KeyObject"];
170
+ (globalThis.CryptoKey || fe?.CryptoKey) && E.push("CryptoKey");
171
+ const he = (...e) => {
172
+ const t = e.filter(Boolean);
173
+ if (t.length === 0 || t.length === 1)
174
+ return !0;
175
+ let r;
176
+ for (const n of t) {
177
+ const o = Object.keys(n);
178
+ if (!r || r.size === 0) {
179
+ r = new Set(o);
180
+ continue;
181
+ }
182
+ for (const a of o) {
183
+ if (r.has(a))
184
+ return !1;
185
+ r.add(a);
186
+ }
187
+ }
188
+ return !0;
189
+ };
190
+ function le(e) {
191
+ return typeof e == "object" && e !== null;
192
+ }
193
+ function A(e) {
194
+ if (!le(e) || Object.prototype.toString.call(e) !== "[object Object]")
195
+ return !1;
196
+ if (Object.getPrototypeOf(e) === null)
197
+ return !0;
198
+ let t = e;
199
+ for (; Object.getPrototypeOf(t) !== null; )
200
+ t = Object.getPrototypeOf(t);
201
+ return Object.getPrototypeOf(e) === t;
202
+ }
203
+ function g(e) {
204
+ return A(e) && typeof e.kty == "string";
205
+ }
206
+ function me(e) {
207
+ return e.kty !== "oct" && typeof e.d == "string";
208
+ }
209
+ function ye(e) {
210
+ return e.kty !== "oct" && typeof e.d > "u";
211
+ }
212
+ function we(e) {
213
+ return g(e) && e.kty === "oct" && typeof e.k == "string";
214
+ }
215
+ const Se = (e) => {
216
+ switch (e) {
217
+ case "prime256v1":
218
+ return "P-256";
219
+ case "secp384r1":
220
+ return "P-384";
221
+ case "secp521r1":
222
+ return "P-521";
223
+ case "secp256k1":
224
+ return "secp256k1";
225
+ default:
226
+ throw new w("Unsupported key curve for this operation");
227
+ }
228
+ }, be = (e, t) => {
229
+ let r;
230
+ if (C(e))
231
+ r = v.from(e);
232
+ else if (z(e))
233
+ r = e;
234
+ else {
235
+ if (g(e))
236
+ return e.crv;
237
+ throw new TypeError(W(e, ...E));
238
+ }
239
+ if (r.type === "secret")
240
+ throw new TypeError('only "private" or "public" type keys can be used for this operation');
241
+ switch (r.asymmetricKeyType) {
242
+ case "ed25519":
243
+ case "ed448":
244
+ return `Ed${r.asymmetricKeyType.slice(2)}`;
245
+ case "x25519":
246
+ case "x448":
247
+ return `X${r.asymmetricKeyType.slice(1)}`;
248
+ case "ec": {
249
+ const n = r.asymmetricKeyDetails.namedCurve;
250
+ return Se(n);
251
+ }
252
+ default:
253
+ throw new TypeError("Invalid asymmetric key type for this operation");
254
+ }
255
+ }, H = (e, t) => {
256
+ let r;
257
+ try {
258
+ e instanceof v ? r = e.asymmetricKeyDetails?.modulusLength : r = Buffer.from(e.n, "base64url").byteLength << 3;
259
+ } catch {
260
+ }
261
+ if (typeof r != "number" || r < 2048)
262
+ throw new TypeError(`${t} requires key modulusLength to be 2048 bits or larger`);
263
+ }, Ee = (e) => G({
264
+ key: F.from(e.replace(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, ""), "base64"),
265
+ type: "spki",
266
+ format: "der"
267
+ }), ge = (e) => e.d ? oe({ format: "jwk", key: e }) : G({ format: "jwk", key: e });
268
+ async function Ge(e, t, r) {
269
+ if (typeof e != "string" || e.indexOf("-----BEGIN PUBLIC KEY-----") !== 0)
270
+ throw new TypeError('"spki" must be SPKI formatted string');
271
+ return Ee(e);
272
+ }
273
+ async function Te(e, t) {
274
+ if (!A(e))
275
+ throw new TypeError("JWK must be an object");
276
+ switch (t ||= e.alg, e.kty) {
277
+ case "oct":
278
+ if (typeof e.k != "string" || !e.k)
279
+ throw new TypeError('missing "k" (Key Value) Parameter value');
280
+ return R(e.k);
281
+ case "RSA":
282
+ if ("oth" in e && e.oth !== void 0)
283
+ throw new w('RSA JWK "oth" (Other Primes Info) Parameter value is not supported');
284
+ case "EC":
285
+ case "OKP":
286
+ return ge({ ...e, alg: t });
287
+ default:
288
+ throw new w('Unsupported "kty" (Key Type) Parameter value');
289
+ }
290
+ }
291
+ const b = (e) => e?.[Symbol.toStringTag], O = (e, t, r) => {
292
+ if (t.use !== void 0 && t.use !== "sig")
293
+ throw new TypeError("Invalid key for this operation, when present its use must be sig");
294
+ if (t.key_ops !== void 0 && t.key_ops.includes?.(r) !== !0)
295
+ throw new TypeError(`Invalid key for this operation, when present its key_ops must include ${r}`);
296
+ if (t.alg !== void 0 && t.alg !== e)
297
+ throw new TypeError(`Invalid key for this operation, when present its alg must be ${e}`);
298
+ return !0;
299
+ }, ve = (e, t, r, n) => {
300
+ if (!(t instanceof Uint8Array)) {
301
+ if (n && g(t)) {
302
+ if (we(t) && O(e, t, r))
303
+ return;
304
+ throw new TypeError('JSON Web Key for symmetric algorithms must have JWK "kty" (Key Type) equal to "oct" and the JWK "k" (Key Value) present');
305
+ }
306
+ if (!Q(t))
307
+ throw new TypeError(Y(e, t, ...E, "Uint8Array", n ? "JSON Web Key" : null));
308
+ if (t.type !== "secret")
309
+ throw new TypeError(`${b(t)} instances for symmetric algorithms must be of type "secret"`);
310
+ }
311
+ }, Ae = (e, t, r, n) => {
312
+ if (n && g(t))
313
+ switch (r) {
314
+ case "sign":
315
+ if (me(t) && O(e, t, r))
316
+ return;
317
+ throw new TypeError("JSON Web Key for this operation be a private JWK");
318
+ case "verify":
319
+ if (ye(t) && O(e, t, r))
320
+ return;
321
+ throw new TypeError("JSON Web Key for this operation be a public JWK");
322
+ }
323
+ if (!Q(t))
324
+ throw new TypeError(Y(e, t, ...E, n ? "JSON Web Key" : null));
325
+ if (t.type === "secret")
326
+ throw new TypeError(`${b(t)} instances for asymmetric algorithms must not be of type "secret"`);
327
+ if (r === "sign" && t.type === "public")
328
+ throw new TypeError(`${b(t)} instances for asymmetric algorithm signing must be of type "private"`);
329
+ if (r === "decrypt" && t.type === "public")
330
+ throw new TypeError(`${b(t)} instances for asymmetric algorithm decryption must be of type "private"`);
331
+ if (t.algorithm && r === "verify" && t.type === "private")
332
+ throw new TypeError(`${b(t)} instances for asymmetric algorithm verifying must be of type "public"`);
333
+ if (t.algorithm && r === "encrypt" && t.type === "private")
334
+ throw new TypeError(`${b(t)} instances for asymmetric algorithm encryption must be of type "public"`);
335
+ };
336
+ function Z(e, t, r, n) {
337
+ t.startsWith("HS") || t === "dir" || t.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(t) ? ve(t, r, n, e) : Ae(t, r, n, e);
338
+ }
339
+ Z.bind(void 0, !1);
340
+ const U = Z.bind(void 0, !0);
341
+ function Ke(e, t, r, n, o) {
342
+ if (o.crit !== void 0 && n?.crit === void 0)
343
+ throw new e('"crit" (Critical) Header Parameter MUST be integrity protected');
344
+ if (!n || n.crit === void 0)
345
+ return /* @__PURE__ */ new Set();
346
+ if (!Array.isArray(n.crit) || n.crit.length === 0 || n.crit.some((i) => typeof i != "string" || i.length === 0))
347
+ throw new e('"crit" (Critical) Header Parameter MUST be an array of non-empty strings when present');
348
+ let a;
349
+ r !== void 0 ? a = new Map([...Object.entries(r), ...t.entries()]) : a = t;
350
+ for (const i of n.crit) {
351
+ if (!a.has(i))
352
+ throw new w(`Extension Header Parameter "${i}" is not recognized`);
353
+ if (o[i] === void 0)
354
+ throw new e(`Extension Header Parameter "${i}" is missing`);
355
+ if (a.get(i) && n[i] === void 0)
356
+ throw new e(`Extension Header Parameter "${i}" MUST be integrity protected`);
357
+ }
358
+ return new Set(n.crit);
359
+ }
360
+ const Pe = (e, t) => {
361
+ if (t !== void 0 && (!Array.isArray(t) || t.some((r) => typeof r != "string")))
362
+ throw new TypeError(`"${e}" option must be an array of strings`);
363
+ if (t)
364
+ return new Set(t);
365
+ };
366
+ function j(e) {
367
+ switch (e) {
368
+ case "PS256":
369
+ case "RS256":
370
+ case "ES256":
371
+ case "ES256K":
372
+ return "sha256";
373
+ case "PS384":
374
+ case "RS384":
375
+ case "ES384":
376
+ return "sha384";
377
+ case "PS512":
378
+ case "RS512":
379
+ case "ES512":
380
+ return "sha512";
381
+ case "Ed25519":
382
+ case "EdDSA":
383
+ return;
384
+ default:
385
+ throw new w(`alg ${e} is not supported either by JOSE or your javascript runtime`);
386
+ }
387
+ }
388
+ const Re = /* @__PURE__ */ new Map([
389
+ ["ES256", "P-256"],
390
+ ["ES256K", "secp256k1"],
391
+ ["ES384", "P-384"],
392
+ ["ES512", "P-521"]
393
+ ]);
394
+ function ee(e, t) {
395
+ let r, n, o;
396
+ if (t instanceof v)
397
+ r = t.asymmetricKeyType, n = t.asymmetricKeyDetails;
398
+ else
399
+ switch (o = !0, t.kty) {
400
+ case "RSA":
401
+ r = "rsa";
402
+ break;
403
+ case "EC":
404
+ r = "ec";
405
+ break;
406
+ case "OKP": {
407
+ if (t.crv === "Ed25519") {
408
+ r = "ed25519";
409
+ break;
410
+ }
411
+ if (t.crv === "Ed448") {
412
+ r = "ed448";
413
+ break;
414
+ }
415
+ throw new TypeError("Invalid key for this operation, its crv must be Ed25519 or Ed448");
416
+ }
417
+ default:
418
+ throw new TypeError("Invalid key for this operation, its kty must be RSA, OKP, or EC");
419
+ }
420
+ let a;
421
+ switch (e) {
422
+ case "Ed25519":
423
+ if (r !== "ed25519")
424
+ throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be ed25519");
425
+ break;
426
+ case "EdDSA":
427
+ if (!["ed25519", "ed448"].includes(r))
428
+ throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be ed25519 or ed448");
429
+ break;
430
+ case "RS256":
431
+ case "RS384":
432
+ case "RS512":
433
+ if (r !== "rsa")
434
+ throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be rsa");
435
+ H(t, e);
436
+ break;
437
+ case "PS256":
438
+ case "PS384":
439
+ case "PS512":
440
+ if (r === "rsa-pss") {
441
+ const { hashAlgorithm: i, mgf1HashAlgorithm: s, saltLength: f } = n, d = parseInt(e.slice(-3), 10);
442
+ if (i !== void 0 && (i !== `sha${d}` || s !== i))
443
+ throw new TypeError(`Invalid key for this operation, its RSA-PSS parameters do not meet the requirements of "alg" ${e}`);
444
+ if (f !== void 0 && f > d >> 3)
445
+ throw new TypeError(`Invalid key for this operation, its RSA-PSS parameter saltLength does not meet the requirements of "alg" ${e}`);
446
+ } else if (r !== "rsa")
447
+ throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be rsa or rsa-pss");
448
+ H(t, e), a = {
449
+ padding: D.RSA_PKCS1_PSS_PADDING,
450
+ saltLength: D.RSA_PSS_SALTLEN_DIGEST
451
+ };
452
+ break;
453
+ case "ES256":
454
+ case "ES256K":
455
+ case "ES384":
456
+ case "ES512": {
457
+ if (r !== "ec")
458
+ throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be ec");
459
+ const i = be(t), s = Re.get(e);
460
+ if (i !== s)
461
+ throw new TypeError(`Invalid key curve for the algorithm, its curve must be ${s}, got ${i}`);
462
+ a = { dsaEncoding: "ieee-p1363" };
463
+ break;
464
+ }
465
+ default:
466
+ throw new w(`alg ${e} is not supported either by JOSE or your javascript runtime`);
467
+ }
468
+ return o ? { format: "jwk", key: t, ...a } : a ? { ...a, key: t } : t;
469
+ }
470
+ function Ie(e) {
471
+ switch (e) {
472
+ case "HS256":
473
+ return "sha256";
474
+ case "HS384":
475
+ return "sha384";
476
+ case "HS512":
477
+ return "sha512";
478
+ default:
479
+ throw new w(`alg ${e} is not supported either by JOSE or your javascript runtime`);
480
+ }
481
+ }
482
+ function te(e, t, r) {
483
+ if (t instanceof Uint8Array) {
484
+ if (!e.startsWith("HS"))
485
+ throw new TypeError(W(t, ...E));
486
+ return L(t);
487
+ }
488
+ if (t instanceof v)
489
+ return t;
490
+ if (C(t))
491
+ return pe(t, e, r), v.from(t);
492
+ if (g(t))
493
+ return e.startsWith("HS") ? L(Buffer.from(t.k, "base64url")) : t;
494
+ throw new TypeError(W(t, ...E, "Uint8Array", "JSON Web Key"));
495
+ }
496
+ const _e = k(K.sign), Je = async (e, t, r) => {
497
+ const n = te(e, t, "sign");
498
+ if (e.startsWith("HS")) {
499
+ const o = K.createHmac(Ie(e), n);
500
+ return o.update(r), o.digest();
501
+ }
502
+ return _e(j(e), r, ee(e, n));
503
+ }, We = k(K.verify), Oe = async (e, t, r, n) => {
504
+ const o = te(e, t, "verify");
505
+ if (e.startsWith("HS")) {
506
+ const s = await Je(e, o, n), f = r;
507
+ try {
508
+ return K.timingSafeEqual(f, s);
509
+ } catch {
510
+ return !1;
511
+ }
512
+ }
513
+ const a = j(e), i = ee(e, o);
514
+ try {
515
+ return await We(a, n, i, r);
516
+ } catch {
517
+ return !1;
518
+ }
519
+ };
520
+ async function Ce(e, t, r) {
521
+ if (!A(e))
522
+ throw new c("Flattened JWS must be an object");
523
+ if (e.protected === void 0 && e.header === void 0)
524
+ throw new c('Flattened JWS must have either of the "protected" or "header" members');
525
+ if (e.protected !== void 0 && typeof e.protected != "string")
526
+ throw new c("JWS Protected Header incorrect type");
527
+ if (e.payload === void 0)
528
+ throw new c("JWS Payload missing");
529
+ if (typeof e.signature != "string")
530
+ throw new c("JWS Signature missing or incorrect type");
531
+ if (e.header !== void 0 && !A(e.header))
532
+ throw new c("JWS Unprotected Header incorrect type");
533
+ let n = {};
534
+ if (e.protected)
535
+ try {
536
+ const _ = R(e.protected);
537
+ n = JSON.parse(I.decode(_));
538
+ } catch {
539
+ throw new c("JWS Protected Header is invalid");
540
+ }
541
+ if (!he(n, e.header))
542
+ throw new c("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
543
+ const o = {
544
+ ...n,
545
+ ...e.header
546
+ }, a = Ke(c, /* @__PURE__ */ new Map([["b64", !0]]), r?.crit, n, o);
547
+ let i = !0;
548
+ if (a.has("b64") && (i = n.b64, typeof i != "boolean"))
549
+ throw new c('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
550
+ const { alg: s } = o;
551
+ if (typeof s != "string" || !s)
552
+ throw new c('JWS "alg" (Algorithm) Header Parameter missing or invalid');
553
+ const f = r && Pe("algorithms", r.algorithms);
554
+ if (f && !f.has(s))
555
+ throw new se('"alg" (Algorithm) Header Parameter value not allowed');
556
+ if (i) {
557
+ if (typeof e.payload != "string")
558
+ throw new c("JWS Payload must be a string");
559
+ } else if (typeof e.payload != "string" && !(e.payload instanceof Uint8Array))
560
+ throw new c("JWS Payload must be a string or an Uint8Array instance");
561
+ let d = !1;
562
+ typeof t == "function" ? (t = await t(n, e), d = !0, U(s, t, "verify"), g(t) && (t = await Te(t, s))) : U(s, t, "verify");
563
+ const y = ie(P.encode(e.protected ?? ""), P.encode("."), typeof e.payload == "string" ? P.encode(e.payload) : e.payload);
564
+ let p;
565
+ try {
566
+ p = R(e.signature);
567
+ } catch {
568
+ throw new c("Failed to base64url decode the signature");
569
+ }
570
+ if (!await Oe(s, t, p, y))
571
+ throw new ce();
572
+ let m;
573
+ if (i)
574
+ try {
575
+ m = R(e.payload);
576
+ } catch {
577
+ throw new c("Failed to base64url decode the payload");
578
+ }
579
+ else typeof e.payload == "string" ? m = P.encode(e.payload) : m = e.payload;
580
+ const u = { payload: m };
581
+ return e.protected !== void 0 && (u.protectedHeader = n), e.header !== void 0 && (u.unprotectedHeader = e.header), d ? { ...u, key: t } : u;
582
+ }
583
+ async function $e(e, t, r) {
584
+ if (e instanceof Uint8Array && (e = I.decode(e)), typeof e != "string")
585
+ throw new c("Compact JWS must be a string or Uint8Array");
586
+ const { 0: n, 1: o, 2: a, length: i } = e.split(".");
587
+ if (i !== 3)
588
+ throw new c("Invalid Compact JWS");
589
+ const s = await Ce({ payload: o, protected: n, signature: a }, t, r), f = { payload: s.payload, protectedHeader: s.protectedHeader };
590
+ return typeof t == "function" ? { ...f, key: s.key } : f;
591
+ }
592
+ const xe = (e) => Math.floor(e.getTime() / 1e3), re = 60, ne = re * 60, $ = ne * 24, De = $ * 7, Le = $ * 365.25, Ne = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i, M = (e) => {
593
+ const t = Ne.exec(e);
594
+ if (!t || t[4] && t[1])
595
+ throw new TypeError("Invalid time period format");
596
+ const r = parseFloat(t[2]), n = t[3].toLowerCase();
597
+ let o;
598
+ switch (n) {
599
+ case "sec":
600
+ case "secs":
601
+ case "second":
602
+ case "seconds":
603
+ case "s":
604
+ o = Math.round(r);
605
+ break;
606
+ case "minute":
607
+ case "minutes":
608
+ case "min":
609
+ case "mins":
610
+ case "m":
611
+ o = Math.round(r * re);
612
+ break;
613
+ case "hour":
614
+ case "hours":
615
+ case "hr":
616
+ case "hrs":
617
+ case "h":
618
+ o = Math.round(r * ne);
619
+ break;
620
+ case "day":
621
+ case "days":
622
+ case "d":
623
+ o = Math.round(r * $);
624
+ break;
625
+ case "week":
626
+ case "weeks":
627
+ case "w":
628
+ o = Math.round(r * De);
629
+ break;
630
+ default:
631
+ o = Math.round(r * Le);
632
+ break;
633
+ }
634
+ return t[1] === "-" || t[4] === "ago" ? -o : o;
635
+ }, V = (e) => e.toLowerCase().replace(/^application\//, ""), He = (e, t) => typeof e == "string" ? t.includes(e) : Array.isArray(e) ? t.some(Set.prototype.has.bind(new Set(e))) : !1, Ue = (e, t, r = {}) => {
636
+ let n;
637
+ try {
638
+ n = JSON.parse(I.decode(t));
639
+ } catch {
640
+ }
641
+ if (!A(n))
642
+ throw new q("JWT Claims Set must be a top-level JSON object");
643
+ const { typ: o } = r;
644
+ if (o && (typeof e.typ != "string" || V(e.typ) !== V(o)))
645
+ throw new h('unexpected "typ" JWT header value', n, "typ", "check_failed");
646
+ const { requiredClaims: a = [], issuer: i, subject: s, audience: f, maxTokenAge: d } = r, y = [...a];
647
+ d !== void 0 && y.push("iat"), f !== void 0 && y.push("aud"), s !== void 0 && y.push("sub"), i !== void 0 && y.push("iss");
648
+ for (const u of new Set(y.reverse()))
649
+ if (!(u in n))
650
+ throw new h(`missing required "${u}" claim`, n, u, "missing");
651
+ if (i && !(Array.isArray(i) ? i : [i]).includes(n.iss))
652
+ throw new h('unexpected "iss" claim value', n, "iss", "check_failed");
653
+ if (s && n.sub !== s)
654
+ throw new h('unexpected "sub" claim value', n, "sub", "check_failed");
655
+ if (f && !He(n.aud, typeof f == "string" ? [f] : f))
656
+ throw new h('unexpected "aud" claim value', n, "aud", "check_failed");
657
+ let p;
658
+ switch (typeof r.clockTolerance) {
659
+ case "string":
660
+ p = M(r.clockTolerance);
661
+ break;
662
+ case "number":
663
+ p = r.clockTolerance;
664
+ break;
665
+ case "undefined":
666
+ p = 0;
667
+ break;
668
+ default:
669
+ throw new TypeError("Invalid clockTolerance option type");
670
+ }
671
+ const { currentDate: x } = r, m = xe(x || /* @__PURE__ */ new Date());
672
+ if ((n.iat !== void 0 || d) && typeof n.iat != "number")
673
+ throw new h('"iat" claim must be a number', n, "iat", "invalid");
674
+ if (n.nbf !== void 0) {
675
+ if (typeof n.nbf != "number")
676
+ throw new h('"nbf" claim must be a number', n, "nbf", "invalid");
677
+ if (n.nbf > m + p)
678
+ throw new h('"nbf" claim timestamp check failed', n, "nbf", "check_failed");
679
+ }
680
+ if (n.exp !== void 0) {
681
+ if (typeof n.exp != "number")
682
+ throw new h('"exp" claim must be a number', n, "exp", "invalid");
683
+ if (n.exp <= m - p)
684
+ throw new N('"exp" claim timestamp check failed', n, "exp", "check_failed");
685
+ }
686
+ if (d) {
687
+ const u = m - n.iat, _ = typeof d == "number" ? d : M(d);
688
+ if (u - p > _)
689
+ throw new N('"iat" claim timestamp check failed (too far in the past)', n, "iat", "check_failed");
690
+ if (u < 0 - p)
691
+ throw new h('"iat" claim timestamp check failed (it should be in the past)', n, "iat", "check_failed");
692
+ }
693
+ return n;
694
+ };
695
+ async function Be(e, t, r) {
696
+ const n = await $e(e, t, r);
697
+ if (n.protectedHeader.crit?.includes("b64") && n.protectedHeader.b64 === !1)
698
+ throw new q("JWTs MUST NOT use unencoded payload");
699
+ const a = { payload: Ue(n.protectedHeader, n.payload, r), protectedHeader: n.protectedHeader };
700
+ return typeof t == "function" ? { ...a, key: n.key } : a;
701
+ }
702
+ export {
703
+ $e as compactVerify,
704
+ Ce as flattenedVerify,
705
+ Te as importJWK,
706
+ Ge as importSPKI,
707
+ Be as jwtVerify
708
+ };