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