@sourceregistry/node-jwt 1.5.10 → 1.5.11

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