@sourceregistry/node-jwt 1.5.7 → 1.5.9
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 +15 -7
- package/dist/index-CaEvPIqc.cjs +2 -0
- package/dist/index-CaEvPIqc.cjs.map +1 -0
- package/dist/{index-CRVBqFI5.js → index-Cp1eA1Xq.js} +258 -240
- package/dist/index-Cp1eA1Xq.js.map +1 -0
- package/dist/{index.cjs.js → index.cjs} +2 -2
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/{index.es.js → index.js} +2 -2
- package/dist/index.js.map +1 -0
- package/dist/jwks/promises.d.ts +1 -1
- package/dist/jwt/index.d.ts +3 -1
- package/dist/jwt/promises.d.ts +4 -4
- package/dist/promises.cjs +2 -0
- package/dist/promises.cjs.map +1 -0
- package/dist/promises.d.ts +2 -2
- package/dist/{promises.es.js → promises.js} +7 -7
- package/dist/promises.js.map +1 -0
- package/package.json +8 -8
- package/dist/index-CRVBqFI5.js.map +0 -1
- package/dist/index-D70ysYoo.cjs +0 -2
- package/dist/index-D70ysYoo.cjs.map +0 -1
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.es.js.map +0 -1
- package/dist/promises.cjs.js +0 -2
- package/dist/promises.cjs.js.map +0 -1
- package/dist/promises.es.js.map +0 -1
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import
|
|
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)),
|
|
6
|
-
function
|
|
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
|
|
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" ||
|
|
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" ||
|
|
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
|
|
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
|
|
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
|
|
118
|
+
const d = n & 127;
|
|
110
119
|
n = 0;
|
|
111
|
-
for (let
|
|
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
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
|
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
|
|
131
|
-
let
|
|
132
|
-
if (
|
|
133
|
-
|
|
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
|
|
136
|
-
let
|
|
137
|
-
for (;
|
|
138
|
-
|
|
139
|
-
|
|
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]),
|
|
150
|
+
return Buffer.concat([Buffer.from([48]), i, a, c]);
|
|
142
151
|
}
|
|
143
|
-
function
|
|
152
|
+
function K(e) {
|
|
144
153
|
return e === "ES256" || e === "ES384" || e === "ES512" || e === "ES256K";
|
|
145
154
|
}
|
|
146
|
-
const
|
|
147
|
-
sign: (t, r) =>
|
|
155
|
+
const D = (e) => ({
|
|
156
|
+
sign: (t, r) => B(e, r).update(t).digest("base64url"),
|
|
148
157
|
verify: (t, r, n) => {
|
|
149
|
-
const
|
|
150
|
-
return Z(
|
|
158
|
+
const a = B(e, r).update(t).digest("base64url");
|
|
159
|
+
return Z(a, n);
|
|
151
160
|
}
|
|
152
|
-
}),
|
|
153
|
-
sign: (t, r) =>
|
|
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
|
|
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) =>
|
|
171
|
+
sign: (r, n) => L(e).update(r).end().sign({
|
|
163
172
|
//@ts-ignore
|
|
164
173
|
key: n,
|
|
165
|
-
padding:
|
|
174
|
+
padding: _.constants.RSA_PKCS1_PSS_PADDING,
|
|
166
175
|
saltLength: t
|
|
167
176
|
}).toString("base64url"),
|
|
168
|
-
verify: (r, n,
|
|
177
|
+
verify: (r, n, a) => {
|
|
169
178
|
try {
|
|
170
|
-
return
|
|
179
|
+
return R(e).update(r).end().verify({
|
|
171
180
|
//@ts-ignore
|
|
172
181
|
key: n,
|
|
173
|
-
padding:
|
|
182
|
+
padding: _.constants.RSA_PKCS1_PSS_PADDING,
|
|
174
183
|
saltLength: t
|
|
175
|
-
}, Buffer.from(
|
|
184
|
+
}, Buffer.from(a, "base64url"));
|
|
176
185
|
} catch {
|
|
177
186
|
return !1;
|
|
178
187
|
}
|
|
179
188
|
}
|
|
180
|
-
}),
|
|
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
|
-
},
|
|
203
|
+
}, v = {
|
|
195
204
|
// HMAC
|
|
196
|
-
HS256:
|
|
197
|
-
HS384:
|
|
198
|
-
HS512:
|
|
205
|
+
HS256: D("sha256"),
|
|
206
|
+
HS384: D("sha384"),
|
|
207
|
+
HS512: D("sha512"),
|
|
199
208
|
// RSA (DER-encoded signatures, base64url)
|
|
200
|
-
RS256:
|
|
201
|
-
RS384:
|
|
202
|
-
RS512:
|
|
203
|
-
// ECDSA (DER
|
|
204
|
-
ES256:
|
|
205
|
-
ES384:
|
|
206
|
-
ES512:
|
|
207
|
-
ES256K:
|
|
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:
|
|
212
|
-
},
|
|
213
|
-
function
|
|
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
|
|
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
|
|
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
|
|
266
|
+
function ae(e) {
|
|
258
267
|
if (typeof e == "object" && "type" in e) return e;
|
|
259
268
|
try {
|
|
260
|
-
return
|
|
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
|
|
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,
|
|
273
|
-
if (!r || !n || !
|
|
281
|
+
const [r, n, a] = t;
|
|
282
|
+
if (!r || !n || !a)
|
|
274
283
|
throw new Error("Invalid JWT: empty part detected");
|
|
275
|
-
if (!
|
|
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
|
|
279
|
-
if (
|
|
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
|
|
282
|
-
if (typeof
|
|
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 (
|
|
293
|
+
if (i.typ !== void 0 && typeof i.typ != "string")
|
|
285
294
|
throw new Error("header.typ must be a string");
|
|
286
|
-
if (
|
|
295
|
+
if (i.kid !== void 0 && typeof i.kid != "string")
|
|
287
296
|
throw new Error("header.kid must be a string");
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
}
|
|
292
|
-
|
|
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
|
|
322
|
+
let a;
|
|
312
323
|
try {
|
|
313
|
-
|
|
314
|
-
} catch (
|
|
324
|
+
a = M(e);
|
|
325
|
+
} catch (o) {
|
|
315
326
|
return {
|
|
316
327
|
valid: !1,
|
|
317
328
|
error: {
|
|
318
|
-
reason:
|
|
329
|
+
reason: o.message,
|
|
319
330
|
code: "INVALID_TOKEN"
|
|
320
331
|
}
|
|
321
332
|
};
|
|
322
333
|
}
|
|
323
|
-
const { header:
|
|
324
|
-
if (
|
|
334
|
+
const { header: c, payload: s, signature: i } = a, g = re(c);
|
|
335
|
+
if (g)
|
|
325
336
|
return {
|
|
326
337
|
valid: !1,
|
|
327
|
-
error:
|
|
338
|
+
error: g
|
|
328
339
|
};
|
|
329
|
-
const
|
|
330
|
-
if (
|
|
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: ${
|
|
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(
|
|
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 "${
|
|
359
|
+
reason: `Algorithm "${d}" is not in the allowed algorithms list`,
|
|
343
360
|
code: "ALGORITHM_NOT_ALLOWED"
|
|
344
361
|
}
|
|
345
362
|
};
|
|
346
|
-
if (
|
|
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 '${
|
|
367
|
+
reason: `Invalid token type: expected 'JWT', got '${c.typ}'`,
|
|
351
368
|
code: "INVALID_TYPE"
|
|
352
369
|
}
|
|
353
370
|
};
|
|
354
|
-
const [
|
|
355
|
-
if (
|
|
356
|
-
const
|
|
357
|
-
let
|
|
358
|
-
if (
|
|
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
|
|
361
|
-
|
|
377
|
+
const h = Buffer.from(i, "base64url"), m = W(h).toString("base64url");
|
|
378
|
+
f = v[d].verify(S, t, m);
|
|
362
379
|
} catch {
|
|
363
|
-
|
|
380
|
+
f = !1;
|
|
364
381
|
}
|
|
365
|
-
else if (
|
|
366
|
-
|
|
367
|
-
else if (
|
|
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
|
|
370
|
-
if (
|
|
371
|
-
const
|
|
372
|
-
|
|
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 (!
|
|
393
|
+
if (!f)
|
|
377
394
|
return { valid: !1, error: { reason: "Signature verification failed", code: "INVALID_SIGNATURE" } };
|
|
378
395
|
} else {
|
|
379
|
-
let
|
|
396
|
+
let o = !1;
|
|
380
397
|
try {
|
|
381
|
-
|
|
398
|
+
o = v[d].verify(S, t, i);
|
|
382
399
|
} catch {
|
|
383
|
-
|
|
400
|
+
o = !1;
|
|
384
401
|
}
|
|
385
|
-
if (!
|
|
402
|
+
if (!o)
|
|
386
403
|
return { valid: !1, error: { reason: "Signature verification failed", code: "INVALID_SIGNATURE" } };
|
|
387
404
|
}
|
|
388
|
-
const
|
|
389
|
-
if (!r.ignoreExpiration &&
|
|
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 (
|
|
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 (
|
|
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 &&
|
|
414
|
-
const
|
|
415
|
-
if (
|
|
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 (${
|
|
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 (
|
|
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 !==
|
|
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 "${
|
|
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 (
|
|
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 !==
|
|
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 "${
|
|
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
|
|
462
|
-
if (
|
|
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
|
|
471
|
-
if (!
|
|
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 (
|
|
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 !==
|
|
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 "${
|
|
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:
|
|
499
|
-
},
|
|
500
|
-
sign:
|
|
501
|
-
verify:
|
|
515
|
+
return { valid: !0, header: c, payload: s, signature: i };
|
|
516
|
+
}, ye = {
|
|
517
|
+
sign: se,
|
|
518
|
+
verify: oe,
|
|
502
519
|
decode: M,
|
|
503
|
-
algorithms:
|
|
520
|
+
algorithms: v
|
|
504
521
|
};
|
|
505
|
-
function
|
|
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
|
|
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
|
|
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" ?
|
|
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
|
|
542
|
+
function fe(e) {
|
|
526
543
|
if (!e || typeof e != "object")
|
|
527
544
|
throw new Error("Invalid KeyObject");
|
|
528
|
-
const r = (e.type === "private" ?
|
|
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((
|
|
569
|
+
Object.keys(r).sort().reduce((a, c) => (a[c] = r[c], a), {})
|
|
553
570
|
);
|
|
554
|
-
return
|
|
571
|
+
return j(t).update(n).digest("base64url");
|
|
555
572
|
}
|
|
556
|
-
function
|
|
557
|
-
|
|
558
|
-
|
|
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
|
|
561
|
-
export:
|
|
562
|
-
import:
|
|
563
|
-
toPublic:
|
|
578
|
+
const me = {
|
|
579
|
+
export: ce,
|
|
580
|
+
import: J,
|
|
581
|
+
toPublic: fe,
|
|
564
582
|
thumbprint: F
|
|
565
583
|
};
|
|
566
|
-
function
|
|
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
|
|
588
|
+
return J(r);
|
|
571
589
|
}
|
|
572
|
-
function
|
|
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 ??
|
|
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: () =>
|
|
606
|
+
value: () => J(r)
|
|
589
607
|
}), r;
|
|
590
608
|
});
|
|
591
609
|
}
|
|
592
|
-
const
|
|
593
|
-
const r = typeof e == "string" ? e : e.toString(), n = t.fetch ?? globalThis.fetch,
|
|
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
|
|
614
|
+
const i = (() => {
|
|
597
615
|
if (t.endpointOverride) {
|
|
598
|
-
const
|
|
616
|
+
const l = t.endpointOverride;
|
|
599
617
|
try {
|
|
600
|
-
return new URL(
|
|
618
|
+
return new URL(l, r).toString();
|
|
601
619
|
} catch {
|
|
602
|
-
return
|
|
620
|
+
return l;
|
|
603
621
|
}
|
|
604
622
|
}
|
|
605
|
-
return t.overrideEndpointCheck || r.endsWith(
|
|
606
|
-
})(),
|
|
623
|
+
return t.overrideEndpointCheck || r.endsWith(s) ? r : `${r.replace(/\/+$/, "")}${s}`;
|
|
624
|
+
})(), g = (() => {
|
|
607
625
|
if (t.cache) return t.cache;
|
|
608
|
-
let
|
|
626
|
+
let l;
|
|
609
627
|
return {
|
|
610
|
-
get: () =>
|
|
611
|
-
set: (
|
|
612
|
-
|
|
628
|
+
get: () => l,
|
|
629
|
+
set: (o, f) => {
|
|
630
|
+
l = f;
|
|
613
631
|
}
|
|
614
632
|
};
|
|
615
633
|
})();
|
|
616
|
-
let
|
|
617
|
-
const S = async (
|
|
618
|
-
if (
|
|
619
|
-
|
|
620
|
-
const
|
|
621
|
-
let
|
|
622
|
-
|
|
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(
|
|
626
|
-
} catch (
|
|
627
|
-
throw
|
|
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
|
-
|
|
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
|
|
634
|
-
if (!
|
|
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
|
|
654
|
+
return P(m);
|
|
637
655
|
})();
|
|
638
656
|
try {
|
|
639
|
-
const
|
|
640
|
-
return
|
|
641
|
-
} catch (
|
|
642
|
-
if (!
|
|
643
|
-
throw
|
|
644
|
-
if (
|
|
645
|
-
const
|
|
646
|
-
Math.max(
|
|
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
|
-
|
|
667
|
+
d = Date.now() + f;
|
|
650
668
|
}
|
|
651
|
-
return console.warn(`JWKS refresh failed for "${
|
|
669
|
+
return console.warn(`JWKS refresh failed for "${i}", using stale cache.`, o), u;
|
|
652
670
|
} finally {
|
|
653
|
-
|
|
671
|
+
y = void 0;
|
|
654
672
|
}
|
|
655
673
|
};
|
|
656
|
-
|
|
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
|
|
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(
|
|
665
|
-
const
|
|
666
|
-
if (
|
|
667
|
-
return E(
|
|
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(
|
|
670
|
-
const
|
|
671
|
-
return
|
|
672
|
-
(h) =>
|
|
673
|
-
const T = h[
|
|
674
|
-
return Array.isArray(
|
|
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(
|
|
679
|
-
return I.find(
|
|
696
|
+
async findFirst(l) {
|
|
697
|
+
return I.find(l).then(([o]) => o);
|
|
680
698
|
},
|
|
681
699
|
export() {
|
|
682
|
-
return
|
|
700
|
+
return u;
|
|
683
701
|
}
|
|
684
702
|
};
|
|
685
703
|
return I;
|
|
686
|
-
},
|
|
687
|
-
toKeyObject:
|
|
688
|
-
normalize:
|
|
689
|
-
fromWeb:
|
|
704
|
+
}, ve = {
|
|
705
|
+
toKeyObject: de,
|
|
706
|
+
normalize: P,
|
|
707
|
+
fromWeb: le
|
|
690
708
|
};
|
|
691
709
|
export {
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
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
|
-
|
|
718
|
+
ue as f,
|
|
701
719
|
M as g,
|
|
702
|
-
|
|
703
|
-
|
|
720
|
+
ce as h,
|
|
721
|
+
le as i,
|
|
704
722
|
F as j,
|
|
705
|
-
|
|
706
|
-
|
|
723
|
+
J as k,
|
|
724
|
+
P as l,
|
|
707
725
|
E as n,
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
726
|
+
se as s,
|
|
727
|
+
fe as t,
|
|
728
|
+
oe as v
|
|
711
729
|
};
|
|
712
|
-
//# sourceMappingURL=index-
|
|
730
|
+
//# sourceMappingURL=index-Cp1eA1Xq.js.map
|