@kevisual/auth 1.0.5 → 2.0.1
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/dist/app.d.ts +57 -0
- package/dist/app.js +1854 -0
- package/package.json +20 -49
- package/readme.md +45 -18
- package/src/auth.ts +123 -0
- package/src/generate.ts +39 -0
- package/src/index.ts +1 -6
- package/src/jwks/common.ts +8 -0
- package/src/jwks/create.ts +23 -0
- package/src/jwks/get.ts +22 -0
- package/src/router.ts +2 -0
- package/dist/create-token.d.ts +0 -31
- package/dist/create-token.mjs +0 -6355
- package/dist/index.d.ts +0 -55
- package/dist/index.mjs +0 -7512
- package/dist/is-me.d.ts +0 -14
- package/dist/is-me.mjs +0 -28
- package/dist/proxy.d.ts +0 -42
- package/dist/proxy.mjs +0 -98
- package/dist/salt.d.ts +0 -22
- package/dist/salt.mjs +0 -1130
- package/src/create-token.ts +0 -46
- package/src/is-me.ts +0 -32
- package/src/proxy.ts +0 -116
- package/src/route.ts +0 -44
- package/src/salt.ts +0 -32
package/dist/app.js
ADDED
|
@@ -0,0 +1,1854 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, {
|
|
5
|
+
get: all[name],
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
set: (newValue) => all[name] = () => newValue
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/buffer_utils.js
|
|
13
|
+
var encoder = new TextEncoder;
|
|
14
|
+
var decoder = new TextDecoder;
|
|
15
|
+
var MAX_INT32 = 2 ** 32;
|
|
16
|
+
function concat(...buffers) {
|
|
17
|
+
const size = buffers.reduce((acc, { length }) => acc + length, 0);
|
|
18
|
+
const buf = new Uint8Array(size);
|
|
19
|
+
let i = 0;
|
|
20
|
+
for (const buffer of buffers) {
|
|
21
|
+
buf.set(buffer, i);
|
|
22
|
+
i += buffer.length;
|
|
23
|
+
}
|
|
24
|
+
return buf;
|
|
25
|
+
}
|
|
26
|
+
function encode(string) {
|
|
27
|
+
const bytes = new Uint8Array(string.length);
|
|
28
|
+
for (let i = 0;i < string.length; i++) {
|
|
29
|
+
const code = string.charCodeAt(i);
|
|
30
|
+
if (code > 127) {
|
|
31
|
+
throw new TypeError("non-ASCII string encountered in encode()");
|
|
32
|
+
}
|
|
33
|
+
bytes[i] = code;
|
|
34
|
+
}
|
|
35
|
+
return bytes;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/base64.js
|
|
39
|
+
function encodeBase64(input) {
|
|
40
|
+
if (Uint8Array.prototype.toBase64) {
|
|
41
|
+
return input.toBase64();
|
|
42
|
+
}
|
|
43
|
+
const CHUNK_SIZE = 32768;
|
|
44
|
+
const arr = [];
|
|
45
|
+
for (let i = 0;i < input.length; i += CHUNK_SIZE) {
|
|
46
|
+
arr.push(String.fromCharCode.apply(null, input.subarray(i, i + CHUNK_SIZE)));
|
|
47
|
+
}
|
|
48
|
+
return btoa(arr.join(""));
|
|
49
|
+
}
|
|
50
|
+
function decodeBase64(encoded) {
|
|
51
|
+
if (Uint8Array.fromBase64) {
|
|
52
|
+
return Uint8Array.fromBase64(encoded);
|
|
53
|
+
}
|
|
54
|
+
const binary = atob(encoded);
|
|
55
|
+
const bytes = new Uint8Array(binary.length);
|
|
56
|
+
for (let i = 0;i < binary.length; i++) {
|
|
57
|
+
bytes[i] = binary.charCodeAt(i);
|
|
58
|
+
}
|
|
59
|
+
return bytes;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/util/base64url.js
|
|
63
|
+
function decode(input) {
|
|
64
|
+
if (Uint8Array.fromBase64) {
|
|
65
|
+
return Uint8Array.fromBase64(typeof input === "string" ? input : decoder.decode(input), {
|
|
66
|
+
alphabet: "base64url"
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
let encoded = input;
|
|
70
|
+
if (encoded instanceof Uint8Array) {
|
|
71
|
+
encoded = decoder.decode(encoded);
|
|
72
|
+
}
|
|
73
|
+
encoded = encoded.replace(/-/g, "+").replace(/_/g, "/");
|
|
74
|
+
try {
|
|
75
|
+
return decodeBase64(encoded);
|
|
76
|
+
} catch {
|
|
77
|
+
throw new TypeError("The input to be decoded is not correctly encoded.");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function encode2(input) {
|
|
81
|
+
let unencoded = input;
|
|
82
|
+
if (typeof unencoded === "string") {
|
|
83
|
+
unencoded = encoder.encode(unencoded);
|
|
84
|
+
}
|
|
85
|
+
if (Uint8Array.prototype.toBase64) {
|
|
86
|
+
return unencoded.toBase64({ alphabet: "base64url", omitPadding: true });
|
|
87
|
+
}
|
|
88
|
+
return encodeBase64(unencoded).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/util/errors.js
|
|
92
|
+
var exports_errors = {};
|
|
93
|
+
__export(exports_errors, {
|
|
94
|
+
JWTInvalid: () => JWTInvalid,
|
|
95
|
+
JWTExpired: () => JWTExpired,
|
|
96
|
+
JWTClaimValidationFailed: () => JWTClaimValidationFailed,
|
|
97
|
+
JWSSignatureVerificationFailed: () => JWSSignatureVerificationFailed,
|
|
98
|
+
JWSInvalid: () => JWSInvalid,
|
|
99
|
+
JWKSTimeout: () => JWKSTimeout,
|
|
100
|
+
JWKSNoMatchingKey: () => JWKSNoMatchingKey,
|
|
101
|
+
JWKSMultipleMatchingKeys: () => JWKSMultipleMatchingKeys,
|
|
102
|
+
JWKSInvalid: () => JWKSInvalid,
|
|
103
|
+
JWKInvalid: () => JWKInvalid,
|
|
104
|
+
JWEInvalid: () => JWEInvalid,
|
|
105
|
+
JWEDecryptionFailed: () => JWEDecryptionFailed,
|
|
106
|
+
JOSENotSupported: () => JOSENotSupported,
|
|
107
|
+
JOSEError: () => JOSEError,
|
|
108
|
+
JOSEAlgNotAllowed: () => JOSEAlgNotAllowed
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
class JOSEError extends Error {
|
|
112
|
+
static code = "ERR_JOSE_GENERIC";
|
|
113
|
+
code = "ERR_JOSE_GENERIC";
|
|
114
|
+
constructor(message, options) {
|
|
115
|
+
super(message, options);
|
|
116
|
+
this.name = this.constructor.name;
|
|
117
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
class JWTClaimValidationFailed extends JOSEError {
|
|
122
|
+
static code = "ERR_JWT_CLAIM_VALIDATION_FAILED";
|
|
123
|
+
code = "ERR_JWT_CLAIM_VALIDATION_FAILED";
|
|
124
|
+
claim;
|
|
125
|
+
reason;
|
|
126
|
+
payload;
|
|
127
|
+
constructor(message, payload, claim = "unspecified", reason = "unspecified") {
|
|
128
|
+
super(message, { cause: { claim, reason, payload } });
|
|
129
|
+
this.claim = claim;
|
|
130
|
+
this.reason = reason;
|
|
131
|
+
this.payload = payload;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
class JWTExpired extends JOSEError {
|
|
136
|
+
static code = "ERR_JWT_EXPIRED";
|
|
137
|
+
code = "ERR_JWT_EXPIRED";
|
|
138
|
+
claim;
|
|
139
|
+
reason;
|
|
140
|
+
payload;
|
|
141
|
+
constructor(message, payload, claim = "unspecified", reason = "unspecified") {
|
|
142
|
+
super(message, { cause: { claim, reason, payload } });
|
|
143
|
+
this.claim = claim;
|
|
144
|
+
this.reason = reason;
|
|
145
|
+
this.payload = payload;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
class JOSEAlgNotAllowed extends JOSEError {
|
|
150
|
+
static code = "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
151
|
+
code = "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
class JOSENotSupported extends JOSEError {
|
|
155
|
+
static code = "ERR_JOSE_NOT_SUPPORTED";
|
|
156
|
+
code = "ERR_JOSE_NOT_SUPPORTED";
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
class JWEDecryptionFailed extends JOSEError {
|
|
160
|
+
static code = "ERR_JWE_DECRYPTION_FAILED";
|
|
161
|
+
code = "ERR_JWE_DECRYPTION_FAILED";
|
|
162
|
+
constructor(message = "decryption operation failed", options) {
|
|
163
|
+
super(message, options);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
class JWEInvalid extends JOSEError {
|
|
168
|
+
static code = "ERR_JWE_INVALID";
|
|
169
|
+
code = "ERR_JWE_INVALID";
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
class JWSInvalid extends JOSEError {
|
|
173
|
+
static code = "ERR_JWS_INVALID";
|
|
174
|
+
code = "ERR_JWS_INVALID";
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
class JWTInvalid extends JOSEError {
|
|
178
|
+
static code = "ERR_JWT_INVALID";
|
|
179
|
+
code = "ERR_JWT_INVALID";
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
class JWKInvalid extends JOSEError {
|
|
183
|
+
static code = "ERR_JWK_INVALID";
|
|
184
|
+
code = "ERR_JWK_INVALID";
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
class JWKSInvalid extends JOSEError {
|
|
188
|
+
static code = "ERR_JWKS_INVALID";
|
|
189
|
+
code = "ERR_JWKS_INVALID";
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
class JWKSNoMatchingKey extends JOSEError {
|
|
193
|
+
static code = "ERR_JWKS_NO_MATCHING_KEY";
|
|
194
|
+
code = "ERR_JWKS_NO_MATCHING_KEY";
|
|
195
|
+
constructor(message = "no applicable key found in the JSON Web Key Set", options) {
|
|
196
|
+
super(message, options);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
class JWKSMultipleMatchingKeys extends JOSEError {
|
|
201
|
+
[Symbol.asyncIterator];
|
|
202
|
+
static code = "ERR_JWKS_MULTIPLE_MATCHING_KEYS";
|
|
203
|
+
code = "ERR_JWKS_MULTIPLE_MATCHING_KEYS";
|
|
204
|
+
constructor(message = "multiple matching keys found in the JSON Web Key Set", options) {
|
|
205
|
+
super(message, options);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
class JWKSTimeout extends JOSEError {
|
|
210
|
+
static code = "ERR_JWKS_TIMEOUT";
|
|
211
|
+
code = "ERR_JWKS_TIMEOUT";
|
|
212
|
+
constructor(message = "request timed out", options) {
|
|
213
|
+
super(message, options);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
class JWSSignatureVerificationFailed extends JOSEError {
|
|
218
|
+
static code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
|
|
219
|
+
code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
|
|
220
|
+
constructor(message = "signature verification failed", options) {
|
|
221
|
+
super(message, options);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/crypto_key.js
|
|
226
|
+
var unusable = (name, prop = "algorithm.name") => new TypeError(`CryptoKey does not support this operation, its ${prop} must be ${name}`);
|
|
227
|
+
var isAlgorithm = (algorithm, name) => algorithm.name === name;
|
|
228
|
+
function getHashLength(hash) {
|
|
229
|
+
return parseInt(hash.name.slice(4), 10);
|
|
230
|
+
}
|
|
231
|
+
function getNamedCurve(alg) {
|
|
232
|
+
switch (alg) {
|
|
233
|
+
case "ES256":
|
|
234
|
+
return "P-256";
|
|
235
|
+
case "ES384":
|
|
236
|
+
return "P-384";
|
|
237
|
+
case "ES512":
|
|
238
|
+
return "P-521";
|
|
239
|
+
default:
|
|
240
|
+
throw new Error("unreachable");
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function checkUsage(key, usage) {
|
|
244
|
+
if (usage && !key.usages.includes(usage)) {
|
|
245
|
+
throw new TypeError(`CryptoKey does not support this operation, its usages must include ${usage}.`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function checkSigCryptoKey(key, alg, usage) {
|
|
249
|
+
switch (alg) {
|
|
250
|
+
case "HS256":
|
|
251
|
+
case "HS384":
|
|
252
|
+
case "HS512": {
|
|
253
|
+
if (!isAlgorithm(key.algorithm, "HMAC"))
|
|
254
|
+
throw unusable("HMAC");
|
|
255
|
+
const expected = parseInt(alg.slice(2), 10);
|
|
256
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
257
|
+
if (actual !== expected)
|
|
258
|
+
throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
case "RS256":
|
|
262
|
+
case "RS384":
|
|
263
|
+
case "RS512": {
|
|
264
|
+
if (!isAlgorithm(key.algorithm, "RSASSA-PKCS1-v1_5"))
|
|
265
|
+
throw unusable("RSASSA-PKCS1-v1_5");
|
|
266
|
+
const expected = parseInt(alg.slice(2), 10);
|
|
267
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
268
|
+
if (actual !== expected)
|
|
269
|
+
throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
case "PS256":
|
|
273
|
+
case "PS384":
|
|
274
|
+
case "PS512": {
|
|
275
|
+
if (!isAlgorithm(key.algorithm, "RSA-PSS"))
|
|
276
|
+
throw unusable("RSA-PSS");
|
|
277
|
+
const expected = parseInt(alg.slice(2), 10);
|
|
278
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
279
|
+
if (actual !== expected)
|
|
280
|
+
throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
case "Ed25519":
|
|
284
|
+
case "EdDSA": {
|
|
285
|
+
if (!isAlgorithm(key.algorithm, "Ed25519"))
|
|
286
|
+
throw unusable("Ed25519");
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
case "ML-DSA-44":
|
|
290
|
+
case "ML-DSA-65":
|
|
291
|
+
case "ML-DSA-87": {
|
|
292
|
+
if (!isAlgorithm(key.algorithm, alg))
|
|
293
|
+
throw unusable(alg);
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
case "ES256":
|
|
297
|
+
case "ES384":
|
|
298
|
+
case "ES512": {
|
|
299
|
+
if (!isAlgorithm(key.algorithm, "ECDSA"))
|
|
300
|
+
throw unusable("ECDSA");
|
|
301
|
+
const expected = getNamedCurve(alg);
|
|
302
|
+
const actual = key.algorithm.namedCurve;
|
|
303
|
+
if (actual !== expected)
|
|
304
|
+
throw unusable(expected, "algorithm.namedCurve");
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
default:
|
|
308
|
+
throw new TypeError("CryptoKey does not support this operation");
|
|
309
|
+
}
|
|
310
|
+
checkUsage(key, usage);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/invalid_key_input.js
|
|
314
|
+
function message(msg, actual, ...types) {
|
|
315
|
+
types = types.filter(Boolean);
|
|
316
|
+
if (types.length > 2) {
|
|
317
|
+
const last = types.pop();
|
|
318
|
+
msg += `one of type ${types.join(", ")}, or ${last}.`;
|
|
319
|
+
} else if (types.length === 2) {
|
|
320
|
+
msg += `one of type ${types[0]} or ${types[1]}.`;
|
|
321
|
+
} else {
|
|
322
|
+
msg += `of type ${types[0]}.`;
|
|
323
|
+
}
|
|
324
|
+
if (actual == null) {
|
|
325
|
+
msg += ` Received ${actual}`;
|
|
326
|
+
} else if (typeof actual === "function" && actual.name) {
|
|
327
|
+
msg += ` Received function ${actual.name}`;
|
|
328
|
+
} else if (typeof actual === "object" && actual != null) {
|
|
329
|
+
if (actual.constructor?.name) {
|
|
330
|
+
msg += ` Received an instance of ${actual.constructor.name}`;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return msg;
|
|
334
|
+
}
|
|
335
|
+
var invalidKeyInput = (actual, ...types) => message("Key must be ", actual, ...types);
|
|
336
|
+
var withAlg = (alg, actual, ...types) => message(`Key for the ${alg} algorithm must be `, actual, ...types);
|
|
337
|
+
|
|
338
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/is_key_like.js
|
|
339
|
+
var isCryptoKey = (key) => {
|
|
340
|
+
if (key?.[Symbol.toStringTag] === "CryptoKey")
|
|
341
|
+
return true;
|
|
342
|
+
try {
|
|
343
|
+
return key instanceof CryptoKey;
|
|
344
|
+
} catch {
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
var isKeyObject = (key) => key?.[Symbol.toStringTag] === "KeyObject";
|
|
349
|
+
var isKeyLike = (key) => isCryptoKey(key) || isKeyObject(key);
|
|
350
|
+
|
|
351
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/is_disjoint.js
|
|
352
|
+
function isDisjoint(...headers) {
|
|
353
|
+
const sources = headers.filter(Boolean);
|
|
354
|
+
if (sources.length === 0 || sources.length === 1) {
|
|
355
|
+
return true;
|
|
356
|
+
}
|
|
357
|
+
let acc;
|
|
358
|
+
for (const header of sources) {
|
|
359
|
+
const parameters = Object.keys(header);
|
|
360
|
+
if (!acc || acc.size === 0) {
|
|
361
|
+
acc = new Set(parameters);
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
for (const parameter of parameters) {
|
|
365
|
+
if (acc.has(parameter)) {
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
acc.add(parameter);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/is_object.js
|
|
375
|
+
var isObjectLike = (value) => typeof value === "object" && value !== null;
|
|
376
|
+
function isObject(input) {
|
|
377
|
+
if (!isObjectLike(input) || Object.prototype.toString.call(input) !== "[object Object]") {
|
|
378
|
+
return false;
|
|
379
|
+
}
|
|
380
|
+
if (Object.getPrototypeOf(input) === null) {
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
let proto = input;
|
|
384
|
+
while (Object.getPrototypeOf(proto) !== null) {
|
|
385
|
+
proto = Object.getPrototypeOf(proto);
|
|
386
|
+
}
|
|
387
|
+
return Object.getPrototypeOf(input) === proto;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/check_key_length.js
|
|
391
|
+
function checkKeyLength(alg, key) {
|
|
392
|
+
if (alg.startsWith("RS") || alg.startsWith("PS")) {
|
|
393
|
+
const { modulusLength } = key.algorithm;
|
|
394
|
+
if (typeof modulusLength !== "number" || modulusLength < 2048) {
|
|
395
|
+
throw new TypeError(`${alg} requires key modulusLength to be 2048 bits or larger`);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/asn1.js
|
|
401
|
+
var bytesEqual = (a, b) => {
|
|
402
|
+
if (a.byteLength !== b.length)
|
|
403
|
+
return false;
|
|
404
|
+
for (let i = 0;i < a.byteLength; i++) {
|
|
405
|
+
if (a[i] !== b[i])
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
return true;
|
|
409
|
+
};
|
|
410
|
+
var createASN1State = (data) => ({ data, pos: 0 });
|
|
411
|
+
var parseLength = (state) => {
|
|
412
|
+
const first = state.data[state.pos++];
|
|
413
|
+
if (first & 128) {
|
|
414
|
+
const lengthOfLen = first & 127;
|
|
415
|
+
let length = 0;
|
|
416
|
+
for (let i = 0;i < lengthOfLen; i++) {
|
|
417
|
+
length = length << 8 | state.data[state.pos++];
|
|
418
|
+
}
|
|
419
|
+
return length;
|
|
420
|
+
}
|
|
421
|
+
return first;
|
|
422
|
+
};
|
|
423
|
+
var expectTag = (state, expectedTag, errorMessage) => {
|
|
424
|
+
if (state.data[state.pos++] !== expectedTag) {
|
|
425
|
+
throw new Error(errorMessage);
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
var getSubarray = (state, length) => {
|
|
429
|
+
const result = state.data.subarray(state.pos, state.pos + length);
|
|
430
|
+
state.pos += length;
|
|
431
|
+
return result;
|
|
432
|
+
};
|
|
433
|
+
var parseAlgorithmOID = (state) => {
|
|
434
|
+
expectTag(state, 6, "Expected algorithm OID");
|
|
435
|
+
const oidLen = parseLength(state);
|
|
436
|
+
return getSubarray(state, oidLen);
|
|
437
|
+
};
|
|
438
|
+
function parsePKCS8Header(state) {
|
|
439
|
+
expectTag(state, 48, "Invalid PKCS#8 structure");
|
|
440
|
+
parseLength(state);
|
|
441
|
+
expectTag(state, 2, "Expected version field");
|
|
442
|
+
const verLen = parseLength(state);
|
|
443
|
+
state.pos += verLen;
|
|
444
|
+
expectTag(state, 48, "Expected algorithm identifier");
|
|
445
|
+
const algIdLen = parseLength(state);
|
|
446
|
+
const algIdStart = state.pos;
|
|
447
|
+
return { algIdStart, algIdLength: algIdLen };
|
|
448
|
+
}
|
|
449
|
+
function parseSPKIHeader(state) {
|
|
450
|
+
expectTag(state, 48, "Invalid SPKI structure");
|
|
451
|
+
parseLength(state);
|
|
452
|
+
expectTag(state, 48, "Expected algorithm identifier");
|
|
453
|
+
const algIdLen = parseLength(state);
|
|
454
|
+
const algIdStart = state.pos;
|
|
455
|
+
return { algIdStart, algIdLength: algIdLen };
|
|
456
|
+
}
|
|
457
|
+
var parseECAlgorithmIdentifier = (state) => {
|
|
458
|
+
const algOid = parseAlgorithmOID(state);
|
|
459
|
+
if (bytesEqual(algOid, [43, 101, 110])) {
|
|
460
|
+
return "X25519";
|
|
461
|
+
}
|
|
462
|
+
if (!bytesEqual(algOid, [42, 134, 72, 206, 61, 2, 1])) {
|
|
463
|
+
throw new Error("Unsupported key algorithm");
|
|
464
|
+
}
|
|
465
|
+
expectTag(state, 6, "Expected curve OID");
|
|
466
|
+
const curveOidLen = parseLength(state);
|
|
467
|
+
const curveOid = getSubarray(state, curveOidLen);
|
|
468
|
+
for (const { name, oid } of [
|
|
469
|
+
{ name: "P-256", oid: [42, 134, 72, 206, 61, 3, 1, 7] },
|
|
470
|
+
{ name: "P-384", oid: [43, 129, 4, 0, 34] },
|
|
471
|
+
{ name: "P-521", oid: [43, 129, 4, 0, 35] }
|
|
472
|
+
]) {
|
|
473
|
+
if (bytesEqual(curveOid, oid)) {
|
|
474
|
+
return name;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
throw new Error("Unsupported named curve");
|
|
478
|
+
};
|
|
479
|
+
var genericImport = async (keyFormat, keyData, alg, options) => {
|
|
480
|
+
let algorithm;
|
|
481
|
+
let keyUsages;
|
|
482
|
+
const isPublic = keyFormat === "spki";
|
|
483
|
+
const getSigUsages = () => isPublic ? ["verify"] : ["sign"];
|
|
484
|
+
const getEncUsages = () => isPublic ? ["encrypt", "wrapKey"] : ["decrypt", "unwrapKey"];
|
|
485
|
+
switch (alg) {
|
|
486
|
+
case "PS256":
|
|
487
|
+
case "PS384":
|
|
488
|
+
case "PS512":
|
|
489
|
+
algorithm = { name: "RSA-PSS", hash: `SHA-${alg.slice(-3)}` };
|
|
490
|
+
keyUsages = getSigUsages();
|
|
491
|
+
break;
|
|
492
|
+
case "RS256":
|
|
493
|
+
case "RS384":
|
|
494
|
+
case "RS512":
|
|
495
|
+
algorithm = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${alg.slice(-3)}` };
|
|
496
|
+
keyUsages = getSigUsages();
|
|
497
|
+
break;
|
|
498
|
+
case "RSA-OAEP":
|
|
499
|
+
case "RSA-OAEP-256":
|
|
500
|
+
case "RSA-OAEP-384":
|
|
501
|
+
case "RSA-OAEP-512":
|
|
502
|
+
algorithm = {
|
|
503
|
+
name: "RSA-OAEP",
|
|
504
|
+
hash: `SHA-${parseInt(alg.slice(-3), 10) || 1}`
|
|
505
|
+
};
|
|
506
|
+
keyUsages = getEncUsages();
|
|
507
|
+
break;
|
|
508
|
+
case "ES256":
|
|
509
|
+
case "ES384":
|
|
510
|
+
case "ES512": {
|
|
511
|
+
const curveMap = { ES256: "P-256", ES384: "P-384", ES512: "P-521" };
|
|
512
|
+
algorithm = { name: "ECDSA", namedCurve: curveMap[alg] };
|
|
513
|
+
keyUsages = getSigUsages();
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
case "ECDH-ES":
|
|
517
|
+
case "ECDH-ES+A128KW":
|
|
518
|
+
case "ECDH-ES+A192KW":
|
|
519
|
+
case "ECDH-ES+A256KW": {
|
|
520
|
+
try {
|
|
521
|
+
const namedCurve = options.getNamedCurve(keyData);
|
|
522
|
+
algorithm = namedCurve === "X25519" ? { name: "X25519" } : { name: "ECDH", namedCurve };
|
|
523
|
+
} catch (cause) {
|
|
524
|
+
throw new JOSENotSupported("Invalid or unsupported key format");
|
|
525
|
+
}
|
|
526
|
+
keyUsages = isPublic ? [] : ["deriveBits"];
|
|
527
|
+
break;
|
|
528
|
+
}
|
|
529
|
+
case "Ed25519":
|
|
530
|
+
case "EdDSA":
|
|
531
|
+
algorithm = { name: "Ed25519" };
|
|
532
|
+
keyUsages = getSigUsages();
|
|
533
|
+
break;
|
|
534
|
+
case "ML-DSA-44":
|
|
535
|
+
case "ML-DSA-65":
|
|
536
|
+
case "ML-DSA-87":
|
|
537
|
+
algorithm = { name: alg };
|
|
538
|
+
keyUsages = getSigUsages();
|
|
539
|
+
break;
|
|
540
|
+
default:
|
|
541
|
+
throw new JOSENotSupported('Invalid or unsupported "alg" (Algorithm) value');
|
|
542
|
+
}
|
|
543
|
+
return crypto.subtle.importKey(keyFormat, keyData, algorithm, options?.extractable ?? (isPublic ? true : false), keyUsages);
|
|
544
|
+
};
|
|
545
|
+
var processPEMData = (pem, pattern) => {
|
|
546
|
+
return decodeBase64(pem.replace(pattern, ""));
|
|
547
|
+
};
|
|
548
|
+
var fromPKCS8 = (pem, alg, options) => {
|
|
549
|
+
const keyData = processPEMData(pem, /(?:-----(?:BEGIN|END) PRIVATE KEY-----|\s)/g);
|
|
550
|
+
let opts = options;
|
|
551
|
+
if (alg?.startsWith?.("ECDH-ES")) {
|
|
552
|
+
opts ||= {};
|
|
553
|
+
opts.getNamedCurve = (keyData2) => {
|
|
554
|
+
const state = createASN1State(keyData2);
|
|
555
|
+
parsePKCS8Header(state);
|
|
556
|
+
return parseECAlgorithmIdentifier(state);
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
return genericImport("pkcs8", keyData, alg, opts);
|
|
560
|
+
};
|
|
561
|
+
var fromSPKI = (pem, alg, options) => {
|
|
562
|
+
const keyData = processPEMData(pem, /(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g);
|
|
563
|
+
let opts = options;
|
|
564
|
+
if (alg?.startsWith?.("ECDH-ES")) {
|
|
565
|
+
opts ||= {};
|
|
566
|
+
opts.getNamedCurve = (keyData2) => {
|
|
567
|
+
const state = createASN1State(keyData2);
|
|
568
|
+
parseSPKIHeader(state);
|
|
569
|
+
return parseECAlgorithmIdentifier(state);
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
return genericImport("spki", keyData, alg, opts);
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/jwk_to_key.js
|
|
576
|
+
function subtleMapping(jwk) {
|
|
577
|
+
let algorithm;
|
|
578
|
+
let keyUsages;
|
|
579
|
+
switch (jwk.kty) {
|
|
580
|
+
case "AKP": {
|
|
581
|
+
switch (jwk.alg) {
|
|
582
|
+
case "ML-DSA-44":
|
|
583
|
+
case "ML-DSA-65":
|
|
584
|
+
case "ML-DSA-87":
|
|
585
|
+
algorithm = { name: jwk.alg };
|
|
586
|
+
keyUsages = jwk.priv ? ["sign"] : ["verify"];
|
|
587
|
+
break;
|
|
588
|
+
default:
|
|
589
|
+
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
|
590
|
+
}
|
|
591
|
+
break;
|
|
592
|
+
}
|
|
593
|
+
case "RSA": {
|
|
594
|
+
switch (jwk.alg) {
|
|
595
|
+
case "PS256":
|
|
596
|
+
case "PS384":
|
|
597
|
+
case "PS512":
|
|
598
|
+
algorithm = { name: "RSA-PSS", hash: `SHA-${jwk.alg.slice(-3)}` };
|
|
599
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
600
|
+
break;
|
|
601
|
+
case "RS256":
|
|
602
|
+
case "RS384":
|
|
603
|
+
case "RS512":
|
|
604
|
+
algorithm = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${jwk.alg.slice(-3)}` };
|
|
605
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
606
|
+
break;
|
|
607
|
+
case "RSA-OAEP":
|
|
608
|
+
case "RSA-OAEP-256":
|
|
609
|
+
case "RSA-OAEP-384":
|
|
610
|
+
case "RSA-OAEP-512":
|
|
611
|
+
algorithm = {
|
|
612
|
+
name: "RSA-OAEP",
|
|
613
|
+
hash: `SHA-${parseInt(jwk.alg.slice(-3), 10) || 1}`
|
|
614
|
+
};
|
|
615
|
+
keyUsages = jwk.d ? ["decrypt", "unwrapKey"] : ["encrypt", "wrapKey"];
|
|
616
|
+
break;
|
|
617
|
+
default:
|
|
618
|
+
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
|
619
|
+
}
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
case "EC": {
|
|
623
|
+
switch (jwk.alg) {
|
|
624
|
+
case "ES256":
|
|
625
|
+
algorithm = { name: "ECDSA", namedCurve: "P-256" };
|
|
626
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
627
|
+
break;
|
|
628
|
+
case "ES384":
|
|
629
|
+
algorithm = { name: "ECDSA", namedCurve: "P-384" };
|
|
630
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
631
|
+
break;
|
|
632
|
+
case "ES512":
|
|
633
|
+
algorithm = { name: "ECDSA", namedCurve: "P-521" };
|
|
634
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
635
|
+
break;
|
|
636
|
+
case "ECDH-ES":
|
|
637
|
+
case "ECDH-ES+A128KW":
|
|
638
|
+
case "ECDH-ES+A192KW":
|
|
639
|
+
case "ECDH-ES+A256KW":
|
|
640
|
+
algorithm = { name: "ECDH", namedCurve: jwk.crv };
|
|
641
|
+
keyUsages = jwk.d ? ["deriveBits"] : [];
|
|
642
|
+
break;
|
|
643
|
+
default:
|
|
644
|
+
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
|
645
|
+
}
|
|
646
|
+
break;
|
|
647
|
+
}
|
|
648
|
+
case "OKP": {
|
|
649
|
+
switch (jwk.alg) {
|
|
650
|
+
case "Ed25519":
|
|
651
|
+
case "EdDSA":
|
|
652
|
+
algorithm = { name: "Ed25519" };
|
|
653
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
654
|
+
break;
|
|
655
|
+
case "ECDH-ES":
|
|
656
|
+
case "ECDH-ES+A128KW":
|
|
657
|
+
case "ECDH-ES+A192KW":
|
|
658
|
+
case "ECDH-ES+A256KW":
|
|
659
|
+
algorithm = { name: jwk.crv };
|
|
660
|
+
keyUsages = jwk.d ? ["deriveBits"] : [];
|
|
661
|
+
break;
|
|
662
|
+
default:
|
|
663
|
+
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
|
664
|
+
}
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
default:
|
|
668
|
+
throw new JOSENotSupported('Invalid or unsupported JWK "kty" (Key Type) Parameter value');
|
|
669
|
+
}
|
|
670
|
+
return { algorithm, keyUsages };
|
|
671
|
+
}
|
|
672
|
+
async function jwkToKey(jwk) {
|
|
673
|
+
if (!jwk.alg) {
|
|
674
|
+
throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
|
|
675
|
+
}
|
|
676
|
+
const { algorithm, keyUsages } = subtleMapping(jwk);
|
|
677
|
+
const keyData = { ...jwk };
|
|
678
|
+
if (keyData.kty !== "AKP") {
|
|
679
|
+
delete keyData.alg;
|
|
680
|
+
}
|
|
681
|
+
delete keyData.use;
|
|
682
|
+
return crypto.subtle.importKey("jwk", keyData, algorithm, jwk.ext ?? (jwk.d || jwk.priv ? false : true), jwk.key_ops ?? keyUsages);
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/key/import.js
|
|
686
|
+
async function importSPKI(spki, alg, options) {
|
|
687
|
+
if (typeof spki !== "string" || spki.indexOf("-----BEGIN PUBLIC KEY-----") !== 0) {
|
|
688
|
+
throw new TypeError('"spki" must be SPKI formatted string');
|
|
689
|
+
}
|
|
690
|
+
return fromSPKI(spki, alg, options);
|
|
691
|
+
}
|
|
692
|
+
async function importPKCS8(pkcs8, alg, options) {
|
|
693
|
+
if (typeof pkcs8 !== "string" || pkcs8.indexOf("-----BEGIN PRIVATE KEY-----") !== 0) {
|
|
694
|
+
throw new TypeError('"pkcs8" must be PKCS#8 formatted string');
|
|
695
|
+
}
|
|
696
|
+
return fromPKCS8(pkcs8, alg, options);
|
|
697
|
+
}
|
|
698
|
+
async function importJWK(jwk, alg, options) {
|
|
699
|
+
if (!isObject(jwk)) {
|
|
700
|
+
throw new TypeError("JWK must be an object");
|
|
701
|
+
}
|
|
702
|
+
let ext;
|
|
703
|
+
alg ??= jwk.alg;
|
|
704
|
+
ext ??= options?.extractable ?? jwk.ext;
|
|
705
|
+
switch (jwk.kty) {
|
|
706
|
+
case "oct":
|
|
707
|
+
if (typeof jwk.k !== "string" || !jwk.k) {
|
|
708
|
+
throw new TypeError('missing "k" (Key Value) Parameter value');
|
|
709
|
+
}
|
|
710
|
+
return decode(jwk.k);
|
|
711
|
+
case "RSA":
|
|
712
|
+
if ("oth" in jwk && jwk.oth !== undefined) {
|
|
713
|
+
throw new JOSENotSupported('RSA JWK "oth" (Other Primes Info) Parameter value is not supported');
|
|
714
|
+
}
|
|
715
|
+
return jwkToKey({ ...jwk, alg, ext });
|
|
716
|
+
case "AKP": {
|
|
717
|
+
if (typeof jwk.alg !== "string" || !jwk.alg) {
|
|
718
|
+
throw new TypeError('missing "alg" (Algorithm) Parameter value');
|
|
719
|
+
}
|
|
720
|
+
if (alg !== undefined && alg !== jwk.alg) {
|
|
721
|
+
throw new TypeError("JWK alg and alg option value mismatch");
|
|
722
|
+
}
|
|
723
|
+
return jwkToKey({ ...jwk, ext });
|
|
724
|
+
}
|
|
725
|
+
case "EC":
|
|
726
|
+
case "OKP":
|
|
727
|
+
return jwkToKey({ ...jwk, alg, ext });
|
|
728
|
+
default:
|
|
729
|
+
throw new JOSENotSupported('Unsupported "kty" (Key Type) Parameter value');
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/validate_crit.js
|
|
734
|
+
function validateCrit(Err, recognizedDefault, recognizedOption, protectedHeader, joseHeader) {
|
|
735
|
+
if (joseHeader.crit !== undefined && protectedHeader?.crit === undefined) {
|
|
736
|
+
throw new Err('"crit" (Critical) Header Parameter MUST be integrity protected');
|
|
737
|
+
}
|
|
738
|
+
if (!protectedHeader || protectedHeader.crit === undefined) {
|
|
739
|
+
return new Set;
|
|
740
|
+
}
|
|
741
|
+
if (!Array.isArray(protectedHeader.crit) || protectedHeader.crit.length === 0 || protectedHeader.crit.some((input) => typeof input !== "string" || input.length === 0)) {
|
|
742
|
+
throw new Err('"crit" (Critical) Header Parameter MUST be an array of non-empty strings when present');
|
|
743
|
+
}
|
|
744
|
+
let recognized;
|
|
745
|
+
if (recognizedOption !== undefined) {
|
|
746
|
+
recognized = new Map([...Object.entries(recognizedOption), ...recognizedDefault.entries()]);
|
|
747
|
+
} else {
|
|
748
|
+
recognized = recognizedDefault;
|
|
749
|
+
}
|
|
750
|
+
for (const parameter of protectedHeader.crit) {
|
|
751
|
+
if (!recognized.has(parameter)) {
|
|
752
|
+
throw new JOSENotSupported(`Extension Header Parameter "${parameter}" is not recognized`);
|
|
753
|
+
}
|
|
754
|
+
if (joseHeader[parameter] === undefined) {
|
|
755
|
+
throw new Err(`Extension Header Parameter "${parameter}" is missing`);
|
|
756
|
+
}
|
|
757
|
+
if (recognized.get(parameter) && protectedHeader[parameter] === undefined) {
|
|
758
|
+
throw new Err(`Extension Header Parameter "${parameter}" MUST be integrity protected`);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
return new Set(protectedHeader.crit);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/validate_algorithms.js
|
|
765
|
+
function validateAlgorithms(option, algorithms) {
|
|
766
|
+
if (algorithms !== undefined && (!Array.isArray(algorithms) || algorithms.some((s) => typeof s !== "string"))) {
|
|
767
|
+
throw new TypeError(`"${option}" option must be an array of strings`);
|
|
768
|
+
}
|
|
769
|
+
if (!algorithms) {
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
return new Set(algorithms);
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/is_jwk.js
|
|
776
|
+
var isJWK = (key) => isObject(key) && typeof key.kty === "string";
|
|
777
|
+
var isPrivateJWK = (key) => key.kty !== "oct" && (key.kty === "AKP" && typeof key.priv === "string" || typeof key.d === "string");
|
|
778
|
+
var isPublicJWK = (key) => key.kty !== "oct" && key.d === undefined && key.priv === undefined;
|
|
779
|
+
var isSecretJWK = (key) => key.kty === "oct" && typeof key.k === "string";
|
|
780
|
+
|
|
781
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/normalize_key.js
|
|
782
|
+
var cache;
|
|
783
|
+
var handleJWK = async (key, jwk, alg, freeze = false) => {
|
|
784
|
+
cache ||= new WeakMap;
|
|
785
|
+
let cached = cache.get(key);
|
|
786
|
+
if (cached?.[alg]) {
|
|
787
|
+
return cached[alg];
|
|
788
|
+
}
|
|
789
|
+
const cryptoKey = await jwkToKey({ ...jwk, alg });
|
|
790
|
+
if (freeze)
|
|
791
|
+
Object.freeze(key);
|
|
792
|
+
if (!cached) {
|
|
793
|
+
cache.set(key, { [alg]: cryptoKey });
|
|
794
|
+
} else {
|
|
795
|
+
cached[alg] = cryptoKey;
|
|
796
|
+
}
|
|
797
|
+
return cryptoKey;
|
|
798
|
+
};
|
|
799
|
+
var handleKeyObject = (keyObject, alg) => {
|
|
800
|
+
cache ||= new WeakMap;
|
|
801
|
+
let cached = cache.get(keyObject);
|
|
802
|
+
if (cached?.[alg]) {
|
|
803
|
+
return cached[alg];
|
|
804
|
+
}
|
|
805
|
+
const isPublic = keyObject.type === "public";
|
|
806
|
+
const extractable = isPublic ? true : false;
|
|
807
|
+
let cryptoKey;
|
|
808
|
+
if (keyObject.asymmetricKeyType === "x25519") {
|
|
809
|
+
switch (alg) {
|
|
810
|
+
case "ECDH-ES":
|
|
811
|
+
case "ECDH-ES+A128KW":
|
|
812
|
+
case "ECDH-ES+A192KW":
|
|
813
|
+
case "ECDH-ES+A256KW":
|
|
814
|
+
break;
|
|
815
|
+
default:
|
|
816
|
+
throw new TypeError("given KeyObject instance cannot be used for this algorithm");
|
|
817
|
+
}
|
|
818
|
+
cryptoKey = keyObject.toCryptoKey(keyObject.asymmetricKeyType, extractable, isPublic ? [] : ["deriveBits"]);
|
|
819
|
+
}
|
|
820
|
+
if (keyObject.asymmetricKeyType === "ed25519") {
|
|
821
|
+
if (alg !== "EdDSA" && alg !== "Ed25519") {
|
|
822
|
+
throw new TypeError("given KeyObject instance cannot be used for this algorithm");
|
|
823
|
+
}
|
|
824
|
+
cryptoKey = keyObject.toCryptoKey(keyObject.asymmetricKeyType, extractable, [
|
|
825
|
+
isPublic ? "verify" : "sign"
|
|
826
|
+
]);
|
|
827
|
+
}
|
|
828
|
+
switch (keyObject.asymmetricKeyType) {
|
|
829
|
+
case "ml-dsa-44":
|
|
830
|
+
case "ml-dsa-65":
|
|
831
|
+
case "ml-dsa-87": {
|
|
832
|
+
if (alg !== keyObject.asymmetricKeyType.toUpperCase()) {
|
|
833
|
+
throw new TypeError("given KeyObject instance cannot be used for this algorithm");
|
|
834
|
+
}
|
|
835
|
+
cryptoKey = keyObject.toCryptoKey(keyObject.asymmetricKeyType, extractable, [
|
|
836
|
+
isPublic ? "verify" : "sign"
|
|
837
|
+
]);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
if (keyObject.asymmetricKeyType === "rsa") {
|
|
841
|
+
let hash;
|
|
842
|
+
switch (alg) {
|
|
843
|
+
case "RSA-OAEP":
|
|
844
|
+
hash = "SHA-1";
|
|
845
|
+
break;
|
|
846
|
+
case "RS256":
|
|
847
|
+
case "PS256":
|
|
848
|
+
case "RSA-OAEP-256":
|
|
849
|
+
hash = "SHA-256";
|
|
850
|
+
break;
|
|
851
|
+
case "RS384":
|
|
852
|
+
case "PS384":
|
|
853
|
+
case "RSA-OAEP-384":
|
|
854
|
+
hash = "SHA-384";
|
|
855
|
+
break;
|
|
856
|
+
case "RS512":
|
|
857
|
+
case "PS512":
|
|
858
|
+
case "RSA-OAEP-512":
|
|
859
|
+
hash = "SHA-512";
|
|
860
|
+
break;
|
|
861
|
+
default:
|
|
862
|
+
throw new TypeError("given KeyObject instance cannot be used for this algorithm");
|
|
863
|
+
}
|
|
864
|
+
if (alg.startsWith("RSA-OAEP")) {
|
|
865
|
+
return keyObject.toCryptoKey({
|
|
866
|
+
name: "RSA-OAEP",
|
|
867
|
+
hash
|
|
868
|
+
}, extractable, isPublic ? ["encrypt"] : ["decrypt"]);
|
|
869
|
+
}
|
|
870
|
+
cryptoKey = keyObject.toCryptoKey({
|
|
871
|
+
name: alg.startsWith("PS") ? "RSA-PSS" : "RSASSA-PKCS1-v1_5",
|
|
872
|
+
hash
|
|
873
|
+
}, extractable, [isPublic ? "verify" : "sign"]);
|
|
874
|
+
}
|
|
875
|
+
if (keyObject.asymmetricKeyType === "ec") {
|
|
876
|
+
const nist = new Map([
|
|
877
|
+
["prime256v1", "P-256"],
|
|
878
|
+
["secp384r1", "P-384"],
|
|
879
|
+
["secp521r1", "P-521"]
|
|
880
|
+
]);
|
|
881
|
+
const namedCurve = nist.get(keyObject.asymmetricKeyDetails?.namedCurve);
|
|
882
|
+
if (!namedCurve) {
|
|
883
|
+
throw new TypeError("given KeyObject instance cannot be used for this algorithm");
|
|
884
|
+
}
|
|
885
|
+
if (alg === "ES256" && namedCurve === "P-256") {
|
|
886
|
+
cryptoKey = keyObject.toCryptoKey({
|
|
887
|
+
name: "ECDSA",
|
|
888
|
+
namedCurve
|
|
889
|
+
}, extractable, [isPublic ? "verify" : "sign"]);
|
|
890
|
+
}
|
|
891
|
+
if (alg === "ES384" && namedCurve === "P-384") {
|
|
892
|
+
cryptoKey = keyObject.toCryptoKey({
|
|
893
|
+
name: "ECDSA",
|
|
894
|
+
namedCurve
|
|
895
|
+
}, extractable, [isPublic ? "verify" : "sign"]);
|
|
896
|
+
}
|
|
897
|
+
if (alg === "ES512" && namedCurve === "P-521") {
|
|
898
|
+
cryptoKey = keyObject.toCryptoKey({
|
|
899
|
+
name: "ECDSA",
|
|
900
|
+
namedCurve
|
|
901
|
+
}, extractable, [isPublic ? "verify" : "sign"]);
|
|
902
|
+
}
|
|
903
|
+
if (alg.startsWith("ECDH-ES")) {
|
|
904
|
+
cryptoKey = keyObject.toCryptoKey({
|
|
905
|
+
name: "ECDH",
|
|
906
|
+
namedCurve
|
|
907
|
+
}, extractable, isPublic ? [] : ["deriveBits"]);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
if (!cryptoKey) {
|
|
911
|
+
throw new TypeError("given KeyObject instance cannot be used for this algorithm");
|
|
912
|
+
}
|
|
913
|
+
if (!cached) {
|
|
914
|
+
cache.set(keyObject, { [alg]: cryptoKey });
|
|
915
|
+
} else {
|
|
916
|
+
cached[alg] = cryptoKey;
|
|
917
|
+
}
|
|
918
|
+
return cryptoKey;
|
|
919
|
+
};
|
|
920
|
+
async function normalizeKey(key, alg) {
|
|
921
|
+
if (key instanceof Uint8Array) {
|
|
922
|
+
return key;
|
|
923
|
+
}
|
|
924
|
+
if (isCryptoKey(key)) {
|
|
925
|
+
return key;
|
|
926
|
+
}
|
|
927
|
+
if (isKeyObject(key)) {
|
|
928
|
+
if (key.type === "secret") {
|
|
929
|
+
return key.export();
|
|
930
|
+
}
|
|
931
|
+
if ("toCryptoKey" in key && typeof key.toCryptoKey === "function") {
|
|
932
|
+
try {
|
|
933
|
+
return handleKeyObject(key, alg);
|
|
934
|
+
} catch (err) {
|
|
935
|
+
if (err instanceof TypeError) {
|
|
936
|
+
throw err;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
let jwk = key.export({ format: "jwk" });
|
|
941
|
+
return handleJWK(key, jwk, alg);
|
|
942
|
+
}
|
|
943
|
+
if (isJWK(key)) {
|
|
944
|
+
if (key.k) {
|
|
945
|
+
return decode(key.k);
|
|
946
|
+
}
|
|
947
|
+
return handleJWK(key, key, alg, true);
|
|
948
|
+
}
|
|
949
|
+
throw new Error("unreachable");
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/check_key_type.js
|
|
953
|
+
var tag = (key) => key?.[Symbol.toStringTag];
|
|
954
|
+
var jwkMatchesOp = (alg, key, usage) => {
|
|
955
|
+
if (key.use !== undefined) {
|
|
956
|
+
let expected;
|
|
957
|
+
switch (usage) {
|
|
958
|
+
case "sign":
|
|
959
|
+
case "verify":
|
|
960
|
+
expected = "sig";
|
|
961
|
+
break;
|
|
962
|
+
case "encrypt":
|
|
963
|
+
case "decrypt":
|
|
964
|
+
expected = "enc";
|
|
965
|
+
break;
|
|
966
|
+
}
|
|
967
|
+
if (key.use !== expected) {
|
|
968
|
+
throw new TypeError(`Invalid key for this operation, its "use" must be "${expected}" when present`);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
if (key.alg !== undefined && key.alg !== alg) {
|
|
972
|
+
throw new TypeError(`Invalid key for this operation, its "alg" must be "${alg}" when present`);
|
|
973
|
+
}
|
|
974
|
+
if (Array.isArray(key.key_ops)) {
|
|
975
|
+
let expectedKeyOp;
|
|
976
|
+
switch (true) {
|
|
977
|
+
case (usage === "sign" || usage === "verify"):
|
|
978
|
+
case alg === "dir":
|
|
979
|
+
case alg.includes("CBC-HS"):
|
|
980
|
+
expectedKeyOp = usage;
|
|
981
|
+
break;
|
|
982
|
+
case alg.startsWith("PBES2"):
|
|
983
|
+
expectedKeyOp = "deriveBits";
|
|
984
|
+
break;
|
|
985
|
+
case /^A\d{3}(?:GCM)?(?:KW)?$/.test(alg):
|
|
986
|
+
if (!alg.includes("GCM") && alg.endsWith("KW")) {
|
|
987
|
+
expectedKeyOp = usage === "encrypt" ? "wrapKey" : "unwrapKey";
|
|
988
|
+
} else {
|
|
989
|
+
expectedKeyOp = usage;
|
|
990
|
+
}
|
|
991
|
+
break;
|
|
992
|
+
case (usage === "encrypt" && alg.startsWith("RSA")):
|
|
993
|
+
expectedKeyOp = "wrapKey";
|
|
994
|
+
break;
|
|
995
|
+
case usage === "decrypt":
|
|
996
|
+
expectedKeyOp = alg.startsWith("RSA") ? "unwrapKey" : "deriveBits";
|
|
997
|
+
break;
|
|
998
|
+
}
|
|
999
|
+
if (expectedKeyOp && key.key_ops?.includes?.(expectedKeyOp) === false) {
|
|
1000
|
+
throw new TypeError(`Invalid key for this operation, its "key_ops" must include "${expectedKeyOp}" when present`);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
return true;
|
|
1004
|
+
};
|
|
1005
|
+
var symmetricTypeCheck = (alg, key, usage) => {
|
|
1006
|
+
if (key instanceof Uint8Array)
|
|
1007
|
+
return;
|
|
1008
|
+
if (isJWK(key)) {
|
|
1009
|
+
if (isSecretJWK(key) && jwkMatchesOp(alg, key, usage))
|
|
1010
|
+
return;
|
|
1011
|
+
throw new TypeError(`JSON Web Key for symmetric algorithms must have JWK "kty" (Key Type) equal to "oct" and the JWK "k" (Key Value) present`);
|
|
1012
|
+
}
|
|
1013
|
+
if (!isKeyLike(key)) {
|
|
1014
|
+
throw new TypeError(withAlg(alg, key, "CryptoKey", "KeyObject", "JSON Web Key", "Uint8Array"));
|
|
1015
|
+
}
|
|
1016
|
+
if (key.type !== "secret") {
|
|
1017
|
+
throw new TypeError(`${tag(key)} instances for symmetric algorithms must be of type "secret"`);
|
|
1018
|
+
}
|
|
1019
|
+
};
|
|
1020
|
+
var asymmetricTypeCheck = (alg, key, usage) => {
|
|
1021
|
+
if (isJWK(key)) {
|
|
1022
|
+
switch (usage) {
|
|
1023
|
+
case "decrypt":
|
|
1024
|
+
case "sign":
|
|
1025
|
+
if (isPrivateJWK(key) && jwkMatchesOp(alg, key, usage))
|
|
1026
|
+
return;
|
|
1027
|
+
throw new TypeError(`JSON Web Key for this operation must be a private JWK`);
|
|
1028
|
+
case "encrypt":
|
|
1029
|
+
case "verify":
|
|
1030
|
+
if (isPublicJWK(key) && jwkMatchesOp(alg, key, usage))
|
|
1031
|
+
return;
|
|
1032
|
+
throw new TypeError(`JSON Web Key for this operation must be a public JWK`);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
if (!isKeyLike(key)) {
|
|
1036
|
+
throw new TypeError(withAlg(alg, key, "CryptoKey", "KeyObject", "JSON Web Key"));
|
|
1037
|
+
}
|
|
1038
|
+
if (key.type === "secret") {
|
|
1039
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithms must not be of type "secret"`);
|
|
1040
|
+
}
|
|
1041
|
+
if (key.type === "public") {
|
|
1042
|
+
switch (usage) {
|
|
1043
|
+
case "sign":
|
|
1044
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm signing must be of type "private"`);
|
|
1045
|
+
case "decrypt":
|
|
1046
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm decryption must be of type "private"`);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
if (key.type === "private") {
|
|
1050
|
+
switch (usage) {
|
|
1051
|
+
case "verify":
|
|
1052
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm verifying must be of type "public"`);
|
|
1053
|
+
case "encrypt":
|
|
1054
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm encryption must be of type "public"`);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
};
|
|
1058
|
+
function checkKeyType(alg, key, usage) {
|
|
1059
|
+
switch (alg.substring(0, 2)) {
|
|
1060
|
+
case "A1":
|
|
1061
|
+
case "A2":
|
|
1062
|
+
case "di":
|
|
1063
|
+
case "HS":
|
|
1064
|
+
case "PB":
|
|
1065
|
+
symmetricTypeCheck(alg, key, usage);
|
|
1066
|
+
break;
|
|
1067
|
+
default:
|
|
1068
|
+
asymmetricTypeCheck(alg, key, usage);
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/subtle_dsa.js
|
|
1073
|
+
function subtleAlgorithm(alg, algorithm) {
|
|
1074
|
+
const hash = `SHA-${alg.slice(-3)}`;
|
|
1075
|
+
switch (alg) {
|
|
1076
|
+
case "HS256":
|
|
1077
|
+
case "HS384":
|
|
1078
|
+
case "HS512":
|
|
1079
|
+
return { hash, name: "HMAC" };
|
|
1080
|
+
case "PS256":
|
|
1081
|
+
case "PS384":
|
|
1082
|
+
case "PS512":
|
|
1083
|
+
return { hash, name: "RSA-PSS", saltLength: parseInt(alg.slice(-3), 10) >> 3 };
|
|
1084
|
+
case "RS256":
|
|
1085
|
+
case "RS384":
|
|
1086
|
+
case "RS512":
|
|
1087
|
+
return { hash, name: "RSASSA-PKCS1-v1_5" };
|
|
1088
|
+
case "ES256":
|
|
1089
|
+
case "ES384":
|
|
1090
|
+
case "ES512":
|
|
1091
|
+
return { hash, name: "ECDSA", namedCurve: algorithm.namedCurve };
|
|
1092
|
+
case "Ed25519":
|
|
1093
|
+
case "EdDSA":
|
|
1094
|
+
return { name: "Ed25519" };
|
|
1095
|
+
case "ML-DSA-44":
|
|
1096
|
+
case "ML-DSA-65":
|
|
1097
|
+
case "ML-DSA-87":
|
|
1098
|
+
return { name: alg };
|
|
1099
|
+
default:
|
|
1100
|
+
throw new JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/get_sign_verify_key.js
|
|
1105
|
+
async function getSigKey(alg, key, usage) {
|
|
1106
|
+
if (key instanceof Uint8Array) {
|
|
1107
|
+
if (!alg.startsWith("HS")) {
|
|
1108
|
+
throw new TypeError(invalidKeyInput(key, "CryptoKey", "KeyObject", "JSON Web Key"));
|
|
1109
|
+
}
|
|
1110
|
+
return crypto.subtle.importKey("raw", key, { hash: `SHA-${alg.slice(-3)}`, name: "HMAC" }, false, [usage]);
|
|
1111
|
+
}
|
|
1112
|
+
checkSigCryptoKey(key, alg, usage);
|
|
1113
|
+
return key;
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/verify.js
|
|
1117
|
+
async function verify(alg, key, signature, data) {
|
|
1118
|
+
const cryptoKey = await getSigKey(alg, key, "verify");
|
|
1119
|
+
checkKeyLength(alg, cryptoKey);
|
|
1120
|
+
const algorithm = subtleAlgorithm(alg, cryptoKey.algorithm);
|
|
1121
|
+
try {
|
|
1122
|
+
return await crypto.subtle.verify(algorithm, cryptoKey, signature, data);
|
|
1123
|
+
} catch {
|
|
1124
|
+
return false;
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/jws/flattened/verify.js
|
|
1129
|
+
async function flattenedVerify(jws, key, options) {
|
|
1130
|
+
if (!isObject(jws)) {
|
|
1131
|
+
throw new JWSInvalid("Flattened JWS must be an object");
|
|
1132
|
+
}
|
|
1133
|
+
if (jws.protected === undefined && jws.header === undefined) {
|
|
1134
|
+
throw new JWSInvalid('Flattened JWS must have either of the "protected" or "header" members');
|
|
1135
|
+
}
|
|
1136
|
+
if (jws.protected !== undefined && typeof jws.protected !== "string") {
|
|
1137
|
+
throw new JWSInvalid("JWS Protected Header incorrect type");
|
|
1138
|
+
}
|
|
1139
|
+
if (jws.payload === undefined) {
|
|
1140
|
+
throw new JWSInvalid("JWS Payload missing");
|
|
1141
|
+
}
|
|
1142
|
+
if (typeof jws.signature !== "string") {
|
|
1143
|
+
throw new JWSInvalid("JWS Signature missing or incorrect type");
|
|
1144
|
+
}
|
|
1145
|
+
if (jws.header !== undefined && !isObject(jws.header)) {
|
|
1146
|
+
throw new JWSInvalid("JWS Unprotected Header incorrect type");
|
|
1147
|
+
}
|
|
1148
|
+
let parsedProt = {};
|
|
1149
|
+
if (jws.protected) {
|
|
1150
|
+
try {
|
|
1151
|
+
const protectedHeader = decode(jws.protected);
|
|
1152
|
+
parsedProt = JSON.parse(decoder.decode(protectedHeader));
|
|
1153
|
+
} catch {
|
|
1154
|
+
throw new JWSInvalid("JWS Protected Header is invalid");
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
if (!isDisjoint(parsedProt, jws.header)) {
|
|
1158
|
+
throw new JWSInvalid("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
|
|
1159
|
+
}
|
|
1160
|
+
const joseHeader = {
|
|
1161
|
+
...parsedProt,
|
|
1162
|
+
...jws.header
|
|
1163
|
+
};
|
|
1164
|
+
const extensions = validateCrit(JWSInvalid, new Map([["b64", true]]), options?.crit, parsedProt, joseHeader);
|
|
1165
|
+
let b64 = true;
|
|
1166
|
+
if (extensions.has("b64")) {
|
|
1167
|
+
b64 = parsedProt.b64;
|
|
1168
|
+
if (typeof b64 !== "boolean") {
|
|
1169
|
+
throw new JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
const { alg } = joseHeader;
|
|
1173
|
+
if (typeof alg !== "string" || !alg) {
|
|
1174
|
+
throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
|
1175
|
+
}
|
|
1176
|
+
const algorithms = options && validateAlgorithms("algorithms", options.algorithms);
|
|
1177
|
+
if (algorithms && !algorithms.has(alg)) {
|
|
1178
|
+
throw new JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter value not allowed');
|
|
1179
|
+
}
|
|
1180
|
+
if (b64) {
|
|
1181
|
+
if (typeof jws.payload !== "string") {
|
|
1182
|
+
throw new JWSInvalid("JWS Payload must be a string");
|
|
1183
|
+
}
|
|
1184
|
+
} else if (typeof jws.payload !== "string" && !(jws.payload instanceof Uint8Array)) {
|
|
1185
|
+
throw new JWSInvalid("JWS Payload must be a string or an Uint8Array instance");
|
|
1186
|
+
}
|
|
1187
|
+
let resolvedKey = false;
|
|
1188
|
+
if (typeof key === "function") {
|
|
1189
|
+
key = await key(parsedProt, jws);
|
|
1190
|
+
resolvedKey = true;
|
|
1191
|
+
}
|
|
1192
|
+
checkKeyType(alg, key, "verify");
|
|
1193
|
+
const data = concat(jws.protected !== undefined ? encode(jws.protected) : new Uint8Array, encode("."), typeof jws.payload === "string" ? b64 ? encode(jws.payload) : encoder.encode(jws.payload) : jws.payload);
|
|
1194
|
+
let signature;
|
|
1195
|
+
try {
|
|
1196
|
+
signature = decode(jws.signature);
|
|
1197
|
+
} catch {
|
|
1198
|
+
throw new JWSInvalid("Failed to base64url decode the signature");
|
|
1199
|
+
}
|
|
1200
|
+
const k = await normalizeKey(key, alg);
|
|
1201
|
+
const verified = await verify(alg, k, signature, data);
|
|
1202
|
+
if (!verified) {
|
|
1203
|
+
throw new JWSSignatureVerificationFailed;
|
|
1204
|
+
}
|
|
1205
|
+
let payload;
|
|
1206
|
+
if (b64) {
|
|
1207
|
+
try {
|
|
1208
|
+
payload = decode(jws.payload);
|
|
1209
|
+
} catch {
|
|
1210
|
+
throw new JWSInvalid("Failed to base64url decode the payload");
|
|
1211
|
+
}
|
|
1212
|
+
} else if (typeof jws.payload === "string") {
|
|
1213
|
+
payload = encoder.encode(jws.payload);
|
|
1214
|
+
} else {
|
|
1215
|
+
payload = jws.payload;
|
|
1216
|
+
}
|
|
1217
|
+
const result = { payload };
|
|
1218
|
+
if (jws.protected !== undefined) {
|
|
1219
|
+
result.protectedHeader = parsedProt;
|
|
1220
|
+
}
|
|
1221
|
+
if (jws.header !== undefined) {
|
|
1222
|
+
result.unprotectedHeader = jws.header;
|
|
1223
|
+
}
|
|
1224
|
+
if (resolvedKey) {
|
|
1225
|
+
return { ...result, key: k };
|
|
1226
|
+
}
|
|
1227
|
+
return result;
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/jws/compact/verify.js
|
|
1231
|
+
async function compactVerify(jws, key, options) {
|
|
1232
|
+
if (jws instanceof Uint8Array) {
|
|
1233
|
+
jws = decoder.decode(jws);
|
|
1234
|
+
}
|
|
1235
|
+
if (typeof jws !== "string") {
|
|
1236
|
+
throw new JWSInvalid("Compact JWS must be a string or Uint8Array");
|
|
1237
|
+
}
|
|
1238
|
+
const { 0: protectedHeader, 1: payload, 2: signature, length } = jws.split(".");
|
|
1239
|
+
if (length !== 3) {
|
|
1240
|
+
throw new JWSInvalid("Invalid Compact JWS");
|
|
1241
|
+
}
|
|
1242
|
+
const verified = await flattenedVerify({ payload, protected: protectedHeader, signature }, key, options);
|
|
1243
|
+
const result = { payload: verified.payload, protectedHeader: verified.protectedHeader };
|
|
1244
|
+
if (typeof key === "function") {
|
|
1245
|
+
return { ...result, key: verified.key };
|
|
1246
|
+
}
|
|
1247
|
+
return result;
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/jwt_claims_set.js
|
|
1251
|
+
var epoch = (date) => Math.floor(date.getTime() / 1000);
|
|
1252
|
+
var minute = 60;
|
|
1253
|
+
var hour = minute * 60;
|
|
1254
|
+
var day = hour * 24;
|
|
1255
|
+
var week = day * 7;
|
|
1256
|
+
var year = day * 365.25;
|
|
1257
|
+
var REGEX = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i;
|
|
1258
|
+
function secs(str) {
|
|
1259
|
+
const matched = REGEX.exec(str);
|
|
1260
|
+
if (!matched || matched[4] && matched[1]) {
|
|
1261
|
+
throw new TypeError("Invalid time period format");
|
|
1262
|
+
}
|
|
1263
|
+
const value = parseFloat(matched[2]);
|
|
1264
|
+
const unit = matched[3].toLowerCase();
|
|
1265
|
+
let numericDate;
|
|
1266
|
+
switch (unit) {
|
|
1267
|
+
case "sec":
|
|
1268
|
+
case "secs":
|
|
1269
|
+
case "second":
|
|
1270
|
+
case "seconds":
|
|
1271
|
+
case "s":
|
|
1272
|
+
numericDate = Math.round(value);
|
|
1273
|
+
break;
|
|
1274
|
+
case "minute":
|
|
1275
|
+
case "minutes":
|
|
1276
|
+
case "min":
|
|
1277
|
+
case "mins":
|
|
1278
|
+
case "m":
|
|
1279
|
+
numericDate = Math.round(value * minute);
|
|
1280
|
+
break;
|
|
1281
|
+
case "hour":
|
|
1282
|
+
case "hours":
|
|
1283
|
+
case "hr":
|
|
1284
|
+
case "hrs":
|
|
1285
|
+
case "h":
|
|
1286
|
+
numericDate = Math.round(value * hour);
|
|
1287
|
+
break;
|
|
1288
|
+
case "day":
|
|
1289
|
+
case "days":
|
|
1290
|
+
case "d":
|
|
1291
|
+
numericDate = Math.round(value * day);
|
|
1292
|
+
break;
|
|
1293
|
+
case "week":
|
|
1294
|
+
case "weeks":
|
|
1295
|
+
case "w":
|
|
1296
|
+
numericDate = Math.round(value * week);
|
|
1297
|
+
break;
|
|
1298
|
+
default:
|
|
1299
|
+
numericDate = Math.round(value * year);
|
|
1300
|
+
break;
|
|
1301
|
+
}
|
|
1302
|
+
if (matched[1] === "-" || matched[4] === "ago") {
|
|
1303
|
+
return -numericDate;
|
|
1304
|
+
}
|
|
1305
|
+
return numericDate;
|
|
1306
|
+
}
|
|
1307
|
+
function validateInput(label, input) {
|
|
1308
|
+
if (!Number.isFinite(input)) {
|
|
1309
|
+
throw new TypeError(`Invalid ${label} input`);
|
|
1310
|
+
}
|
|
1311
|
+
return input;
|
|
1312
|
+
}
|
|
1313
|
+
var normalizeTyp = (value) => {
|
|
1314
|
+
if (value.includes("/")) {
|
|
1315
|
+
return value.toLowerCase();
|
|
1316
|
+
}
|
|
1317
|
+
return `application/${value.toLowerCase()}`;
|
|
1318
|
+
};
|
|
1319
|
+
var checkAudiencePresence = (audPayload, audOption) => {
|
|
1320
|
+
if (typeof audPayload === "string") {
|
|
1321
|
+
return audOption.includes(audPayload);
|
|
1322
|
+
}
|
|
1323
|
+
if (Array.isArray(audPayload)) {
|
|
1324
|
+
return audOption.some(Set.prototype.has.bind(new Set(audPayload)));
|
|
1325
|
+
}
|
|
1326
|
+
return false;
|
|
1327
|
+
};
|
|
1328
|
+
function validateClaimsSet(protectedHeader, encodedPayload, options = {}) {
|
|
1329
|
+
let payload;
|
|
1330
|
+
try {
|
|
1331
|
+
payload = JSON.parse(decoder.decode(encodedPayload));
|
|
1332
|
+
} catch {}
|
|
1333
|
+
if (!isObject(payload)) {
|
|
1334
|
+
throw new JWTInvalid("JWT Claims Set must be a top-level JSON object");
|
|
1335
|
+
}
|
|
1336
|
+
const { typ } = options;
|
|
1337
|
+
if (typ && (typeof protectedHeader.typ !== "string" || normalizeTyp(protectedHeader.typ) !== normalizeTyp(typ))) {
|
|
1338
|
+
throw new JWTClaimValidationFailed('unexpected "typ" JWT header value', payload, "typ", "check_failed");
|
|
1339
|
+
}
|
|
1340
|
+
const { requiredClaims = [], issuer, subject, audience, maxTokenAge } = options;
|
|
1341
|
+
const presenceCheck = [...requiredClaims];
|
|
1342
|
+
if (maxTokenAge !== undefined)
|
|
1343
|
+
presenceCheck.push("iat");
|
|
1344
|
+
if (audience !== undefined)
|
|
1345
|
+
presenceCheck.push("aud");
|
|
1346
|
+
if (subject !== undefined)
|
|
1347
|
+
presenceCheck.push("sub");
|
|
1348
|
+
if (issuer !== undefined)
|
|
1349
|
+
presenceCheck.push("iss");
|
|
1350
|
+
for (const claim of new Set(presenceCheck.reverse())) {
|
|
1351
|
+
if (!(claim in payload)) {
|
|
1352
|
+
throw new JWTClaimValidationFailed(`missing required "${claim}" claim`, payload, claim, "missing");
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
if (issuer && !(Array.isArray(issuer) ? issuer : [issuer]).includes(payload.iss)) {
|
|
1356
|
+
throw new JWTClaimValidationFailed('unexpected "iss" claim value', payload, "iss", "check_failed");
|
|
1357
|
+
}
|
|
1358
|
+
if (subject && payload.sub !== subject) {
|
|
1359
|
+
throw new JWTClaimValidationFailed('unexpected "sub" claim value', payload, "sub", "check_failed");
|
|
1360
|
+
}
|
|
1361
|
+
if (audience && !checkAudiencePresence(payload.aud, typeof audience === "string" ? [audience] : audience)) {
|
|
1362
|
+
throw new JWTClaimValidationFailed('unexpected "aud" claim value', payload, "aud", "check_failed");
|
|
1363
|
+
}
|
|
1364
|
+
let tolerance;
|
|
1365
|
+
switch (typeof options.clockTolerance) {
|
|
1366
|
+
case "string":
|
|
1367
|
+
tolerance = secs(options.clockTolerance);
|
|
1368
|
+
break;
|
|
1369
|
+
case "number":
|
|
1370
|
+
tolerance = options.clockTolerance;
|
|
1371
|
+
break;
|
|
1372
|
+
case "undefined":
|
|
1373
|
+
tolerance = 0;
|
|
1374
|
+
break;
|
|
1375
|
+
default:
|
|
1376
|
+
throw new TypeError("Invalid clockTolerance option type");
|
|
1377
|
+
}
|
|
1378
|
+
const { currentDate } = options;
|
|
1379
|
+
const now = epoch(currentDate || new Date);
|
|
1380
|
+
if ((payload.iat !== undefined || maxTokenAge) && typeof payload.iat !== "number") {
|
|
1381
|
+
throw new JWTClaimValidationFailed('"iat" claim must be a number', payload, "iat", "invalid");
|
|
1382
|
+
}
|
|
1383
|
+
if (payload.nbf !== undefined) {
|
|
1384
|
+
if (typeof payload.nbf !== "number") {
|
|
1385
|
+
throw new JWTClaimValidationFailed('"nbf" claim must be a number', payload, "nbf", "invalid");
|
|
1386
|
+
}
|
|
1387
|
+
if (payload.nbf > now + tolerance) {
|
|
1388
|
+
throw new JWTClaimValidationFailed('"nbf" claim timestamp check failed', payload, "nbf", "check_failed");
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
if (payload.exp !== undefined) {
|
|
1392
|
+
if (typeof payload.exp !== "number") {
|
|
1393
|
+
throw new JWTClaimValidationFailed('"exp" claim must be a number', payload, "exp", "invalid");
|
|
1394
|
+
}
|
|
1395
|
+
if (payload.exp <= now - tolerance) {
|
|
1396
|
+
throw new JWTExpired('"exp" claim timestamp check failed', payload, "exp", "check_failed");
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
if (maxTokenAge) {
|
|
1400
|
+
const age = now - payload.iat;
|
|
1401
|
+
const max = typeof maxTokenAge === "number" ? maxTokenAge : secs(maxTokenAge);
|
|
1402
|
+
if (age - tolerance > max) {
|
|
1403
|
+
throw new JWTExpired('"iat" claim timestamp check failed (too far in the past)', payload, "iat", "check_failed");
|
|
1404
|
+
}
|
|
1405
|
+
if (age < 0 - tolerance) {
|
|
1406
|
+
throw new JWTClaimValidationFailed('"iat" claim timestamp check failed (it should be in the past)', payload, "iat", "check_failed");
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
return payload;
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
class JWTClaimsBuilder {
|
|
1413
|
+
#payload;
|
|
1414
|
+
constructor(payload) {
|
|
1415
|
+
if (!isObject(payload)) {
|
|
1416
|
+
throw new TypeError("JWT Claims Set MUST be an object");
|
|
1417
|
+
}
|
|
1418
|
+
this.#payload = structuredClone(payload);
|
|
1419
|
+
}
|
|
1420
|
+
data() {
|
|
1421
|
+
return encoder.encode(JSON.stringify(this.#payload));
|
|
1422
|
+
}
|
|
1423
|
+
get iss() {
|
|
1424
|
+
return this.#payload.iss;
|
|
1425
|
+
}
|
|
1426
|
+
set iss(value) {
|
|
1427
|
+
this.#payload.iss = value;
|
|
1428
|
+
}
|
|
1429
|
+
get sub() {
|
|
1430
|
+
return this.#payload.sub;
|
|
1431
|
+
}
|
|
1432
|
+
set sub(value) {
|
|
1433
|
+
this.#payload.sub = value;
|
|
1434
|
+
}
|
|
1435
|
+
get aud() {
|
|
1436
|
+
return this.#payload.aud;
|
|
1437
|
+
}
|
|
1438
|
+
set aud(value) {
|
|
1439
|
+
this.#payload.aud = value;
|
|
1440
|
+
}
|
|
1441
|
+
set jti(value) {
|
|
1442
|
+
this.#payload.jti = value;
|
|
1443
|
+
}
|
|
1444
|
+
set nbf(value) {
|
|
1445
|
+
if (typeof value === "number") {
|
|
1446
|
+
this.#payload.nbf = validateInput("setNotBefore", value);
|
|
1447
|
+
} else if (value instanceof Date) {
|
|
1448
|
+
this.#payload.nbf = validateInput("setNotBefore", epoch(value));
|
|
1449
|
+
} else {
|
|
1450
|
+
this.#payload.nbf = epoch(new Date) + secs(value);
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
set exp(value) {
|
|
1454
|
+
if (typeof value === "number") {
|
|
1455
|
+
this.#payload.exp = validateInput("setExpirationTime", value);
|
|
1456
|
+
} else if (value instanceof Date) {
|
|
1457
|
+
this.#payload.exp = validateInput("setExpirationTime", epoch(value));
|
|
1458
|
+
} else {
|
|
1459
|
+
this.#payload.exp = epoch(new Date) + secs(value);
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
set iat(value) {
|
|
1463
|
+
if (value === undefined) {
|
|
1464
|
+
this.#payload.iat = epoch(new Date);
|
|
1465
|
+
} else if (value instanceof Date) {
|
|
1466
|
+
this.#payload.iat = validateInput("setIssuedAt", epoch(value));
|
|
1467
|
+
} else if (typeof value === "string") {
|
|
1468
|
+
this.#payload.iat = validateInput("setIssuedAt", epoch(new Date) + secs(value));
|
|
1469
|
+
} else {
|
|
1470
|
+
this.#payload.iat = validateInput("setIssuedAt", value);
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/jwt/verify.js
|
|
1476
|
+
async function jwtVerify(jwt, key, options) {
|
|
1477
|
+
const verified = await compactVerify(jwt, key, options);
|
|
1478
|
+
if (verified.protectedHeader.crit?.includes("b64") && verified.protectedHeader.b64 === false) {
|
|
1479
|
+
throw new JWTInvalid("JWTs MUST NOT use unencoded payload");
|
|
1480
|
+
}
|
|
1481
|
+
const payload = validateClaimsSet(verified.protectedHeader, verified.payload, options);
|
|
1482
|
+
const result = { payload, protectedHeader: verified.protectedHeader };
|
|
1483
|
+
if (typeof key === "function") {
|
|
1484
|
+
return { ...result, key: verified.key };
|
|
1485
|
+
}
|
|
1486
|
+
return result;
|
|
1487
|
+
}
|
|
1488
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/sign.js
|
|
1489
|
+
async function sign(alg, key, data) {
|
|
1490
|
+
const cryptoKey = await getSigKey(alg, key, "sign");
|
|
1491
|
+
checkKeyLength(alg, cryptoKey);
|
|
1492
|
+
const signature = await crypto.subtle.sign(subtleAlgorithm(alg, cryptoKey.algorithm), cryptoKey, data);
|
|
1493
|
+
return new Uint8Array(signature);
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/jws/flattened/sign.js
|
|
1497
|
+
class FlattenedSign {
|
|
1498
|
+
#payload;
|
|
1499
|
+
#protectedHeader;
|
|
1500
|
+
#unprotectedHeader;
|
|
1501
|
+
constructor(payload) {
|
|
1502
|
+
if (!(payload instanceof Uint8Array)) {
|
|
1503
|
+
throw new TypeError("payload must be an instance of Uint8Array");
|
|
1504
|
+
}
|
|
1505
|
+
this.#payload = payload;
|
|
1506
|
+
}
|
|
1507
|
+
setProtectedHeader(protectedHeader) {
|
|
1508
|
+
if (this.#protectedHeader) {
|
|
1509
|
+
throw new TypeError("setProtectedHeader can only be called once");
|
|
1510
|
+
}
|
|
1511
|
+
this.#protectedHeader = protectedHeader;
|
|
1512
|
+
return this;
|
|
1513
|
+
}
|
|
1514
|
+
setUnprotectedHeader(unprotectedHeader) {
|
|
1515
|
+
if (this.#unprotectedHeader) {
|
|
1516
|
+
throw new TypeError("setUnprotectedHeader can only be called once");
|
|
1517
|
+
}
|
|
1518
|
+
this.#unprotectedHeader = unprotectedHeader;
|
|
1519
|
+
return this;
|
|
1520
|
+
}
|
|
1521
|
+
async sign(key, options) {
|
|
1522
|
+
if (!this.#protectedHeader && !this.#unprotectedHeader) {
|
|
1523
|
+
throw new JWSInvalid("either setProtectedHeader or setUnprotectedHeader must be called before #sign()");
|
|
1524
|
+
}
|
|
1525
|
+
if (!isDisjoint(this.#protectedHeader, this.#unprotectedHeader)) {
|
|
1526
|
+
throw new JWSInvalid("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
|
|
1527
|
+
}
|
|
1528
|
+
const joseHeader = {
|
|
1529
|
+
...this.#protectedHeader,
|
|
1530
|
+
...this.#unprotectedHeader
|
|
1531
|
+
};
|
|
1532
|
+
const extensions = validateCrit(JWSInvalid, new Map([["b64", true]]), options?.crit, this.#protectedHeader, joseHeader);
|
|
1533
|
+
let b64 = true;
|
|
1534
|
+
if (extensions.has("b64")) {
|
|
1535
|
+
b64 = this.#protectedHeader.b64;
|
|
1536
|
+
if (typeof b64 !== "boolean") {
|
|
1537
|
+
throw new JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
const { alg } = joseHeader;
|
|
1541
|
+
if (typeof alg !== "string" || !alg) {
|
|
1542
|
+
throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
|
1543
|
+
}
|
|
1544
|
+
checkKeyType(alg, key, "sign");
|
|
1545
|
+
let payloadS;
|
|
1546
|
+
let payloadB;
|
|
1547
|
+
if (b64) {
|
|
1548
|
+
payloadS = encode2(this.#payload);
|
|
1549
|
+
payloadB = encode(payloadS);
|
|
1550
|
+
} else {
|
|
1551
|
+
payloadB = this.#payload;
|
|
1552
|
+
payloadS = "";
|
|
1553
|
+
}
|
|
1554
|
+
let protectedHeaderString;
|
|
1555
|
+
let protectedHeaderBytes;
|
|
1556
|
+
if (this.#protectedHeader) {
|
|
1557
|
+
protectedHeaderString = encode2(JSON.stringify(this.#protectedHeader));
|
|
1558
|
+
protectedHeaderBytes = encode(protectedHeaderString);
|
|
1559
|
+
} else {
|
|
1560
|
+
protectedHeaderString = "";
|
|
1561
|
+
protectedHeaderBytes = new Uint8Array;
|
|
1562
|
+
}
|
|
1563
|
+
const data = concat(protectedHeaderBytes, encode("."), payloadB);
|
|
1564
|
+
const k = await normalizeKey(key, alg);
|
|
1565
|
+
const signature = await sign(alg, k, data);
|
|
1566
|
+
const jws = {
|
|
1567
|
+
signature: encode2(signature),
|
|
1568
|
+
payload: payloadS
|
|
1569
|
+
};
|
|
1570
|
+
if (this.#unprotectedHeader) {
|
|
1571
|
+
jws.header = this.#unprotectedHeader;
|
|
1572
|
+
}
|
|
1573
|
+
if (this.#protectedHeader) {
|
|
1574
|
+
jws.protected = protectedHeaderString;
|
|
1575
|
+
}
|
|
1576
|
+
return jws;
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/jws/compact/sign.js
|
|
1581
|
+
class CompactSign {
|
|
1582
|
+
#flattened;
|
|
1583
|
+
constructor(payload) {
|
|
1584
|
+
this.#flattened = new FlattenedSign(payload);
|
|
1585
|
+
}
|
|
1586
|
+
setProtectedHeader(protectedHeader) {
|
|
1587
|
+
this.#flattened.setProtectedHeader(protectedHeader);
|
|
1588
|
+
return this;
|
|
1589
|
+
}
|
|
1590
|
+
async sign(key, options) {
|
|
1591
|
+
const jws = await this.#flattened.sign(key, options);
|
|
1592
|
+
if (jws.payload === undefined) {
|
|
1593
|
+
throw new TypeError("use the flattened module for creating JWS with b64: false");
|
|
1594
|
+
}
|
|
1595
|
+
return `${jws.protected}.${jws.payload}.${jws.signature}`;
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/jwt/sign.js
|
|
1600
|
+
class SignJWT {
|
|
1601
|
+
#protectedHeader;
|
|
1602
|
+
#jwt;
|
|
1603
|
+
constructor(payload = {}) {
|
|
1604
|
+
this.#jwt = new JWTClaimsBuilder(payload);
|
|
1605
|
+
}
|
|
1606
|
+
setIssuer(issuer) {
|
|
1607
|
+
this.#jwt.iss = issuer;
|
|
1608
|
+
return this;
|
|
1609
|
+
}
|
|
1610
|
+
setSubject(subject) {
|
|
1611
|
+
this.#jwt.sub = subject;
|
|
1612
|
+
return this;
|
|
1613
|
+
}
|
|
1614
|
+
setAudience(audience) {
|
|
1615
|
+
this.#jwt.aud = audience;
|
|
1616
|
+
return this;
|
|
1617
|
+
}
|
|
1618
|
+
setJti(jwtId) {
|
|
1619
|
+
this.#jwt.jti = jwtId;
|
|
1620
|
+
return this;
|
|
1621
|
+
}
|
|
1622
|
+
setNotBefore(input) {
|
|
1623
|
+
this.#jwt.nbf = input;
|
|
1624
|
+
return this;
|
|
1625
|
+
}
|
|
1626
|
+
setExpirationTime(input) {
|
|
1627
|
+
this.#jwt.exp = input;
|
|
1628
|
+
return this;
|
|
1629
|
+
}
|
|
1630
|
+
setIssuedAt(input) {
|
|
1631
|
+
this.#jwt.iat = input;
|
|
1632
|
+
return this;
|
|
1633
|
+
}
|
|
1634
|
+
setProtectedHeader(protectedHeader) {
|
|
1635
|
+
this.#protectedHeader = protectedHeader;
|
|
1636
|
+
return this;
|
|
1637
|
+
}
|
|
1638
|
+
async sign(key, options) {
|
|
1639
|
+
const sig = new CompactSign(this.#jwt.data());
|
|
1640
|
+
sig.setProtectedHeader(this.#protectedHeader);
|
|
1641
|
+
if (Array.isArray(this.#protectedHeader?.crit) && this.#protectedHeader.crit.includes("b64") && this.#protectedHeader.b64 === false) {
|
|
1642
|
+
throw new JWTInvalid("JWTs MUST NOT use unencoded payload");
|
|
1643
|
+
}
|
|
1644
|
+
return sig.sign(key, options);
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/jwks/local.js
|
|
1648
|
+
function getKtyFromAlg(alg) {
|
|
1649
|
+
switch (typeof alg === "string" && alg.slice(0, 2)) {
|
|
1650
|
+
case "RS":
|
|
1651
|
+
case "PS":
|
|
1652
|
+
return "RSA";
|
|
1653
|
+
case "ES":
|
|
1654
|
+
return "EC";
|
|
1655
|
+
case "Ed":
|
|
1656
|
+
return "OKP";
|
|
1657
|
+
case "ML":
|
|
1658
|
+
return "AKP";
|
|
1659
|
+
default:
|
|
1660
|
+
throw new JOSENotSupported('Unsupported "alg" value for a JSON Web Key Set');
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
function isJWKSLike(jwks) {
|
|
1664
|
+
return jwks && typeof jwks === "object" && Array.isArray(jwks.keys) && jwks.keys.every(isJWKLike);
|
|
1665
|
+
}
|
|
1666
|
+
function isJWKLike(key) {
|
|
1667
|
+
return isObject(key);
|
|
1668
|
+
}
|
|
1669
|
+
|
|
1670
|
+
class LocalJWKSet {
|
|
1671
|
+
#jwks;
|
|
1672
|
+
#cached = new WeakMap;
|
|
1673
|
+
constructor(jwks) {
|
|
1674
|
+
if (!isJWKSLike(jwks)) {
|
|
1675
|
+
throw new JWKSInvalid("JSON Web Key Set malformed");
|
|
1676
|
+
}
|
|
1677
|
+
this.#jwks = structuredClone(jwks);
|
|
1678
|
+
}
|
|
1679
|
+
jwks() {
|
|
1680
|
+
return this.#jwks;
|
|
1681
|
+
}
|
|
1682
|
+
async getKey(protectedHeader, token) {
|
|
1683
|
+
const { alg, kid } = { ...protectedHeader, ...token?.header };
|
|
1684
|
+
const kty = getKtyFromAlg(alg);
|
|
1685
|
+
const candidates = this.#jwks.keys.filter((jwk2) => {
|
|
1686
|
+
let candidate = kty === jwk2.kty;
|
|
1687
|
+
if (candidate && typeof kid === "string") {
|
|
1688
|
+
candidate = kid === jwk2.kid;
|
|
1689
|
+
}
|
|
1690
|
+
if (candidate && (typeof jwk2.alg === "string" || kty === "AKP")) {
|
|
1691
|
+
candidate = alg === jwk2.alg;
|
|
1692
|
+
}
|
|
1693
|
+
if (candidate && typeof jwk2.use === "string") {
|
|
1694
|
+
candidate = jwk2.use === "sig";
|
|
1695
|
+
}
|
|
1696
|
+
if (candidate && Array.isArray(jwk2.key_ops)) {
|
|
1697
|
+
candidate = jwk2.key_ops.includes("verify");
|
|
1698
|
+
}
|
|
1699
|
+
if (candidate) {
|
|
1700
|
+
switch (alg) {
|
|
1701
|
+
case "ES256":
|
|
1702
|
+
candidate = jwk2.crv === "P-256";
|
|
1703
|
+
break;
|
|
1704
|
+
case "ES384":
|
|
1705
|
+
candidate = jwk2.crv === "P-384";
|
|
1706
|
+
break;
|
|
1707
|
+
case "ES512":
|
|
1708
|
+
candidate = jwk2.crv === "P-521";
|
|
1709
|
+
break;
|
|
1710
|
+
case "Ed25519":
|
|
1711
|
+
case "EdDSA":
|
|
1712
|
+
candidate = jwk2.crv === "Ed25519";
|
|
1713
|
+
break;
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
return candidate;
|
|
1717
|
+
});
|
|
1718
|
+
const { 0: jwk, length } = candidates;
|
|
1719
|
+
if (length === 0) {
|
|
1720
|
+
throw new JWKSNoMatchingKey;
|
|
1721
|
+
}
|
|
1722
|
+
if (length !== 1) {
|
|
1723
|
+
const error = new JWKSMultipleMatchingKeys;
|
|
1724
|
+
const _cached = this.#cached;
|
|
1725
|
+
error[Symbol.asyncIterator] = async function* () {
|
|
1726
|
+
for (const jwk2 of candidates) {
|
|
1727
|
+
try {
|
|
1728
|
+
yield await importWithAlgCache(_cached, jwk2, alg);
|
|
1729
|
+
} catch {}
|
|
1730
|
+
}
|
|
1731
|
+
};
|
|
1732
|
+
throw error;
|
|
1733
|
+
}
|
|
1734
|
+
return importWithAlgCache(this.#cached, jwk, alg);
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
async function importWithAlgCache(cache2, jwk, alg) {
|
|
1738
|
+
const cached = cache2.get(jwk) || cache2.set(jwk, {}).get(jwk);
|
|
1739
|
+
if (cached[alg] === undefined) {
|
|
1740
|
+
const key = await importJWK({ ...jwk, ext: true }, alg);
|
|
1741
|
+
if (key instanceof Uint8Array || key.type !== "public") {
|
|
1742
|
+
throw new JWKSInvalid("JSON Web Key Set members must be public keys");
|
|
1743
|
+
}
|
|
1744
|
+
cached[alg] = key;
|
|
1745
|
+
}
|
|
1746
|
+
return cached[alg];
|
|
1747
|
+
}
|
|
1748
|
+
function createLocalJWKSet(jwks) {
|
|
1749
|
+
const set = new LocalJWKSet(jwks);
|
|
1750
|
+
const localJWKSet = async (protectedHeader, token) => set.getKey(protectedHeader, token);
|
|
1751
|
+
Object.defineProperties(localJWKSet, {
|
|
1752
|
+
jwks: {
|
|
1753
|
+
value: () => structuredClone(set.jwks()),
|
|
1754
|
+
enumerable: false,
|
|
1755
|
+
configurable: false,
|
|
1756
|
+
writable: false
|
|
1757
|
+
}
|
|
1758
|
+
});
|
|
1759
|
+
return localJWKSet;
|
|
1760
|
+
}
|
|
1761
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/util/decode_jwt.js
|
|
1762
|
+
function decodeJwt(jwt) {
|
|
1763
|
+
if (typeof jwt !== "string")
|
|
1764
|
+
throw new JWTInvalid("JWTs must use Compact JWS serialization, JWT must be a string");
|
|
1765
|
+
const { 1: payload, length } = jwt.split(".");
|
|
1766
|
+
if (length === 5)
|
|
1767
|
+
throw new JWTInvalid("Only JWTs using Compact JWS serialization can be decoded");
|
|
1768
|
+
if (length !== 3)
|
|
1769
|
+
throw new JWTInvalid("Invalid JWT");
|
|
1770
|
+
if (!payload)
|
|
1771
|
+
throw new JWTInvalid("JWTs must contain a payload");
|
|
1772
|
+
let decoded;
|
|
1773
|
+
try {
|
|
1774
|
+
decoded = decode(payload);
|
|
1775
|
+
} catch {
|
|
1776
|
+
throw new JWTInvalid("Failed to base64url decode the payload");
|
|
1777
|
+
}
|
|
1778
|
+
let result;
|
|
1779
|
+
try {
|
|
1780
|
+
result = JSON.parse(decoder.decode(decoded));
|
|
1781
|
+
} catch {
|
|
1782
|
+
throw new JWTInvalid("Failed to parse the decoded payload as JSON");
|
|
1783
|
+
}
|
|
1784
|
+
if (!isObject(result))
|
|
1785
|
+
throw new JWTInvalid("Invalid JWT Claims Set");
|
|
1786
|
+
return result;
|
|
1787
|
+
}
|
|
1788
|
+
// src/auth.ts
|
|
1789
|
+
async function verifyJWT(token, publicKey) {
|
|
1790
|
+
try {
|
|
1791
|
+
let key;
|
|
1792
|
+
if (typeof publicKey === "string") {
|
|
1793
|
+
key = await importSPKI(publicKey, "RS256");
|
|
1794
|
+
} else if (typeof publicKey === "object" && publicKey !== null && "keys" in publicKey) {
|
|
1795
|
+
key = createLocalJWKSet(publicKey);
|
|
1796
|
+
} else {
|
|
1797
|
+
key = publicKey;
|
|
1798
|
+
}
|
|
1799
|
+
const { payload } = await jwtVerify(token, key, {
|
|
1800
|
+
algorithms: ["RS256"]
|
|
1801
|
+
});
|
|
1802
|
+
return payload;
|
|
1803
|
+
} catch (error) {
|
|
1804
|
+
if (error instanceof exports_errors.JWTExpired) {
|
|
1805
|
+
console.error("JWT has expired:");
|
|
1806
|
+
throw new Error("Token has expired");
|
|
1807
|
+
}
|
|
1808
|
+
console.error("JWT verification failed:", error);
|
|
1809
|
+
throw new Error("Invalid token");
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
async function signJWT(payload, privateKey) {
|
|
1813
|
+
const expirationTime = Math.floor(Date.now() / 1000) + 2 * 60 * 60;
|
|
1814
|
+
if (!payload.exp) {
|
|
1815
|
+
payload.exp = expirationTime;
|
|
1816
|
+
}
|
|
1817
|
+
let cryptoKey;
|
|
1818
|
+
if (typeof privateKey === "string") {
|
|
1819
|
+
cryptoKey = await importPKCS8(privateKey, "RS256");
|
|
1820
|
+
} else if (typeof privateKey === "object" && privateKey !== null && "kty" in privateKey) {
|
|
1821
|
+
cryptoKey = await importJWK(privateKey, "RS256");
|
|
1822
|
+
} else {
|
|
1823
|
+
cryptoKey = privateKey;
|
|
1824
|
+
}
|
|
1825
|
+
const iss = payload.iss || "https://convex.kevisual.cn";
|
|
1826
|
+
if (!payload.iss) {
|
|
1827
|
+
payload.iss = iss;
|
|
1828
|
+
}
|
|
1829
|
+
if (!payload.iat) {
|
|
1830
|
+
payload.iat = Math.floor(Date.now() / 1000);
|
|
1831
|
+
}
|
|
1832
|
+
if (!payload.aud) {
|
|
1833
|
+
payload.aud = "convex-app";
|
|
1834
|
+
}
|
|
1835
|
+
const token = await new SignJWT(payload).setProtectedHeader({
|
|
1836
|
+
alg: "RS256",
|
|
1837
|
+
typ: "JWT",
|
|
1838
|
+
kid: "kid-key-1"
|
|
1839
|
+
}).setIssuedAt().setExpirationTime(payload.exp).setIssuer(iss).setSubject(payload.sub || "").sign(cryptoKey);
|
|
1840
|
+
return token;
|
|
1841
|
+
}
|
|
1842
|
+
var decodeJWT = (token) => {
|
|
1843
|
+
try {
|
|
1844
|
+
const decoded = decodeJwt(token);
|
|
1845
|
+
return decoded;
|
|
1846
|
+
} catch (error) {
|
|
1847
|
+
throw new Error("Invalid token");
|
|
1848
|
+
}
|
|
1849
|
+
};
|
|
1850
|
+
export {
|
|
1851
|
+
verifyJWT,
|
|
1852
|
+
signJWT,
|
|
1853
|
+
decodeJWT
|
|
1854
|
+
};
|