@sourceregistry/node-jwt 1.5.7 → 1.5.8

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,15 +1,15 @@
1
- import J, { verify as z, sign as X, createVerify as $, createSign as W, createHmac as P, createPrivateKey as R, createSecretKey as V, timingSafeEqual as Y, createHash as L, createPublicKey as U } from "crypto";
1
+ import _, { verify as z, sign as X, createVerify as R, createSign as L, createHmac as B, createPrivateKey as V, createSecretKey as U, timingSafeEqual as Y, createHash as j, createPublicKey as H } from "crypto";
2
2
  const x = {
3
3
  encode: (e) => Buffer.from(e).toString("base64url"),
4
4
  decode: (e) => Buffer.from(e, "base64url").toString()
5
- }, Z = (e, t) => e.length !== t.length ? !1 : Y(Buffer.from(e), Buffer.from(t)), N = /^[A-Za-z0-9_-]+$/;
6
- function _(e) {
5
+ }, Z = (e, t) => e.length !== t.length ? !1 : Y(Buffer.from(e), Buffer.from(t)), O = /^[A-Za-z0-9_-]+$/;
6
+ function $(e) {
7
7
  return typeof e == "object" && e !== null && !Array.isArray(e);
8
8
  }
9
9
  function w(e) {
10
10
  return typeof e == "number" && Number.isFinite(e);
11
11
  }
