@versini/auth-common 4.4.0 → 4.5.1

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/dist/index.js CHANGED
@@ -1,20 +1,17 @@
1
- var ne = Object.defineProperty;
2
- var ae = (e, t, r) => t in e ? ne(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
3
- var u = (e, t, r) => ae(e, typeof t != "symbol" ? t + "" : t, r);
4
1
  /*!
5
- @versini/auth-common v4.4.0
2
+ @versini/auth-common v4.5.1
6
3
  © 2025 gizmette.com
7
4
  */
8
5
  try {
9
6
  window.__VERSINI_AUTH_COMMON__ || (window.__VERSINI_AUTH_COMMON__ = {
10
- version: "4.4.0",
11
- buildTime: "06/02/2025 05:45 PM EDT",
7
+ version: "4.5.1",
8
+ buildTime: "09/01/2025 07:36 PM EDT",
12
9
  homepage: "https://github.com/aversini/auth-client",
13
10
  license: "MIT"
14
11
  });
15
12
  } catch {
16
13
  }
17
- const nt = {
14
+ const it = {
18
15
  ID_TOKEN: "id_token",
19
16
  ACCESS_TOKEN: "token",
20
17
  ID_AND_ACCESS_TOKEN: "id_token token",
@@ -22,12 +19,12 @@ const nt = {
22
19
  REFRESH_TOKEN: "refresh_token",
23
20
  PASSKEY: "passkey",
24
21
  AUTH0: "auth0"
25
- }, at = {
22
+ }, st = {
26
23
  CLIENT_ID: "X-Auth-ClientId",
27
24
  AUTH_TYPE: "X-Auth-Type"
28
- }, ie = {
25
+ }, ae = {
29
26
  ACCESS_TOKEN: "access_token"
30
- }, K = {
27
+ }, y = {
31
28
  ALG: "RS256",
32
29
  USER_ID_KEY: "sub",
33
30
  USERNAME_KEY: "username",
@@ -38,6 +35,7 @@ const nt = {
38
35
  EXPIRES_AT_KEY: "exp",
39
36
  CREATED_AT_KEY: "iat",
40
37
  SCOPES_KEY: "scopes",
38
+ SCOPE_KEY: "scope",
41
39
  CLIENT_ID_KEY: "aud",
42
40
  ISSUER: "gizmette.com"
43
41
  }, oe = `-----BEGIN PUBLIC KEY-----
@@ -48,24 +46,24 @@ aMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+
48
46
  l0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE
49
47
  sjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81
50
48
  awIDAQAB
51
- -----END PUBLIC KEY-----`, it = {
49
+ -----END PUBLIC KEY-----`, ct = {
52
50
  ACCESS: "5m",
53
51
  ID: "90d",
54
52
  REFRESH: "90d"
55
- }, ot = {
53
+ }, dt = {
56
54
  CODE: "code",
57
55
  LOGOUT: "logout",
58
56
  LOGIN: "login",
59
57
  REFRESH: "refresh"
60
- }, O = new TextEncoder(), T = new TextDecoder();
61
- function se(...e) {
62
- const t = e.reduce((a, { length: i }) => a + i, 0), r = new Uint8Array(t);
58
+ }, R = new TextEncoder(), v = new TextDecoder();
59
+ function ie(...e) {
60
+ const t = e.reduce((a, { length: o }) => a + o, 0), r = new Uint8Array(t);
63
61
  let n = 0;
64
62
  for (const a of e)
65
63
  r.set(a, n), n += a.length;
66
64
  return r;
67
65
  }
68
- function ce(e) {
66
+ function V(e) {
69
67
  if (Uint8Array.fromBase64)
70
68
  return Uint8Array.fromBase64(e);
71
69
  const t = atob(e), r = new Uint8Array(t.length);
@@ -73,95 +71,79 @@ function ce(e) {
73
71
  r[n] = t.charCodeAt(n);
74
72
  return r;
75
73
  }
76
- function P(e) {
74
+ function T(e) {
77
75
  if (Uint8Array.fromBase64)
78
- return Uint8Array.fromBase64(typeof e == "string" ? e : T.decode(e), {
76
+ return Uint8Array.fromBase64(typeof e == "string" ? e : v.decode(e), {
79
77
  alphabet: "base64url"
80
78
  });
81
79
  let t = e;
82
- t instanceof Uint8Array && (t = T.decode(t)), t = t.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
80
+ t instanceof Uint8Array && (t = v.decode(t)), t = t.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
83
81
  try {
84
- return ce(t);
82
+ return V(t);
85
83
  } catch {
86
84
  throw new TypeError("The input to be decoded is not correctly encoded.");
87
85
  }
88
86
  }
89
- class w extends Error {
90
- constructor(r, n) {
91
- var a;
92
- super(r, n);
93
- u(this, "code", "ERR_JOSE_GENERIC");
94
- this.name = this.constructor.name, (a = Error.captureStackTrace) == null || a.call(Error, this, this.constructor);
87
+ class A extends Error {
88
+ static code = "ERR_JOSE_GENERIC";
89
+ code = "ERR_JOSE_GENERIC";
90
+ constructor(t, r) {
91
+ super(t, r), this.name = this.constructor.name, Error.captureStackTrace?.(this, this.constructor);
95
92
  }
96
93
  }
97
- u(w, "code", "ERR_JOSE_GENERIC");
98
- class h extends w {
99
- constructor(r, n, a = "unspecified", i = "unspecified") {
100
- super(r, { cause: { claim: a, reason: i, payload: n } });
101
- u(this, "code", "ERR_JWT_CLAIM_VALIDATION_FAILED");
102
- u(this, "claim");
103
- u(this, "reason");
104
- u(this, "payload");
105
- this.claim = a, this.reason = i, this.payload = n;
94
+ class p extends A {
95
+ static code = "ERR_JWT_CLAIM_VALIDATION_FAILED";
96
+ code = "ERR_JWT_CLAIM_VALIDATION_FAILED";
97
+ claim;
98
+ reason;
99
+ payload;
100
+ constructor(t, r, n = "unspecified", a = "unspecified") {
101
+ super(t, { cause: { claim: n, reason: a, payload: r } }), this.claim = n, this.reason = a, this.payload = r;
106
102
  }
107
103
  }
108
- u(h, "code", "ERR_JWT_CLAIM_VALIDATION_FAILED");
109
- class J extends w {
110
- constructor(r, n, a = "unspecified", i = "unspecified") {
111
- super(r, { cause: { claim: a, reason: i, payload: n } });
112
- u(this, "code", "ERR_JWT_EXPIRED");
113
- u(this, "claim");
114
- u(this, "reason");
115
- u(this, "payload");
116
- this.claim = a, this.reason = i, this.payload = n;
104
+ class U extends A {
105
+ static code = "ERR_JWT_EXPIRED";
106
+ code = "ERR_JWT_EXPIRED";
107
+ claim;
108
+ reason;
109
+ payload;
110
+ constructor(t, r, n = "unspecified", a = "unspecified") {
111
+ super(t, { cause: { claim: n, reason: a, payload: r } }), this.claim = n, this.reason = a, this.payload = r;
117
112
  }
118
113
  }
119
- u(J, "code", "ERR_JWT_EXPIRED");
120
- class F extends w {
121
- constructor() {
122
- super(...arguments);
123
- u(this, "code", "ERR_JOSE_ALG_NOT_ALLOWED");
124
- }
114
+ class se extends A {
115
+ static code = "ERR_JOSE_ALG_NOT_ALLOWED";
116
+ code = "ERR_JOSE_ALG_NOT_ALLOWED";
125
117
  }
126
- u(F, "code", "ERR_JOSE_ALG_NOT_ALLOWED");
127
- class E extends w {
128
- constructor() {
129
- super(...arguments);
130
- u(this, "code", "ERR_JOSE_NOT_SUPPORTED");
131
- }
118
+ class E extends A {
119
+ static code = "ERR_JOSE_NOT_SUPPORTED";
120
+ code = "ERR_JOSE_NOT_SUPPORTED";
132
121
  }
133
- u(E, "code", "ERR_JOSE_NOT_SUPPORTED");
134
- class d extends w {
135
- constructor() {
136
- super(...arguments);
137
- u(this, "code", "ERR_JWS_INVALID");
138
- }
122
+ class d extends A {
123
+ static code = "ERR_JWS_INVALID";
124
+ code = "ERR_JWS_INVALID";
139
125
  }
140
- u(d, "code", "ERR_JWS_INVALID");
141
- class y extends w {
142
- constructor() {
143
- super(...arguments);
144
- u(this, "code", "ERR_JWT_INVALID");
145
- }
126
+ class S extends A {
127
+ static code = "ERR_JWT_INVALID";
128
+ code = "ERR_JWT_INVALID";
146
129
  }
147
- u(y, "code", "ERR_JWT_INVALID");
148
- class V extends w {
149
- constructor(r = "signature verification failed", n) {
150
- super(r, n);
151
- u(this, "code", "ERR_JWS_SIGNATURE_VERIFICATION_FAILED");
130
+ class ce extends A {
131
+ static code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
132
+ code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
133
+ constructor(t = "signature verification failed", r) {
134
+ super(t, r);
152
135
  }
153
136
  }
154
- u(V, "code", "ERR_JWS_SIGNATURE_VERIFICATION_FAILED");
155
- function S(e, t = "algorithm.name") {
137
+ function h(e, t = "algorithm.name") {
156
138
  return new TypeError(`CryptoKey does not support this operation, its ${t} must be ${e}`);
157
139
  }
158
- function v(e, t) {
140
+ function b(e, t) {
159
141
  return e.name === t;
160
142
  }
161
- function D(e) {
143
+ function W(e) {
162
144
  return parseInt(e.name.slice(4), 10);
163
145
  }
164
- function ue(e) {
146
+ function de(e) {
165
147
  switch (e) {
166
148
  case "ES256":
167
149
  return "P-256";
@@ -173,7 +155,7 @@ function ue(e) {
173
155
  throw new Error("unreachable");
174
156
  }
175
157
  }
176
- function de(e, t) {
158
+ function ue(e, t) {
177
159
  if (!e.usages.includes(t))
178
160
  throw new TypeError(`CryptoKey does not support this operation, its usages must include ${t}.`);
179
161
  }
@@ -182,73 +164,79 @@ function fe(e, t, r) {
182
164
  case "HS256":
183
165
  case "HS384":
184
166
  case "HS512": {
185
- if (!v(e.algorithm, "HMAC"))
186
- throw S("HMAC");
167
+ if (!b(e.algorithm, "HMAC"))
168
+ throw h("HMAC");
187
169
  const n = parseInt(t.slice(2), 10);
188
- if (D(e.algorithm.hash) !== n)
189
- throw S(`SHA-${n}`, "algorithm.hash");
170
+ if (W(e.algorithm.hash) !== n)
171
+ throw h(`SHA-${n}`, "algorithm.hash");
190
172
  break;
191
173
  }
192
174
  case "RS256":
193
175
  case "RS384":
194
176
  case "RS512": {
195
- if (!v(e.algorithm, "RSASSA-PKCS1-v1_5"))
196
- throw S("RSASSA-PKCS1-v1_5");
177
+ if (!b(e.algorithm, "RSASSA-PKCS1-v1_5"))
178
+ throw h("RSASSA-PKCS1-v1_5");
197
179
  const n = parseInt(t.slice(2), 10);
198
- if (D(e.algorithm.hash) !== n)
199
- throw S(`SHA-${n}`, "algorithm.hash");
180
+ if (W(e.algorithm.hash) !== n)
181
+ throw h(`SHA-${n}`, "algorithm.hash");
200
182
  break;
201
183
  }
202
184
  case "PS256":
203
185
  case "PS384":
204
186
  case "PS512": {
205
- if (!v(e.algorithm, "RSA-PSS"))
206
- throw S("RSA-PSS");
187
+ if (!b(e.algorithm, "RSA-PSS"))
188
+ throw h("RSA-PSS");
207
189
  const n = parseInt(t.slice(2), 10);
208
- if (D(e.algorithm.hash) !== n)
209
- throw S(`SHA-${n}`, "algorithm.hash");
190
+ if (W(e.algorithm.hash) !== n)
191
+ throw h(`SHA-${n}`, "algorithm.hash");
210
192
  break;
211
193
  }
212
194
  case "Ed25519":
213
195
  case "EdDSA": {
214
- if (!v(e.algorithm, "Ed25519"))
215
- throw S("Ed25519");
196
+ if (!b(e.algorithm, "Ed25519"))
197
+ throw h("Ed25519");
198
+ break;
199
+ }
200
+ case "ML-DSA-44":
201
+ case "ML-DSA-65":
202
+ case "ML-DSA-87": {
203
+ if (!b(e.algorithm, t))
204
+ throw h(t);
216
205
  break;
217
206
  }
218
207
  case "ES256":
219
208
  case "ES384":
220
209
  case "ES512": {
221
- if (!v(e.algorithm, "ECDSA"))
222
- throw S("ECDSA");
223
- const n = ue(t);
210
+ if (!b(e.algorithm, "ECDSA"))
211
+ throw h("ECDSA");
212
+ const n = de(t);
224
213
  if (e.algorithm.namedCurve !== n)
225
- throw S(n, "algorithm.namedCurve");
214
+ throw h(n, "algorithm.namedCurve");
226
215
  break;
227
216
  }
228
217
  default:
229
218
  throw new TypeError("CryptoKey does not support this operation");
230
219
  }
231
- de(e, r);
220
+ ue(e, r);
232
221
  }
233
222
  function G(e, t, ...r) {
234
- var n;
235
223
  if (r = r.filter(Boolean), r.length > 2) {
236
- const a = r.pop();
237
- e += `one of type ${r.join(", ")}, or ${a}.`;
224
+ const n = r.pop();
225
+ e += `one of type ${r.join(", ")}, or ${n}.`;
238
226
  } else r.length === 2 ? e += `one of type ${r[0]} or ${r[1]}.` : e += `of type ${r[0]}.`;
239
- return t == null ? e += ` Received ${t}` : typeof t == "function" && t.name ? e += ` Received function ${t.name}` : typeof t == "object" && t != null && (n = t.constructor) != null && n.name && (e += ` Received an instance of ${t.constructor.name}`), e;
227
+ 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;
240
228
  }
241
229
  const le = (e, ...t) => G("Key must be ", e, ...t);
242
230
  function q(e, t, ...r) {
243
231
  return G(`Key for the ${e} algorithm must be `, t, ...r);
244
232
  }
245
233
  function z(e) {
246
- return (e == null ? void 0 : e[Symbol.toStringTag]) === "CryptoKey";
234
+ return e?.[Symbol.toStringTag] === "CryptoKey";
247
235
  }
248
236
  function X(e) {
249
- return (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject";
237
+ return e?.[Symbol.toStringTag] === "KeyObject";
250
238
  }
251
- const Q = (e) => z(e) || X(e), he = (...e) => {
239
+ const Q = (e) => z(e) || X(e), pe = (...e) => {
252
240
  const t = e.filter(Boolean);
253
241
  if (t.length === 0 || t.length === 1)
254
242
  return !0;
@@ -259,19 +247,19 @@ const Q = (e) => z(e) || X(e), he = (...e) => {
259
247
  r = new Set(a);
260
248
  continue;
261
249
  }
262
- for (const i of a) {
263
- if (r.has(i))
250
+ for (const o of a) {
251
+ if (r.has(o))
264
252
  return !1;
265
- r.add(i);
253
+ r.add(o);
266
254
  }
267
255
  }
268
256
  return !0;
269
257
  };
270
- function pe(e) {
258
+ function he(e) {
271
259
  return typeof e == "object" && e !== null;
272
260
  }
273
- const _ = (e) => {
274
- if (!pe(e) || Object.prototype.toString.call(e) !== "[object Object]")
261
+ const P = (e) => {
262
+ if (!he(e) || Object.prototype.toString.call(e) !== "[object Object]")
275
263
  return !1;
276
264
  if (Object.getPrototypeOf(e) === null)
277
265
  return !0;
@@ -285,76 +273,133 @@ const _ = (e) => {
285
273
  if (typeof r != "number" || r < 2048)
286
274
  throw new TypeError(`${e} requires key modulusLength to be 2048 bits or larger`);
287
275
  }
288
- }, W = (e, t, r = 0) => {
289
- r === 0 && (t.unshift(t.length), t.unshift(6));
290
- const n = e.indexOf(t[0], r);
291
- if (n === -1)
276
+ }, x = (e, t) => {
277
+ if (e.byteLength !== t.length)
292
278
  return !1;
293
- const a = e.subarray(n, n + t.length);
294
- return a.length !== t.length ? !1 : a.every((i, o) => i === t[o]) || W(e, t, n + 1);
295
- }, me = (e) => {
296
- switch (!0) {
297
- case W(e, [42, 134, 72, 206, 61, 3, 1, 7]):
298
- return "P-256";
299
- case W(e, [43, 129, 4, 0, 34]):
300
- return "P-384";
301
- case W(e, [43, 129, 4, 0, 35]):
302
- return "P-521";
303
- default:
304
- return;
279
+ for (let r = 0; r < e.byteLength; r++)
280
+ if (e[r] !== t[r])
281
+ return !1;
282
+ return !0;
283
+ }, me = (e) => ({ data: e, pos: 0 }), O = (e) => {
284
+ const t = e.data[e.pos++];
285
+ if (t & 128) {
286
+ const r = t & 127;
287
+ let n = 0;
288
+ for (let a = 0; a < r; a++)
289
+ n = n << 8 | e.data[e.pos++];
290
+ return n;
305
291
  }
306
- }, Se = async (e, t, r, n, a) => {
307
- let i, o;
308
- const c = new Uint8Array(atob(r.replace(e, "")).split("").map((s) => s.charCodeAt(0)));
309
- switch (n) {
292
+ return t;
293
+ }, D = (e, t, r) => {
294
+ if (e.data[e.pos++] !== t)
295
+ throw new Error(r);
296
+ }, Z = (e, t) => {
297
+ const r = e.data.subarray(e.pos, e.pos + t);
298
+ return e.pos += t, r;
299
+ }, Se = (e) => {
300
+ D(e, 6, "Expected algorithm OID");
301
+ const t = O(e);
302
+ return Z(e, t);
303
+ };
304
+ function Ee(e) {
305
+ D(e, 48, "Invalid SPKI structure"), O(e), D(e, 48, "Expected algorithm identifier");
306
+ const t = O(e);
307
+ return { algIdStart: e.pos, algIdLength: t };
308
+ }
309
+ const we = (e) => {
310
+ const t = Se(e);
311
+ if (x(t, [43, 101, 110]))
312
+ return "X25519";
313
+ if (!x(t, [42, 134, 72, 206, 61, 2, 1]))
314
+ throw new Error("Unsupported key algorithm");
315
+ D(e, 6, "Expected curve OID");
316
+ const r = O(e), n = Z(e, r);
317
+ for (const { name: a, oid: o } of [
318
+ { name: "P-256", oid: [42, 134, 72, 206, 61, 3, 1, 7] },
319
+ { name: "P-384", oid: [43, 129, 4, 0, 34] },
320
+ { name: "P-521", oid: [43, 129, 4, 0, 35] }
321
+ ])
322
+ if (x(n, o))
323
+ return a;
324
+ throw new Error("Unsupported named curve");
325
+ }, Ae = async (e, t, r, n) => {
326
+ let a, o;
327
+ const i = () => ["verify"], s = () => ["encrypt", "wrapKey"];
328
+ switch (r) {
310
329
  case "PS256":
311
330
  case "PS384":
312
331
  case "PS512":
313
- i = { name: "RSA-PSS", hash: `SHA-${n.slice(-3)}` }, o = ["verify"];
332
+ a = { name: "RSA-PSS", hash: `SHA-${r.slice(-3)}` }, o = i();
314
333
  break;
315
334
  case "RS256":
316
335
  case "RS384":
317
336
  case "RS512":
318
- i = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${n.slice(-3)}` }, o = ["verify"];
337
+ a = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${r.slice(-3)}` }, o = i();
319
338
  break;
320
339
  case "RSA-OAEP":
321
340
  case "RSA-OAEP-256":
322
341
  case "RSA-OAEP-384":
323
342
  case "RSA-OAEP-512":
324
- i = {
343
+ a = {
325
344
  name: "RSA-OAEP",
326
- hash: `SHA-${parseInt(n.slice(-3), 10) || 1}`
327
- }, o = ["encrypt", "wrapKey"];
345
+ hash: `SHA-${parseInt(r.slice(-3), 10) || 1}`
346
+ }, o = s();
328
347
  break;
329
348
  case "ES256":
330
- i = { name: "ECDSA", namedCurve: "P-256" }, o = ["verify"];
331
- break;
332
349
  case "ES384":
333
- i = { name: "ECDSA", namedCurve: "P-384" }, o = ["verify"];
334
- break;
335
- case "ES512":
336
- i = { name: "ECDSA", namedCurve: "P-521" }, o = ["verify"];
350
+ case "ES512": {
351
+ a = { name: "ECDSA", namedCurve: { ES256: "P-256", ES384: "P-384", ES512: "P-521" }[r] }, o = i();
337
352
  break;
353
+ }
338
354
  case "ECDH-ES":
339
355
  case "ECDH-ES+A128KW":
340
356
  case "ECDH-ES+A192KW":
341
357
  case "ECDH-ES+A256KW": {
342
- const s = me(c);
343
- i = s != null && s.startsWith("P-") ? { name: "ECDH", namedCurve: s } : { name: "X25519" }, o = [];
358
+ try {
359
+ const u = n.getNamedCurve(t);
360
+ a = u === "X25519" ? { name: "X25519" } : { name: "ECDH", namedCurve: u };
361
+ } catch {
362
+ throw new E("Invalid or unsupported key format");
363
+ }
364
+ o = [];
344
365
  break;
345
366
  }
346
367
  case "Ed25519":
347
368
  case "EdDSA":
348
- i = { name: "Ed25519" }, o = ["verify"];
369
+ a = { name: "Ed25519" }, o = i();
370
+ break;
371
+ case "ML-DSA-44":
372
+ case "ML-DSA-65":
373
+ case "ML-DSA-87":
374
+ a = { name: r }, o = i();
349
375
  break;
350
376
  default:
351
377
  throw new E('Invalid or unsupported "alg" (Algorithm) value');
352
378
  }
353
- return crypto.subtle.importKey(t, c, i, !0, o);
354
- }, Ee = (e, t, r) => Se(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, "spki", e, t);
355
- function we(e) {
379
+ return crypto.subtle.importKey(e, t, a, n?.extractable ?? !0, o);
380
+ }, ge = (e, t) => V(e.replace(t, "")), be = (e, t, r) => {
381
+ const n = ge(e, /(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g);
382
+ let a = r;
383
+ return t?.startsWith?.("ECDH-ES") && (a ||= {}, a.getNamedCurve = (o) => {
384
+ const i = me(o);
385
+ return Ee(i), we(i);
386
+ }), Ae("spki", n, t, a);
387
+ };
388
+ function Ce(e) {
356
389
  let t, r;
357
390
  switch (e.kty) {
391
+ case "AKP": {
392
+ switch (e.alg) {
393
+ case "ML-DSA-44":
394
+ case "ML-DSA-65":
395
+ case "ML-DSA-87":
396
+ t = { name: e.alg }, r = e.priv ? ["sign"] : ["verify"];
397
+ break;
398
+ default:
399
+ throw new E('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
400
+ }
401
+ break;
402
+ }
358
403
  case "RSA": {
359
404
  switch (e.alg) {
360
405
  case "PS256":
@@ -425,69 +470,68 @@ function we(e) {
425
470
  }
426
471
  return { algorithm: t, keyUsages: r };
427
472
  }
428
- const Ae = async (e) => {
473
+ const Ke = async (e) => {
429
474
  if (!e.alg)
430
475
  throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
431
- const { algorithm: t, keyUsages: r } = we(e), n = { ...e };
432
- return delete n.alg, delete n.use, crypto.subtle.importKey("jwk", n, t, e.ext ?? !e.d, e.key_ops ?? r);
476
+ const { algorithm: t, keyUsages: r } = Ce(e), n = { ...e };
477
+ return n.kty !== "AKP" && delete n.alg, delete n.use, crypto.subtle.importKey("jwk", n, t, e.ext ?? !(e.d || e.priv), e.key_ops ?? r);
433
478
  };
434
- async function be(e, t, r) {
479
+ async function ve(e, t, r) {
435
480
  if (e.indexOf("-----BEGIN PUBLIC KEY-----") !== 0)
436
481
  throw new TypeError('"spki" must be SPKI formatted string');
437
- return Ee(e, t);
482
+ return be(e, t, r);
438
483
  }
439
- const ge = (e, t, r, n, a) => {
440
- if (a.crit !== void 0 && (n == null ? void 0 : n.crit) === void 0)
484
+ const Te = (e, t, r, n, a) => {
485
+ if (a.crit !== void 0 && n?.crit === void 0)
441
486
  throw new e('"crit" (Critical) Header Parameter MUST be integrity protected');
442
487
  if (!n || n.crit === void 0)
443
488
  return /* @__PURE__ */ new Set();
444
- if (!Array.isArray(n.crit) || n.crit.length === 0 || n.crit.some((o) => typeof o != "string" || o.length === 0))
489
+ if (!Array.isArray(n.crit) || n.crit.length === 0 || n.crit.some((i) => typeof i != "string" || i.length === 0))
445
490
  throw new e('"crit" (Critical) Header Parameter MUST be an array of non-empty strings when present');
446
- let i;
447
- r !== void 0 ? i = new Map([...Object.entries(r), ...t.entries()]) : i = t;
448
- for (const o of n.crit) {
449
- if (!i.has(o))
450
- throw new E(`Extension Header Parameter "${o}" is not recognized`);
451
- if (a[o] === void 0)
452
- throw new e(`Extension Header Parameter "${o}" is missing`);
453
- if (i.get(o) && n[o] === void 0)
454
- throw new e(`Extension Header Parameter "${o}" MUST be integrity protected`);
491
+ let o;
492
+ r !== void 0 ? o = new Map([...Object.entries(r), ...t.entries()]) : o = t;
493
+ for (const i of n.crit) {
494
+ if (!o.has(i))
495
+ throw new E(`Extension Header Parameter "${i}" is not recognized`);
496
+ if (a[i] === void 0)
497
+ throw new e(`Extension Header Parameter "${i}" is missing`);
498
+ if (o.get(i) && n[i] === void 0)
499
+ throw new e(`Extension Header Parameter "${i}" MUST be integrity protected`);
455
500
  }
456
501
  return new Set(n.crit);
457
- }, Ce = (e, t) => {
502
+ }, Pe = (e, t) => {
458
503
  if (t !== void 0 && (!Array.isArray(t) || t.some((r) => typeof r != "string")))
459
504
  throw new TypeError(`"${e}" option must be an array of strings`);
460
505
  if (t)
461
506
  return new Set(t);
462
507
  };
463
- function N(e) {
464
- return _(e) && typeof e.kty == "string";
508
+ function L(e) {
509
+ return P(e) && typeof e.kty == "string";
465
510
  }
466
- function Ke(e) {
467
- return e.kty !== "oct" && typeof e.d == "string";
511
+ function _e(e) {
512
+ return e.kty !== "oct" && (e.kty === "AKP" && typeof e.priv == "string" || typeof e.d == "string");
468
513
  }
469
- function Te(e) {
470
- return e.kty !== "oct" && typeof e.d > "u";
514
+ function Ie(e) {
515
+ return e.kty !== "oct" && typeof e.d > "u" && typeof e.priv > "u";
471
516
  }
472
- function ve(e) {
517
+ function Re(e) {
473
518
  return e.kty === "oct" && typeof e.k == "string";
474
519
  }
475
- let C;
476
- const $ = async (e, t, r, n = !1) => {
477
- C || (C = /* @__PURE__ */ new WeakMap());
478
- let a = C.get(e);
479
- if (a != null && a[r])
520
+ let K;
521
+ const M = async (e, t, r, n = !1) => {
522
+ K ||= /* @__PURE__ */ new WeakMap();
523
+ let a = K.get(e);
524
+ if (a?.[r])
480
525
  return a[r];
481
- const i = await Ae({ ...t, alg: r });
482
- return n && Object.freeze(e), a ? a[r] = i : C.set(e, { [r]: i }), i;
483
- }, Pe = (e, t) => {
484
- var o;
485
- C || (C = /* @__PURE__ */ new WeakMap());
486
- let r = C.get(e);
487
- if (r != null && r[t])
526
+ const o = await Ke({ ...t, alg: r });
527
+ return n && Object.freeze(e), a ? a[r] = o : K.set(e, { [r]: o }), o;
528
+ }, Oe = (e, t) => {
529
+ K ||= /* @__PURE__ */ new WeakMap();
530
+ let r = K.get(e);
531
+ if (r?.[t])
488
532
  return r[t];
489
533
  const n = e.type === "public", a = !!n;
490
- let i;
534
+ let o;
491
535
  if (e.asymmetricKeyType === "x25519") {
492
536
  switch (t) {
493
537
  case "ECDH-ES":
@@ -498,35 +542,46 @@ const $ = async (e, t, r, n = !1) => {
498
542
  default:
499
543
  throw new TypeError("given KeyObject instance cannot be used for this algorithm");
500
544
  }
501
- i = e.toCryptoKey(e.asymmetricKeyType, a, n ? [] : ["deriveBits"]);
545
+ o = e.toCryptoKey(e.asymmetricKeyType, a, n ? [] : ["deriveBits"]);
502
546
  }
503
547
  if (e.asymmetricKeyType === "ed25519") {
504
548
  if (t !== "EdDSA" && t !== "Ed25519")
505
549
  throw new TypeError("given KeyObject instance cannot be used for this algorithm");
506
- i = e.toCryptoKey(e.asymmetricKeyType, a, [
550
+ o = e.toCryptoKey(e.asymmetricKeyType, a, [
507
551
  n ? "verify" : "sign"
508
552
  ]);
509
553
  }
554
+ switch (e.asymmetricKeyType) {
555
+ case "ml-dsa-44":
556
+ case "ml-dsa-65":
557
+ case "ml-dsa-87": {
558
+ if (t !== e.asymmetricKeyType.toUpperCase())
559
+ throw new TypeError("given KeyObject instance cannot be used for this algorithm");
560
+ o = e.toCryptoKey(e.asymmetricKeyType, a, [
561
+ n ? "verify" : "sign"
562
+ ]);
563
+ }
564
+ }
510
565
  if (e.asymmetricKeyType === "rsa") {
511
- let c;
566
+ let i;
512
567
  switch (t) {
513
568
  case "RSA-OAEP":
514
- c = "SHA-1";
569
+ i = "SHA-1";
515
570
  break;
516
571
  case "RS256":
517
572
  case "PS256":
518
573
  case "RSA-OAEP-256":
519
- c = "SHA-256";
574
+ i = "SHA-256";
520
575
  break;
521
576
  case "RS384":
522
577
  case "PS384":
523
578
  case "RSA-OAEP-384":
524
- c = "SHA-384";
579
+ i = "SHA-384";
525
580
  break;
526
581
  case "RS512":
527
582
  case "PS512":
528
583
  case "RSA-OAEP-512":
529
- c = "SHA-512";
584
+ i = "SHA-512";
530
585
  break;
531
586
  default:
532
587
  throw new TypeError("given KeyObject instance cannot be used for this algorithm");
@@ -534,11 +589,11 @@ const $ = async (e, t, r, n = !1) => {
534
589
  if (t.startsWith("RSA-OAEP"))
535
590
  return e.toCryptoKey({
536
591
  name: "RSA-OAEP",
537
- hash: c
592
+ hash: i
538
593
  }, a, n ? ["encrypt"] : ["decrypt"]);
539
- i = e.toCryptoKey({
594
+ o = e.toCryptoKey({
540
595
  name: t.startsWith("PS") ? "RSA-PSS" : "RSASSA-PKCS1-v1_5",
541
- hash: c
596
+ hash: i
542
597
  }, a, [n ? "verify" : "sign"]);
543
598
  }
544
599
  if (e.asymmetricKeyType === "ec") {
@@ -546,27 +601,27 @@ const $ = async (e, t, r, n = !1) => {
546
601
  ["prime256v1", "P-256"],
547
602
  ["secp384r1", "P-384"],
548
603
  ["secp521r1", "P-521"]
549
- ])).get((o = e.asymmetricKeyDetails) == null ? void 0 : o.namedCurve);
604
+ ])).get(e.asymmetricKeyDetails?.namedCurve);
550
605
  if (!s)
551
606
  throw new TypeError("given KeyObject instance cannot be used for this algorithm");
552
- t === "ES256" && s === "P-256" && (i = e.toCryptoKey({
607
+ t === "ES256" && s === "P-256" && (o = e.toCryptoKey({
553
608
  name: "ECDSA",
554
609
  namedCurve: s
555
- }, a, [n ? "verify" : "sign"])), t === "ES384" && s === "P-384" && (i = e.toCryptoKey({
610
+ }, a, [n ? "verify" : "sign"])), t === "ES384" && s === "P-384" && (o = e.toCryptoKey({
556
611
  name: "ECDSA",
557
612
  namedCurve: s
558
- }, a, [n ? "verify" : "sign"])), t === "ES512" && s === "P-521" && (i = e.toCryptoKey({
613
+ }, a, [n ? "verify" : "sign"])), t === "ES512" && s === "P-521" && (o = e.toCryptoKey({
559
614
  name: "ECDSA",
560
615
  namedCurve: s
561
- }, a, [n ? "verify" : "sign"])), t.startsWith("ECDH-ES") && (i = e.toCryptoKey({
616
+ }, a, [n ? "verify" : "sign"])), t.startsWith("ECDH-ES") && (o = e.toCryptoKey({
562
617
  name: "ECDH",
563
618
  namedCurve: s
564
619
  }, a, n ? [] : ["deriveBits"]));
565
620
  }
566
- if (!i)
621
+ if (!o)
567
622
  throw new TypeError("given KeyObject instance cannot be used for this algorithm");
568
- return r ? r[t] = i : C.set(e, { [t]: i }), i;
569
- }, _e = async (e, t) => {
623
+ return r ? r[t] = o : K.set(e, { [t]: o }), o;
624
+ }, De = async (e, t) => {
570
625
  if (e instanceof Uint8Array || z(e))
571
626
  return e;
572
627
  if (X(e)) {
@@ -574,108 +629,107 @@ const $ = async (e, t, r, n = !1) => {
574
629
  return e.export();
575
630
  if ("toCryptoKey" in e && typeof e.toCryptoKey == "function")
576
631
  try {
577
- return Pe(e, t);
632
+ return Oe(e, t);
578
633
  } catch (n) {
579
634
  if (n instanceof TypeError)
580
635
  throw n;
581
636
  }
582
637
  let r = e.export({ format: "jwk" });
583
- return $(e, r, t);
638
+ return M(e, r, t);
584
639
  }
585
- if (N(e))
586
- return e.k ? P(e.k) : $(e, e, t, !0);
640
+ if (L(e))
641
+ return e.k ? T(e.k) : M(e, e, t, !0);
587
642
  throw new Error("unreachable");
588
- }, g = (e) => e == null ? void 0 : e[Symbol.toStringTag], x = (e, t, r) => {
589
- var n, a;
643
+ }, C = (e) => e?.[Symbol.toStringTag], J = (e, t, r) => {
590
644
  if (t.use !== void 0) {
591
- let i;
645
+ let n;
592
646
  switch (r) {
593
647
  case "sign":
594
648
  case "verify":
595
- i = "sig";
649
+ n = "sig";
596
650
  break;
597
651
  case "encrypt":
598
652
  case "decrypt":
599
- i = "enc";
653
+ n = "enc";
600
654
  break;
601
655
  }
602
- if (t.use !== i)
603
- throw new TypeError(`Invalid key for this operation, its "use" must be "${i}" when present`);
656
+ if (t.use !== n)
657
+ throw new TypeError(`Invalid key for this operation, its "use" must be "${n}" when present`);
604
658
  }
605
659
  if (t.alg !== void 0 && t.alg !== e)
606
660
  throw new TypeError(`Invalid key for this operation, its "alg" must be "${e}" when present`);
607
661
  if (Array.isArray(t.key_ops)) {
608
- let i;
662
+ let n;
609
663
  switch (!0) {
610
664
  case r === "verify":
611
665
  case e === "dir":
612
666
  case e.includes("CBC-HS"):
613
- i = r;
667
+ n = r;
614
668
  break;
615
669
  case e.startsWith("PBES2"):
616
- i = "deriveBits";
670
+ n = "deriveBits";
617
671
  break;
618
672
  case /^A\d{3}(?:GCM)?(?:KW)?$/.test(e):
619
- !e.includes("GCM") && e.endsWith("KW") ? i = "unwrapKey" : i = r;
673
+ !e.includes("GCM") && e.endsWith("KW") ? n = "unwrapKey" : n = r;
620
674
  break;
621
675
  case r === "encrypt":
622
- i = "wrapKey";
676
+ n = "wrapKey";
623
677
  break;
624
678
  case r === "decrypt":
625
- i = e.startsWith("RSA") ? "unwrapKey" : "deriveBits";
679
+ n = e.startsWith("RSA") ? "unwrapKey" : "deriveBits";
626
680
  break;
627
681
  }
628
- if (i && ((a = (n = t.key_ops) == null ? void 0 : n.includes) == null ? void 0 : a.call(n, i)) === !1)
629
- throw new TypeError(`Invalid key for this operation, its "key_ops" must include "${i}" when present`);
682
+ if (n && t.key_ops?.includes?.(n) === !1)
683
+ throw new TypeError(`Invalid key for this operation, its "key_ops" must include "${n}" when present`);
630
684
  }
631
685
  return !0;
632
- }, Re = (e, t, r) => {
686
+ }, We = (e, t, r) => {
633
687
  if (!(t instanceof Uint8Array)) {
634
- if (N(t)) {
635
- if (ve(t) && x(e, t, r))
688
+ if (L(t)) {
689
+ if (Re(t) && J(e, t, r))
636
690
  return;
637
691
  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');
638
692
  }
639
693
  if (!Q(t))
640
694
  throw new TypeError(q(e, t, "CryptoKey", "KeyObject", "JSON Web Key", "Uint8Array"));
641
695
  if (t.type !== "secret")
642
- throw new TypeError(`${g(t)} instances for symmetric algorithms must be of type "secret"`);
696
+ throw new TypeError(`${C(t)} instances for symmetric algorithms must be of type "secret"`);
643
697
  }
644
- }, Ie = (e, t, r) => {
645
- if (N(t))
698
+ }, xe = (e, t, r) => {
699
+ if (L(t))
646
700
  switch (r) {
647
701
  case "decrypt":
648
702
  case "sign":
649
- if (Ke(t) && x(e, t, r))
703
+ if (_e(t) && J(e, t, r))
650
704
  return;
651
705
  throw new TypeError("JSON Web Key for this operation be a private JWK");
652
706
  case "encrypt":
653
707
  case "verify":
654
- if (Te(t) && x(e, t, r))
708
+ if (Ie(t) && J(e, t, r))
655
709
  return;
656
710
  throw new TypeError("JSON Web Key for this operation be a public JWK");
657
711
  }
658
712
  if (!Q(t))
659
713
  throw new TypeError(q(e, t, "CryptoKey", "KeyObject", "JSON Web Key"));
660
714
  if (t.type === "secret")
661
- throw new TypeError(`${g(t)} instances for asymmetric algorithms must not be of type "secret"`);
715
+ throw new TypeError(`${C(t)} instances for asymmetric algorithms must not be of type "secret"`);
662
716
  if (t.type === "public")
663
717
  switch (r) {
664
718
  case "sign":
665
- throw new TypeError(`${g(t)} instances for asymmetric algorithm signing must be of type "private"`);
719
+ throw new TypeError(`${C(t)} instances for asymmetric algorithm signing must be of type "private"`);
666
720
  case "decrypt":
667
- throw new TypeError(`${g(t)} instances for asymmetric algorithm decryption must be of type "private"`);
721
+ throw new TypeError(`${C(t)} instances for asymmetric algorithm decryption must be of type "private"`);
668
722
  }
669
723
  if (t.type === "private")
670
724
  switch (r) {
671
725
  case "verify":
672
- throw new TypeError(`${g(t)} instances for asymmetric algorithm verifying must be of type "public"`);
726
+ throw new TypeError(`${C(t)} instances for asymmetric algorithm verifying must be of type "public"`);
673
727
  case "encrypt":
674
- throw new TypeError(`${g(t)} instances for asymmetric algorithm encryption must be of type "public"`);
728
+ throw new TypeError(`${C(t)} instances for asymmetric algorithm encryption must be of type "public"`);
675
729
  }
676
- }, Oe = (e, t, r) => {
677
- e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A(?:128|192|256)(?:GCM)?(?:KW)?$/.test(e) || /^A(?:128|192|256)CBC-HS(?:256|384|512)$/.test(e) ? Re(e, t, r) : Ie(e, t, r);
678
- }, We = (e, t) => {
730
+ }, He = (e, t, r) => {
731
+ e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A(?:128|192|256)(?:GCM)?(?:KW)?$/.test(e) || /^A(?:128|192|256)CBC-HS(?:256|384|512)$/.test(e) ? We(e, t, r) : xe(e, t, r);
732
+ }, Je = (e, t) => {
679
733
  const r = `SHA-${e.slice(-3)}`;
680
734
  switch (e) {
681
735
  case "HS256":
@@ -697,28 +751,32 @@ const $ = async (e, t, r, n = !1) => {
697
751
  case "Ed25519":
698
752
  case "EdDSA":
699
753
  return { name: "Ed25519" };
754
+ case "ML-DSA-44":
755
+ case "ML-DSA-65":
756
+ case "ML-DSA-87":
757
+ return { name: e };
700
758
  default:
701
759
  throw new E(`alg ${e} is not supported either by JOSE or your javascript runtime`);
702
760
  }
703
- }, De = async (e, t, r) => {
761
+ }, Le = async (e, t, r) => {
704
762
  if (t instanceof Uint8Array) {
705
763
  if (!e.startsWith("HS"))
706
764
  throw new TypeError(le(t, "CryptoKey", "KeyObject", "JSON Web Key"));
707
765
  return crypto.subtle.importKey("raw", t, { hash: `SHA-${e.slice(-3)}`, name: "HMAC" }, !1, [r]);
708
766
  }
709
767
  return fe(t, e, r), t;
710
- }, He = async (e, t, r, n) => {
711
- const a = await De(e, t, "verify");
768
+ }, Ne = async (e, t, r, n) => {
769
+ const a = await Le(e, t, "verify");
712
770
  ye(e, a);
713
- const i = We(e, a.algorithm);
771
+ const o = Je(e, a.algorithm);
714
772
  try {
715
- return await crypto.subtle.verify(i, a, r, n);
773
+ return await crypto.subtle.verify(o, a, r, n);
716
774
  } catch {
717
775
  return !1;
718
776
  }
719
777
  };
720
- async function Je(e, t, r) {
721
- if (!_(e))
778
+ async function Ue(e, t, r) {
779
+ if (!P(e))
722
780
  throw new d("Flattened JWS must be an object");
723
781
  if (e.protected === void 0 && e.header === void 0)
724
782
  throw new d('Flattened JWS must have either of the "protected" or "header" members');
@@ -728,70 +786,70 @@ async function Je(e, t, r) {
728
786
  throw new d("JWS Payload missing");
729
787
  if (typeof e.signature != "string")
730
788
  throw new d("JWS Signature missing or incorrect type");
731
- if (e.header !== void 0 && !_(e.header))
789
+ if (e.header !== void 0 && !P(e.header))
732
790
  throw new d("JWS Unprotected Header incorrect type");
733
791
  let n = {};
734
792
  if (e.protected)
735
793
  try {
736
- const re = P(e.protected);
737
- n = JSON.parse(T.decode(re));
794
+ const ne = T(e.protected);
795
+ n = JSON.parse(v.decode(ne));
738
796
  } catch {
739
797
  throw new d("JWS Protected Header is invalid");
740
798
  }
741
- if (!he(n, e.header))
799
+ if (!pe(n, e.header))
742
800
  throw new d("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
743
801
  const a = {
744
802
  ...n,
745
803
  ...e.header
746
- }, i = ge(d, /* @__PURE__ */ new Map([["b64", !0]]), r == null ? void 0 : r.crit, n, a);
747
- let o = !0;
748
- if (i.has("b64") && (o = n.b64, typeof o != "boolean"))
804
+ }, o = Te(d, /* @__PURE__ */ new Map([["b64", !0]]), r?.crit, n, a);
805
+ let i = !0;
806
+ if (o.has("b64") && (i = n.b64, typeof i != "boolean"))
749
807
  throw new d('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
750
- const { alg: c } = a;
751
- if (typeof c != "string" || !c)
808
+ const { alg: s } = a;
809
+ if (typeof s != "string" || !s)
752
810
  throw new d('JWS "alg" (Algorithm) Header Parameter missing or invalid');
753
- const s = r && Ce("algorithms", r.algorithms);
754
- if (s && !s.has(c))
755
- throw new F('"alg" (Algorithm) Header Parameter value not allowed');
756
- if (o) {
811
+ const u = r && Pe("algorithms", r.algorithms);
812
+ if (u && !u.has(s))
813
+ throw new se('"alg" (Algorithm) Header Parameter value not allowed');
814
+ if (i) {
757
815
  if (typeof e.payload != "string")
758
816
  throw new d("JWS Payload must be a string");
759
817
  } else if (typeof e.payload != "string" && !(e.payload instanceof Uint8Array))
760
818
  throw new d("JWS Payload must be a string or an Uint8Array instance");
761
819
  let m = !1;
762
- typeof t == "function" && (t = await t(n, e), m = !0), Oe(c, t, "verify");
763
- const A = se(O.encode(e.protected ?? ""), O.encode("."), typeof e.payload == "string" ? O.encode(e.payload) : e.payload);
764
- let p;
820
+ typeof t == "function" && (t = await t(n, e), m = !0), He(s, t, "verify");
821
+ const w = ie(R.encode(e.protected ?? ""), R.encode("."), typeof e.payload == "string" ? R.encode(e.payload) : e.payload);
822
+ let l;
765
823
  try {
766
- p = P(e.signature);
824
+ l = T(e.signature);
767
825
  } catch {
768
826
  throw new d("Failed to base64url decode the signature");
769
827
  }
770
- const R = await _e(t, c);
771
- if (!await He(c, R, p, A))
772
- throw new V();
773
- let l;
774
- if (o)
828
+ const _ = await De(t, s);
829
+ if (!await Ne(s, _, l, w))
830
+ throw new ce();
831
+ let f;
832
+ if (i)
775
833
  try {
776
- l = P(e.payload);
834
+ f = T(e.payload);
777
835
  } catch {
778
836
  throw new d("Failed to base64url decode the payload");
779
837
  }
780
- else typeof e.payload == "string" ? l = O.encode(e.payload) : l = e.payload;
781
- const b = { payload: l };
782
- return e.protected !== void 0 && (b.protectedHeader = n), e.header !== void 0 && (b.unprotectedHeader = e.header), m ? { ...b, key: R } : b;
838
+ else typeof e.payload == "string" ? f = R.encode(e.payload) : f = e.payload;
839
+ const g = { payload: f };
840
+ return e.protected !== void 0 && (g.protectedHeader = n), e.header !== void 0 && (g.unprotectedHeader = e.header), m ? { ...g, key: _ } : g;
783
841
  }
784
- async function xe(e, t, r) {
785
- if (e instanceof Uint8Array && (e = T.decode(e)), typeof e != "string")
842
+ async function Me(e, t, r) {
843
+ if (e instanceof Uint8Array && (e = v.decode(e)), typeof e != "string")
786
844
  throw new d("Compact JWS must be a string or Uint8Array");
787
- const { 0: n, 1: a, 2: i, length: o } = e.split(".");
788
- if (o !== 3)
845
+ const { 0: n, 1: a, 2: o, length: i } = e.split(".");
846
+ if (i !== 3)
789
847
  throw new d("Invalid Compact JWS");
790
- const c = await Je({ payload: a, protected: n, signature: i }, t, r), s = { payload: c.payload, protectedHeader: c.protectedHeader };
791
- return typeof t == "function" ? { ...s, key: c.key } : s;
848
+ const s = await Ue({ payload: a, protected: n, signature: o }, t, r), u = { payload: s.payload, protectedHeader: s.protectedHeader };
849
+ return typeof t == "function" ? { ...u, key: s.key } : u;
792
850
  }
793
- const Ne = (e) => Math.floor(e.getTime() / 1e3), Z = 60, j = Z * 60, U = j * 24, Ue = U * 7, $e = U * 365.25, Le = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i, L = (e) => {
794
- const t = Le.exec(e);
851
+ const $e = (e) => Math.floor(e.getTime() / 1e3), j = 60, ee = j * 60, N = ee * 24, ke = N * 7, Be = N * 365.25, Ye = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i, $ = (e) => {
852
+ const t = Ye.exec(e);
795
853
  if (!t || t[4] && t[1])
796
854
  throw new TypeError("Invalid time period format");
797
855
  const r = parseFloat(t[2]), n = t[3].toLowerCase();
@@ -809,255 +867,264 @@ const Ne = (e) => Math.floor(e.getTime() / 1e3), Z = 60, j = Z * 60, U = j * 24,
809
867
  case "min":
810
868
  case "mins":
811
869
  case "m":
812
- a = Math.round(r * Z);
870
+ a = Math.round(r * j);
813
871
  break;
814
872
  case "hour":
815
873
  case "hours":
816
874
  case "hr":
817
875
  case "hrs":
818
876
  case "h":
819
- a = Math.round(r * j);
877
+ a = Math.round(r * ee);
820
878
  break;
821
879
  case "day":
822
880
  case "days":
823
881
  case "d":
824
- a = Math.round(r * U);
882
+ a = Math.round(r * N);
825
883
  break;
826
884
  case "week":
827
885
  case "weeks":
828
886
  case "w":
829
- a = Math.round(r * Ue);
887
+ a = Math.round(r * ke);
830
888
  break;
831
889
  default:
832
- a = Math.round(r * $e);
890
+ a = Math.round(r * Be);
833
891
  break;
834
892
  }
835
893
  return t[1] === "-" || t[4] === "ago" ? -a : a;
836
- }, B = (e) => e.toLowerCase().replace(/^application\//, ""), Be = (e, t) => typeof e == "string" ? t.includes(e) : Array.isArray(e) ? t.some(Set.prototype.has.bind(new Set(e))) : !1;
837
- function Me(e, t, r = {}) {
894
+ }, k = (e) => e.includes("/") ? e.toLowerCase() : `application/${e.toLowerCase()}`, Fe = (e, t) => typeof e == "string" ? t.includes(e) : Array.isArray(e) ? t.some(Set.prototype.has.bind(new Set(e))) : !1;
895
+ function Ve(e, t, r = {}) {
838
896
  let n;
839
897
  try {
840
- n = JSON.parse(T.decode(t));
898
+ n = JSON.parse(v.decode(t));
841
899
  } catch {
842
900
  }
843
- if (!_(n))
844
- throw new y("JWT Claims Set must be a top-level JSON object");
901
+ if (!P(n))
902
+ throw new S("JWT Claims Set must be a top-level JSON object");
845
903
  const { typ: a } = r;
846
- if (a && (typeof e.typ != "string" || B(e.typ) !== B(a)))
847
- throw new h('unexpected "typ" JWT header value', n, "typ", "check_failed");
848
- const { requiredClaims: i = [], issuer: o, subject: c, audience: s, maxTokenAge: m } = r, A = [...i];
849
- m !== void 0 && A.push("iat"), s !== void 0 && A.push("aud"), c !== void 0 && A.push("sub"), o !== void 0 && A.push("iss");
850
- for (const l of new Set(A.reverse()))
851
- if (!(l in n))
852
- throw new h(`missing required "${l}" claim`, n, l, "missing");
853
- if (o && !(Array.isArray(o) ? o : [o]).includes(n.iss))
854
- throw new h('unexpected "iss" claim value', n, "iss", "check_failed");
855
- if (c && n.sub !== c)
856
- throw new h('unexpected "sub" claim value', n, "sub", "check_failed");
857
- if (s && !Be(n.aud, typeof s == "string" ? [s] : s))
858
- throw new h('unexpected "aud" claim value', n, "aud", "check_failed");
859
- let p;
904
+ if (a && (typeof e.typ != "string" || k(e.typ) !== k(a)))
905
+ throw new p('unexpected "typ" JWT header value', n, "typ", "check_failed");
906
+ const { requiredClaims: o = [], issuer: i, subject: s, audience: u, maxTokenAge: m } = r, w = [...o];
907
+ m !== void 0 && w.push("iat"), u !== void 0 && w.push("aud"), s !== void 0 && w.push("sub"), i !== void 0 && w.push("iss");
908
+ for (const f of new Set(w.reverse()))
909
+ if (!(f in n))
910
+ throw new p(`missing required "${f}" claim`, n, f, "missing");
911
+ if (i && !(Array.isArray(i) ? i : [i]).includes(n.iss))
912
+ throw new p('unexpected "iss" claim value', n, "iss", "check_failed");
913
+ if (s && n.sub !== s)
914
+ throw new p('unexpected "sub" claim value', n, "sub", "check_failed");
915
+ if (u && !Fe(n.aud, typeof u == "string" ? [u] : u))
916
+ throw new p('unexpected "aud" claim value', n, "aud", "check_failed");
917
+ let l;
860
918
  switch (typeof r.clockTolerance) {
861
919
  case "string":
862
- p = L(r.clockTolerance);
920
+ l = $(r.clockTolerance);
863
921
  break;
864
922
  case "number":
865
- p = r.clockTolerance;
923
+ l = r.clockTolerance;
866
924
  break;
867
925
  case "undefined":
868
- p = 0;
926
+ l = 0;
869
927
  break;
870
928
  default:
871
929
  throw new TypeError("Invalid clockTolerance option type");
872
930
  }
873
- const { currentDate: R } = r, I = Ne(R || /* @__PURE__ */ new Date());
931
+ const { currentDate: _ } = r, I = $e(_ || /* @__PURE__ */ new Date());
874
932
  if ((n.iat !== void 0 || m) && typeof n.iat != "number")
875
- throw new h('"iat" claim must be a number', n, "iat", "invalid");
933
+ throw new p('"iat" claim must be a number', n, "iat", "invalid");
876
934
  if (n.nbf !== void 0) {
877
935
  if (typeof n.nbf != "number")
878
- throw new h('"nbf" claim must be a number', n, "nbf", "invalid");
879
- if (n.nbf > I + p)
880
- throw new h('"nbf" claim timestamp check failed', n, "nbf", "check_failed");
936
+ throw new p('"nbf" claim must be a number', n, "nbf", "invalid");
937
+ if (n.nbf > I + l)
938
+ throw new p('"nbf" claim timestamp check failed', n, "nbf", "check_failed");
881
939
  }
882
940
  if (n.exp !== void 0) {
883
941
  if (typeof n.exp != "number")
884
- throw new h('"exp" claim must be a number', n, "exp", "invalid");
885
- if (n.exp <= I - p)
886
- throw new J('"exp" claim timestamp check failed', n, "exp", "check_failed");
942
+ throw new p('"exp" claim must be a number', n, "exp", "invalid");
943
+ if (n.exp <= I - l)
944
+ throw new U('"exp" claim timestamp check failed', n, "exp", "check_failed");
887
945
  }
888
946
  if (m) {
889
- const l = I - n.iat, b = typeof m == "number" ? m : L(m);
890
- if (l - p > b)
891
- throw new J('"iat" claim timestamp check failed (too far in the past)', n, "iat", "check_failed");
892
- if (l < 0 - p)
893
- throw new h('"iat" claim timestamp check failed (it should be in the past)', n, "iat", "check_failed");
947
+ const f = I - n.iat, g = typeof m == "number" ? m : $(m);
948
+ if (f - l > g)
949
+ throw new U('"iat" claim timestamp check failed (too far in the past)', n, "iat", "check_failed");
950
+ if (f < 0 - l)
951
+ throw new p('"iat" claim timestamp check failed (it should be in the past)', n, "iat", "check_failed");
894
952
  }
895
953
  return n;
896
954
  }
897
- async function ke(e, t, r) {
898
- var o;
899
- const n = await xe(e, t, r);
900
- if ((o = n.protectedHeader.crit) != null && o.includes("b64") && n.protectedHeader.b64 === !1)
901
- throw new y("JWTs MUST NOT use unencoded payload");
902
- const i = { payload: Me(n.protectedHeader, n.payload, r), protectedHeader: n.protectedHeader };
903
- return typeof t == "function" ? { ...i, key: n.key } : i;
955
+ async function Ge(e, t, r) {
956
+ const n = await Me(e, t, r);
957
+ if (n.protectedHeader.crit?.includes("b64") && n.protectedHeader.b64 === !1)
958
+ throw new S("JWTs MUST NOT use unencoded payload");
959
+ const o = { payload: Ve(n.protectedHeader, n.payload, r), protectedHeader: n.protectedHeader };
960
+ return typeof t == "function" ? { ...o, key: n.key } : o;
904
961
  }
905
- function ee(e) {
962
+ function te(e) {
906
963
  if (typeof e != "string")
907
- throw new y("JWTs must use Compact JWS serialization, JWT must be a string");
964
+ throw new S("JWTs must use Compact JWS serialization, JWT must be a string");
908
965
  const { 1: t, length: r } = e.split(".");
909
966
  if (r === 5)
910
- throw new y("Only JWTs using Compact JWS serialization can be decoded");
967
+ throw new S("Only JWTs using Compact JWS serialization can be decoded");
911
968
  if (r !== 3)
912
- throw new y("Invalid JWT");
969
+ throw new S("Invalid JWT");
913
970
  if (!t)
914
- throw new y("JWTs must contain a payload");
971
+ throw new S("JWTs must contain a payload");
915
972
  let n;
916
973
  try {
917
- n = P(t);
974
+ n = T(t);
918
975
  } catch {
919
- throw new y("Failed to base64url decode the payload");
976
+ throw new S("Failed to base64url decode the payload");
920
977
  }
921
978
  let a;
922
979
  try {
923
- a = JSON.parse(T.decode(n));
980
+ a = JSON.parse(v.decode(n));
924
981
  } catch {
925
- throw new y("Failed to parse the decoded payload as JSON");
982
+ throw new S("Failed to parse the decoded payload as JSON");
926
983
  }
927
- if (!_(a))
928
- throw new y("Invalid JWT Claims Set");
984
+ if (!P(a))
985
+ throw new S("Invalid JWT Claims Set");
929
986
  return a;
930
987
  }
931
- const Ye = async (e) => {
988
+ const qe = async (e) => {
932
989
  try {
933
- const t = K.ALG, n = await be(oe, t);
934
- return await ke(e, n, {
935
- issuer: K.ISSUER
990
+ const t = y.ALG, n = await ve(oe, t);
991
+ return await Ge(e, n, {
992
+ issuer: y.ISSUER
936
993
  });
937
994
  } catch {
938
995
  return;
939
996
  }
940
- }, st = (e) => {
997
+ }, ut = (e) => {
941
998
  try {
942
- return ee(e);
999
+ return te(e);
943
1000
  } catch {
944
1001
  return;
945
1002
  }
946
- }, f = [];
1003
+ }, c = [];
947
1004
  for (let e = 0; e < 256; ++e)
948
- f.push((e + 256).toString(16).slice(1));
949
- function Fe(e, t = 0) {
950
- return (f[e[t + 0]] + f[e[t + 1]] + f[e[t + 2]] + f[e[t + 3]] + "-" + f[e[t + 4]] + f[e[t + 5]] + "-" + f[e[t + 6]] + f[e[t + 7]] + "-" + f[e[t + 8]] + f[e[t + 9]] + "-" + f[e[t + 10]] + f[e[t + 11]] + f[e[t + 12]] + f[e[t + 13]] + f[e[t + 14]] + f[e[t + 15]]).toLowerCase();
1005
+ c.push((e + 256).toString(16).slice(1));
1006
+ function ze(e, t = 0) {
1007
+ return (c[e[t + 0]] + c[e[t + 1]] + c[e[t + 2]] + c[e[t + 3]] + "-" + c[e[t + 4]] + c[e[t + 5]] + "-" + c[e[t + 6]] + c[e[t + 7]] + "-" + c[e[t + 8]] + c[e[t + 9]] + "-" + c[e[t + 10]] + c[e[t + 11]] + c[e[t + 12]] + c[e[t + 13]] + c[e[t + 14]] + c[e[t + 15]]).toLowerCase();
951
1008
  }
952
1009
  let H;
953
- const Ve = new Uint8Array(16);
954
- function Ge() {
1010
+ const Xe = new Uint8Array(16);
1011
+ function Qe() {
955
1012
  if (!H) {
956
1013
  if (typeof crypto > "u" || !crypto.getRandomValues)
957
1014
  throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
958
1015
  H = crypto.getRandomValues.bind(crypto);
959
1016
  }
960
- return H(Ve);
1017
+ return H(Xe);
961
1018
  }
962
- const qe = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), M = { randomUUID: qe };
963
- function k(e, t, r) {
964
- var a;
965
- if (M.randomUUID && !e)
966
- return M.randomUUID();
1019
+ const Ze = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), B = { randomUUID: Ze };
1020
+ function Y(e, t, r) {
1021
+ if (B.randomUUID && !e)
1022
+ return B.randomUUID();
967
1023
  e = e || {};
968
- const n = e.random ?? ((a = e.rng) == null ? void 0 : a.call(e)) ?? Ge();
1024
+ const n = e.random ?? e.rng?.() ?? Qe();
969
1025
  if (n.length < 16)
970
1026
  throw new Error("Random bytes length must be >= 16");
971
- return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, Fe(n);
1027
+ return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, ze(n);
972
1028
  }
973
- const Y = globalThis.crypto, ze = (e) => `${k()}${k()}`.slice(0, e), Xe = (e) => btoa(
1029
+ const F = globalThis.crypto, je = (e) => `${Y()}${Y()}`.slice(0, e), et = (e) => btoa(
974
1030
  [...new Uint8Array(e)].map((t) => String.fromCharCode(t)).join("")
975
1031
  );
976
- async function te(e) {
977
- if (!Y.subtle)
1032
+ async function re(e) {
1033
+ if (!F.subtle)
978
1034
  throw new Error(
979
1035
  "crypto.subtle is available only in secure contexts (HTTPS)."
980
1036
  );
981
- const t = new TextEncoder().encode(e), r = await Y.subtle.digest("SHA-256", t);
982
- return Xe(r).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
1037
+ const t = new TextEncoder().encode(e), r = await F.subtle.digest("SHA-256", t);
1038
+ return et(r).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
983
1039
  }
984
- async function ct(e) {
1040
+ async function ft(e) {
985
1041
  const t = e || 43;
986
1042
  if (t < 43 || t > 128)
987
1043
  throw `Expected a length between 43 and 128. Received ${e}.`;
988
- const r = ze(t), n = await te(r);
1044
+ const r = je(t), n = await re(r);
989
1045
  return {
990
1046
  code_verifier: r,
991
1047
  code_challenge: n
992
1048
  };
993
1049
  }
994
- async function ut(e, t) {
995
- return t === await te(e);
1050
+ async function lt(e, t) {
1051
+ return t === await re(e);
996
1052
  }
997
- const Qe = /^Bearer (.+)$/i, Ze = (e) => {
998
- if (typeof (e == null ? void 0 : e.authorization) != "string")
1053
+ const tt = /^Bearer (.+)$/i, rt = (e) => {
1054
+ if (typeof e?.authorization != "string")
999
1055
  return;
1000
- const t = e.authorization.match(Qe);
1056
+ const t = e.authorization.match(tt);
1001
1057
  if (t)
1002
1058
  return t[1];
1003
- }, je = (e, t) => {
1004
- const r = e == null ? void 0 : e.cookie;
1059
+ }, nt = (e, t) => {
1060
+ const r = e?.cookie;
1005
1061
  if (typeof r != "string")
1006
1062
  return;
1007
1063
  const n = new RegExp(`auth.${t}=(.+?)(?:;|$)`), a = r.match(n);
1008
1064
  if (a)
1009
1065
  return a[1];
1010
- }, et = (e) => {
1011
- const t = e == null ? void 0 : e[ie.ACCESS_TOKEN];
1066
+ }, at = (e) => {
1067
+ const t = e?.[ae.ACCESS_TOKEN];
1012
1068
  if (typeof t == "string")
1013
1069
  return t;
1014
- }, dt = ({ headers: e, body: t, clientId: r }) => {
1015
- const n = Ze(e), a = je(e, r);
1016
- return et(t) || a || n || "";
1017
- }, ft = async (e, t) => {
1018
- var a;
1019
- const r = await Ye(e);
1020
- if (!r || !Array.isArray((a = r.payload) == null ? void 0 : a[K.SCOPES_KEY]))
1070
+ }, pt = ({ headers: e, body: t, clientId: r }) => {
1071
+ const n = rt(e), a = nt(e, r);
1072
+ return at(t) || a || n || "";
1073
+ }, ht = async (e, t) => {
1074
+ const r = await qe(e);
1075
+ if (!r || !r.payload)
1021
1076
  return !1;
1022
- const n = r.payload[K.SCOPES_KEY];
1023
- return Array.isArray(t) ? t.every((i) => n.includes(i)) : Object.keys(t).some(
1024
- (i) => t[i].every((o) => n.includes(o))
1077
+ let n = [];
1078
+ if (Array.isArray(r.payload[y.SCOPES_KEY]))
1079
+ n = r.payload[y.SCOPES_KEY];
1080
+ else if (typeof r.payload[y.SCOPE_KEY] == "string")
1081
+ n = r.payload[y.SCOPE_KEY].split(" ").filter((o) => o.trim() !== "");
1082
+ else
1083
+ return !1;
1084
+ return Array.isArray(t) ? t.every((a) => n.includes(a)) : Object.keys(t).some(
1085
+ (a) => t[a].every((o) => n.includes(o))
1025
1086
  );
1026
- }, lt = (e, t) => {
1087
+ }, yt = (e, t) => {
1027
1088
  try {
1028
- const r = ee(e);
1029
- if (!r || !Array.isArray(r[K.SCOPES_KEY]))
1089
+ const r = te(e);
1090
+ if (!r)
1091
+ return !1;
1092
+ let n = [];
1093
+ if (Array.isArray(r[y.SCOPES_KEY]))
1094
+ n = r[y.SCOPES_KEY];
1095
+ else if (typeof r[y.SCOPE_KEY] == "string")
1096
+ n = r[y.SCOPE_KEY].split(" ").filter((o) => o.trim() !== "");
1097
+ else
1030
1098
  return !1;
1031
- const n = r[K.SCOPES_KEY];
1032
1099
  return Array.isArray(t) ? t.every((a) => n.includes(a)) : Object.keys(t).some(
1033
- (a) => t[a].every((i) => n.includes(i))
1100
+ (a) => t[a].every((o) => n.includes(o))
1034
1101
  );
1035
1102
  } catch {
1036
1103
  return !1;
1037
1104
  }
1038
- }, tt = (e, t) => {
1039
- const r = e == null ? void 0 : e.cookie;
1105
+ }, ot = (e, t) => {
1106
+ const r = e?.cookie;
1040
1107
  if (typeof r != "string")
1041
1108
  return;
1042
1109
  const n = new RegExp(`auth.${t}.session=(.+?)(?:;|$)`), a = r.match(n);
1043
1110
  if (a)
1044
1111
  return a[1];
1045
- }, ht = ({ headers: e, clientId: t }) => tt(e, t) || "";
1112
+ }, mt = ({ headers: e, clientId: t }) => ot(e, t) || "";
1046
1113
  export {
1047
- ot as API_TYPE,
1048
- nt as AUTH_TYPES,
1049
- ie as BODY,
1050
- at as HEADERS,
1051
- K as JWT,
1114
+ dt as API_TYPE,
1115
+ it as AUTH_TYPES,
1116
+ ae as BODY,
1117
+ st as HEADERS,
1118
+ y as JWT,
1052
1119
  oe as JWT_PUBLIC_KEY,
1053
- it as TOKEN_EXPIRATION,
1054
- st as decodeToken,
1055
- te as generateCodeChallenge,
1056
- ht as getSession,
1057
- dt as getToken,
1058
- ft as isGranted,
1059
- lt as isGrantedSync,
1060
- ct as pkceChallengePair,
1061
- Ye as verifyAndExtractToken,
1062
- ut as verifyChallenge
1120
+ ct as TOKEN_EXPIRATION,
1121
+ ut as decodeToken,
1122
+ re as generateCodeChallenge,
1123
+ mt as getSession,
1124
+ pt as getToken,
1125
+ ht as isGranted,
1126
+ yt as isGrantedSync,
1127
+ ft as pkceChallengePair,
1128
+ qe as verifyAndExtractToken,
1129
+ lt as verifyChallenge
1063
1130
  };