@sourceregistry/node-jwt 1.5.11 → 1.6.0
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 +19 -0
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +753 -22
- package/dist/index.js.map +1 -1
- package/dist/jwt/index.d.ts +5 -3
- package/dist/jwt/promises.d.ts +28 -28
- package/dist/promises.cjs +2 -2
- package/dist/promises.cjs.map +1 -1
- package/dist/promises.js +30 -43
- package/dist/promises.js.map +1 -1
- package/package.json +10 -10
- package/dist/index-BJnjMyHr.js +0 -803
- package/dist/index-BJnjMyHr.js.map +0 -1
- package/dist/index-BzmHAEeR.cjs +0 -2
- package/dist/index-BzmHAEeR.cjs.map +0 -1
package/dist/index-BJnjMyHr.js
DELETED
|
@@ -1,803 +0,0 @@
|
|
|
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
|
-
encode: (e) => Buffer.from(e).toString("base64url"),
|
|
4
|
-
decode: (e) => Buffer.from(e, "base64url").toString()
|
|
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
|
-
return typeof e == "object" && e !== null && !Array.isArray(e);
|
|
8
|
-
}
|
|
9
|
-
function x(e) {
|
|
10
|
-
return typeof e == "number" && Number.isFinite(e);
|
|
11
|
-
}
|
|
12
|
-
function D(e) {
|
|
13
|
-
return Array.isArray(e) && e.every((t) => typeof t == "string");
|
|
14
|
-
}
|
|
15
|
-
function re(e) {
|
|
16
|
-
const t = [
|
|
17
|
-
{ claim: "iss", valid: e.iss === void 0 || typeof e.iss == "string", expected: "string" },
|
|
18
|
-
{ claim: "sub", valid: e.sub === void 0 || typeof e.sub == "string", expected: "string" },
|
|
19
|
-
{ claim: "jti", valid: e.jti === void 0 || typeof e.jti == "string", expected: "string" },
|
|
20
|
-
{ claim: "sid", valid: e.sid === void 0 || typeof e.sid == "string", expected: "string" },
|
|
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
|
-
];
|
|
25
|
-
for (const r of t)
|
|
26
|
-
if (!r.valid)
|
|
27
|
-
return {
|
|
28
|
-
reason: `Invalid "${r.claim}" claim: expected ${r.expected}`,
|
|
29
|
-
code: "INVALID_CLAIM"
|
|
30
|
-
};
|
|
31
|
-
return e.aud !== void 0 && !(typeof e.aud == "string" || D(e.aud)) ? {
|
|
32
|
-
reason: 'Invalid "aud" claim: expected string or string[]',
|
|
33
|
-
code: "INVALID_CLAIM"
|
|
34
|
-
} : null;
|
|
35
|
-
}
|
|
36
|
-
function te(e) {
|
|
37
|
-
if (e.signatureFormat !== void 0 && e.signatureFormat !== "der" && e.signatureFormat !== "jose")
|
|
38
|
-
return {
|
|
39
|
-
reason: 'Invalid signatureFormat option: expected "der" or "jose"',
|
|
40
|
-
code: "INVALID_OPTIONS"
|
|
41
|
-
};
|
|
42
|
-
if (e.clockSkew !== void 0 && (!x(e.clockSkew) || e.clockSkew < 0))
|
|
43
|
-
return {
|
|
44
|
-
reason: "Invalid clockSkew option: expected a non-negative finite number",
|
|
45
|
-
code: "INVALID_OPTIONS"
|
|
46
|
-
};
|
|
47
|
-
if (e.maxTokenAge !== void 0 && (!x(e.maxTokenAge) || e.maxTokenAge < 0))
|
|
48
|
-
return {
|
|
49
|
-
reason: "Invalid maxTokenAge option: expected a non-negative finite number",
|
|
50
|
-
code: "INVALID_OPTIONS"
|
|
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
|
-
};
|
|
57
|
-
if (e.issuer !== void 0 && typeof e.issuer != "string")
|
|
58
|
-
return {
|
|
59
|
-
reason: "Invalid issuer option: expected string",
|
|
60
|
-
code: "INVALID_OPTIONS"
|
|
61
|
-
};
|
|
62
|
-
if (e.subject !== void 0 && typeof e.subject != "string")
|
|
63
|
-
return {
|
|
64
|
-
reason: "Invalid subject option: expected string",
|
|
65
|
-
code: "INVALID_OPTIONS"
|
|
66
|
-
};
|
|
67
|
-
if (e.jwtId !== void 0 && typeof e.jwtId != "string")
|
|
68
|
-
return {
|
|
69
|
-
reason: "Invalid jwtId option: expected string",
|
|
70
|
-
code: "INVALID_OPTIONS"
|
|
71
|
-
};
|
|
72
|
-
if (e.audience !== void 0 && !(typeof e.audience == "string" || D(e.audience)))
|
|
73
|
-
return {
|
|
74
|
-
reason: "Invalid audience option: expected string or string[]",
|
|
75
|
-
code: "INVALID_OPTIONS"
|
|
76
|
-
};
|
|
77
|
-
if (e.algorithms !== void 0) {
|
|
78
|
-
if (!Array.isArray(e.algorithms) || e.algorithms.length === 0)
|
|
79
|
-
return {
|
|
80
|
-
reason: "Invalid algorithms option: expected a non-empty array",
|
|
81
|
-
code: "INVALID_OPTIONS"
|
|
82
|
-
};
|
|
83
|
-
const t = e.algorithms.find((r) => !(r in p));
|
|
84
|
-
if (t)
|
|
85
|
-
return {
|
|
86
|
-
reason: `Invalid algorithms option: unsupported algorithm "${t}"`,
|
|
87
|
-
code: "INVALID_OPTIONS"
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
return null;
|
|
91
|
-
}
|
|
92
|
-
function ne(e) {
|
|
93
|
-
return e.crit !== void 0 ? !D(e.crit) || e.crit.length === 0 ? {
|
|
94
|
-
reason: 'Invalid "crit" header: expected non-empty string[]',
|
|
95
|
-
code: "INVALID_HEADER"
|
|
96
|
-
} : {
|
|
97
|
-
reason: `Unsupported critical header parameters: ${e.crit.join(", ")}`,
|
|
98
|
-
code: "UNSUPPORTED_CRITICAL_HEADER"
|
|
99
|
-
} : null;
|
|
100
|
-
}
|
|
101
|
-
function j(e) {
|
|
102
|
-
switch (e) {
|
|
103
|
-
case "ES256":
|
|
104
|
-
case "ES256K":
|
|
105
|
-
return 64;
|
|
106
|
-
// 32 + 32
|
|
107
|
-
case "ES384":
|
|
108
|
-
return 96;
|
|
109
|
-
// 48 + 48
|
|
110
|
-
case "ES512":
|
|
111
|
-
return 132;
|
|
112
|
-
// 66 + 66 (P-521)
|
|
113
|
-
/* c8 ignore next 2 */
|
|
114
|
-
default:
|
|
115
|
-
throw new Error(`Unsupported ECDSA alg for JOSE conversion: ${e}`);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
function ie(e, t) {
|
|
119
|
-
let r = 0;
|
|
120
|
-
if (e[r++] !== 48) throw new Error("Invalid DER ECDSA signature");
|
|
121
|
-
let n = e[r++];
|
|
122
|
-
if (n & 128) {
|
|
123
|
-
const d = n & 127;
|
|
124
|
-
n = 0;
|
|
125
|
-
for (let g = 0; g < d; g++) n = n << 8 | e[r++];
|
|
126
|
-
}
|
|
127
|
-
if (e[r++] !== 2) throw new Error("Invalid DER ECDSA signature (r)");
|
|
128
|
-
const a = e[r++];
|
|
129
|
-
let o = e.subarray(r, r + a);
|
|
130
|
-
if (r += a, e[r++] !== 2) throw new Error("Invalid DER ECDSA signature (s)");
|
|
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]);
|
|
137
|
-
}
|
|
138
|
-
function L(e) {
|
|
139
|
-
const t = e.length / 2;
|
|
140
|
-
let r = e.subarray(0, t), n = e.subarray(t);
|
|
141
|
-
for (; r.length > 1 && r[0] === 0 && (r[1] & 128) === 0; ) r = r.subarray(1);
|
|
142
|
-
for (; n.length > 1 && n[0] === 0 && (n[1] & 128) === 0; ) n = n.subarray(1);
|
|
143
|
-
r[0] & 128 && (r = Buffer.concat([Buffer.from([0]), r])), n[0] & 128 && (n = Buffer.concat([Buffer.from([0]), n]));
|
|
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]);
|
|
148
|
-
else {
|
|
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]);
|
|
154
|
-
}
|
|
155
|
-
return Buffer.concat([Buffer.from([48]), s, a, o]);
|
|
156
|
-
}
|
|
157
|
-
function k(e) {
|
|
158
|
-
return e === "ES256" || e === "ES384" || e === "ES512" || e === "ES256K";
|
|
159
|
-
}
|
|
160
|
-
const P = (e) => ({
|
|
161
|
-
sign: (t, r) => R(e, r).update(t).digest("base64url"),
|
|
162
|
-
verify: (t, r, n) => {
|
|
163
|
-
const a = R(e, r).update(t).digest("base64url");
|
|
164
|
-
return ee(a, n);
|
|
165
|
-
}
|
|
166
|
-
}), E = (e) => ({
|
|
167
|
-
sign: (t, r) => U(e).update(t).end().sign(r).toString("base64url"),
|
|
168
|
-
verify: (t, r, n) => {
|
|
169
|
-
try {
|
|
170
|
-
return V(e).update(t).end().verify(r, Buffer.from(n, "base64url"));
|
|
171
|
-
} catch {
|
|
172
|
-
return !1;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}), _ = (e, t) => ({
|
|
176
|
-
sign: (r, n) => U(e).update(r).end().sign({
|
|
177
|
-
//@ts-ignore
|
|
178
|
-
key: n,
|
|
179
|
-
padding: B.constants.RSA_PKCS1_PSS_PADDING,
|
|
180
|
-
saltLength: t
|
|
181
|
-
}).toString("base64url"),
|
|
182
|
-
verify: (r, n, a) => {
|
|
183
|
-
try {
|
|
184
|
-
return V(e).update(r).end().verify({
|
|
185
|
-
//@ts-ignore
|
|
186
|
-
key: n,
|
|
187
|
-
padding: B.constants.RSA_PKCS1_PSS_PADDING,
|
|
188
|
-
saltLength: t
|
|
189
|
-
}, Buffer.from(a, "base64url"));
|
|
190
|
-
} catch {
|
|
191
|
-
return !1;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}), ae = {
|
|
195
|
-
sign: (e, t) => Z(null, typeof e == "string" ? Buffer.from(e, "utf8") : e, t).toString("base64url"),
|
|
196
|
-
verify: (e, t, r) => {
|
|
197
|
-
try {
|
|
198
|
-
return Y(
|
|
199
|
-
null,
|
|
200
|
-
typeof e == "string" ? Buffer.from(e, "utf8") : e,
|
|
201
|
-
t,
|
|
202
|
-
Buffer.from(r, "base64url")
|
|
203
|
-
);
|
|
204
|
-
} catch {
|
|
205
|
-
return !1;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}, p = {
|
|
209
|
-
// HMAC
|
|
210
|
-
HS256: P("sha256"),
|
|
211
|
-
HS384: P("sha384"),
|
|
212
|
-
HS512: P("sha512"),
|
|
213
|
-
// RSA (DER-encoded signatures, base64url)
|
|
214
|
-
RS256: E("RSA-SHA256"),
|
|
215
|
-
RS384: E("RSA-SHA384"),
|
|
216
|
-
RS512: E("RSA-SHA512"),
|
|
217
|
-
// ECDSA (Node/OpenSSL emits DER; sign() converts to JOSE by default for JWTs)
|
|
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) {
|
|
228
|
-
if (e.type === "secret") return "HS256";
|
|
229
|
-
if (e.type !== "private") throw new Error("Only private or symmetric keys can be used to sign JWTs");
|
|
230
|
-
const t = e.asymmetricKeyType, r = e.asymmetricKeyDetails;
|
|
231
|
-
switch (t) {
|
|
232
|
-
case "rsa":
|
|
233
|
-
return "RS256";
|
|
234
|
-
case "rsa-pss": {
|
|
235
|
-
const n = (r == null ? void 0 : r.hashAlgorithm) ?? "sha256";
|
|
236
|
-
switch (n) {
|
|
237
|
-
case "sha256":
|
|
238
|
-
return "PS256";
|
|
239
|
-
case "sha384":
|
|
240
|
-
return "PS384";
|
|
241
|
-
case "sha512":
|
|
242
|
-
return "PS512";
|
|
243
|
-
default:
|
|
244
|
-
throw new Error(`Unsupported RSA-PSS hash algorithm: ${n}`);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
case "ec": {
|
|
248
|
-
const n = r == null ? void 0 : r.namedCurve;
|
|
249
|
-
switch (n) {
|
|
250
|
-
case "P-256":
|
|
251
|
-
case "prime256v1":
|
|
252
|
-
return "ES256";
|
|
253
|
-
case "P-384":
|
|
254
|
-
case "secp384r1":
|
|
255
|
-
return "ES384";
|
|
256
|
-
case "P-521":
|
|
257
|
-
case "secp521r1":
|
|
258
|
-
return "ES512";
|
|
259
|
-
case "secp256k1":
|
|
260
|
-
return "ES256K";
|
|
261
|
-
default:
|
|
262
|
-
throw new Error(`Unsupported EC curve: ${n}`);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
case "ed25519":
|
|
266
|
-
return "EdDSA";
|
|
267
|
-
default:
|
|
268
|
-
throw new Error(`Unsupported asymmetric key type: ${t}`);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
function M(e, t) {
|
|
272
|
-
if (typeof e == "object" && "type" in e) return e;
|
|
273
|
-
try {
|
|
274
|
-
return t === "sign" ? C(e) : N(e);
|
|
275
|
-
} catch {
|
|
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`);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
const F = (e) => {
|
|
330
|
-
const t = e.split(".");
|
|
331
|
-
if (t.length !== 3)
|
|
332
|
-
throw new Error('Invalid JWT: must contain exactly 3 parts separated by "."');
|
|
333
|
-
const [r, n, a] = t;
|
|
334
|
-
if (!r || !n || !a)
|
|
335
|
-
throw new Error("Invalid JWT: empty part detected");
|
|
336
|
-
if (!K.test(r) || !K.test(n) || !K.test(a))
|
|
337
|
-
throw new Error("Invalid JWT: non-base64url characters detected");
|
|
338
|
-
try {
|
|
339
|
-
const o = JSON.parse(O.decode(r)), i = JSON.parse(O.decode(n));
|
|
340
|
-
if (!W(o) || !W(i))
|
|
341
|
-
throw new Error("header and payload must be JSON objects");
|
|
342
|
-
const s = o, y = i;
|
|
343
|
-
if (typeof s.alg != "string" || s.alg.length === 0)
|
|
344
|
-
throw new Error("header.alg must be a non-empty string");
|
|
345
|
-
if (s.typ !== void 0 && typeof s.typ != "string")
|
|
346
|
-
throw new Error("header.typ must be a string");
|
|
347
|
-
if (s.kid !== void 0 && typeof s.kid != "string")
|
|
348
|
-
throw new Error("header.kid must be a string");
|
|
349
|
-
if (s.crit !== void 0 && (!D(s.crit) || s.crit.length === 0))
|
|
350
|
-
throw new Error("header.crit must be a non-empty string[]");
|
|
351
|
-
return { header: s, payload: y, signature: a };
|
|
352
|
-
} catch (o) {
|
|
353
|
-
throw new Error(`Invalid JWT: malformed header or payload (${o.message})`);
|
|
354
|
-
}
|
|
355
|
-
}, oe = (e, t, 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);
|
|
370
|
-
if (n)
|
|
371
|
-
return {
|
|
372
|
-
valid: !1,
|
|
373
|
-
error: n
|
|
374
|
-
};
|
|
375
|
-
let a;
|
|
376
|
-
try {
|
|
377
|
-
a = F(e);
|
|
378
|
-
} catch (c) {
|
|
379
|
-
return {
|
|
380
|
-
valid: !1,
|
|
381
|
-
error: {
|
|
382
|
-
reason: c.message,
|
|
383
|
-
code: "INVALID_TOKEN"
|
|
384
|
-
}
|
|
385
|
-
};
|
|
386
|
-
}
|
|
387
|
-
const { header: o, payload: i, signature: s } = a, y = ne(o);
|
|
388
|
-
if (y)
|
|
389
|
-
return {
|
|
390
|
-
valid: !1,
|
|
391
|
-
error: y
|
|
392
|
-
};
|
|
393
|
-
const f = re(i);
|
|
394
|
-
if (f)
|
|
395
|
-
return {
|
|
396
|
-
valid: !1,
|
|
397
|
-
error: f
|
|
398
|
-
};
|
|
399
|
-
const d = o.alg;
|
|
400
|
-
if (!(d in p))
|
|
401
|
-
return {
|
|
402
|
-
valid: !1,
|
|
403
|
-
error: {
|
|
404
|
-
reason: `Unsupported or unknown algorithm: ${o.alg}`,
|
|
405
|
-
code: "INVALID_ALGORITHM"
|
|
406
|
-
}
|
|
407
|
-
};
|
|
408
|
-
if (r.algorithms && r.algorithms.length > 0 && !r.algorithms.includes(d))
|
|
409
|
-
return {
|
|
410
|
-
valid: !1,
|
|
411
|
-
error: {
|
|
412
|
-
reason: `Algorithm "${d}" is not in the allowed algorithms list`,
|
|
413
|
-
code: "ALGORITHM_NOT_ALLOWED"
|
|
414
|
-
}
|
|
415
|
-
};
|
|
416
|
-
if (o.typ !== void 0 && o.typ !== "JWT")
|
|
417
|
-
return {
|
|
418
|
-
valid: !1,
|
|
419
|
-
error: {
|
|
420
|
-
reason: `Invalid token type: expected 'JWT', got '${o.typ}'`,
|
|
421
|
-
code: "INVALID_TYPE"
|
|
422
|
-
}
|
|
423
|
-
};
|
|
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")
|
|
441
|
-
try {
|
|
442
|
-
const v = Buffer.from(s, "base64url"), S = L(v).toString("base64url");
|
|
443
|
-
l = p[d].verify(I, m, S);
|
|
444
|
-
} catch {
|
|
445
|
-
l = !1;
|
|
446
|
-
}
|
|
447
|
-
else if (c === "der")
|
|
448
|
-
l = p[d].verify(I, m, s);
|
|
449
|
-
else if (l = p[d].verify(I, m, s), !l)
|
|
450
|
-
try {
|
|
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);
|
|
455
|
-
}
|
|
456
|
-
} catch {
|
|
457
|
-
}
|
|
458
|
-
if (!l)
|
|
459
|
-
return { valid: !1, error: { reason: "Signature verification failed", code: "INVALID_SIGNATURE" } };
|
|
460
|
-
} else {
|
|
461
|
-
let c = !1;
|
|
462
|
-
try {
|
|
463
|
-
c = p[d].verify(I, m, s);
|
|
464
|
-
} catch {
|
|
465
|
-
c = !1;
|
|
466
|
-
}
|
|
467
|
-
if (!c)
|
|
468
|
-
return { valid: !1, error: { reason: "Signature verification failed", code: "INVALID_SIGNATURE" } };
|
|
469
|
-
}
|
|
470
|
-
const h = Math.floor(Date.now() / 1e3), u = r.clockSkew ?? 0;
|
|
471
|
-
if (!r.ignoreExpiration && i.exp !== void 0 && h > i.exp + u)
|
|
472
|
-
return {
|
|
473
|
-
valid: !1,
|
|
474
|
-
error: {
|
|
475
|
-
reason: "Token expired",
|
|
476
|
-
code: "TOKEN_EXPIRED"
|
|
477
|
-
}
|
|
478
|
-
};
|
|
479
|
-
if (i.nbf !== void 0 && h + u < i.nbf)
|
|
480
|
-
return {
|
|
481
|
-
valid: !1,
|
|
482
|
-
error: {
|
|
483
|
-
reason: "Token not yet valid",
|
|
484
|
-
code: "TOKEN_NOT_ACTIVE"
|
|
485
|
-
}
|
|
486
|
-
};
|
|
487
|
-
if (i.iat !== void 0 && h + u < i.iat)
|
|
488
|
-
return {
|
|
489
|
-
valid: !1,
|
|
490
|
-
error: {
|
|
491
|
-
reason: "Token issued in the future",
|
|
492
|
-
code: "TOKEN_FUTURE_ISSUED"
|
|
493
|
-
}
|
|
494
|
-
};
|
|
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)
|
|
506
|
-
return {
|
|
507
|
-
valid: !1,
|
|
508
|
-
error: {
|
|
509
|
-
reason: `Token age (${c}s) exceeds maximum allowed age (${r.maxTokenAge}s)`,
|
|
510
|
-
code: "TOKEN_TOO_OLD"
|
|
511
|
-
}
|
|
512
|
-
};
|
|
513
|
-
}
|
|
514
|
-
if (r.issuer !== void 0) {
|
|
515
|
-
if (i.iss === void 0)
|
|
516
|
-
return {
|
|
517
|
-
valid: !1,
|
|
518
|
-
error: {
|
|
519
|
-
reason: 'Token missing required issuer claim ("iss")',
|
|
520
|
-
code: "MISSING_ISSUER"
|
|
521
|
-
}
|
|
522
|
-
};
|
|
523
|
-
if (r.issuer !== i.iss)
|
|
524
|
-
return {
|
|
525
|
-
valid: !1,
|
|
526
|
-
error: {
|
|
527
|
-
reason: `Invalid token issuer: expected "${r.issuer}", got "${i.iss}"`,
|
|
528
|
-
code: "INVALID_ISSUER"
|
|
529
|
-
}
|
|
530
|
-
};
|
|
531
|
-
}
|
|
532
|
-
if (r.subject !== void 0) {
|
|
533
|
-
if (i.sub === void 0)
|
|
534
|
-
return {
|
|
535
|
-
valid: !1,
|
|
536
|
-
error: {
|
|
537
|
-
reason: 'Token missing required subject claim ("sub")',
|
|
538
|
-
code: "MISSING_SUBJECT"
|
|
539
|
-
}
|
|
540
|
-
};
|
|
541
|
-
if (r.subject !== i.sub)
|
|
542
|
-
return {
|
|
543
|
-
valid: !1,
|
|
544
|
-
error: {
|
|
545
|
-
reason: `Invalid token subject: expected "${r.subject}", got "${i.sub}"`,
|
|
546
|
-
code: "INVALID_SUBJECT"
|
|
547
|
-
}
|
|
548
|
-
};
|
|
549
|
-
}
|
|
550
|
-
if (r.audience !== void 0) {
|
|
551
|
-
const c = i.aud;
|
|
552
|
-
if (c === void 0)
|
|
553
|
-
return {
|
|
554
|
-
valid: !1,
|
|
555
|
-
error: {
|
|
556
|
-
reason: 'Token missing required audience claim ("aud")',
|
|
557
|
-
code: "MISSING_AUDIENCE"
|
|
558
|
-
}
|
|
559
|
-
};
|
|
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)))
|
|
562
|
-
return {
|
|
563
|
-
valid: !1,
|
|
564
|
-
error: {
|
|
565
|
-
reason: "Audience claim mismatch",
|
|
566
|
-
code: "INVALID_AUDIENCE"
|
|
567
|
-
}
|
|
568
|
-
};
|
|
569
|
-
}
|
|
570
|
-
if (r.jwtId !== void 0) {
|
|
571
|
-
if (i.jti === void 0)
|
|
572
|
-
return {
|
|
573
|
-
valid: !1,
|
|
574
|
-
error: {
|
|
575
|
-
reason: 'Token missing required JWT ID claim ("jti")',
|
|
576
|
-
code: "MISSING_JTI"
|
|
577
|
-
}
|
|
578
|
-
};
|
|
579
|
-
if (r.jwtId !== i.jti)
|
|
580
|
-
return {
|
|
581
|
-
valid: !1,
|
|
582
|
-
error: {
|
|
583
|
-
reason: `Invalid JWT ID: expected "${r.jwtId}", got "${i.jti}"`,
|
|
584
|
-
code: "INVALID_JTI"
|
|
585
|
-
}
|
|
586
|
-
};
|
|
587
|
-
}
|
|
588
|
-
return { valid: !0, header: o, payload: i, signature: s };
|
|
589
|
-
}, me = {
|
|
590
|
-
sign: oe,
|
|
591
|
-
verify: ce,
|
|
592
|
-
decode: F,
|
|
593
|
-
algorithms: p
|
|
594
|
-
};
|
|
595
|
-
function fe(e) {
|
|
596
|
-
if (!e || typeof e != "object") throw new Error("Invalid KeyObject");
|
|
597
|
-
return e.export({ format: "jwk" });
|
|
598
|
-
}
|
|
599
|
-
function $(e) {
|
|
600
|
-
if (!e || typeof e != "object") throw new Error("Invalid JWK");
|
|
601
|
-
switch (e.kty) {
|
|
602
|
-
case "oct": {
|
|
603
|
-
if (!("k" in e) || typeof e.k != "string")
|
|
604
|
-
throw new Error('Invalid oct JWK: missing "k"');
|
|
605
|
-
return w(Buffer.from(e.k, "base64url"));
|
|
606
|
-
}
|
|
607
|
-
case "RSA":
|
|
608
|
-
case "EC":
|
|
609
|
-
case "OKP":
|
|
610
|
-
return "d" in e && typeof e.d == "string" ? C({ format: "jwk", key: e }) : N({ format: "jwk", key: e });
|
|
611
|
-
default:
|
|
612
|
-
throw new Error(`Unsupported JWK key type: ${e.kty}`);
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
function ue(e) {
|
|
616
|
-
if (!e || typeof e != "object")
|
|
617
|
-
throw new Error("Invalid KeyObject");
|
|
618
|
-
const r = (e.type === "private" ? N(e) : e).export({ format: "jwk" });
|
|
619
|
-
return delete r.d, delete r.p, delete r.q, delete r.dp, delete r.dq, delete r.qi, r;
|
|
620
|
-
}
|
|
621
|
-
function G(e, t = "sha256") {
|
|
622
|
-
if (!e || typeof e != "object")
|
|
623
|
-
throw new Error("Invalid JWK");
|
|
624
|
-
let r;
|
|
625
|
-
switch (e.kty) {
|
|
626
|
-
case "RSA":
|
|
627
|
-
r = { e: e.e, kty: e.kty, n: e.n };
|
|
628
|
-
break;
|
|
629
|
-
case "EC":
|
|
630
|
-
r = { crv: e.crv, kty: e.kty, x: e.x, y: e.y };
|
|
631
|
-
break;
|
|
632
|
-
case "OKP":
|
|
633
|
-
r = { crv: e.crv, kty: e.kty, x: e.x };
|
|
634
|
-
break;
|
|
635
|
-
case "oct":
|
|
636
|
-
r = { k: e.k, kty: e.kty };
|
|
637
|
-
break;
|
|
638
|
-
default:
|
|
639
|
-
throw new Error(`Unsupported JWK key type: ${e.kty}`);
|
|
640
|
-
}
|
|
641
|
-
const n = JSON.stringify(
|
|
642
|
-
Object.keys(r).sort().reduce((a, o) => (a[o] = r[o], a), {})
|
|
643
|
-
);
|
|
644
|
-
return H(t).update(n).digest("base64url");
|
|
645
|
-
}
|
|
646
|
-
function de(e) {
|
|
647
|
-
var t;
|
|
648
|
-
if ((t = e.x5c) != null && t.length)
|
|
649
|
-
return H("sha1").update(Buffer.from(e.x5c[0], "base64")).digest("base64url");
|
|
650
|
-
}
|
|
651
|
-
const ve = {
|
|
652
|
-
export: fe,
|
|
653
|
-
import: $,
|
|
654
|
-
toPublic: ue,
|
|
655
|
-
thumbprint: G
|
|
656
|
-
};
|
|
657
|
-
function le(e, t) {
|
|
658
|
-
if (!e || !Array.isArray(e.keys)) throw new Error("Invalid JWKS");
|
|
659
|
-
let r;
|
|
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");
|
|
661
|
-
return $(r);
|
|
662
|
-
}
|
|
663
|
-
function J(e) {
|
|
664
|
-
return {
|
|
665
|
-
keys: T(...e.keys)
|
|
666
|
-
};
|
|
667
|
-
}
|
|
668
|
-
function T(...e) {
|
|
669
|
-
return e.map((t) => {
|
|
670
|
-
const r = {
|
|
671
|
-
...t,
|
|
672
|
-
kid: t.kid ?? G(t),
|
|
673
|
-
x5t: t.x5t ?? de(t)
|
|
674
|
-
};
|
|
675
|
-
return Object.defineProperty(r, "toKeyObject", {
|
|
676
|
-
enumerable: !1,
|
|
677
|
-
configurable: !1,
|
|
678
|
-
writable: !1,
|
|
679
|
-
value: () => $(r)
|
|
680
|
-
}), r;
|
|
681
|
-
});
|
|
682
|
-
}
|
|
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";
|
|
685
|
-
if (!n)
|
|
686
|
-
throw new Error("No fetch implementation available");
|
|
687
|
-
const s = (() => {
|
|
688
|
-
if (t.endpointOverride) {
|
|
689
|
-
const h = t.endpointOverride;
|
|
690
|
-
try {
|
|
691
|
-
return new URL(h, r).toString();
|
|
692
|
-
} catch {
|
|
693
|
-
return h;
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
return t.overrideEndpointCheck || r.endsWith(i) ? r : `${r.replace(/\/+$/, "")}${i}`;
|
|
697
|
-
})(), y = (() => {
|
|
698
|
-
if (t.cache) return t.cache;
|
|
699
|
-
let h;
|
|
700
|
-
return {
|
|
701
|
-
get: () => h,
|
|
702
|
-
set: (u, c) => {
|
|
703
|
-
h = c;
|
|
704
|
-
}
|
|
705
|
-
};
|
|
706
|
-
})();
|
|
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;
|
|
715
|
-
try {
|
|
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;
|
|
719
|
-
} finally {
|
|
720
|
-
c && clearTimeout(c);
|
|
721
|
-
}
|
|
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))
|
|
726
|
-
throw new Error("Invalid JWKS");
|
|
727
|
-
return J(v);
|
|
728
|
-
})();
|
|
729
|
-
try {
|
|
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),
|
|
738
|
-
9e5
|
|
739
|
-
);
|
|
740
|
-
d = Date.now() + c;
|
|
741
|
-
}
|
|
742
|
-
return console.warn(`JWKS refresh failed for "${s}", using stale cache.`, u), f;
|
|
743
|
-
} finally {
|
|
744
|
-
g = void 0;
|
|
745
|
-
}
|
|
746
|
-
};
|
|
747
|
-
f = await y.get(s), f ? (f = J(f), a > 0 && (d = Date.now() + a)) : f = await I(!1);
|
|
748
|
-
const m = {
|
|
749
|
-
async list() {
|
|
750
|
-
return a > 0 && Date.now() >= d && await I(!0), T(...f.keys);
|
|
751
|
-
},
|
|
752
|
-
async refresh() {
|
|
753
|
-
return await I(!1), m;
|
|
754
|
-
},
|
|
755
|
-
async key(h) {
|
|
756
|
-
const c = (await m.list()).find((l) => l.kid === h);
|
|
757
|
-
if (c)
|
|
758
|
-
return T(c)[0];
|
|
759
|
-
},
|
|
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;
|
|
766
|
-
})
|
|
767
|
-
));
|
|
768
|
-
},
|
|
769
|
-
async findFirst(h) {
|
|
770
|
-
return m.find(h).then(([u]) => u);
|
|
771
|
-
},
|
|
772
|
-
export() {
|
|
773
|
-
return f;
|
|
774
|
-
}
|
|
775
|
-
};
|
|
776
|
-
return m;
|
|
777
|
-
}, Se = {
|
|
778
|
-
toKeyObject: le,
|
|
779
|
-
normalize: J,
|
|
780
|
-
fromWeb: he
|
|
781
|
-
};
|
|
782
|
-
export {
|
|
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
|
|
802
|
-
};
|
|
803
|
-
//# sourceMappingURL=index-BJnjMyHr.js.map
|