12
- function j(e) {
12
+ function N(e) {
13
13
  return Array.isArray(e) && e.every((t) => typeof t == "string");
14
14
  }
15
15
  function Q(e) {
@@ -28,7 +28,7 @@ function Q(e) {
28
28
  reason: `Invalid "${r.claim}" claim: expected ${r.expected}`,
29
29
  code: "INVALID_CLAIM"
30
30
  };
31
- return e.aud !== void 0 && !(typeof e.aud == "string" || j(e.aud)) ? {
31
+ return e.aud !== void 0 && !(typeof e.aud == "string" || N(e.aud)) ? {
32
32
  reason: 'Invalid "aud" claim: expected string or string[]',
33
33
  code: "INVALID_CLAIM"
34
34
  } : null;
@@ -64,7 +64,7 @@ function ee(e) {
64
64
  reason: "Invalid jwtId option: expected string",
65
65
  code: "INVALID_OPTIONS"
66
66
  };
67
- if (e.audience !== void 0 && !(typeof e.audience == "string" || j(e.audience)))
67
+ if (e.audience !== void 0 && !(typeof e.audience == "string" || N(e.audience)))
68
68
  return {
69
69
  reason: "Invalid audience option: expected string or string[]",
70
70
  code: "INVALID_OPTIONS"
@@ -75,7 +75,7 @@ function ee(e) {
75
75
  reason: "Invalid algorithms option: expected an array",
76
76
  code: "INVALID_OPTIONS"
77
77
  };
78
- const t = e.algorithms.find((r) => !(r in m));
78
+ const t = e.algorithms.find((r) => !(r in v));
79
79
  if (t)
80
80
  return {
81
81
  reason: `Invalid algorithms option: unsupported algorithm "${t}"`,
@@ -84,6 +84,15 @@ function ee(e) {
84
84
  }
85
85
  return null;
86
86
  }
87
+ function re(e) {
88
+ return e.crit !== void 0 ? !N(e.crit) || e.crit.length === 0 ? {
89
+ reason: 'Invalid "crit" header: expected non-empty string[]',
90
+ code: "INVALID_HEADER"
91
+ } : {
92
+ reason: `Unsupported critical header parameters: ${e.crit.join(", ")}`,
93
+ code: "UNSUPPORTED_CRITICAL_HEADER"
94
+ } : null;
95
+ }
87
96
  function C(e) {
88
97
  switch (e) {
89
98
  case "ES256":
@@ -101,83 +110,83 @@ function C(e) {
101
110
  throw new Error(`Unsupported ECDSA alg for JOSE conversion: ${e}`);
102
111
  }
103
112
  }
104
- function re(e, t) {
113
+ function te(e, t) {
105
114
  let r = 0;
106
115
  if (e[r++] !== 48) throw new Error("Invalid DER ECDSA signature");
107
116
  let n = e[r++];
108
117
  if (n & 128) {
109
- const v = n & 127;
118
+ const d = n & 127;
110
119
  n = 0;
111
- for (let g = 0; g < v; g++) n = n << 8 | e[r++];
120
+ for (let y = 0; y < d; y++) n = n << 8 | e[r++];
112
121
  }
113
122
  if (e[r++] !== 2) throw new Error("Invalid DER ECDSA signature (r)");
114
- const i = e[r++];
115
- let o = e.subarray(r, r + i);
116
- if (r += i, e[r++] !== 2) throw new Error("Invalid DER ECDSA signature (s)");
117
123
  const a = e[r++];
118
- let s = e.subarray(r, r + a);
119
- for (; o.length > t / 2 && o[0] === 0; ) o = o.subarray(1);
120
- for (; s.length > t / 2 && s[0] === 0; ) s = s.subarray(1);
121
- const l = Buffer.concat([Buffer.alloc(t / 2 - o.length, 0), o]), c = Buffer.concat([Buffer.alloc(t / 2 - s.length, 0), s]);
122
- return Buffer.concat([l, c]);
124
+ let c = e.subarray(r, r + a);
125
+ if (r += a, e[r++] !== 2) throw new Error("Invalid DER ECDSA signature (s)");
126
+ const s = e[r++];
127
+ let i = e.subarray(r, r + s);
128
+ for (; c.length > t / 2 && c[0] === 0; ) c = c.subarray(1);
129
+ for (; i.length > t / 2 && i[0] === 0; ) i = i.subarray(1);
130
+ const g = Buffer.concat([Buffer.alloc(t / 2 - c.length, 0), c]), u = Buffer.concat([Buffer.alloc(t / 2 - i.length, 0), i]);
131
+ return Buffer.concat([g, u]);
123
132
  }
124
- function B(e) {
133
+ function W(e) {
125
134
  const t = e.length / 2;
126
135
  let r = e.subarray(0, t), n = e.subarray(t);
127
136
  for (; r.length > 1 && r[0] === 0 && (r[1] & 128) === 0; ) r = r.subarray(1);
128
137
  for (; n.length > 1 && n[0] === 0 && (n[1] & 128) === 0; ) n = n.subarray(1);
129
138
  r[0] & 128 && (r = Buffer.concat([Buffer.from([0]), r])), n[0] & 128 && (n = Buffer.concat([Buffer.from([0]), n]));
130
- const i = Buffer.concat([Buffer.from([2, r.length]), r]), o = Buffer.concat([Buffer.from([2, n.length]), n]), a = i.length + o.length;
131
- let s;
132
- if (a < 128)
133
- s = Buffer.from([a]);
139
+ const a = Buffer.concat([Buffer.from([2, r.length]), r]), c = Buffer.concat([Buffer.from([2, n.length]), n]), s = a.length + c.length;
140
+ let i;
141
+ if (s < 128)
142
+ i = Buffer.from([s]);
134
143
  else {
135
- const l = [];
136
- let c = a;
137
- for (; c > 0; )
138
- l.unshift(c & 255), c >>= 8;
139
- s = Buffer.from([128 | l.length, ...l]);
144
+ const g = [];
145
+ let u = s;
146
+ for (; u > 0; )
147
+ g.unshift(u & 255), u >>= 8;
148
+ i = Buffer.from([128 | g.length, ...g]);
140
149
  }
141
- return Buffer.concat([Buffer.from([48]), s, i, o]);
150
+ return Buffer.concat([Buffer.from([48]), i, a, c]);
142
151
  }
143
- function H(e) {
152
+ function K(e) {
144
153
  return e === "ES256" || e === "ES384" || e === "ES512" || e === "ES256K";
145
154
  }
146
- const O = (e) => ({
147
- sign: (t, r) => P(e, r).update(t).digest("base64url"),
155
+ const D = (e) => ({
156
+ sign: (t, r) => B(e, r).update(t).digest("base64url"),
148
157
  verify: (t, r, n) => {
149
- const i = P(e, r).update(t).digest("base64url");
150
- return Z(i, n);
158
+ const a = B(e, r).update(t).digest("base64url");
159
+ return Z(a, n);
151
160
  }
152
- }), b = (e) => ({
153
- sign: (t, r) => W(e).update(t).end().sign(r).toString("base64url"),
161
+ }), p = (e) => ({
162
+ sign: (t, r) => L(e).update(t).end().sign(r).toString("base64url"),
154
163
  verify: (t, r, n) => {
155
164
  try {
156
- return $(e).update(t).end().verify(r, Buffer.from(n, "base64url"));
165
+ return R(e).update(t).end().verify(r, Buffer.from(n, "base64url"));
157
166
  } catch {
158
167
  return !1;
159
168
  }
160
169
  }
161
170
  }), k = (e, t) => ({
162
- sign: (r, n) => W(e).update(r).end().sign({
171
+ sign: (r, n) => L(e).update(r).end().sign({
163
172
  //@ts-ignore
164
173
  key: n,
165
- padding: J.constants.RSA_PKCS1_PSS_PADDING,
174
+ padding: _.constants.RSA_PKCS1_PSS_PADDING,
166
175
  saltLength: t
167
176
  }).toString("base64url"),
168
- verify: (r, n, i) => {
177
+ verify: (r, n, a) => {
169
178
  try {
170
- return $(e).update(r).end().verify({
179
+ return R(e).update(r).end().verify({
171
180
  //@ts-ignore
172
181
  key: n,
173
- padding: J.constants.RSA_PKCS1_PSS_PADDING,
182
+ padding: _.constants.RSA_PKCS1_PSS_PADDING,
174
183
  saltLength: t
175
- }, Buffer.from(i, "base64url"));
184
+ }, Buffer.from(a, "base64url"));
176
185
  } catch {
177
186
  return !1;
178
187
  }
179
188
  }
180
- }), te = {
189
+ }), ne = {
181
190
  sign: (e, t) => X(null, typeof e == "string" ? Buffer.from(e, "utf8") : e, t).toString("base64url"),
182
191
  verify: (e, t, r) => {
183
192
  try {
@@ -191,26 +200,26 @@ const O = (e) => ({
191
200
  return !1;
192
201
  }
193
202
  }
194
- }, m = {
203
+ }, v = {
195
204
  // HMAC
196
- HS256: O("sha256"),
197
- HS384: O("sha384"),
198
- HS512: O("sha512"),
205
+ HS256: D("sha256"),
206
+ HS384: D("sha384"),
207
+ HS512: D("sha512"),
199
208
  // RSA (DER-encoded signatures, base64url)
200
- RS256: b("RSA-SHA256"),
201
- RS384: b("RSA-SHA384"),
202
- RS512: b("RSA-SHA512"),
203
- // ECDSA (DER-encoded by default no dsaEncoding!)
204
- ES256: b("SHA256"),
205
- ES384: b("SHA384"),
206
- ES512: b("SHA512"),
207
- ES256K: b("SHA256"),
209
+ RS256: p("RSA-SHA256"),
210
+ RS384: p("RSA-SHA384"),
211
+ RS512: p("RSA-SHA512"),
212
+ // ECDSA (Node/OpenSSL emits DER; sign() converts to JOSE by default for JWTs)
213
+ ES256: p("SHA256"),
214
+ ES384: p("SHA384"),
215
+ ES512: p("SHA512"),
216
+ ES256K: p("SHA256"),
208
217
  PS256: k("RSA-SHA256", 32),
209
218
  PS384: k("RSA-SHA384", 48),
210
219
  PS512: k("RSA-SHA512", 64),
211
- EdDSA: te
212
- }, ge = Object.keys(m);
213
- function ne(e) {
220
+ EdDSA: ne
221
+ }, he = Object.keys(v);
222
+ function ie(e) {
214
223
  if (e.type === "secret") return "HS256";
215
224
  if (e.type !== "private") throw new Error("Only private or symmetric keys can be used to sign JWTs");
216
225
  const t = e.asymmetricKeyType, r = e.asymmetricKeyDetails;
@@ -218,7 +227,7 @@ function ne(e) {
218
227
  case "rsa":
219
228
  return "RS256";
220
229
  case "rsa-pss": {
221
- const n = r?.hashAlgorithm ?? "sha256";
230
+ const n = (r == null ? void 0 : r.hashAlgorithm) ?? "sha256";
222
231
  switch (n) {
223
232
  case "sha256":
224
233
  return "PS256";
@@ -231,7 +240,7 @@ function ne(e) {
231
240
  }
232
241
  }
233
242
  case "ec": {
234
- const n = r?.namedCurve;
243
+ const n = r == null ? void 0 : r.namedCurve;
235
244
  switch (n) {
236
245
  case "P-256":
237
246
  case "prime256v1":
@@ -254,139 +263,147 @@ function ne(e) {
254
263
  throw new Error(`Unsupported asymmetric key type: ${t}`);
255
264
  }
256
265
  }
257
- function ie(e) {
266
+ function ae(e) {
258
267
  if (typeof e == "object" && "type" in e) return e;
259
268
  try {
260
- return R(e);
269
+ return V(e);
261
270
  } catch {
262
271
  const t = typeof e == "string" ? Buffer.from(e, "utf8") : Buffer.isBuffer(e) ? e : (() => {
263
272
  throw new Error("Unsupported key type");
264
273
  })();
265
- return V(t);
274
+ return U(t);
266
275
  }
267
276
  }
268
277
  const M = (e) => {
269
278
  const t = e.split(".");
270
279
  if (t.length !== 3)
271
280
  throw new Error('Invalid JWT: must contain exactly 3 parts separated by "."');
272
- const [r, n, i] = t;
273
- if (!r || !n || !i)
281
+ const [r, n, a] = t;
282
+ if (!r || !n || !a)
274
283
  throw new Error("Invalid JWT: empty part detected");
275
- if (!N.test(r) || !N.test(n) || !N.test(i))
284
+ if (!O.test(r) || !O.test(n) || !O.test(a))
276
285
  throw new Error("Invalid JWT: non-base64url characters detected");
277
286
  try {
278
- const o = JSON.parse(x.decode(r)), a = JSON.parse(x.decode(n));
279
- if (!_(o) || !_(a))
287
+ const c = JSON.parse(x.decode(r)), s = JSON.parse(x.decode(n));
288
+ if (!$(c) || !$(s))
280
289
  throw new Error("header and payload must be JSON objects");
281
- const s = o, l = a;
282
- if (typeof s.alg != "string" || s.alg.length === 0)
290
+ const i = c, g = s;
291
+ if (typeof i.alg != "string" || i.alg.length === 0)
283
292
  throw new Error("header.alg must be a non-empty string");
284
- if (s.typ !== void 0 && typeof s.typ != "string")
293
+ if (i.typ !== void 0 && typeof i.typ != "string")
285
294
  throw new Error("header.typ must be a string");
286
- if (s.kid !== void 0 && typeof s.kid != "string")
295
+ if (i.kid !== void 0 && typeof i.kid != "string")
287
296
  throw new Error("header.kid must be a string");
288
- return { header: s, payload: l, signature: i };
289
- } catch (o) {
290
- throw new Error(`Invalid JWT: malformed header or payload (${o.message})`);
291
- }
292
- }, ae = (e, t, r = {}) => {
293
- const n = ie(t), i = r.alg ?? ne(n), o = r.signatureFormat ?? "der", a = r.typ ?? "JWT";
294
- if (!(i in m)) throw new Error(`Unsupported algorithm: ${i}`);
295
- const s = { alg: i, typ: a };
296
- r.kid && (s.kid = r.kid);
297
- const l = x.encode(JSON.stringify(s)), c = x.encode(JSON.stringify(e)), v = `${l}.${c}`;
298
- let g = m[i].sign(v, t);
299
- if (o === "jose" && H(i)) {
300
- const y = Buffer.from(g, "base64url");
301
- g = re(y, C(i)).toString("base64url");
297
+ if (i.crit !== void 0 && (!N(i.crit) || i.crit.length === 0))
298
+ throw new Error("header.crit must be a non-empty string[]");
299
+ return { header: i, payload: g, signature: a };
300
+ } catch (c) {
301
+ throw new Error(`Invalid JWT: malformed header or payload (${c.message})`);
302
302
  }
303
- return `${l}.${c}.${g}`;
304
303
  }, se = (e, t, r = {}) => {
304
+ const n = ae(t), a = r.alg ?? ie(n), c = r.signatureFormat ?? (K(a) ? "jose" : "der"), s = r.typ ?? "JWT";
305
+ if (!(a in v)) throw new Error(`Unsupported algorithm: ${a}`);
306
+ const i = { alg: a, typ: s };
307
+ r.kid && (i.kid = r.kid);
308
+ const g = x.encode(JSON.stringify(i)), u = x.encode(JSON.stringify(e)), d = `${g}.${u}`;
309
+ let y = v[a].sign(d, t);
310
+ if (c === "jose" && K(a)) {
311
+ const b = Buffer.from(y, "base64url");
312
+ y = te(b, C(a)).toString("base64url");
313
+ }
314
+ return `${g}.${u}.${y}`;
315
+ }, oe = (e, t, r = {}) => {
305
316
  const n = ee(r);
306
317
  if (n)
307
318
  return {
308
319
  valid: !1,
309
320
  error: n
310
321
  };
311
- let i;
322
+ let a;
312
323
  try {
313
- i = M(e);
314
- } catch (f) {
324
+ a = M(e);
325
+ } catch (o) {
315
326
  return {
316
327
  valid: !1,
317
328
  error: {
318
- reason: f.message,
329
+ reason: o.message,
319
330
  code: "INVALID_TOKEN"
320
331
  }
321
332
  };
322
333
  }
323
- const { header: o, payload: a, signature: s } = i, l = Q(a);
324
- if (l)
334
+ const { header: c, payload: s, signature: i } = a, g = re(c);
335
+ if (g)
325
336
  return {
326
337
  valid: !1,
327
- error: l
338
+ error: g
328
339
  };
329
- const c = o.alg;
330
- if (!(c in m))
340
+ const u = Q(s);
341
+ if (u)
342
+ return {
343
+ valid: !1,
344
+ error: u
345
+ };
346
+ const d = c.alg;
347
+ if (!(d in v))
331
348
  return {
332
349
  valid: !1,
333
350
  error: {
334
- reason: `Unsupported or unknown algorithm: ${o.alg}`,
351
+ reason: `Unsupported or unknown algorithm: ${c.alg}`,
335
352
  code: "INVALID_ALGORITHM"
336
353
  }
337
354
  };
338
- if (r.algorithms && r.algorithms.length > 0 && !r.algorithms.includes(c))
355
+ if (r.algorithms && r.algorithms.length > 0 && !r.algorithms.includes(d))
339
356
  return {
340
357
  valid: !1,
341
358
  error: {
342
- reason: `Algorithm "${c}" is not in the allowed algorithms list`,
359
+ reason: `Algorithm "${d}" is not in the allowed algorithms list`,
343
360
  code: "ALGORITHM_NOT_ALLOWED"
344
361
  }
345
362
  };
346
- if (o.typ !== void 0 && o.typ !== "JWT")
363
+ if (c.typ !== void 0 && c.typ !== "JWT")
347
364
  return {
348
365
  valid: !1,
349
366
  error: {
350
- reason: `Invalid token type: expected 'JWT', got '${o.typ}'`,
367
+ reason: `Invalid token type: expected 'JWT', got '${c.typ}'`,
351
368
  code: "INVALID_TYPE"
352
369
  }
353
370
  };
354
- const [v, g] = e.split("."), y = `${v}.${g}`;
355
- if (H(c)) {
356
- const f = r.signatureFormat;
357
- let u;
358
- if (f === "jose")
371
+ const [y, b] = e.split("."), S = `${y}.${b}`;
372
+ if (K(d)) {
373
+ const o = r.signatureFormat;
374
+ let f;
375
+ if (o === "jose")
359
376
  try {
360
- const d = Buffer.from(s, "base64url"), h = B(d).toString("base64url");
361
- u = m[c].verify(y, t, h);
377
+ const h = Buffer.from(i, "base64url"), m = W(h).toString("base64url");
378
+ f = v[d].verify(S, t, m);
362
379
  } catch {
363
- u = !1;
380
+ f = !1;
364
381
  }
365
- else if (f === "der")
366
- u = m[c].verify(y, t, s);
367
- else if (u = m[c].verify(y, t, s), !u)
382
+ else if (o === "der")
383
+ f = v[d].verify(S, t, i);
384
+ else if (f = v[d].verify(S, t, i), !f)
368
385
  try {
369
- const d = Buffer.from(s, "base64url");
370
- if (d.length === C(c)) {
371
- const h = B(d).toString("base64url");
372
- u = m[c].verify(y, t, h);
386
+ const h = Buffer.from(i, "base64url");
387
+ if (h.length === C(d)) {
388
+ const m = W(h).toString("base64url");
389
+ f = v[d].verify(S, t, m);
373
390
  }
374
391
  } catch {
375
392
  }
376
- if (!u)
393
+ if (!f)
377
394
  return { valid: !1, error: { reason: "Signature verification failed", code: "INVALID_SIGNATURE" } };
378
395
  } else {
379
- let f = !1;
396
+ let o = !1;
380
397
  try {
381
- f = m[c].verify(y, t, s);
398
+ o = v[d].verify(S, t, i);
382
399
  } catch {
383
- f = !1;
400
+ o = !1;
384
401
  }
385
- if (!f)
402
+ if (!o)
386
403
  return { valid: !1, error: { reason: "Signature verification failed", code: "INVALID_SIGNATURE" } };
387
404
  }
388
- const S = Math.floor(Date.now() / 1e3), I = r.clockSkew ?? 0;
389
- if (!r.ignoreExpiration && a.exp !== void 0 && S > a.exp + I)
405
+ const I = Math.floor(Date.now() / 1e3), l = r.clockSkew ?? 0;
406
+ if (!r.ignoreExpiration && s.exp !== void 0 && I > s.exp + l)
390
407
  return {
391
408
  valid: !1,
392
409
  error: {
@@ -394,7 +411,7 @@ const M = (e) => {
394
411
  code: "TOKEN_EXPIRED"
395
412
  }
396
413
  };
397
- if (a.nbf !== void 0 && S + I < a.nbf)
414
+ if (s.nbf !== void 0 && I + l < s.nbf)
398
415
  return {
399
416
  valid: !1,
400
417
  error: {
@@ -402,7 +419,7 @@ const M = (e) => {
402
419
  code: "TOKEN_NOT_ACTIVE"
403
420
  }
404
421
  };
405
- if (a.iat !== void 0 && S + I < a.iat)
422
+ if (s.iat !== void 0 && I + l < s.iat)
406
423
  return {
407
424
  valid: !1,
408
425
  error: {
@@ -410,19 +427,19 @@ const M = (e) => {
410
427
  code: "TOKEN_FUTURE_ISSUED"
411
428
  }
412
429
  };
413
- if (r.maxTokenAge !== void 0 && a.iat !== void 0) {
414
- const f = S - a.iat;
415
- if (f > r.maxTokenAge)
430
+ if (r.maxTokenAge !== void 0 && s.iat !== void 0) {
431
+ const o = I - s.iat;
432
+ if (o > r.maxTokenAge)
416
433
  return {
417
434
  valid: !1,
418
435
  error: {
419
- reason: `Token age (${f}s) exceeds maximum allowed age (${r.maxTokenAge}s)`,
436
+ reason: `Token age (${o}s) exceeds maximum allowed age (${r.maxTokenAge}s)`,
420
437
  code: "TOKEN_TOO_OLD"
421
438
  }
422
439
  };
423
440
  }
424
441
  if (r.issuer !== void 0) {
425
- if (a.iss === void 0)
442
+ if (s.iss === void 0)
426
443
  return {
427
444
  valid: !1,
428
445
  error: {
@@ -430,17 +447,17 @@ const M = (e) => {
430
447
  code: "MISSING_ISSUER"
431
448
  }
432
449
  };
433
- if (r.issuer !== a.iss)
450
+ if (r.issuer !== s.iss)
434
451
  return {
435
452
  valid: !1,
436
453
  error: {
437
- reason: `Invalid token issuer: expected "${r.issuer}", got "${a.iss}"`,
454
+ reason: `Invalid token issuer: expected "${r.issuer}", got "${s.iss}"`,
438
455
  code: "INVALID_ISSUER"
439
456
  }
440
457
  };
441
458
  }
442
459
  if (r.subject !== void 0) {
443
- if (a.sub === void 0)
460
+ if (s.sub === void 0)
444
461
  return {
445
462
  valid: !1,
446
463
  error: {
@@ -448,18 +465,18 @@ const M = (e) => {
448
465
  code: "MISSING_SUBJECT"
449
466
  }
450
467
  };
451
- if (r.subject !== a.sub)
468
+ if (r.subject !== s.sub)
452
469
  return {
453
470
  valid: !1,
454
471
  error: {
455
- reason: `Invalid token subject: expected "${r.subject}", got "${a.sub}"`,
472
+ reason: `Invalid token subject: expected "${r.subject}", got "${s.sub}"`,
456
473
  code: "INVALID_SUBJECT"
457
474
  }
458
475
  };
459
476
  }
460
477
  if (r.audience !== void 0) {
461
- const f = a.aud;
462
- if (f === void 0)
478
+ const o = s.aud;
479
+ if (o === void 0)
463
480
  return {
464
481
  valid: !1,
465
482
  error: {
@@ -467,8 +484,8 @@ const M = (e) => {
467
484
  code: "MISSING_AUDIENCE"
468
485
  }
469
486
  };
470
- const u = Array.isArray(r.audience) ? r.audience : [r.audience], d = Array.isArray(f) ? f : [f];
471
- if (!u.some((A) => d.includes(A)))
487
+ const f = Array.isArray(r.audience) ? r.audience : [r.audience], h = Array.isArray(o) ? o : [o];
488
+ if (!f.some((A) => h.includes(A)))
472
489
  return {
473
490
  valid: !1,
474
491
  error: {
@@ -478,7 +495,7 @@ const M = (e) => {
478
495
  };
479
496
  }
480
497
  if (r.jwtId !== void 0) {
481
- if (a.jti === void 0)
498
+ if (s.jti === void 0)
482
499
  return {
483
500
  valid: !1,
484
501
  error: {
@@ -486,46 +503,46 @@ const M = (e) => {
486
503
  code: "MISSING_JTI"
487
504
  }
488
505
  };
489
- if (r.jwtId !== a.jti)
506
+ if (r.jwtId !== s.jti)
490
507
  return {
491
508
  valid: !1,
492
509
  error: {
493
- reason: `Invalid JWT ID: expected "${r.jwtId}", got "${a.jti}"`,
510
+ reason: `Invalid JWT ID: expected "${r.jwtId}", got "${s.jti}"`,
494
511
  code: "INVALID_JTI"
495
512
  }
496
513
  };
497
514
  }
498
- return { valid: !0, header: o, payload: a, signature: s };
499
- }, he = {
500
- sign: ae,
501
- verify: se,
515
+ return { valid: !0, header: c, payload: s, signature: i };
516
+ }, ye = {
517
+ sign: se,
518
+ verify: oe,
502
519
  decode: M,
503
- algorithms: m
520
+ algorithms: v
504
521
  };
505
- function oe(e) {
522
+ function ce(e) {
506
523
  if (!e || typeof e != "object") throw new Error("Invalid KeyObject");
507
524
  return e.export({ format: "jwk" });
508
525
  }
509
- function D(e) {
526
+ function J(e) {
510
527
  if (!e || typeof e != "object") throw new Error("Invalid JWK");
511
528
  switch (e.kty) {
512
529
  case "oct": {
513
530
  if (!("k" in e) || typeof e.k != "string")
514
531
  throw new Error('Invalid oct JWK: missing "k"');
515
- return V(Buffer.from(e.k, "base64url"));
532
+ return U(Buffer.from(e.k, "base64url"));
516
533
  }
517
534
  case "RSA":
518
535
  case "EC":
519
536
  case "OKP":
520
- return "d" in e && typeof e.d == "string" ? R({ format: "jwk", key: e }) : U({ format: "jwk", key: e });
537
+ return "d" in e && typeof e.d == "string" ? V({ format: "jwk", key: e }) : H({ format: "jwk", key: e });
521
538
  default:
522
539
  throw new Error(`Unsupported JWK key type: ${e.kty}`);
523
540
  }
524
541
  }
525
- function ce(e) {
542
+ function fe(e) {
526
543
  if (!e || typeof e != "object")
527
544
  throw new Error("Invalid KeyObject");
528
- const r = (e.type === "private" ? U(e) : e).export({ format: "jwk" });
545
+ const r = (e.type === "private" ? H(e) : e).export({ format: "jwk" });
529
546
  return delete r.d, delete r.p, delete r.q, delete r.dp, delete r.dq, delete r.qi, r;
530
547
  }
531
548
  function F(e, t = "sha256") {
@@ -549,27 +566,28 @@ function F(e, t = "sha256") {
549
566
  throw new Error(`Unsupported JWK key type: ${e.kty}`);
550
567
  }
551
568
  const n = JSON.stringify(
552
- Object.keys(r).sort().reduce((i, o) => (i[o] = r[o], i), {})
569
+ Object.keys(r).sort().reduce((a, c) => (a[c] = r[c], a), {})
553
570
  );
554
- return L(t).update(n).digest("base64url");
571
+ return j(t).update(n).digest("base64url");
555
572
  }
556
- function fe(e) {
557
- if (e.x5c?.length)
558
- return L("sha1").update(Buffer.from(e.x5c[0], "base64")).digest("base64url");
573
+ function ue(e) {
574
+ var t;
575
+ if ((t = e.x5c) != null && t.length)
576
+ return j("sha1").update(Buffer.from(e.x5c[0], "base64")).digest("base64url");
559
577
  }
560
- const ye = {
561
- export: oe,
562
- import: D,
563
- toPublic: ce,
578
+ const me = {
579
+ export: ce,
580
+ import: J,
581
+ toPublic: fe,
564
582
  thumbprint: F
565
583
  };
566
- function ue(e, t) {
584
+ function de(e, t) {
567
585
  if (!e || !Array.isArray(e.keys)) throw new Error("Invalid JWKS");
568
586
  let r;
569
587
  if (t && (r = e.keys.find((n) => n.kid === t)), !r && e.keys.length === 1 && (r = e.keys[0]), !r) throw new Error("Key not found in JWKS");
570
- return D(r);
588
+ return J(r);
571
589
  }
572
- function K(e) {
590
+ function P(e) {
573
591
  return {
574
592
  keys: E(...e.keys)
575
593
  };
@@ -579,134 +597,134 @@ function E(...e) {
579
597
  const r = {
580
598
  ...t,
581
599
  kid: t.kid ?? F(t),
582
- x5t: t.x5t ?? fe(t)
600
+ x5t: t.x5t ?? ue(t)
583
601
  };
584
602
  return Object.defineProperty(r, "toKeyObject", {
585
603
  enumerable: !1,
586
604
  configurable: !1,
587
605
  writable: !1,
588
- value: () => D(r)
606
+ value: () => J(r)
589
607
  }), r;
590
608
  });
591
609
  }
592
- const de = async (e, t = {}) => {
593
- const r = typeof e == "string" ? e : e.toString(), n = t.fetch ?? globalThis.fetch, i = Math.max(0, t.ttl ?? 5 * 6e4), o = Math.max(0, t.timeoutMs ?? 5e3), a = "/.well-known/jwks.json";
610
+ const le = async (e, t = {}) => {
611
+ const r = typeof e == "string" ? e : e.toString(), n = t.fetch ?? globalThis.fetch, a = Math.max(0, t.ttl ?? 5 * 6e4), c = Math.max(0, t.timeoutMs ?? 5e3), s = "/.well-known/jwks.json";
594
612
  if (!n)
595
613
  throw new Error("No fetch implementation available");
596
- const s = (() => {
614
+ const i = (() => {
597
615
  if (t.endpointOverride) {
598
- const f = t.endpointOverride;
616
+ const l = t.endpointOverride;
599
617
  try {
600
- return new URL(f, r).toString();
618
+ return new URL(l, r).toString();
601
619
  } catch {
602
- return f;
620
+ return l;
603
621
  }
604
622
  }
605
- return t.overrideEndpointCheck || r.endsWith(a) ? r : `${r.replace(/\/+$/, "")}${a}`;
606
- })(), l = (() => {
623
+ return t.overrideEndpointCheck || r.endsWith(s) ? r : `${r.replace(/\/+$/, "")}${s}`;
624
+ })(), g = (() => {
607
625
  if (t.cache) return t.cache;
608
- let f;
626
+ let l;
609
627
  return {
610
- get: () => f,
611
- set: (u, d) => {
612
- f = d;
628
+ get: () => l,
629
+ set: (o, f) => {
630
+ l = f;
613
631
  }
614
632
  };
615
633
  })();
616
- let c, v = 0, g, y = 0;
617
- const S = async (f) => {
618
- if (g) return g;
619
- g = (async () => {
620
- const u = new AbortController();
621
- let d;
622
- o > 0 && (d = setTimeout(() => u.abort(), o));
634
+ let u, d = 0, y, b = 0;
635
+ const S = async (l) => {
636
+ if (y) return y;
637
+ y = (async () => {
638
+ const o = new AbortController();
639
+ let f;
640
+ c > 0 && (f = setTimeout(() => o.abort(), c));
623
641
  let h;
624
642
  try {
625
- h = await n(s, { signal: u.signal });
626
- } catch (p) {
627
- throw u.signal.aborted ? new Error(`JWKS fetch timed out after ${o}ms`) : p;
643
+ h = await n(i, { signal: o.signal });
644
+ } catch (A) {
645
+ throw o.signal.aborted ? new Error(`JWKS fetch timed out after ${c}ms`) : A;
628
646
  } finally {
629
- d && clearTimeout(d);
647
+ f && clearTimeout(f);
630
648
  }
631
649
  if (!h.ok)
632
650
  throw new Error(`Failed to fetch JWKS: ${h.status} ${h.statusText}`);
633
- const A = await h.json();
634
- if (!A || typeof A != "object" || !Array.isArray(A.keys))
651
+ const m = await h.json();
652
+ if (!m || typeof m != "object" || !Array.isArray(m.keys))
635
653
  throw new Error("Invalid JWKS");
636
- return K(A);
654
+ return P(m);
637
655
  })();
638
656
  try {
639
- const u = await g;
640
- return c = u, await l.set(s, u), y = 0, i > 0 && (v = Date.now() + i), u;
641
- } catch (u) {
642
- if (!f || !c)
643
- throw u;
644
- if (y += 1, i > 0) {
645
- const d = Math.min(
646
- Math.max(i, 3e4) * Math.pow(2, y - 1),
657
+ const o = await y;
658
+ return u = o, await g.set(i, o), b = 0, a > 0 && (d = Date.now() + a), o;
659
+ } catch (o) {
660
+ if (!l || !u)
661
+ throw o;
662
+ if (b += 1, a > 0) {
663
+ const f = Math.min(
664
+ Math.max(a, 3e4) * Math.pow(2, b - 1),
647
665
  9e5
648
666
  );
649
- v = Date.now() + d;
667
+ d = Date.now() + f;
650
668
  }
651
- return console.warn(`JWKS refresh failed for "${s}", using stale cache.`, u), c;
669
+ return console.warn(`JWKS refresh failed for "${i}", using stale cache.`, o), u;
652
670
  } finally {
653
- g = void 0;
671
+ y = void 0;
654
672
  }
655
673
  };
656
- c = await l.get(s), c ? (c = K(c), i > 0 && (v = Date.now() + i)) : c = await S(!1);
674
+ u = await g.get(i), u ? (u = P(u), a > 0 && (d = Date.now() + a)) : u = await S(!1);
657
675
  const I = {
658
676
  async list() {
659
- return i > 0 && Date.now() >= v && await S(!0), E(...c.keys);
677
+ return a > 0 && Date.now() >= d && await S(!0), E(...u.keys);
660
678
  },
661
679
  async refresh() {
662
680
  return await S(!1), I;
663
681
  },
664
- async key(f) {
665
- const d = (await I.list()).find((h) => h.kid === f);
666
- if (d)
667
- return E(d)[0];
682
+ async key(l) {
683
+ const f = (await I.list()).find((h) => h.kid === l);
684
+ if (f)
685
+ return E(f)[0];
668
686
  },
669
- async find(f) {
670
- const u = await I.list(), d = Object.entries(f);
671
- return d.length === 0 ? E(...u) : E(...u.filter(
672
- (h) => d.every(([A, p]) => {
673
- const T = h[A];
674
- return Array.isArray(p) ? Array.isArray(T) && T.length === p.length && T.every((G, q) => G === p[q]) : T === p;
687
+ async find(l) {
688
+ const o = await I.list(), f = Object.entries(l);
689
+ return f.length === 0 ? E(...o) : E(...o.filter(
690
+ (h) => f.every(([m, A]) => {
691
+ const T = h[m];
692
+ return Array.isArray(A) ? Array.isArray(T) && T.length === A.length && T.every((G, q) => G === A[q]) : T === A;
675
693
  })
676
694
  ));
677
695
  },
678
- async findFirst(f) {
679
- return I.find(f).then(([u]) => u);
696
+ async findFirst(l) {
697
+ return I.find(l).then(([o]) => o);
680
698
  },
681
699
  export() {
682
- return c;
700
+ return u;
683
701
  }
684
702
  };
685
703
  return I;
686
- }, me = {
687
- toKeyObject: ue,
688
- normalize: K,
689
- fromWeb: de
704
+ }, ve = {
705
+ toKeyObject: de,
706
+ normalize: P,
707
+ fromWeb: le
690
708
  };
691
709
  export {
692
- ne as A,
693
- ye as J,
694
- m as S,
695
- me as a,
696
- ue as b,
697
- he as c,
698
- ge as d,
710
+ ie as A,
711
+ me as J,
712
+ v as S,
713
+ ve as a,
714
+ de as b,
715
+ ye as c,
716
+ he as d,
699
717
  x as e,
700
- fe as f,
718
+ ue as f,
701
719
  M as g,
702
- oe as h,
703
- de as i,
720
+ ce as h,
721
+ le as i,
704
722
  F as j,
705
- D as k,
706
- K as l,
723
+ J as k,
724
+ P as l,
707
725
  E as n,
708
- ae as s,
709
- ce as t,
710
- se as v
726
+ se as s,
727
+ fe as t,
728
+ oe as v
711
729
  };
712
- //# sourceMappingURL=index-CRVBqFI5.js.map
730
+ //# sourceMappingURL=index-Cp1eA1Xq.js.map