@genation/sdk 0.2.11 → 0.2.12

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.
@@ -1,62 +1,62 @@
1
- class N extends Error {
1
+ class L extends Error {
2
2
  code;
3
3
  cause;
4
- constructor(t, r, n) {
5
- super(t), this.name = "GenationError", this.code = r, this.cause = n;
4
+ constructor(e, r, n) {
5
+ super(e), this.name = "GenationError", this.code = r, this.cause = n;
6
6
  }
7
7
  }
8
- class g extends N {
9
- constructor(t, r, n) {
10
- super(t, r, n), this.name = "AuthError";
8
+ class g extends L {
9
+ constructor(e, r, n) {
10
+ super(e, r, n), this.name = "AuthError";
11
11
  }
12
- static invalidGrant(t = "Invalid authorization code or refresh token") {
13
- return new g(t, "invalid_grant");
12
+ static invalidGrant(e = "Invalid authorization code or refresh token") {
13
+ return new g(e, "invalid_grant");
14
14
  }
15
- static accessDenied(t = "User denied access") {
16
- return new g(t, "access_denied");
15
+ static accessDenied(e = "User denied access") {
16
+ return new g(e, "access_denied");
17
17
  }
18
- static expiredToken(t = "Token has expired") {
19
- return new g(t, "expired_token");
18
+ static expiredToken(e = "Token has expired") {
19
+ return new g(e, "expired_token");
20
20
  }
21
- static invalidState(t = "State mismatch, possible CSRF attack") {
22
- return new g(t, "invalid_state");
21
+ static invalidState(e = "State mismatch, possible CSRF attack") {
22
+ return new g(e, "invalid_state");
23
23
  }
24
- static pkceVerificationFailed(t = "PKCE verification failed") {
25
- return new g(t, "pkce_verification_failed");
24
+ static pkceVerificationFailed(e = "PKCE verification failed") {
25
+ return new g(e, "pkce_verification_failed");
26
26
  }
27
27
  }
28
- class S extends N {
28
+ class S extends L {
29
29
  status;
30
- constructor(t, r, n) {
31
- super(t, "network_error", n), this.name = "NetworkError", this.status = r;
30
+ constructor(e, r, n) {
31
+ super(e, "network_error", n), this.name = "NetworkError", this.status = r;
32
32
  }
33
- static fromResponse(t) {
33
+ static fromResponse(e) {
34
34
  return new S(
35
- `HTTP ${t.status}: ${t.statusText}`,
36
- t.status
35
+ `HTTP ${e.status}: ${e.statusText}`,
36
+ e.status
37
37
  );
38
38
  }
39
39
  }
40
- class v extends N {
41
- constructor(t) {
42
- super(t, "config_error"), this.name = "ConfigError";
40
+ class v extends L {
41
+ constructor(e) {
42
+ super(e, "config_error"), this.name = "ConfigError";
43
43
  }
44
- static missingField(t) {
45
- return new v(`Missing required config field: ${t}`);
44
+ static missingField(e) {
45
+ return new v(`Missing required config field: ${e}`);
46
46
  }
47
47
  }
48
- class D {
48
+ class U {
49
49
  baseUrl;
50
50
  timeout;
51
- constructor(t) {
52
- this.baseUrl = t.baseUrl.replace(/\/$/, ""), this.timeout = t.timeout ?? 3e4;
51
+ constructor(e) {
52
+ this.baseUrl = e.baseUrl.replace(/\/$/, ""), this.timeout = e.timeout ?? 3e4;
53
53
  }
54
54
  /**
55
55
  * Make an HTTP request
56
56
  */
57
- async request(t, r = {}) {
57
+ async request(e, r = {}) {
58
58
  const { method: n = "GET", headers: s = {}, body: a, params: i } = r;
59
- let c = `${this.baseUrl}${t}`;
59
+ let c = `${this.baseUrl}${e}`;
60
60
  if (i) {
61
61
  const o = new URLSearchParams(i);
62
62
  c += `?${o.toString()}`;
@@ -82,8 +82,8 @@ class D {
82
82
  /**
83
83
  * POST request with form data (for OAuth token exchange)
84
84
  */
85
- async postForm(t, r, n = {}) {
86
- const s = `${this.baseUrl}${t}`, a = new AbortController(), i = setTimeout(() => a.abort(), this.timeout);
85
+ async postForm(e, r, n = {}) {
86
+ const s = `${this.baseUrl}${e}`, a = new AbortController(), i = setTimeout(() => a.abort(), this.timeout);
87
87
  try {
88
88
  const c = await fetch(s, {
89
89
  method: "POST",
@@ -102,49 +102,49 @@ class D {
102
102
  }
103
103
  }
104
104
  }
105
- function $(e) {
106
- return btoa(String.fromCharCode(...e)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
105
+ function $(t) {
106
+ return btoa(String.fromCharCode(...t)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
107
107
  }
108
108
  function fe() {
109
- const e = new Uint8Array(32);
110
- return crypto.getRandomValues(e), $(e);
109
+ const t = new Uint8Array(32);
110
+ return crypto.getRandomValues(t), $(t);
111
111
  }
112
- async function de(e) {
113
- const r = new TextEncoder().encode(e), n = await crypto.subtle.digest("SHA-256", r);
112
+ async function de(t) {
113
+ const r = new TextEncoder().encode(t), n = await crypto.subtle.digest("SHA-256", r);
114
114
  return $(new Uint8Array(n));
115
115
  }
116
116
  async function le() {
117
- const e = fe(), t = await de(e);
117
+ const t = fe(), e = await de(t);
118
118
  return {
119
- codeVerifier: e,
120
- codeChallenge: t,
119
+ codeVerifier: t,
120
+ codeChallenge: e,
121
121
  codeChallengeMethod: "S256"
122
122
  };
123
123
  }
124
124
  function pe() {
125
- const e = new Uint8Array(16);
126
- return crypto.getRandomValues(e), $(e);
125
+ const t = new Uint8Array(16);
126
+ return crypto.getRandomValues(t), $(t);
127
127
  }
128
- const W = "tokens", k = "pkce", I = "state";
128
+ const k = "tokens", I = "pkce", J = "state";
129
129
  class ye {
130
130
  storage;
131
- constructor(t) {
132
- this.storage = t;
131
+ constructor(e) {
132
+ this.storage = e;
133
133
  }
134
134
  /**
135
135
  * Store token set
136
136
  */
137
- async setTokens(t) {
138
- await this.storage.set(W, JSON.stringify(t));
137
+ async setTokens(e) {
138
+ await this.storage.set(k, JSON.stringify(e));
139
139
  }
140
140
  /**
141
141
  * Get stored tokens
142
142
  */
143
143
  async getTokens() {
144
- const t = await this.storage.get(W);
145
- if (!t) return null;
144
+ const e = await this.storage.get(k);
145
+ if (!e) return null;
146
146
  try {
147
- return JSON.parse(t);
147
+ return JSON.parse(e);
148
148
  } catch {
149
149
  return null;
150
150
  }
@@ -153,42 +153,42 @@ class ye {
153
153
  * Clear stored tokens
154
154
  */
155
155
  async clearTokens() {
156
- await this.storage.remove(W);
156
+ await this.storage.remove(k);
157
157
  }
158
158
  /**
159
159
  * Check if access token is expired
160
160
  */
161
161
  async isTokenExpired() {
162
- const t = await this.getTokens();
163
- if (!t) return !0;
164
- const r = t.issuedAt + t.expiresIn * 1e3;
162
+ const e = await this.getTokens();
163
+ if (!e) return !0;
164
+ const r = e.issuedAt + e.expiresIn * 1e3;
165
165
  return Date.now() > r - 6e4;
166
166
  }
167
167
  /**
168
168
  * Store PKCE verifier for later validation
169
169
  */
170
- async setPKCE(t) {
171
- await this.storage.set(k, t);
170
+ async setPKCE(e) {
171
+ await this.storage.set(I, e);
172
172
  }
173
173
  /**
174
174
  * Get and clear stored PKCE verifier
175
175
  */
176
176
  async consumePKCE() {
177
- const t = await this.storage.get(k);
178
- return t && await this.storage.remove(k), t;
177
+ const e = await this.storage.get(I);
178
+ return e && await this.storage.remove(I), e;
179
179
  }
180
180
  /**
181
181
  * Store state for CSRF validation
182
182
  */
183
- async setState(t) {
184
- await this.storage.set(I, t);
183
+ async setState(e) {
184
+ await this.storage.set(J, e);
185
185
  }
186
186
  /**
187
187
  * Get and clear stored state
188
188
  */
189
189
  async consumeState() {
190
- const t = await this.storage.get(I);
191
- return t && await this.storage.remove(I), t;
190
+ const e = await this.storage.get(J);
191
+ return e && await this.storage.remove(J), e;
192
192
  }
193
193
  /**
194
194
  * Clear all auth-related data
@@ -197,41 +197,41 @@ class ye {
197
197
  await this.storage.clear();
198
198
  }
199
199
  }
200
- const ee = "https://mnnoheowoowbtpuoguul.supabase.co/auth/v1";
200
+ const F = "https://mnnoheowoowbtpuoguul.supabase.co/auth/v1";
201
201
  class me {
202
202
  config;
203
203
  http;
204
204
  tokenManager;
205
- constructor(t, r) {
205
+ constructor(e, r) {
206
206
  this.config = {
207
- clientId: t.clientId,
208
- clientSecret: t.clientSecret,
209
- redirectUri: t.redirectUri,
210
- scopes: t.scopes,
211
- authUrl: t.authUrl ?? ee
212
- }, this.http = new D({ baseUrl: this.config.authUrl }), this.tokenManager = r;
207
+ clientId: e.clientId,
208
+ clientSecret: e.clientSecret,
209
+ redirectUri: e.redirectUri,
210
+ scopes: e.scopes,
211
+ authUrl: e.authUrl ?? F
212
+ }, this.http = new U({ baseUrl: this.config.authUrl }), this.tokenManager = r;
213
213
  }
214
214
  /**
215
215
  * Generate authorization URL for OAuth flow
216
216
  * Stores PKCE verifier and state for later validation
217
217
  */
218
218
  async getAuthorizationUrl() {
219
- const t = await le(), r = pe();
220
- await this.tokenManager.setPKCE(t.codeVerifier), await this.tokenManager.setState(r);
219
+ const e = await le(), r = pe();
220
+ await this.tokenManager.setPKCE(e.codeVerifier), await this.tokenManager.setState(r);
221
221
  const n = new URLSearchParams({
222
222
  response_type: "code",
223
223
  client_id: this.config.clientId,
224
224
  redirect_uri: this.config.redirectUri,
225
225
  state: r,
226
- code_challenge: t.codeChallenge,
227
- code_challenge_method: t.codeChallengeMethod
226
+ code_challenge: e.codeChallenge,
227
+ code_challenge_method: e.codeChallengeMethod
228
228
  });
229
229
  return this.config.scopes && this.config.scopes.length > 0 && n.append("scope", this.config.scopes.join(" ")), `${this.config.authUrl}/oauth/authorize?${n.toString()}`;
230
230
  }
231
231
  /**
232
232
  * Exchange authorization code for tokens
233
233
  */
234
- async exchangeCode(t, r) {
234
+ async exchangeCode(e, r) {
235
235
  const n = await this.tokenManager.consumeState();
236
236
  if (!n || n !== r)
237
237
  throw g.invalidState();
@@ -242,7 +242,7 @@ class me {
242
242
  "/oauth/token",
243
243
  {
244
244
  grant_type: "authorization_code",
245
- code: t,
245
+ code: e,
246
246
  redirect_uri: this.config.redirectUri,
247
247
  client_id: this.config.clientId,
248
248
  client_secret: this.config.clientSecret,
@@ -255,14 +255,14 @@ class me {
255
255
  * Refresh access token using refresh token
256
256
  */
257
257
  async refreshToken() {
258
- const t = await this.tokenManager.getTokens();
259
- if (!t?.refreshToken)
258
+ const e = await this.tokenManager.getTokens();
259
+ if (!e?.refreshToken)
260
260
  throw g.invalidGrant("No refresh token available");
261
261
  const r = await this.http.postForm(
262
262
  "/oauth/token",
263
263
  {
264
264
  grant_type: "refresh_token",
265
- refresh_token: t.refreshToken,
265
+ refresh_token: e.refreshToken,
266
266
  client_id: this.config.clientId,
267
267
  client_secret: this.config.clientSecret
268
268
  }
@@ -273,11 +273,11 @@ class me {
273
273
  * Revoke current tokens
274
274
  */
275
275
  async revokeToken() {
276
- const t = await this.tokenManager.getTokens();
277
- if (t)
276
+ const e = await this.tokenManager.getTokens();
277
+ if (e)
278
278
  try {
279
279
  await this.http.postForm("/oauth/revoke", {
280
- token: t.accessToken,
280
+ token: e.accessToken,
281
281
  client_id: this.config.clientId,
282
282
  client_secret: this.config.clientSecret
283
283
  });
@@ -288,134 +288,138 @@ class me {
288
288
  /**
289
289
  * Map OAuth token response to TokenSet
290
290
  */
291
- mapTokenResponse(t) {
291
+ mapTokenResponse(e) {
292
292
  return {
293
- accessToken: t.access_token,
294
- refreshToken: t.refresh_token,
295
- tokenType: t.token_type,
296
- expiresIn: t.expires_in,
293
+ accessToken: e.access_token,
294
+ refreshToken: e.refresh_token,
295
+ tokenType: e.token_type,
296
+ expiresIn: e.expires_in,
297
297
  issuedAt: Date.now(),
298
- scope: t.scope
298
+ scope: e.scope
299
299
  };
300
300
  }
301
301
  }
302
- const G = new TextEncoder(), _ = new TextDecoder();
303
- function we(...e) {
304
- const t = e.reduce((s, { length: a }) => s + a, 0), r = new Uint8Array(t);
302
+ const B = new TextEncoder(), _ = new TextDecoder();
303
+ function we(...t) {
304
+ const e = t.reduce((s, { length: a }) => s + a, 0), r = new Uint8Array(e);
305
305
  let n = 0;
306
- for (const s of e)
306
+ for (const s of t)
307
307
  r.set(s, n), n += s.length;
308
308
  return r;
309
309
  }
310
- function J(e) {
311
- const t = new Uint8Array(e.length);
312
- for (let r = 0; r < e.length; r++) {
313
- const n = e.charCodeAt(r);
310
+ function x(t) {
311
+ const e = new Uint8Array(t.length);
312
+ for (let r = 0; r < t.length; r++) {
313
+ const n = t.charCodeAt(r);
314
314
  if (n > 127)
315
315
  throw new TypeError("non-ASCII string encountered in encode()");
316
- t[r] = n;
316
+ e[r] = n;
317
317
  }
318
- return t;
318
+ return e;
319
319
  }
320
- function Se(e) {
320
+ function Se(t) {
321
321
  if (Uint8Array.fromBase64)
322
- return Uint8Array.fromBase64(e);
323
- const t = atob(e), r = new Uint8Array(t.length);
324
- for (let n = 0; n < t.length; n++)
325
- r[n] = t.charCodeAt(n);
322
+ return Uint8Array.fromBase64(t);
323
+ const e = atob(t), r = new Uint8Array(e.length);
324
+ for (let n = 0; n < e.length; n++)
325
+ r[n] = e.charCodeAt(n);
326
326
  return r;
327
327
  }
328
- function C(e) {
328
+ function C(t) {
329
329
  if (Uint8Array.fromBase64)
330
- return Uint8Array.fromBase64(typeof e == "string" ? e : _.decode(e), {
330
+ return Uint8Array.fromBase64(typeof t == "string" ? t : _.decode(t), {
331
331
  alphabet: "base64url"
332
332
  });
333
- let t = e;
334
- t instanceof Uint8Array && (t = _.decode(t)), t = t.replace(/-/g, "+").replace(/_/g, "/");
333
+ let e = t;
334
+ e instanceof Uint8Array && (e = _.decode(e)), e = e.replace(/-/g, "+").replace(/_/g, "/");
335
335
  try {
336
- return Se(t);
336
+ return Se(e);
337
337
  } catch {
338
338
  throw new TypeError("The input to be decoded is not correctly encoded.");
339
339
  }
340
340
  }
341
- class l extends Error {
341
+ class d extends Error {
342
342
  static code = "ERR_JOSE_GENERIC";
343
343
  code = "ERR_JOSE_GENERIC";
344
- constructor(t, r) {
345
- super(t, r), this.name = this.constructor.name, Error.captureStackTrace?.(this, this.constructor);
344
+ constructor(e, r) {
345
+ super(e, r), this.name = this.constructor.name, Error.captureStackTrace?.(this, this.constructor);
346
346
  }
347
347
  }
348
- class y extends l {
348
+ class y extends d {
349
349
  static code = "ERR_JWT_CLAIM_VALIDATION_FAILED";
350
350
  code = "ERR_JWT_CLAIM_VALIDATION_FAILED";
351
351
  claim;
352
352
  reason;
353
353
  payload;
354
- constructor(t, r, n = "unspecified", s = "unspecified") {
355
- super(t, { cause: { claim: n, reason: s, payload: r } }), this.claim = n, this.reason = s, this.payload = r;
354
+ constructor(e, r, n = "unspecified", s = "unspecified") {
355
+ super(e, { cause: { claim: n, reason: s, payload: r } }), this.claim = n, this.reason = s, this.payload = r;
356
356
  }
357
357
  }
358
- class z extends l {
358
+ class z extends d {
359
359
  static code = "ERR_JWT_EXPIRED";
360
360
  code = "ERR_JWT_EXPIRED";
361
361
  claim;
362
362
  reason;
363
363
  payload;
364
- constructor(t, r, n = "unspecified", s = "unspecified") {
365
- super(t, { cause: { claim: n, reason: s, payload: r } }), this.claim = n, this.reason = s, this.payload = r;
364
+ constructor(e, r, n = "unspecified", s = "unspecified") {
365
+ super(e, { cause: { claim: n, reason: s, payload: r } }), this.claim = n, this.reason = s, this.payload = r;
366
366
  }
367
367
  }
368
- class w extends l {
368
+ class ge extends d {
369
+ static code = "ERR_JOSE_ALG_NOT_ALLOWED";
370
+ code = "ERR_JOSE_ALG_NOT_ALLOWED";
371
+ }
372
+ class w extends d {
369
373
  static code = "ERR_JOSE_NOT_SUPPORTED";
370
374
  code = "ERR_JOSE_NOT_SUPPORTED";
371
375
  }
372
- class f extends l {
376
+ class f extends d {
373
377
  static code = "ERR_JWS_INVALID";
374
378
  code = "ERR_JWS_INVALID";
375
379
  }
376
- class te extends l {
380
+ class ee extends d {
377
381
  static code = "ERR_JWT_INVALID";
378
382
  code = "ERR_JWT_INVALID";
379
383
  }
380
- class re extends l {
384
+ class te extends d {
381
385
  static code = "ERR_JWKS_INVALID";
382
386
  code = "ERR_JWKS_INVALID";
383
387
  }
384
- class ne extends l {
388
+ class re extends d {
385
389
  static code = "ERR_JWKS_NO_MATCHING_KEY";
386
390
  code = "ERR_JWKS_NO_MATCHING_KEY";
387
- constructor(t = "no applicable key found in the JSON Web Key Set", r) {
388
- super(t, r);
391
+ constructor(e = "no applicable key found in the JSON Web Key Set", r) {
392
+ super(e, r);
389
393
  }
390
394
  }
391
- class ge extends l {
395
+ class be extends d {
392
396
  [Symbol.asyncIterator];
393
397
  static code = "ERR_JWKS_MULTIPLE_MATCHING_KEYS";
394
398
  code = "ERR_JWKS_MULTIPLE_MATCHING_KEYS";
395
- constructor(t = "multiple matching keys found in the JSON Web Key Set", r) {
396
- super(t, r);
399
+ constructor(e = "multiple matching keys found in the JSON Web Key Set", r) {
400
+ super(e, r);
397
401
  }
398
402
  }
399
- class be extends l {
403
+ class Ee extends d {
400
404
  static code = "ERR_JWKS_TIMEOUT";
401
405
  code = "ERR_JWKS_TIMEOUT";
402
- constructor(t = "request timed out", r) {
403
- super(t, r);
406
+ constructor(e = "request timed out", r) {
407
+ super(e, r);
404
408
  }
405
409
  }
406
- class Ee extends l {
410
+ class Ae extends d {
407
411
  static code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
408
412
  code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
409
- constructor(t = "signature verification failed", r) {
410
- super(t, r);
413
+ constructor(e = "signature verification failed", r) {
414
+ super(e, r);
411
415
  }
412
416
  }
413
- const m = (e, t = "algorithm.name") => new TypeError(`CryptoKey does not support this operation, its ${t} must be ${e}`), A = (e, t) => e.name === t;
414
- function x(e) {
415
- return parseInt(e.name.slice(4), 10);
417
+ const m = (t, e = "algorithm.name") => new TypeError(`CryptoKey does not support this operation, its ${e} must be ${t}`), A = (t, e) => t.name === e;
418
+ function O(t) {
419
+ return parseInt(t.name.slice(4), 10);
416
420
  }
417
- function Ae(e) {
418
- switch (e) {
421
+ function Te(t) {
422
+ switch (t) {
419
423
  case "ES256":
420
424
  return "P-256";
421
425
  case "ES384":
@@ -426,92 +430,92 @@ function Ae(e) {
426
430
  throw new Error("unreachable");
427
431
  }
428
432
  }
429
- function Ke(e, t) {
430
- if (!e.usages.includes(t))
431
- throw new TypeError(`CryptoKey does not support this operation, its usages must include ${t}.`);
433
+ function Ke(t, e) {
434
+ if (!t.usages.includes(e))
435
+ throw new TypeError(`CryptoKey does not support this operation, its usages must include ${e}.`);
432
436
  }
433
- function Te(e, t, r) {
434
- switch (t) {
437
+ function ve(t, e, r) {
438
+ switch (e) {
435
439
  case "HS256":
436
440
  case "HS384":
437
441
  case "HS512": {
438
- if (!A(e.algorithm, "HMAC"))
442
+ if (!A(t.algorithm, "HMAC"))
439
443
  throw m("HMAC");
440
- const n = parseInt(t.slice(2), 10);
441
- if (x(e.algorithm.hash) !== n)
444
+ const n = parseInt(e.slice(2), 10);
445
+ if (O(t.algorithm.hash) !== n)
442
446
  throw m(`SHA-${n}`, "algorithm.hash");
443
447
  break;
444
448
  }
445
449
  case "RS256":
446
450
  case "RS384":
447
451
  case "RS512": {
448
- if (!A(e.algorithm, "RSASSA-PKCS1-v1_5"))
452
+ if (!A(t.algorithm, "RSASSA-PKCS1-v1_5"))
449
453
  throw m("RSASSA-PKCS1-v1_5");
450
- const n = parseInt(t.slice(2), 10);
451
- if (x(e.algorithm.hash) !== n)
454
+ const n = parseInt(e.slice(2), 10);
455
+ if (O(t.algorithm.hash) !== n)
452
456
  throw m(`SHA-${n}`, "algorithm.hash");
453
457
  break;
454
458
  }
455
459
  case "PS256":
456
460
  case "PS384":
457
461
  case "PS512": {
458
- if (!A(e.algorithm, "RSA-PSS"))
462
+ if (!A(t.algorithm, "RSA-PSS"))
459
463
  throw m("RSA-PSS");
460
- const n = parseInt(t.slice(2), 10);
461
- if (x(e.algorithm.hash) !== n)
464
+ const n = parseInt(e.slice(2), 10);
465
+ if (O(t.algorithm.hash) !== n)
462
466
  throw m(`SHA-${n}`, "algorithm.hash");
463
467
  break;
464
468
  }
465
469
  case "Ed25519":
466
470
  case "EdDSA": {
467
- if (!A(e.algorithm, "Ed25519"))
471
+ if (!A(t.algorithm, "Ed25519"))
468
472
  throw m("Ed25519");
469
473
  break;
470
474
  }
471
475
  case "ML-DSA-44":
472
476
  case "ML-DSA-65":
473
477
  case "ML-DSA-87": {
474
- if (!A(e.algorithm, t))
475
- throw m(t);
478
+ if (!A(t.algorithm, e))
479
+ throw m(e);
476
480
  break;
477
481
  }
478
482
  case "ES256":
479
483
  case "ES384":
480
484
  case "ES512": {
481
- if (!A(e.algorithm, "ECDSA"))
485
+ if (!A(t.algorithm, "ECDSA"))
482
486
  throw m("ECDSA");
483
- const n = Ae(t);
484
- if (e.algorithm.namedCurve !== n)
487
+ const n = Te(e);
488
+ if (t.algorithm.namedCurve !== n)
485
489
  throw m(n, "algorithm.namedCurve");
486
490
  break;
487
491
  }
488
492
  default:
489
493
  throw new TypeError("CryptoKey does not support this operation");
490
494
  }
491
- Ke(e, r);
495
+ Ke(t, r);
492
496
  }
493
- function se(e, t, ...r) {
497
+ function ne(t, e, ...r) {
494
498
  if (r = r.filter(Boolean), r.length > 2) {
495
499
  const n = r.pop();
496
- e += `one of type ${r.join(", ")}, or ${n}.`;
497
- } else r.length === 2 ? e += `one of type ${r[0]} or ${r[1]}.` : e += `of type ${r[0]}.`;
498
- 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;
500
+ t += `one of type ${r.join(", ")}, or ${n}.`;
501
+ } else r.length === 2 ? t += `one of type ${r[0]} or ${r[1]}.` : t += `of type ${r[0]}.`;
502
+ return e == null ? t += ` Received ${e}` : typeof e == "function" && e.name ? t += ` Received function ${e.name}` : typeof e == "object" && e != null && e.constructor?.name && (t += ` Received an instance of ${e.constructor.name}`), t;
499
503
  }
500
- const ve = (e, ...t) => se("Key must be ", e, ...t), ae = (e, t, ...r) => se(`Key for the ${e} algorithm must be `, t, ...r), ie = (e) => {
501
- if (e?.[Symbol.toStringTag] === "CryptoKey")
504
+ const Ce = (t, ...e) => ne("Key must be ", t, ...e), se = (t, e, ...r) => ne(`Key for the ${t} algorithm must be `, e, ...r), ae = (t) => {
505
+ if (t?.[Symbol.toStringTag] === "CryptoKey")
502
506
  return !0;
503
507
  try {
504
- return e instanceof CryptoKey;
508
+ return t instanceof CryptoKey;
505
509
  } catch {
506
510
  return !1;
507
511
  }
508
- }, oe = (e) => e?.[Symbol.toStringTag] === "KeyObject", ce = (e) => ie(e) || oe(e);
509
- function Ce(...e) {
510
- const t = e.filter(Boolean);
511
- if (t.length === 0 || t.length === 1)
512
+ }, ie = (t) => t?.[Symbol.toStringTag] === "KeyObject", oe = (t) => ae(t) || ie(t);
513
+ function _e(...t) {
514
+ const e = t.filter(Boolean);
515
+ if (e.length === 0 || e.length === 1)
512
516
  return !0;
513
517
  let r;
514
- for (const n of t) {
518
+ for (const n of e) {
515
519
  const s = Object.keys(n);
516
520
  if (!r || r.size === 0) {
517
521
  r = new Set(s);
@@ -525,33 +529,33 @@ function Ce(...e) {
525
529
  }
526
530
  return !0;
527
531
  }
528
- const _e = (e) => typeof e == "object" && e !== null;
529
- function E(e) {
530
- if (!_e(e) || Object.prototype.toString.call(e) !== "[object Object]")
532
+ const Re = (t) => typeof t == "object" && t !== null;
533
+ function b(t) {
534
+ if (!Re(t) || Object.prototype.toString.call(t) !== "[object Object]")
531
535
  return !1;
532
- if (Object.getPrototypeOf(e) === null)
536
+ if (Object.getPrototypeOf(t) === null)
533
537
  return !0;
534
- let t = e;
535
- for (; Object.getPrototypeOf(t) !== null; )
536
- t = Object.getPrototypeOf(t);
537
- return Object.getPrototypeOf(e) === t;
538
- }
539
- function Re(e, t) {
540
- if (e.startsWith("RS") || e.startsWith("PS")) {
541
- const { modulusLength: r } = t.algorithm;
538
+ let e = t;
539
+ for (; Object.getPrototypeOf(e) !== null; )
540
+ e = Object.getPrototypeOf(e);
541
+ return Object.getPrototypeOf(t) === e;
542
+ }
543
+ function Pe(t, e) {
544
+ if (t.startsWith("RS") || t.startsWith("PS")) {
545
+ const { modulusLength: r } = e.algorithm;
542
546
  if (typeof r != "number" || r < 2048)
543
- throw new TypeError(`${e} requires key modulusLength to be 2048 bits or larger`);
547
+ throw new TypeError(`${t} requires key modulusLength to be 2048 bits or larger`);
544
548
  }
545
549
  }
546
- function Pe(e) {
547
- let t, r;
548
- switch (e.kty) {
550
+ function We(t) {
551
+ let e, r;
552
+ switch (t.kty) {
549
553
  case "AKP": {
550
- switch (e.alg) {
554
+ switch (t.alg) {
551
555
  case "ML-DSA-44":
552
556
  case "ML-DSA-65":
553
557
  case "ML-DSA-87":
554
- t = { name: e.alg }, r = e.priv ? ["sign"] : ["verify"];
558
+ e = { name: t.alg }, r = t.priv ? ["sign"] : ["verify"];
555
559
  break;
556
560
  default:
557
561
  throw new w('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
@@ -559,25 +563,25 @@ function Pe(e) {
559
563
  break;
560
564
  }
561
565
  case "RSA": {
562
- switch (e.alg) {
566
+ switch (t.alg) {
563
567
  case "PS256":
564
568
  case "PS384":
565
569
  case "PS512":
566
- t = { name: "RSA-PSS", hash: `SHA-${e.alg.slice(-3)}` }, r = e.d ? ["sign"] : ["verify"];
570
+ e = { name: "RSA-PSS", hash: `SHA-${t.alg.slice(-3)}` }, r = t.d ? ["sign"] : ["verify"];
567
571
  break;
568
572
  case "RS256":
569
573
  case "RS384":
570
574
  case "RS512":
571
- t = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${e.alg.slice(-3)}` }, r = e.d ? ["sign"] : ["verify"];
575
+ e = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${t.alg.slice(-3)}` }, r = t.d ? ["sign"] : ["verify"];
572
576
  break;
573
577
  case "RSA-OAEP":
574
578
  case "RSA-OAEP-256":
575
579
  case "RSA-OAEP-384":
576
580
  case "RSA-OAEP-512":
577
- t = {
581
+ e = {
578
582
  name: "RSA-OAEP",
579
- hash: `SHA-${parseInt(e.alg.slice(-3), 10) || 1}`
580
- }, r = e.d ? ["decrypt", "unwrapKey"] : ["encrypt", "wrapKey"];
583
+ hash: `SHA-${parseInt(t.alg.slice(-3), 10) || 1}`
584
+ }, r = t.d ? ["decrypt", "unwrapKey"] : ["encrypt", "wrapKey"];
581
585
  break;
582
586
  default:
583
587
  throw new w('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
@@ -585,21 +589,21 @@ function Pe(e) {
585
589
  break;
586
590
  }
587
591
  case "EC": {
588
- switch (e.alg) {
592
+ switch (t.alg) {
589
593
  case "ES256":
590
- t = { name: "ECDSA", namedCurve: "P-256" }, r = e.d ? ["sign"] : ["verify"];
594
+ e = { name: "ECDSA", namedCurve: "P-256" }, r = t.d ? ["sign"] : ["verify"];
591
595
  break;
592
596
  case "ES384":
593
- t = { name: "ECDSA", namedCurve: "P-384" }, r = e.d ? ["sign"] : ["verify"];
597
+ e = { name: "ECDSA", namedCurve: "P-384" }, r = t.d ? ["sign"] : ["verify"];
594
598
  break;
595
599
  case "ES512":
596
- t = { name: "ECDSA", namedCurve: "P-521" }, r = e.d ? ["sign"] : ["verify"];
600
+ e = { name: "ECDSA", namedCurve: "P-521" }, r = t.d ? ["sign"] : ["verify"];
597
601
  break;
598
602
  case "ECDH-ES":
599
603
  case "ECDH-ES+A128KW":
600
604
  case "ECDH-ES+A192KW":
601
605
  case "ECDH-ES+A256KW":
602
- t = { name: "ECDH", namedCurve: e.crv }, r = e.d ? ["deriveBits"] : [];
606
+ e = { name: "ECDH", namedCurve: t.crv }, r = t.d ? ["deriveBits"] : [];
603
607
  break;
604
608
  default:
605
609
  throw new w('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
@@ -607,16 +611,16 @@ function Pe(e) {
607
611
  break;
608
612
  }
609
613
  case "OKP": {
610
- switch (e.alg) {
614
+ switch (t.alg) {
611
615
  case "Ed25519":
612
616
  case "EdDSA":
613
- t = { name: "Ed25519" }, r = e.d ? ["sign"] : ["verify"];
617
+ e = { name: "Ed25519" }, r = t.d ? ["sign"] : ["verify"];
614
618
  break;
615
619
  case "ECDH-ES":
616
620
  case "ECDH-ES+A128KW":
617
621
  case "ECDH-ES+A192KW":
618
622
  case "ECDH-ES+A256KW":
619
- t = { name: e.crv }, r = e.d ? ["deriveBits"] : [];
623
+ e = { name: t.crv }, r = t.d ? ["deriveBits"] : [];
620
624
  break;
621
625
  default:
622
626
  throw new w('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
@@ -626,78 +630,84 @@ function Pe(e) {
626
630
  default:
627
631
  throw new w('Invalid or unsupported JWK "kty" (Key Type) Parameter value');
628
632
  }
629
- return { algorithm: t, keyUsages: r };
633
+ return { algorithm: e, keyUsages: r };
630
634
  }
631
- async function R(e) {
632
- if (!e.alg)
635
+ async function W(t) {
636
+ if (!t.alg)
633
637
  throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
634
- const { algorithm: t, keyUsages: r } = Pe(e), n = { ...e };
635
- 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);
638
+ const { algorithm: e, keyUsages: r } = We(t), n = { ...t };
639
+ return n.kty !== "AKP" && delete n.alg, delete n.use, crypto.subtle.importKey("jwk", n, e, t.ext ?? !(t.d || t.priv), t.key_ops ?? r);
636
640
  }
637
- async function We(e, t, r) {
638
- if (!E(e))
641
+ async function ke(t, e, r) {
642
+ if (!b(t))
639
643
  throw new TypeError("JWK must be an object");
640
644
  let n;
641
- switch (t ??= e.alg, n ??= e.ext, e.kty) {
645
+ switch (e ??= t.alg, n ??= t.ext, t.kty) {
642
646
  case "oct":
643
- if (typeof e.k != "string" || !e.k)
647
+ if (typeof t.k != "string" || !t.k)
644
648
  throw new TypeError('missing "k" (Key Value) Parameter value');
645
- return C(e.k);
649
+ return C(t.k);
646
650
  case "RSA":
647
- if ("oth" in e && e.oth !== void 0)
651
+ if ("oth" in t && t.oth !== void 0)
648
652
  throw new w('RSA JWK "oth" (Other Primes Info) Parameter value is not supported');
649
- return R({ ...e, alg: t, ext: n });
653
+ return W({ ...t, alg: e, ext: n });
650
654
  case "AKP": {
651
- if (typeof e.alg != "string" || !e.alg)
655
+ if (typeof t.alg != "string" || !t.alg)
652
656
  throw new TypeError('missing "alg" (Algorithm) Parameter value');
653
- if (t !== void 0 && t !== e.alg)
657
+ if (e !== void 0 && e !== t.alg)
654
658
  throw new TypeError("JWK alg and alg option value mismatch");
655
- return R({ ...e, ext: n });
659
+ return W({ ...t, ext: n });
656
660
  }
657
661
  case "EC":
658
662
  case "OKP":
659
- return R({ ...e, alg: t, ext: n });
663
+ return W({ ...t, alg: e, ext: n });
660
664
  default:
661
665
  throw new w('Unsupported "kty" (Key Type) Parameter value');
662
666
  }
663
667
  }
664
- function ke(e, t, r, n, s) {
668
+ function Ie(t, e, r, n, s) {
665
669
  if (s.crit !== void 0 && n?.crit === void 0)
666
- throw new e('"crit" (Critical) Header Parameter MUST be integrity protected');
670
+ throw new t('"crit" (Critical) Header Parameter MUST be integrity protected');
667
671
  if (!n || n.crit === void 0)
668
672
  return /* @__PURE__ */ new Set();
669
673
  if (!Array.isArray(n.crit) || n.crit.length === 0 || n.crit.some((i) => typeof i != "string" || i.length === 0))
670
- throw new e('"crit" (Critical) Header Parameter MUST be an array of non-empty strings when present');
674
+ throw new t('"crit" (Critical) Header Parameter MUST be an array of non-empty strings when present');
671
675
  let a;
672
- a = t;
676
+ r !== void 0 ? a = new Map([...Object.entries(r), ...e.entries()]) : a = e;
673
677
  for (const i of n.crit) {
674
678
  if (!a.has(i))
675
679
  throw new w(`Extension Header Parameter "${i}" is not recognized`);
676
680
  if (s[i] === void 0)
677
- throw new e(`Extension Header Parameter "${i}" is missing`);
681
+ throw new t(`Extension Header Parameter "${i}" is missing`);
678
682
  if (a.get(i) && n[i] === void 0)
679
- throw new e(`Extension Header Parameter "${i}" MUST be integrity protected`);
683
+ throw new t(`Extension Header Parameter "${i}" MUST be integrity protected`);
680
684
  }
681
685
  return new Set(n.crit);
682
686
  }
683
- const L = (e) => E(e) && typeof e.kty == "string", Ie = (e) => e.kty !== "oct" && (e.kty === "AKP" && typeof e.priv == "string" || typeof e.d == "string"), Je = (e) => e.kty !== "oct" && e.d === void 0 && e.priv === void 0, xe = (e) => e.kty === "oct" && typeof e.k == "string";
684
- let T;
685
- const B = async (e, t, r, n = !1) => {
686
- T ||= /* @__PURE__ */ new WeakMap();
687
- let s = T.get(e);
687
+ function Je(t, e) {
688
+ if (e !== void 0 && (!Array.isArray(e) || e.some((r) => typeof r != "string")))
689
+ throw new TypeError(`"${t}" option must be an array of strings`);
690
+ if (e)
691
+ return new Set(e);
692
+ }
693
+ const V = (t) => b(t) && typeof t.kty == "string", xe = (t) => t.kty !== "oct" && (t.kty === "AKP" && typeof t.priv == "string" || typeof t.d == "string"), Oe = (t) => t.kty !== "oct" && t.d === void 0 && t.priv === void 0, De = (t) => t.kty === "oct" && typeof t.k == "string";
694
+ let K;
695
+ const q = async (t, e, r, n = !1) => {
696
+ K ||= /* @__PURE__ */ new WeakMap();
697
+ let s = K.get(t);
688
698
  if (s?.[r])
689
699
  return s[r];
690
- const a = await R({ ...t, alg: r });
691
- return n && Object.freeze(e), s ? s[r] = a : T.set(e, { [r]: a }), a;
692
- }, Ue = (e, t) => {
693
- T ||= /* @__PURE__ */ new WeakMap();
694
- let r = T.get(e);
695
- if (r?.[t])
696
- return r[t];
697
- const n = e.type === "public", s = !!n;
700
+ const a = await W({ ...e, alg: r });
701
+ return n && Object.freeze(t), s ? s[r] = a : K.set(t, { [r]: a }), a;
702
+ }, Ue = (t, e) => {
703
+ K ||= /* @__PURE__ */ new WeakMap();
704
+ let r = K.get(t);
705
+ if (r?.[e])
706
+ return r[e];
707
+ const n = t.type === "public", s = !!n;
698
708
  let a;
699
- if (e.asymmetricKeyType === "x25519") {
700
- switch (t) {
709
+ if (t.asymmetricKeyType === "x25519") {
710
+ switch (e) {
701
711
  case "ECDH-ES":
702
712
  case "ECDH-ES+A128KW":
703
713
  case "ECDH-ES+A192KW":
@@ -706,29 +716,29 @@ const B = async (e, t, r, n = !1) => {
706
716
  default:
707
717
  throw new TypeError("given KeyObject instance cannot be used for this algorithm");
708
718
  }
709
- a = e.toCryptoKey(e.asymmetricKeyType, s, n ? [] : ["deriveBits"]);
719
+ a = t.toCryptoKey(t.asymmetricKeyType, s, n ? [] : ["deriveBits"]);
710
720
  }
711
- if (e.asymmetricKeyType === "ed25519") {
712
- if (t !== "EdDSA" && t !== "Ed25519")
721
+ if (t.asymmetricKeyType === "ed25519") {
722
+ if (e !== "EdDSA" && e !== "Ed25519")
713
723
  throw new TypeError("given KeyObject instance cannot be used for this algorithm");
714
- a = e.toCryptoKey(e.asymmetricKeyType, s, [
724
+ a = t.toCryptoKey(t.asymmetricKeyType, s, [
715
725
  n ? "verify" : "sign"
716
726
  ]);
717
727
  }
718
- switch (e.asymmetricKeyType) {
728
+ switch (t.asymmetricKeyType) {
719
729
  case "ml-dsa-44":
720
730
  case "ml-dsa-65":
721
731
  case "ml-dsa-87": {
722
- if (t !== e.asymmetricKeyType.toUpperCase())
732
+ if (e !== t.asymmetricKeyType.toUpperCase())
723
733
  throw new TypeError("given KeyObject instance cannot be used for this algorithm");
724
- a = e.toCryptoKey(e.asymmetricKeyType, s, [
734
+ a = t.toCryptoKey(t.asymmetricKeyType, s, [
725
735
  n ? "verify" : "sign"
726
736
  ]);
727
737
  }
728
738
  }
729
- if (e.asymmetricKeyType === "rsa") {
739
+ if (t.asymmetricKeyType === "rsa") {
730
740
  let i;
731
- switch (t) {
741
+ switch (e) {
732
742
  case "RSA-OAEP":
733
743
  i = "SHA-1";
734
744
  break;
@@ -750,64 +760,64 @@ const B = async (e, t, r, n = !1) => {
750
760
  default:
751
761
  throw new TypeError("given KeyObject instance cannot be used for this algorithm");
752
762
  }
753
- if (t.startsWith("RSA-OAEP"))
754
- return e.toCryptoKey({
763
+ if (e.startsWith("RSA-OAEP"))
764
+ return t.toCryptoKey({
755
765
  name: "RSA-OAEP",
756
766
  hash: i
757
767
  }, s, n ? ["encrypt"] : ["decrypt"]);
758
- a = e.toCryptoKey({
759
- name: t.startsWith("PS") ? "RSA-PSS" : "RSASSA-PKCS1-v1_5",
768
+ a = t.toCryptoKey({
769
+ name: e.startsWith("PS") ? "RSA-PSS" : "RSASSA-PKCS1-v1_5",
760
770
  hash: i
761
771
  }, s, [n ? "verify" : "sign"]);
762
772
  }
763
- if (e.asymmetricKeyType === "ec") {
773
+ if (t.asymmetricKeyType === "ec") {
764
774
  const c = (/* @__PURE__ */ new Map([
765
775
  ["prime256v1", "P-256"],
766
776
  ["secp384r1", "P-384"],
767
777
  ["secp521r1", "P-521"]
768
- ])).get(e.asymmetricKeyDetails?.namedCurve);
778
+ ])).get(t.asymmetricKeyDetails?.namedCurve);
769
779
  if (!c)
770
780
  throw new TypeError("given KeyObject instance cannot be used for this algorithm");
771
- t === "ES256" && c === "P-256" && (a = e.toCryptoKey({
781
+ e === "ES256" && c === "P-256" && (a = t.toCryptoKey({
772
782
  name: "ECDSA",
773
783
  namedCurve: c
774
- }, s, [n ? "verify" : "sign"])), t === "ES384" && c === "P-384" && (a = e.toCryptoKey({
784
+ }, s, [n ? "verify" : "sign"])), e === "ES384" && c === "P-384" && (a = t.toCryptoKey({
775
785
  name: "ECDSA",
776
786
  namedCurve: c
777
- }, s, [n ? "verify" : "sign"])), t === "ES512" && c === "P-521" && (a = e.toCryptoKey({
787
+ }, s, [n ? "verify" : "sign"])), e === "ES512" && c === "P-521" && (a = t.toCryptoKey({
778
788
  name: "ECDSA",
779
789
  namedCurve: c
780
- }, s, [n ? "verify" : "sign"])), t.startsWith("ECDH-ES") && (a = e.toCryptoKey({
790
+ }, s, [n ? "verify" : "sign"])), e.startsWith("ECDH-ES") && (a = t.toCryptoKey({
781
791
  name: "ECDH",
782
792
  namedCurve: c
783
793
  }, s, n ? [] : ["deriveBits"]));
784
794
  }
785
795
  if (!a)
786
796
  throw new TypeError("given KeyObject instance cannot be used for this algorithm");
787
- return r ? r[t] = a : T.set(e, { [t]: a }), a;
797
+ return r ? r[e] = a : K.set(t, { [e]: a }), a;
788
798
  };
789
- async function De(e, t) {
790
- if (e instanceof Uint8Array || ie(e))
791
- return e;
792
- if (oe(e)) {
793
- if (e.type === "secret")
794
- return e.export();
795
- if ("toCryptoKey" in e && typeof e.toCryptoKey == "function")
799
+ async function Me(t, e) {
800
+ if (t instanceof Uint8Array || ae(t))
801
+ return t;
802
+ if (ie(t)) {
803
+ if (t.type === "secret")
804
+ return t.export();
805
+ if ("toCryptoKey" in t && typeof t.toCryptoKey == "function")
796
806
  try {
797
- return Ue(e, t);
807
+ return Ue(t, e);
798
808
  } catch (n) {
799
809
  if (n instanceof TypeError)
800
810
  throw n;
801
811
  }
802
- let r = e.export({ format: "jwk" });
803
- return B(e, r, t);
812
+ let r = t.export({ format: "jwk" });
813
+ return q(t, r, e);
804
814
  }
805
- if (L(e))
806
- return e.k ? C(e.k) : B(e, e, t, !0);
815
+ if (V(t))
816
+ return t.k ? C(t.k) : q(t, t, e, !0);
807
817
  throw new Error("unreachable");
808
818
  }
809
- const K = (e) => e?.[Symbol.toStringTag], M = (e, t, r) => {
810
- if (t.use !== void 0) {
819
+ const T = (t) => t?.[Symbol.toStringTag], M = (t, e, r) => {
820
+ if (e.use !== void 0) {
811
821
  let n;
812
822
  switch (r) {
813
823
  case "sign":
@@ -819,97 +829,97 @@ const K = (e) => e?.[Symbol.toStringTag], M = (e, t, r) => {
819
829
  n = "enc";
820
830
  break;
821
831
  }
822
- if (t.use !== n)
832
+ if (e.use !== n)
823
833
  throw new TypeError(`Invalid key for this operation, its "use" must be "${n}" when present`);
824
834
  }
825
- if (t.alg !== void 0 && t.alg !== e)
826
- throw new TypeError(`Invalid key for this operation, its "alg" must be "${e}" when present`);
827
- if (Array.isArray(t.key_ops)) {
835
+ if (e.alg !== void 0 && e.alg !== t)
836
+ throw new TypeError(`Invalid key for this operation, its "alg" must be "${t}" when present`);
837
+ if (Array.isArray(e.key_ops)) {
828
838
  let n;
829
839
  switch (!0) {
830
840
  case r === "verify":
831
- case e === "dir":
832
- case e.includes("CBC-HS"):
841
+ case t === "dir":
842
+ case t.includes("CBC-HS"):
833
843
  n = r;
834
844
  break;
835
- case e.startsWith("PBES2"):
845
+ case t.startsWith("PBES2"):
836
846
  n = "deriveBits";
837
847
  break;
838
- case /^A\d{3}(?:GCM)?(?:KW)?$/.test(e):
839
- !e.includes("GCM") && e.endsWith("KW") ? n = "unwrapKey" : n = r;
848
+ case /^A\d{3}(?:GCM)?(?:KW)?$/.test(t):
849
+ !t.includes("GCM") && t.endsWith("KW") ? n = "unwrapKey" : n = r;
840
850
  break;
841
851
  case r === "encrypt":
842
852
  n = "wrapKey";
843
853
  break;
844
854
  case r === "decrypt":
845
- n = e.startsWith("RSA") ? "unwrapKey" : "deriveBits";
855
+ n = t.startsWith("RSA") ? "unwrapKey" : "deriveBits";
846
856
  break;
847
857
  }
848
- if (n && t.key_ops?.includes?.(n) === !1)
858
+ if (n && e.key_ops?.includes?.(n) === !1)
849
859
  throw new TypeError(`Invalid key for this operation, its "key_ops" must include "${n}" when present`);
850
860
  }
851
861
  return !0;
852
- }, Me = (e, t, r) => {
853
- if (!(t instanceof Uint8Array)) {
854
- if (L(t)) {
855
- if (xe(t) && M(e, t, r))
862
+ }, He = (t, e, r) => {
863
+ if (!(e instanceof Uint8Array)) {
864
+ if (V(e)) {
865
+ if (De(e) && M(t, e, r))
856
866
  return;
857
867
  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');
858
868
  }
859
- if (!ce(t))
860
- throw new TypeError(ae(e, t, "CryptoKey", "KeyObject", "JSON Web Key", "Uint8Array"));
861
- if (t.type !== "secret")
862
- throw new TypeError(`${K(t)} instances for symmetric algorithms must be of type "secret"`);
869
+ if (!oe(e))
870
+ throw new TypeError(se(t, e, "CryptoKey", "KeyObject", "JSON Web Key", "Uint8Array"));
871
+ if (e.type !== "secret")
872
+ throw new TypeError(`${T(e)} instances for symmetric algorithms must be of type "secret"`);
863
873
  }
864
- }, Oe = (e, t, r) => {
865
- if (L(t))
874
+ }, Ne = (t, e, r) => {
875
+ if (V(e))
866
876
  switch (r) {
867
877
  case "decrypt":
868
878
  case "sign":
869
- if (Ie(t) && M(e, t, r))
879
+ if (xe(e) && M(t, e, r))
870
880
  return;
871
881
  throw new TypeError("JSON Web Key for this operation must be a private JWK");
872
882
  case "encrypt":
873
883
  case "verify":
874
- if (Je(t) && M(e, t, r))
884
+ if (Oe(e) && M(t, e, r))
875
885
  return;
876
886
  throw new TypeError("JSON Web Key for this operation must be a public JWK");
877
887
  }
878
- if (!ce(t))
879
- throw new TypeError(ae(e, t, "CryptoKey", "KeyObject", "JSON Web Key"));
880
- if (t.type === "secret")
881
- throw new TypeError(`${K(t)} instances for asymmetric algorithms must not be of type "secret"`);
882
- if (t.type === "public")
888
+ if (!oe(e))
889
+ throw new TypeError(se(t, e, "CryptoKey", "KeyObject", "JSON Web Key"));
890
+ if (e.type === "secret")
891
+ throw new TypeError(`${T(e)} instances for asymmetric algorithms must not be of type "secret"`);
892
+ if (e.type === "public")
883
893
  switch (r) {
884
894
  case "sign":
885
- throw new TypeError(`${K(t)} instances for asymmetric algorithm signing must be of type "private"`);
895
+ throw new TypeError(`${T(e)} instances for asymmetric algorithm signing must be of type "private"`);
886
896
  case "decrypt":
887
- throw new TypeError(`${K(t)} instances for asymmetric algorithm decryption must be of type "private"`);
897
+ throw new TypeError(`${T(e)} instances for asymmetric algorithm decryption must be of type "private"`);
888
898
  }
889
- if (t.type === "private")
899
+ if (e.type === "private")
890
900
  switch (r) {
891
901
  case "verify":
892
- throw new TypeError(`${K(t)} instances for asymmetric algorithm verifying must be of type "public"`);
902
+ throw new TypeError(`${T(e)} instances for asymmetric algorithm verifying must be of type "public"`);
893
903
  case "encrypt":
894
- throw new TypeError(`${K(t)} instances for asymmetric algorithm encryption must be of type "public"`);
904
+ throw new TypeError(`${T(e)} instances for asymmetric algorithm encryption must be of type "public"`);
895
905
  }
896
906
  };
897
- function He(e, t, r) {
898
- switch (e.substring(0, 2)) {
907
+ function Le(t, e, r) {
908
+ switch (t.substring(0, 2)) {
899
909
  case "A1":
900
910
  case "A2":
901
911
  case "di":
902
912
  case "HS":
903
913
  case "PB":
904
- Me(e, t, r);
914
+ He(t, e, r);
905
915
  break;
906
916
  default:
907
- Oe(e, t, r);
917
+ Ne(t, e, r);
908
918
  }
909
919
  }
910
- function Ne(e, t) {
911
- const r = `SHA-${e.slice(-3)}`;
912
- switch (e) {
920
+ function $e(t, e) {
921
+ const r = `SHA-${t.slice(-3)}`;
922
+ switch (t) {
913
923
  case "HS256":
914
924
  case "HS384":
915
925
  case "HS512":
@@ -917,7 +927,7 @@ function Ne(e, t) {
917
927
  case "PS256":
918
928
  case "PS384":
919
929
  case "PS512":
920
- return { hash: r, name: "RSA-PSS", saltLength: parseInt(e.slice(-3), 10) >> 3 };
930
+ return { hash: r, name: "RSA-PSS", saltLength: parseInt(t.slice(-3), 10) >> 3 };
921
931
  case "RS256":
922
932
  case "RS384":
923
933
  case "RS512":
@@ -925,112 +935,115 @@ function Ne(e, t) {
925
935
  case "ES256":
926
936
  case "ES384":
927
937
  case "ES512":
928
- return { hash: r, name: "ECDSA", namedCurve: t.namedCurve };
938
+ return { hash: r, name: "ECDSA", namedCurve: e.namedCurve };
929
939
  case "Ed25519":
930
940
  case "EdDSA":
931
941
  return { name: "Ed25519" };
932
942
  case "ML-DSA-44":
933
943
  case "ML-DSA-65":
934
944
  case "ML-DSA-87":
935
- return { name: e };
945
+ return { name: t };
936
946
  default:
937
- throw new w(`alg ${e} is not supported either by JOSE or your javascript runtime`);
947
+ throw new w(`alg ${t} is not supported either by JOSE or your javascript runtime`);
938
948
  }
939
949
  }
940
- async function $e(e, t, r) {
941
- if (t instanceof Uint8Array) {
942
- if (!e.startsWith("HS"))
943
- throw new TypeError(ve(t, "CryptoKey", "KeyObject", "JSON Web Key"));
944
- return crypto.subtle.importKey("raw", t, { hash: `SHA-${e.slice(-3)}`, name: "HMAC" }, !1, [r]);
950
+ async function Fe(t, e, r) {
951
+ if (e instanceof Uint8Array) {
952
+ if (!t.startsWith("HS"))
953
+ throw new TypeError(Ce(e, "CryptoKey", "KeyObject", "JSON Web Key"));
954
+ return crypto.subtle.importKey("raw", e, { hash: `SHA-${t.slice(-3)}`, name: "HMAC" }, !1, [r]);
945
955
  }
946
- return Te(t, e, r), t;
956
+ return ve(e, t, r), e;
947
957
  }
948
- async function Le(e, t, r, n) {
949
- const s = await $e(e, t, "verify");
950
- Re(e, s);
951
- const a = Ne(e, s.algorithm);
958
+ async function Ve(t, e, r, n) {
959
+ const s = await Fe(t, e, "verify");
960
+ Pe(t, s);
961
+ const a = $e(t, s.algorithm);
952
962
  try {
953
963
  return await crypto.subtle.verify(a, s, r, n);
954
964
  } catch {
955
965
  return !1;
956
966
  }
957
967
  }
958
- async function Fe(e, t, r) {
959
- if (!E(e))
968
+ async function Ge(t, e, r) {
969
+ if (!b(t))
960
970
  throw new f("Flattened JWS must be an object");
961
- if (e.protected === void 0 && e.header === void 0)
971
+ if (t.protected === void 0 && t.header === void 0)
962
972
  throw new f('Flattened JWS must have either of the "protected" or "header" members');
963
- if (e.protected !== void 0 && typeof e.protected != "string")
973
+ if (t.protected !== void 0 && typeof t.protected != "string")
964
974
  throw new f("JWS Protected Header incorrect type");
965
- if (e.payload === void 0)
975
+ if (t.payload === void 0)
966
976
  throw new f("JWS Payload missing");
967
- if (typeof e.signature != "string")
977
+ if (typeof t.signature != "string")
968
978
  throw new f("JWS Signature missing or incorrect type");
969
- if (e.header !== void 0 && !E(e.header))
979
+ if (t.header !== void 0 && !b(t.header))
970
980
  throw new f("JWS Unprotected Header incorrect type");
971
981
  let n = {};
972
- if (e.protected)
982
+ if (t.protected)
973
983
  try {
974
- const P = C(e.protected);
975
- n = JSON.parse(_.decode(P));
984
+ const he = C(t.protected);
985
+ n = JSON.parse(_.decode(he));
976
986
  } catch {
977
987
  throw new f("JWS Protected Header is invalid");
978
988
  }
979
- if (!Ce(n, e.header))
989
+ if (!_e(n, t.header))
980
990
  throw new f("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
981
991
  const s = {
982
992
  ...n,
983
- ...e.header
984
- }, a = ke(f, /* @__PURE__ */ new Map([["b64", !0]]), r?.crit, n, s);
993
+ ...t.header
994
+ }, a = Ie(f, /* @__PURE__ */ new Map([["b64", !0]]), r?.crit, n, s);
985
995
  let i = !0;
986
996
  if (a.has("b64") && (i = n.b64, typeof i != "boolean"))
987
997
  throw new f('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
988
998
  const { alg: c } = s;
989
999
  if (typeof c != "string" || !c)
990
1000
  throw new f('JWS "alg" (Algorithm) Header Parameter missing or invalid');
1001
+ const h = r && Je("algorithms", r.algorithms);
1002
+ if (h && !h.has(c))
1003
+ throw new ge('"alg" (Algorithm) Header Parameter value not allowed');
991
1004
  if (i) {
992
- if (typeof e.payload != "string")
1005
+ if (typeof t.payload != "string")
993
1006
  throw new f("JWS Payload must be a string");
994
- } else if (typeof e.payload != "string" && !(e.payload instanceof Uint8Array))
1007
+ } else if (typeof t.payload != "string" && !(t.payload instanceof Uint8Array))
995
1008
  throw new f("JWS Payload must be a string or an Uint8Array instance");
996
- let h = !1;
997
- typeof t == "function" && (t = await t(n, e), h = !0), He(c, t, "verify");
998
- const u = we(e.protected !== void 0 ? J(e.protected) : new Uint8Array(), J("."), typeof e.payload == "string" ? i ? J(e.payload) : G.encode(e.payload) : e.payload);
999
- let o;
1009
+ let u = !1;
1010
+ typeof e == "function" && (e = await e(n, t), u = !0), Le(c, e, "verify");
1011
+ const o = we(t.protected !== void 0 ? x(t.protected) : new Uint8Array(), x("."), typeof t.payload == "string" ? i ? x(t.payload) : B.encode(t.payload) : t.payload);
1012
+ let l;
1000
1013
  try {
1001
- o = C(e.signature);
1014
+ l = C(t.signature);
1002
1015
  } catch {
1003
1016
  throw new f("Failed to base64url decode the signature");
1004
1017
  }
1005
- const d = await De(t, c);
1006
- if (!await Le(c, d, o, u))
1007
- throw new Ee();
1008
- let b;
1018
+ const R = await Me(e, c);
1019
+ if (!await Ve(c, R, l, o))
1020
+ throw new Ae();
1021
+ let p;
1009
1022
  if (i)
1010
1023
  try {
1011
- b = C(e.payload);
1024
+ p = C(t.payload);
1012
1025
  } catch {
1013
1026
  throw new f("Failed to base64url decode the payload");
1014
1027
  }
1015
- else typeof e.payload == "string" ? b = G.encode(e.payload) : b = e.payload;
1016
- const p = { payload: b };
1017
- return e.protected !== void 0 && (p.protectedHeader = n), e.header !== void 0 && (p.unprotectedHeader = e.header), h ? { ...p, key: d } : p;
1028
+ else typeof t.payload == "string" ? p = B.encode(t.payload) : p = t.payload;
1029
+ const E = { payload: p };
1030
+ return t.protected !== void 0 && (E.protectedHeader = n), t.header !== void 0 && (E.unprotectedHeader = t.header), u ? { ...E, key: R } : E;
1018
1031
  }
1019
- async function Ve(e, t, r) {
1020
- if (e instanceof Uint8Array && (e = _.decode(e)), typeof e != "string")
1032
+ async function Be(t, e, r) {
1033
+ if (t instanceof Uint8Array && (t = _.decode(t)), typeof t != "string")
1021
1034
  throw new f("Compact JWS must be a string or Uint8Array");
1022
- const { 0: n, 1: s, 2: a, length: i } = e.split(".");
1035
+ const { 0: n, 1: s, 2: a, length: i } = t.split(".");
1023
1036
  if (i !== 3)
1024
1037
  throw new f("Invalid Compact JWS");
1025
- const c = await Fe({ payload: s, protected: n, signature: a }, t, r), h = { payload: c.payload, protectedHeader: c.protectedHeader };
1026
- return typeof t == "function" ? { ...h, key: c.key } : h;
1038
+ const c = await Ge({ payload: s, protected: n, signature: a }, e, r), h = { payload: c.payload, protectedHeader: c.protectedHeader };
1039
+ return typeof e == "function" ? { ...h, key: c.key } : h;
1027
1040
  }
1028
- const Ge = (e) => Math.floor(e.getTime() / 1e3), ue = 60, he = ue * 60, F = he * 24, ze = F * 7, Be = F * 365.25, qe = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i;
1029
- function q(e) {
1030
- const t = qe.exec(e);
1031
- if (!t || t[4] && t[1])
1041
+ const ze = (t) => Math.floor(t.getTime() / 1e3), ce = 60, ue = ce * 60, G = ue * 24, qe = G * 7, Ye = G * 365.25, Xe = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i;
1042
+ function Y(t) {
1043
+ const e = Xe.exec(t);
1044
+ if (!e || e[4] && e[1])
1032
1045
  throw new TypeError("Invalid time period format");
1033
- const r = parseFloat(t[2]), n = t[3].toLowerCase();
1046
+ const r = parseFloat(e[2]), n = e[3].toLowerCase();
1034
1047
  let s;
1035
1048
  switch (n) {
1036
1049
  case "sec":
@@ -1045,42 +1058,42 @@ function q(e) {
1045
1058
  case "min":
1046
1059
  case "mins":
1047
1060
  case "m":
1048
- s = Math.round(r * ue);
1061
+ s = Math.round(r * ce);
1049
1062
  break;
1050
1063
  case "hour":
1051
1064
  case "hours":
1052
1065
  case "hr":
1053
1066
  case "hrs":
1054
1067
  case "h":
1055
- s = Math.round(r * he);
1068
+ s = Math.round(r * ue);
1056
1069
  break;
1057
1070
  case "day":
1058
1071
  case "days":
1059
1072
  case "d":
1060
- s = Math.round(r * F);
1073
+ s = Math.round(r * G);
1061
1074
  break;
1062
1075
  case "week":
1063
1076
  case "weeks":
1064
1077
  case "w":
1065
- s = Math.round(r * ze);
1078
+ s = Math.round(r * qe);
1066
1079
  break;
1067
1080
  default:
1068
- s = Math.round(r * Be);
1081
+ s = Math.round(r * Ye);
1069
1082
  break;
1070
1083
  }
1071
- return t[1] === "-" || t[4] === "ago" ? -s : s;
1084
+ return e[1] === "-" || e[4] === "ago" ? -s : s;
1072
1085
  }
1073
- const Y = (e) => e.includes("/") ? e.toLowerCase() : `application/${e.toLowerCase()}`, Ye = (e, t) => typeof e == "string" ? t.includes(e) : Array.isArray(e) ? t.some(Set.prototype.has.bind(new Set(e))) : !1;
1074
- function Xe(e, t, r = {}) {
1086
+ const X = (t) => t.includes("/") ? t.toLowerCase() : `application/${t.toLowerCase()}`, Qe = (t, e) => typeof t == "string" ? e.includes(t) : Array.isArray(t) ? e.some(Set.prototype.has.bind(new Set(t))) : !1;
1087
+ function Ze(t, e, r = {}) {
1075
1088
  let n;
1076
1089
  try {
1077
- n = JSON.parse(_.decode(t));
1090
+ n = JSON.parse(_.decode(e));
1078
1091
  } catch {
1079
1092
  }
1080
- if (!E(n))
1081
- throw new te("JWT Claims Set must be a top-level JSON object");
1093
+ if (!b(n))
1094
+ throw new ee("JWT Claims Set must be a top-level JSON object");
1082
1095
  const { typ: s } = r;
1083
- if (s && (typeof e.typ != "string" || Y(e.typ) !== Y(s)))
1096
+ if (s && (typeof t.typ != "string" || X(t.typ) !== X(s)))
1084
1097
  throw new y('unexpected "typ" JWT header value', n, "typ", "check_failed");
1085
1098
  const { requiredClaims: a = [], issuer: i, subject: c, audience: h, maxTokenAge: u } = r, o = [...a];
1086
1099
  u !== void 0 && o.push("iat"), h !== void 0 && o.push("aud"), c !== void 0 && o.push("sub"), i !== void 0 && o.push("iss");
@@ -1091,55 +1104,55 @@ function Xe(e, t, r = {}) {
1091
1104
  throw new y('unexpected "iss" claim value', n, "iss", "check_failed");
1092
1105
  if (c && n.sub !== c)
1093
1106
  throw new y('unexpected "sub" claim value', n, "sub", "check_failed");
1094
- if (h && !Ye(n.aud, typeof h == "string" ? [h] : h))
1107
+ if (h && !Qe(n.aud, typeof h == "string" ? [h] : h))
1095
1108
  throw new y('unexpected "aud" claim value', n, "aud", "check_failed");
1096
- let d;
1109
+ let l;
1097
1110
  switch (typeof r.clockTolerance) {
1098
1111
  case "string":
1099
- d = q(r.clockTolerance);
1112
+ l = Y(r.clockTolerance);
1100
1113
  break;
1101
1114
  case "number":
1102
- d = r.clockTolerance;
1115
+ l = r.clockTolerance;
1103
1116
  break;
1104
1117
  case "undefined":
1105
- d = 0;
1118
+ l = 0;
1106
1119
  break;
1107
1120
  default:
1108
1121
  throw new TypeError("Invalid clockTolerance option type");
1109
1122
  }
1110
- const { currentDate: V } = r, b = Ge(V || /* @__PURE__ */ new Date());
1123
+ const { currentDate: R } = r, P = ze(R || /* @__PURE__ */ new Date());
1111
1124
  if ((n.iat !== void 0 || u) && typeof n.iat != "number")
1112
1125
  throw new y('"iat" claim must be a number', n, "iat", "invalid");
1113
1126
  if (n.nbf !== void 0) {
1114
1127
  if (typeof n.nbf != "number")
1115
1128
  throw new y('"nbf" claim must be a number', n, "nbf", "invalid");
1116
- if (n.nbf > b + d)
1129
+ if (n.nbf > P + l)
1117
1130
  throw new y('"nbf" claim timestamp check failed', n, "nbf", "check_failed");
1118
1131
  }
1119
1132
  if (n.exp !== void 0) {
1120
1133
  if (typeof n.exp != "number")
1121
1134
  throw new y('"exp" claim must be a number', n, "exp", "invalid");
1122
- if (n.exp <= b - d)
1135
+ if (n.exp <= P - l)
1123
1136
  throw new z('"exp" claim timestamp check failed', n, "exp", "check_failed");
1124
1137
  }
1125
1138
  if (u) {
1126
- const p = b - n.iat, P = typeof u == "number" ? u : q(u);
1127
- if (p - d > P)
1139
+ const p = P - n.iat, E = typeof u == "number" ? u : Y(u);
1140
+ if (p - l > E)
1128
1141
  throw new z('"iat" claim timestamp check failed (too far in the past)', n, "iat", "check_failed");
1129
- if (p < 0 - d)
1142
+ if (p < 0 - l)
1130
1143
  throw new y('"iat" claim timestamp check failed (it should be in the past)', n, "iat", "check_failed");
1131
1144
  }
1132
1145
  return n;
1133
1146
  }
1134
- async function Qe(e, t, r) {
1135
- const n = await Ve(e, t, r);
1147
+ async function je(t, e, r) {
1148
+ const n = await Be(t, e, r);
1136
1149
  if (n.protectedHeader.crit?.includes("b64") && n.protectedHeader.b64 === !1)
1137
- throw new te("JWTs MUST NOT use unencoded payload");
1138
- const a = { payload: Xe(n.protectedHeader, n.payload, r), protectedHeader: n.protectedHeader };
1139
- return typeof t == "function" ? { ...a, key: n.key } : a;
1150
+ throw new ee("JWTs MUST NOT use unencoded payload");
1151
+ const a = { payload: Ze(n.protectedHeader, n.payload, r), protectedHeader: n.protectedHeader };
1152
+ return typeof e == "function" ? { ...a, key: n.key } : a;
1140
1153
  }
1141
- function Ze(e) {
1142
- switch (typeof e == "string" && e.slice(0, 2)) {
1154
+ function et(t) {
1155
+ switch (typeof t == "string" && t.slice(0, 2)) {
1143
1156
  case "RS":
1144
1157
  case "PS":
1145
1158
  return "RSA";
@@ -1153,25 +1166,25 @@ function Ze(e) {
1153
1166
  throw new w('Unsupported "alg" value for a JSON Web Key Set');
1154
1167
  }
1155
1168
  }
1156
- function je(e) {
1157
- return e && typeof e == "object" && Array.isArray(e.keys) && e.keys.every(et);
1169
+ function tt(t) {
1170
+ return t && typeof t == "object" && Array.isArray(t.keys) && t.keys.every(rt);
1158
1171
  }
1159
- function et(e) {
1160
- return E(e);
1172
+ function rt(t) {
1173
+ return b(t);
1161
1174
  }
1162
- class tt {
1175
+ class nt {
1163
1176
  #r;
1164
1177
  #i = /* @__PURE__ */ new WeakMap();
1165
- constructor(t) {
1166
- if (!je(t))
1167
- throw new re("JSON Web Key Set malformed");
1168
- this.#r = structuredClone(t);
1178
+ constructor(e) {
1179
+ if (!tt(e))
1180
+ throw new te("JSON Web Key Set malformed");
1181
+ this.#r = structuredClone(e);
1169
1182
  }
1170
1183
  jwks() {
1171
1184
  return this.#r;
1172
1185
  }
1173
- async getKey(t, r) {
1174
- const { alg: n, kid: s } = { ...t, ...r?.header }, a = Ze(n), i = this.#r.keys.filter((u) => {
1186
+ async getKey(e, r) {
1187
+ const { alg: n, kid: s } = { ...e, ...r?.header }, a = et(n), i = this.#r.keys.filter((u) => {
1175
1188
  let o = a === u.kty;
1176
1189
  if (o && typeof s == "string" && (o = s === u.kid), o && (typeof u.alg == "string" || a === "AKP") && (o = n === u.alg), o && typeof u.use == "string" && (o = u.use === "sig"), o && Array.isArray(u.key_ops) && (o = u.key_ops.includes("verify")), o)
1177
1190
  switch (n) {
@@ -1192,69 +1205,69 @@ class tt {
1192
1205
  return o;
1193
1206
  }), { 0: c, length: h } = i;
1194
1207
  if (h === 0)
1195
- throw new ne();
1208
+ throw new re();
1196
1209
  if (h !== 1) {
1197
- const u = new ge(), o = this.#i;
1210
+ const u = new be(), o = this.#i;
1198
1211
  throw u[Symbol.asyncIterator] = async function* () {
1199
- for (const d of i)
1212
+ for (const l of i)
1200
1213
  try {
1201
- yield await X(o, d, n);
1214
+ yield await Q(o, l, n);
1202
1215
  } catch {
1203
1216
  }
1204
1217
  }, u;
1205
1218
  }
1206
- return X(this.#i, c, n);
1219
+ return Q(this.#i, c, n);
1207
1220
  }
1208
1221
  }
1209
- async function X(e, t, r) {
1210
- const n = e.get(t) || e.set(t, {}).get(t);
1222
+ async function Q(t, e, r) {
1223
+ const n = t.get(e) || t.set(e, {}).get(e);
1211
1224
  if (n[r] === void 0) {
1212
- const s = await We({ ...t, ext: !0 }, r);
1225
+ const s = await ke({ ...e, ext: !0 }, r);
1213
1226
  if (s instanceof Uint8Array || s.type !== "public")
1214
- throw new re("JSON Web Key Set members must be public keys");
1227
+ throw new te("JSON Web Key Set members must be public keys");
1215
1228
  n[r] = s;
1216
1229
  }
1217
1230
  return n[r];
1218
1231
  }
1219
- function Q(e) {
1220
- const t = new tt(e), r = async (n, s) => t.getKey(n, s);
1232
+ function Z(t) {
1233
+ const e = new nt(t), r = async (n, s) => e.getKey(n, s);
1221
1234
  return Object.defineProperties(r, {
1222
1235
  jwks: {
1223
- value: () => structuredClone(t.jwks()),
1236
+ value: () => structuredClone(e.jwks()),
1224
1237
  enumerable: !1,
1225
1238
  configurable: !1,
1226
1239
  writable: !1
1227
1240
  }
1228
1241
  }), r;
1229
1242
  }
1230
- function rt() {
1243
+ function st() {
1231
1244
  return typeof WebSocketPair < "u" || typeof navigator < "u" && navigator.userAgent === "Cloudflare-Workers" || typeof EdgeRuntime < "u" && EdgeRuntime === "vercel";
1232
1245
  }
1233
- let O;
1234
- (typeof navigator > "u" || !navigator.userAgent?.startsWith?.("Mozilla/5.0 ")) && (O = "jose/v6.1.3");
1235
- const nt = /* @__PURE__ */ Symbol();
1236
- async function st(e, t, r, n = fetch) {
1237
- const s = await n(e, {
1246
+ let H;
1247
+ (typeof navigator > "u" || !navigator.userAgent?.startsWith?.("Mozilla/5.0 ")) && (H = "jose/v6.1.3");
1248
+ const at = /* @__PURE__ */ Symbol();
1249
+ async function it(t, e, r, n = fetch) {
1250
+ const s = await n(t, {
1238
1251
  method: "GET",
1239
1252
  signal: r,
1240
1253
  redirect: "manual",
1241
- headers: t
1254
+ headers: e
1242
1255
  }).catch((a) => {
1243
- throw a.name === "TimeoutError" ? new be() : a;
1256
+ throw a.name === "TimeoutError" ? new Ee() : a;
1244
1257
  });
1245
1258
  if (s.status !== 200)
1246
- throw new l("Expected 200 OK from the JSON Web Key Set HTTP response");
1259
+ throw new d("Expected 200 OK from the JSON Web Key Set HTTP response");
1247
1260
  try {
1248
1261
  return await s.json();
1249
1262
  } catch {
1250
- throw new l("Failed to parse the JSON Web Key Set HTTP response as JSON");
1263
+ throw new d("Failed to parse the JSON Web Key Set HTTP response as JSON");
1251
1264
  }
1252
1265
  }
1253
- const U = /* @__PURE__ */ Symbol();
1254
- function at(e, t) {
1255
- return !(typeof e != "object" || e === null || !("uat" in e) || typeof e.uat != "number" || Date.now() - e.uat >= t || !("jwks" in e) || !E(e.jwks) || !Array.isArray(e.jwks.keys) || !Array.prototype.every.call(e.jwks.keys, E));
1266
+ const D = /* @__PURE__ */ Symbol();
1267
+ function ot(t, e) {
1268
+ return !(typeof t != "object" || t === null || !("uat" in t) || typeof t.uat != "number" || Date.now() - t.uat >= e || !("jwks" in t) || !b(t.jwks) || !Array.isArray(t.jwks.keys) || !Array.prototype.every.call(t.jwks.keys, b));
1256
1269
  }
1257
- class it {
1270
+ class ct {
1258
1271
  #r;
1259
1272
  #i;
1260
1273
  #c;
@@ -1265,10 +1278,10 @@ class it {
1265
1278
  #u;
1266
1279
  #s;
1267
1280
  #a;
1268
- constructor(t, r) {
1269
- if (!(t instanceof URL))
1281
+ constructor(e, r) {
1282
+ if (!(e instanceof URL))
1270
1283
  throw new TypeError("url must be an instance of URL");
1271
- this.#r = new URL(t.href), this.#i = typeof r?.timeoutDuration == "number" ? r?.timeoutDuration : 5e3, this.#c = typeof r?.cooldownDuration == "number" ? r?.cooldownDuration : 3e4, this.#o = typeof r?.cacheMaxAge == "number" ? r?.cacheMaxAge : 6e5, this.#t = new Headers(r?.headers), O && !this.#t.has("User-Agent") && this.#t.set("User-Agent", O), this.#t.has("accept") || (this.#t.set("accept", "application/json"), this.#t.append("accept", "application/jwk-set+json")), this.#u = r?.[nt], r?.[U] !== void 0 && (this.#a = r?.[U], at(r?.[U], this.#o) && (this.#n = this.#a.uat, this.#s = Q(this.#a.jwks)));
1284
+ this.#r = new URL(e.href), this.#i = typeof r?.timeoutDuration == "number" ? r?.timeoutDuration : 5e3, this.#c = typeof r?.cooldownDuration == "number" ? r?.cooldownDuration : 3e4, this.#o = typeof r?.cacheMaxAge == "number" ? r?.cacheMaxAge : 6e5, this.#t = new Headers(r?.headers), H && !this.#t.has("User-Agent") && this.#t.set("User-Agent", H), this.#t.has("accept") || (this.#t.set("accept", "application/json"), this.#t.append("accept", "application/jwk-set+json")), this.#u = r?.[at], r?.[D] !== void 0 && (this.#a = r?.[D], ot(r?.[D], this.#o) && (this.#n = this.#a.uat, this.#s = Z(this.#a.jwks)));
1272
1285
  }
1273
1286
  pendingFetch() {
1274
1287
  return !!this.#e;
@@ -1282,26 +1295,26 @@ class it {
1282
1295
  jwks() {
1283
1296
  return this.#s?.jwks();
1284
1297
  }
1285
- async getKey(t, r) {
1298
+ async getKey(e, r) {
1286
1299
  (!this.#s || !this.fresh()) && await this.reload();
1287
1300
  try {
1288
- return await this.#s(t, r);
1301
+ return await this.#s(e, r);
1289
1302
  } catch (n) {
1290
- if (n instanceof ne && this.coolingDown() === !1)
1291
- return await this.reload(), this.#s(t, r);
1303
+ if (n instanceof re && this.coolingDown() === !1)
1304
+ return await this.reload(), this.#s(e, r);
1292
1305
  throw n;
1293
1306
  }
1294
1307
  }
1295
1308
  async reload() {
1296
- this.#e && rt() && (this.#e = void 0), this.#e ||= st(this.#r.href, this.#t, AbortSignal.timeout(this.#i), this.#u).then((t) => {
1297
- this.#s = Q(t), this.#a && (this.#a.uat = Date.now(), this.#a.jwks = t), this.#n = Date.now(), this.#e = void 0;
1298
- }).catch((t) => {
1299
- throw this.#e = void 0, t;
1309
+ this.#e && st() && (this.#e = void 0), this.#e ||= it(this.#r.href, this.#t, AbortSignal.timeout(this.#i), this.#u).then((e) => {
1310
+ this.#s = Z(e), this.#a && (this.#a.uat = Date.now(), this.#a.jwks = e), this.#n = Date.now(), this.#e = void 0;
1311
+ }).catch((e) => {
1312
+ throw this.#e = void 0, e;
1300
1313
  }), await this.#e;
1301
1314
  }
1302
1315
  }
1303
- function ot(e, t) {
1304
- const r = new it(e, t), n = async (s, a) => r.getKey(s, a);
1316
+ function ut(t, e) {
1317
+ const r = new ct(t, e), n = async (s, a) => r.getKey(s, a);
1305
1318
  return Object.defineProperties(n, {
1306
1319
  coolingDown: {
1307
1320
  get: () => r.coolingDown(),
@@ -1332,21 +1345,30 @@ function ot(e, t) {
1332
1345
  }
1333
1346
  }), n;
1334
1347
  }
1335
- const Z = /* @__PURE__ */ new Map();
1336
- async function ct(e, t = `${ee}/.well-known/jwks.json`) {
1337
- let r = Z.get(t);
1338
- return r || (r = ot(new URL(t)), Z.set(t, r)), Qe(e, r);
1348
+ const ht = ut(
1349
+ new URL(`${F}/.well-known/jwks.json`)
1350
+ );
1351
+ async function ft(t) {
1352
+ try {
1353
+ const { payload: e } = await je(t, ht, {
1354
+ issuer: F,
1355
+ audience: "authenticated"
1356
+ });
1357
+ return e;
1358
+ } catch (e) {
1359
+ return console.error("Token verification failed:", e), null;
1360
+ }
1339
1361
  }
1340
- class ut {
1362
+ class dt {
1341
1363
  store = /* @__PURE__ */ new Map();
1342
- async get(t) {
1343
- return this.store.get(t) ?? null;
1364
+ async get(e) {
1365
+ return this.store.get(e) ?? null;
1344
1366
  }
1345
- async set(t, r) {
1346
- this.store.set(t, r);
1367
+ async set(e, r) {
1368
+ this.store.set(e, r);
1347
1369
  }
1348
- async remove(t) {
1349
- this.store.delete(t);
1370
+ async remove(e) {
1371
+ this.store.delete(e);
1350
1372
  }
1351
1373
  async clear() {
1352
1374
  this.store.clear();
@@ -1354,20 +1376,20 @@ class ut {
1354
1376
  }
1355
1377
  class j {
1356
1378
  prefix;
1357
- constructor(t = "genation") {
1358
- this.prefix = t;
1379
+ constructor(e = "genation") {
1380
+ this.prefix = e;
1359
1381
  }
1360
- getKey(t) {
1361
- return `${this.prefix}:${t}`;
1382
+ getKey(e) {
1383
+ return `${this.prefix}:${e}`;
1362
1384
  }
1363
- async get(t) {
1364
- return typeof window > "u" ? null : localStorage.getItem(this.getKey(t));
1385
+ async get(e) {
1386
+ return typeof window > "u" ? null : localStorage.getItem(this.getKey(e));
1365
1387
  }
1366
- async set(t, r) {
1367
- typeof window > "u" || localStorage.setItem(this.getKey(t), r);
1388
+ async set(e, r) {
1389
+ typeof window > "u" || localStorage.setItem(this.getKey(e), r);
1368
1390
  }
1369
- async remove(t) {
1370
- typeof window > "u" || localStorage.removeItem(this.getKey(t));
1391
+ async remove(e) {
1392
+ typeof window > "u" || localStorage.removeItem(this.getKey(e));
1371
1393
  }
1372
1394
  async clear() {
1373
1395
  if (typeof window > "u") return;
@@ -1376,22 +1398,22 @@ class j {
1376
1398
  ).forEach((r) => localStorage.removeItem(r));
1377
1399
  }
1378
1400
  }
1379
- class ht {
1401
+ class lt {
1380
1402
  prefix;
1381
- constructor(t = "genation") {
1382
- this.prefix = t;
1403
+ constructor(e = "genation") {
1404
+ this.prefix = e;
1383
1405
  }
1384
- getKey(t) {
1385
- return `${this.prefix}:${t}`;
1406
+ getKey(e) {
1407
+ return `${this.prefix}:${e}`;
1386
1408
  }
1387
- async get(t) {
1388
- return typeof window > "u" ? null : sessionStorage.getItem(this.getKey(t));
1409
+ async get(e) {
1410
+ return typeof window > "u" ? null : sessionStorage.getItem(this.getKey(e));
1389
1411
  }
1390
- async set(t, r) {
1391
- typeof window > "u" || sessionStorage.setItem(this.getKey(t), r);
1412
+ async set(e, r) {
1413
+ typeof window > "u" || sessionStorage.setItem(this.getKey(e), r);
1392
1414
  }
1393
- async remove(t) {
1394
- typeof window > "u" || sessionStorage.removeItem(this.getKey(t));
1415
+ async remove(e) {
1416
+ typeof window > "u" || sessionStorage.removeItem(this.getKey(e));
1395
1417
  }
1396
1418
  async clear() {
1397
1419
  if (typeof window > "u") return;
@@ -1400,57 +1422,56 @@ class ht {
1400
1422
  ).forEach((r) => sessionStorage.removeItem(r));
1401
1423
  }
1402
1424
  }
1403
- function ft(e = "localStorage") {
1404
- switch (e) {
1425
+ function pt(t = "localStorage") {
1426
+ switch (t) {
1405
1427
  case "memory":
1406
- return new ut();
1428
+ return new dt();
1407
1429
  case "localStorage":
1408
1430
  return new j();
1409
1431
  case "sessionStorage":
1410
- return new ht();
1432
+ return new lt();
1411
1433
  default:
1412
1434
  return new j();
1413
1435
  }
1414
1436
  }
1415
- function H(e) {
1416
- return Array.isArray(e) ? e.map(H) : typeof e == "object" && e !== null ? Object.fromEntries(
1417
- Object.entries(e).map(([t, r]) => [
1418
- t.replace(/_([a-z])/g, (n, s) => s.toUpperCase()),
1419
- H(r)
1437
+ function N(t) {
1438
+ return Array.isArray(t) ? t.map(N) : typeof t == "object" && t !== null ? Object.fromEntries(
1439
+ Object.entries(t).map(([e, r]) => [
1440
+ e.replace(/_([a-z])/g, (n, s) => s.toUpperCase()),
1441
+ N(r)
1420
1442
  ])
1421
- ) : e;
1443
+ ) : t;
1422
1444
  }
1423
- class dt {
1445
+ class yt {
1424
1446
  oauth;
1425
1447
  tokenManager;
1426
- jwksUrl;
1427
1448
  http;
1428
1449
  httpServer;
1429
1450
  listeners = /* @__PURE__ */ new Set();
1430
1451
  initialized = !1;
1431
- constructor(t) {
1432
- this.validateConfig(t);
1433
- const r = typeof t.storage == "object" ? t.storage : ft(t.storage), n = t.authUrl ?? "https://mnnoheowoowbtpuoguul.supabase.co/auth/v1";
1434
- this.tokenManager = new ye(r), this.oauth = new me(t, this.tokenManager), this.jwksUrl = `${n}/.well-known/jwks.json`, this.http = new D({
1452
+ constructor(e) {
1453
+ this.validateConfig(e);
1454
+ const r = typeof e.storage == "object" ? e.storage : pt(e.storage), n = e.authUrl ?? "https://mnnoheowoowbtpuoguul.supabase.co/auth/v1";
1455
+ this.tokenManager = new ye(r), this.oauth = new me(e, this.tokenManager), this.http = new U({
1435
1456
  baseUrl: n
1436
- }), this.httpServer = new D({
1457
+ }), this.httpServer = new U({
1437
1458
  baseUrl: "https://ff-api.genation.ai/api/v2/client"
1438
1459
  });
1439
1460
  }
1440
- validateConfig(t) {
1441
- if (!t.clientId) throw v.missingField("clientId");
1442
- if (!t.clientSecret)
1461
+ validateConfig(e) {
1462
+ if (!e.clientId) throw v.missingField("clientId");
1463
+ if (!e.clientSecret)
1443
1464
  throw v.missingField("clientSecret");
1444
- if (!t.redirectUri) throw v.missingField("redirectUri");
1465
+ if (!e.redirectUri) throw v.missingField("redirectUri");
1445
1466
  }
1446
1467
  /**
1447
1468
  * Emit auth state change event to all listeners
1448
1469
  */
1449
- async emitAuthStateChange(t) {
1470
+ async emitAuthStateChange(e) {
1450
1471
  const r = await this.getSession();
1451
1472
  this.listeners.forEach((n) => {
1452
1473
  try {
1453
- n(t, r);
1474
+ n(e, r);
1454
1475
  } catch (s) {
1455
1476
  console.error("Error in auth state change callback:", s);
1456
1477
  }
@@ -1491,15 +1512,15 @@ class dt {
1491
1512
  * subscription.unsubscribe();
1492
1513
  * ```
1493
1514
  */
1494
- onAuthStateChange(t) {
1495
- return this.listeners.add(t), this.initialized ? setTimeout(() => {
1515
+ onAuthStateChange(e) {
1516
+ return this.listeners.add(e), this.initialized ? setTimeout(() => {
1496
1517
  this.emitAuthStateChange("INITIAL_SESSION");
1497
1518
  }, 0) : (this.initialized = !0, setTimeout(() => {
1498
1519
  this.emitAuthStateChange("INITIAL_SESSION");
1499
1520
  }, 0)), {
1500
1521
  subscription: {
1501
1522
  unsubscribe: () => {
1502
- this.listeners.delete(t);
1523
+ this.listeners.delete(e);
1503
1524
  }
1504
1525
  }
1505
1526
  };
@@ -1545,8 +1566,8 @@ class dt {
1545
1566
  * }
1546
1567
  * ```
1547
1568
  */
1548
- async handleCallback(t) {
1549
- const r = new URL(t), n = r.searchParams.get("code"), s = r.searchParams.get("state");
1569
+ async handleCallback(e) {
1570
+ const r = new URL(e), n = r.searchParams.get("code"), s = r.searchParams.get("state");
1550
1571
  if (!n || !s)
1551
1572
  throw new Error("Missing code or state");
1552
1573
  const a = await this.oauth.exchangeCode(n, s);
@@ -1617,9 +1638,8 @@ class dt {
1617
1638
  * @returns Decoded token payload if valid
1618
1639
  * @throws Error if token is invalid
1619
1640
  */
1620
- async verifyToken(t) {
1621
- const { payload: r } = await ct(t, this.jwksUrl);
1622
- return r;
1641
+ async verifyToken(e) {
1642
+ return await ft(e);
1623
1643
  }
1624
1644
  /**
1625
1645
  * Get licenses
@@ -1628,23 +1648,23 @@ class dt {
1628
1648
  * @param options.expiresAfter - Query licenses that are expired after the given date, default set to today to get all valid licenses (unexpired licenses)
1629
1649
  * @returns The licenses
1630
1650
  */
1631
- async getLicenses(t = {}) {
1651
+ async getLicenses(e = {}) {
1632
1652
  const r = await this.getSession();
1633
1653
  if (!r)
1634
1654
  return null;
1635
- const n = r.accessToken, { expiresAfter: s = /* @__PURE__ */ new Date() } = t, a = await this.httpServer.request("/licenses", {
1655
+ const n = r.accessToken, { expiresAfter: s = /* @__PURE__ */ new Date() } = e, a = await this.httpServer.request("/licenses", {
1636
1656
  headers: { Authorization: `Bearer ${n}` },
1637
1657
  params: { expiresAfter: s.toISOString() }
1638
1658
  });
1639
- return a.ok ? H(a.data) : (console.error("GenationClient: Error fetching licenses:", a.error), null);
1659
+ return a.ok ? N(a.data) : (console.error("GenationClient: Error fetching licenses:", a.error), null);
1640
1660
  }
1641
1661
  /**
1642
1662
  * Fetch user info from auth server
1643
1663
  */
1644
- async fetchUser(t) {
1664
+ async fetchUser(e) {
1645
1665
  try {
1646
1666
  const r = await this.http.request("/oauth/userinfo", {
1647
- headers: { Authorization: `Bearer ${t}` }
1667
+ headers: { Authorization: `Bearer ${e}` }
1648
1668
  });
1649
1669
  return {
1650
1670
  sub: r.sub,
@@ -1660,20 +1680,20 @@ class dt {
1660
1680
  }
1661
1681
  }
1662
1682
  }
1663
- function lt(e) {
1664
- return new dt(e);
1683
+ function mt(t) {
1684
+ return new yt(t);
1665
1685
  }
1666
1686
  export {
1667
1687
  g as AuthError,
1668
1688
  v as ConfigError,
1669
- dt as GenationClient,
1670
- N as GenationError,
1689
+ yt as GenationClient,
1690
+ L as GenationError,
1671
1691
  j as LocalStorage,
1672
- ut as MemoryStorage,
1692
+ dt as MemoryStorage,
1673
1693
  S as NetworkError,
1674
- ht as SessionStorage,
1675
- lt as createClient,
1676
- ft as createStorage,
1677
- ct as verifyToken
1694
+ lt as SessionStorage,
1695
+ mt as createClient,
1696
+ pt as createStorage,
1697
+ ft as verifyToken
1678
1698
  };
1679
1699
  //# sourceMappingURL=genation.es.js.map