@pagopa/io-wallet-utils 1.5.8 → 2.0.0-apt
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/index.d.mts +610 -61
- package/dist/index.d.ts +610 -61
- package/dist/index.js +662 -41
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +629 -42
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -4
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/config.ts
|
|
2
2
|
var ItWalletSpecsVersion = /* @__PURE__ */ ((ItWalletSpecsVersion2) => {
|
|
3
|
+
ItWalletSpecsVersion2["APTITUDE"] = "APTITUDE";
|
|
3
4
|
ItWalletSpecsVersion2["V1_0"] = "V1_0";
|
|
4
5
|
ItWalletSpecsVersion2["V1_3"] = "V1_3";
|
|
5
6
|
ItWalletSpecsVersion2["V1_4"] = "V1_4";
|
|
@@ -48,6 +49,59 @@ var HEADERS = {
|
|
|
48
49
|
var MAX_IAT_AGE_SECONDS = 5 * 60;
|
|
49
50
|
var CLOCK_SKEW_TOLERANCE_SECONDS = 60;
|
|
50
51
|
|
|
52
|
+
// src/content-type.ts
|
|
53
|
+
var ContentType = /* @__PURE__ */ ((ContentType2) => {
|
|
54
|
+
ContentType2["Html"] = "text/html";
|
|
55
|
+
ContentType2["Json"] = "application/json";
|
|
56
|
+
ContentType2["JwkSet"] = "application/jwk-set+json";
|
|
57
|
+
ContentType2["Jwt"] = "application/jwt";
|
|
58
|
+
ContentType2["OAuthAuthorizationRequestJwt"] = "application/oauth-authz-req+jwt";
|
|
59
|
+
ContentType2["XWwwFormUrlencoded"] = "application/x-www-form-urlencoded";
|
|
60
|
+
return ContentType2;
|
|
61
|
+
})(ContentType || {});
|
|
62
|
+
|
|
63
|
+
// src/crypto/hash.ts
|
|
64
|
+
var HashAlgorithm = /* @__PURE__ */ ((HashAlgorithm2) => {
|
|
65
|
+
HashAlgorithm2["Sha256"] = "sha-256";
|
|
66
|
+
HashAlgorithm2["Sha384"] = "sha-384";
|
|
67
|
+
HashAlgorithm2["Sha512"] = "sha-512";
|
|
68
|
+
return HashAlgorithm2;
|
|
69
|
+
})(HashAlgorithm || {});
|
|
70
|
+
|
|
71
|
+
// src/encoding.ts
|
|
72
|
+
var utf8Encoder = new TextEncoder();
|
|
73
|
+
var utf8Decoder = new TextDecoder();
|
|
74
|
+
function base64UrlToBase64(base64) {
|
|
75
|
+
const normalized = base64.replace(/-/g, "+").replace(/_/g, "/");
|
|
76
|
+
const paddingLength = normalized.length % 4;
|
|
77
|
+
return paddingLength === 0 ? normalized : normalized.padEnd(normalized.length + (4 - paddingLength), "=");
|
|
78
|
+
}
|
|
79
|
+
function base64ToBase64Url(base64) {
|
|
80
|
+
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
81
|
+
}
|
|
82
|
+
function encodeBase64(data) {
|
|
83
|
+
let binary = "";
|
|
84
|
+
for (const byte of data) {
|
|
85
|
+
binary += String.fromCharCode(byte);
|
|
86
|
+
}
|
|
87
|
+
return btoa(binary);
|
|
88
|
+
}
|
|
89
|
+
function decodeUtf8String(value) {
|
|
90
|
+
return utf8Encoder.encode(value);
|
|
91
|
+
}
|
|
92
|
+
function encodeToUtf8String(data) {
|
|
93
|
+
return utf8Decoder.decode(data);
|
|
94
|
+
}
|
|
95
|
+
function decodeBase64(base64) {
|
|
96
|
+
const binary = atob(base64UrlToBase64(base64));
|
|
97
|
+
return Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
98
|
+
}
|
|
99
|
+
function encodeToBase64Url(data) {
|
|
100
|
+
return base64ToBase64Url(
|
|
101
|
+
encodeBase64(typeof data === "string" ? decodeUtf8String(data) : data)
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
51
105
|
// src/errors/parse.ts
|
|
52
106
|
var constants = {
|
|
53
107
|
identifierRegex: /[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*/u,
|
|
@@ -106,6 +160,27 @@ function formatZodError(error) {
|
|
|
106
160
|
}
|
|
107
161
|
|
|
108
162
|
// src/errors/errors.ts
|
|
163
|
+
var JsonParseError = class extends Error {
|
|
164
|
+
constructor(message, jsonString) {
|
|
165
|
+
super(`${message}
|
|
166
|
+
${jsonString}`);
|
|
167
|
+
this.name = "JsonParseError";
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
var JwtParseError = class extends Error {
|
|
171
|
+
constructor(message) {
|
|
172
|
+
super(message ?? "Error parsing jwt");
|
|
173
|
+
this.name = "JwtParseError";
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
var JwtVerificationError = class extends Error {
|
|
177
|
+
constructor(message, options) {
|
|
178
|
+
const errorMessage = message ?? "Error verifying jwt.";
|
|
179
|
+
const causeMessage = options?.cause instanceof Error ? ` ${options.cause.message}` : options?.cause ? ` ${String(options.cause)}` : "";
|
|
180
|
+
super(`${errorMessage}${causeMessage}`, options);
|
|
181
|
+
this.name = "JwtVerificationError";
|
|
182
|
+
}
|
|
183
|
+
};
|
|
109
184
|
var serializeAttrs = (attrs) => Object.entries(attrs).filter(([, v]) => v !== void 0).map(([k, v]) => {
|
|
110
185
|
if (Array.isArray(v)) return [k, `(${v.join(", ")})`];
|
|
111
186
|
if (typeof v !== "string") return [k, JSON.stringify(v)];
|
|
@@ -160,21 +235,6 @@ ${formattedError}`;
|
|
|
160
235
|
}
|
|
161
236
|
};
|
|
162
237
|
|
|
163
|
-
// src/fetcher.ts
|
|
164
|
-
var hasStatusOrThrow = (status, customError) => async (res) => {
|
|
165
|
-
if (Array.isArray(status) ? !status.includes(res.status) : res.status !== status) {
|
|
166
|
-
const ErrorClass = customError ?? UnexpectedStatusCodeError;
|
|
167
|
-
throw new ErrorClass({
|
|
168
|
-
message: `Http request failed. Expected ${status}, got ${res.status}, url: ${res.url}`,
|
|
169
|
-
reason: await parseRawHttpResponse(res),
|
|
170
|
-
// Pass the response body as reason so the original error can surface
|
|
171
|
-
statusCode: res.status
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
return res;
|
|
175
|
-
};
|
|
176
|
-
var parseRawHttpResponse = (response) => response.headers.get("content-type")?.includes("application/json") ? response.json() : response.text();
|
|
177
|
-
|
|
178
238
|
// src/parse.ts
|
|
179
239
|
var SAFE_STRINGIFY_MAX_LENGTH = 200;
|
|
180
240
|
function safeStringify(value) {
|
|
@@ -191,6 +251,26 @@ function safeStringify(value) {
|
|
|
191
251
|
return "[unserializable]";
|
|
192
252
|
}
|
|
193
253
|
}
|
|
254
|
+
function stringToJsonWithErrorHandling(value, errorMessage) {
|
|
255
|
+
try {
|
|
256
|
+
return JSON.parse(value);
|
|
257
|
+
} catch {
|
|
258
|
+
throw new JsonParseError(
|
|
259
|
+
errorMessage ?? "Unable to parse string to JSON.",
|
|
260
|
+
value
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function parseIfJson(data) {
|
|
265
|
+
if (typeof data !== "string") {
|
|
266
|
+
return data;
|
|
267
|
+
}
|
|
268
|
+
try {
|
|
269
|
+
return JSON.parse(data);
|
|
270
|
+
} catch {
|
|
271
|
+
return data;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
194
274
|
function parseWithErrorHandling(schema, data, customErrorMessage) {
|
|
195
275
|
const parseResult = schema.safeParse(data);
|
|
196
276
|
if (!parseResult.success) {
|
|
@@ -205,9 +285,522 @@ function formatError(message, prefix) {
|
|
|
205
285
|
return prefix ? `${prefix} ${message}` : message;
|
|
206
286
|
}
|
|
207
287
|
|
|
208
|
-
// src/
|
|
288
|
+
// src/crypto/jwk/z-jwk.ts
|
|
289
|
+
import z2 from "zod";
|
|
290
|
+
|
|
291
|
+
// src/crypto/z-common.ts
|
|
209
292
|
import z from "zod";
|
|
210
|
-
var
|
|
293
|
+
var zTrustChain = z.tuple([z.string()], z.string());
|
|
294
|
+
var zCertificateChain = z.array(z.string()).nonempty();
|
|
295
|
+
var zAlgValueNotNone = z.string().refine((alg) => alg !== "none", { message: `alg value may not be 'none'` });
|
|
296
|
+
|
|
297
|
+
// src/crypto/jwk/z-jwk.ts
|
|
298
|
+
var zJwk = z2.looseObject({
|
|
299
|
+
alg: z2.optional(z2.string()),
|
|
300
|
+
crv: z2.optional(z2.string()),
|
|
301
|
+
d: z2.optional(z2.string()),
|
|
302
|
+
dp: z2.optional(z2.string()),
|
|
303
|
+
dq: z2.optional(z2.string()),
|
|
304
|
+
e: z2.optional(z2.string()),
|
|
305
|
+
ext: z2.optional(z2.boolean()),
|
|
306
|
+
k: z2.optional(z2.string()),
|
|
307
|
+
key_ops: z2.optional(z2.array(z2.string())),
|
|
308
|
+
kid: z2.optional(z2.string()),
|
|
309
|
+
kty: z2.string(),
|
|
310
|
+
n: z2.optional(z2.string()),
|
|
311
|
+
oth: z2.optional(
|
|
312
|
+
z2.array(
|
|
313
|
+
z2.looseObject({
|
|
314
|
+
d: z2.optional(z2.string()),
|
|
315
|
+
r: z2.optional(z2.string()),
|
|
316
|
+
t: z2.optional(z2.string())
|
|
317
|
+
})
|
|
318
|
+
)
|
|
319
|
+
),
|
|
320
|
+
p: z2.optional(z2.string()),
|
|
321
|
+
q: z2.optional(z2.string()),
|
|
322
|
+
qi: z2.optional(z2.string()),
|
|
323
|
+
use: z2.optional(z2.string()),
|
|
324
|
+
x: z2.optional(z2.string()),
|
|
325
|
+
x5c: z2.optional(zCertificateChain),
|
|
326
|
+
x5t: z2.optional(z2.string()),
|
|
327
|
+
"x5t#S256": z2.optional(z2.string()),
|
|
328
|
+
x5u: z2.optional(z2.string()),
|
|
329
|
+
y: z2.optional(z2.string())
|
|
330
|
+
});
|
|
331
|
+
var zJwkSet = z2.looseObject({ keys: z2.array(zJwk) });
|
|
332
|
+
|
|
333
|
+
// src/crypto/jwk/calculate-jwk-thumbprint.ts
|
|
334
|
+
function createJwkThumbprintComponents(jwk) {
|
|
335
|
+
if (jwk.kty === "EC") {
|
|
336
|
+
if (!jwk.crv || !jwk.x || !jwk.y) {
|
|
337
|
+
throw new ValidationError(
|
|
338
|
+
"Provided jwk does not match a supported jwk structure. Either the 'kty' is not supported, or required values are missing."
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
return {
|
|
342
|
+
crv: jwk.crv,
|
|
343
|
+
kty: jwk.kty,
|
|
344
|
+
x: jwk.x,
|
|
345
|
+
y: jwk.y
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
if (jwk.kty === "RSA") {
|
|
349
|
+
if (!jwk.e || !jwk.n) {
|
|
350
|
+
throw new ValidationError(
|
|
351
|
+
"Provided jwk does not match a supported jwk structure. Either the 'kty' is not supported, or required values are missing."
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
return {
|
|
355
|
+
e: jwk.e,
|
|
356
|
+
kty: jwk.kty,
|
|
357
|
+
n: jwk.n
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
if (jwk.kty === "oct") {
|
|
361
|
+
if (!jwk.k) {
|
|
362
|
+
throw new ValidationError(
|
|
363
|
+
"Provided jwk does not match a supported jwk structure. Either the 'kty' is not supported, or required values are missing."
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
return {
|
|
367
|
+
k: jwk.k,
|
|
368
|
+
kty: jwk.kty
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
throw new ValidationError(
|
|
372
|
+
"Provided jwk does not match a supported jwk structure. Either the 'kty' is not supported, or required values are missing."
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
async function calculateJwkThumbprint(options) {
|
|
376
|
+
const parsedJwk = parseWithErrorHandling(
|
|
377
|
+
zJwk,
|
|
378
|
+
options.jwk,
|
|
379
|
+
"Provided jwk does not match a supported jwk structure. Either the 'kty' is not supported, or required values are missing."
|
|
380
|
+
);
|
|
381
|
+
const jwkThumbprintComponents = createJwkThumbprintComponents(parsedJwk);
|
|
382
|
+
return encodeToBase64Url(
|
|
383
|
+
await options.hashCallback(
|
|
384
|
+
decodeUtf8String(JSON.stringify(jwkThumbprintComponents)),
|
|
385
|
+
options.hashAlgorithm
|
|
386
|
+
)
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// src/crypto/jwt/z-jwt.ts
|
|
391
|
+
import z3 from "zod";
|
|
392
|
+
var MAX_JTI_LENGTH = 256;
|
|
393
|
+
var zCompactJwt = z3.string().regex(/^([a-zA-Z0-9-_]+)\.([a-zA-Z0-9-_]+)\.([a-zA-Z0-9-_]+)$/, {
|
|
394
|
+
message: "Not a valid compact jwt"
|
|
395
|
+
});
|
|
396
|
+
var zJwtConfirmationPayload = z3.looseObject({
|
|
397
|
+
// RFC9449. jwk thumbprint of the dpop public key to which the access token is bound
|
|
398
|
+
jkt: z3.string().optional(),
|
|
399
|
+
jwk: zJwk.optional()
|
|
400
|
+
});
|
|
401
|
+
var zJwtPayload = z3.looseObject({
|
|
402
|
+
aud: z3.string().or(z3.array(z3.string())).optional(),
|
|
403
|
+
cnf: zJwtConfirmationPayload.optional(),
|
|
404
|
+
exp: z3.number().int().optional(),
|
|
405
|
+
iat: z3.number().int().optional(),
|
|
406
|
+
iss: z3.string().optional(),
|
|
407
|
+
jti: z3.string().max(MAX_JTI_LENGTH).optional(),
|
|
408
|
+
nbf: z3.number().int().optional(),
|
|
409
|
+
nonce: z3.string().optional(),
|
|
410
|
+
// Reserved for status parameters
|
|
411
|
+
status: z3.record(z3.string(), z3.any()).optional(),
|
|
412
|
+
sub: z3.string().optional(),
|
|
413
|
+
// Reserved for OpenID Federation
|
|
414
|
+
trust_chain: zTrustChain.optional()
|
|
415
|
+
});
|
|
416
|
+
var zJwtHeader = z3.looseObject({
|
|
417
|
+
alg: zAlgValueNotNone,
|
|
418
|
+
jwk: zJwk.optional(),
|
|
419
|
+
kid: z3.string().optional(),
|
|
420
|
+
// Reserved for OpenID Federation
|
|
421
|
+
trust_chain: zTrustChain.optional(),
|
|
422
|
+
typ: z3.string().optional(),
|
|
423
|
+
x5c: zCertificateChain.optional()
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
// src/crypto/jwt/decode-jwt-header.ts
|
|
427
|
+
function decodeJwtHeader(options) {
|
|
428
|
+
const jwtParts = options.jwt.split(".");
|
|
429
|
+
if (jwtParts.length <= 2) {
|
|
430
|
+
throw new JwtParseError(
|
|
431
|
+
formatError(
|
|
432
|
+
"Unable to decode because Jwt is not a valid!",
|
|
433
|
+
options.errorMessagePrefix
|
|
434
|
+
)
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
const [headerPart] = jwtParts;
|
|
438
|
+
let headerJson;
|
|
439
|
+
try {
|
|
440
|
+
headerJson = stringToJsonWithErrorHandling(
|
|
441
|
+
encodeToUtf8String(decodeBase64(headerPart)),
|
|
442
|
+
formatError(
|
|
443
|
+
"Unable to parse jwt header to JSON",
|
|
444
|
+
options.errorMessagePrefix
|
|
445
|
+
)
|
|
446
|
+
);
|
|
447
|
+
} catch (error) {
|
|
448
|
+
throw new JwtParseError(
|
|
449
|
+
formatError(
|
|
450
|
+
`Error parsing JWT. ${error instanceof Error ? error.message : ""}`,
|
|
451
|
+
options.errorMessagePrefix
|
|
452
|
+
)
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
const header = parseWithErrorHandling(
|
|
456
|
+
options.headerSchema ?? zJwtHeader,
|
|
457
|
+
headerJson,
|
|
458
|
+
formatError("Invalid JWT header", options.errorMessagePrefix)
|
|
459
|
+
);
|
|
460
|
+
return {
|
|
461
|
+
header
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
function jwtHeaderFromJwtSigner(signer) {
|
|
465
|
+
if (signer.method === "did") {
|
|
466
|
+
return {
|
|
467
|
+
alg: signer.alg,
|
|
468
|
+
kid: signer.didUrl
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
if (signer.method === "federation") {
|
|
472
|
+
return {
|
|
473
|
+
alg: signer.alg,
|
|
474
|
+
kid: signer.kid,
|
|
475
|
+
trust_chain: signer.trustChain
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
if (signer.method === "jwk") {
|
|
479
|
+
return {
|
|
480
|
+
alg: signer.alg,
|
|
481
|
+
jwk: signer.publicJwk
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
if (signer.method === "x5c") {
|
|
485
|
+
return {
|
|
486
|
+
alg: signer.alg,
|
|
487
|
+
kid: signer.kid,
|
|
488
|
+
trust_chain: signer.trustChain,
|
|
489
|
+
x5c: signer.x5c
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
return { alg: signer.alg };
|
|
493
|
+
}
|
|
494
|
+
function jwtSignerFromJwt(options) {
|
|
495
|
+
const { allowedSignerMethods, header, payload } = options;
|
|
496
|
+
const found = [];
|
|
497
|
+
if (header.x5c) {
|
|
498
|
+
found.push({
|
|
499
|
+
method: "x5c",
|
|
500
|
+
signer: {
|
|
501
|
+
alg: header.alg,
|
|
502
|
+
kid: header.kid,
|
|
503
|
+
method: "x5c",
|
|
504
|
+
trustChain: header.trust_chain,
|
|
505
|
+
x5c: header.x5c
|
|
506
|
+
},
|
|
507
|
+
valid: true
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
if (header.trust_chain) {
|
|
511
|
+
if (!header.kid) {
|
|
512
|
+
found.push({
|
|
513
|
+
error: "When 'trust_chain' is used in jwt header, the 'kid' parameter is required.",
|
|
514
|
+
method: "federation",
|
|
515
|
+
valid: false
|
|
516
|
+
});
|
|
517
|
+
} else {
|
|
518
|
+
found.push({
|
|
519
|
+
method: "federation",
|
|
520
|
+
signer: {
|
|
521
|
+
alg: header.alg,
|
|
522
|
+
kid: header.kid,
|
|
523
|
+
method: "federation",
|
|
524
|
+
trustChain: header.trust_chain
|
|
525
|
+
},
|
|
526
|
+
valid: true
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
if (header.kid?.startsWith("did:") || payload.iss?.startsWith("did:")) {
|
|
531
|
+
if (payload.iss && header.kid?.startsWith("did:") && !header.kid.startsWith(payload.iss)) {
|
|
532
|
+
found.push({
|
|
533
|
+
error: "kid in header starts with did that is different from did value in 'iss'",
|
|
534
|
+
method: "did",
|
|
535
|
+
valid: false
|
|
536
|
+
});
|
|
537
|
+
} else if (!header.kid?.startsWith("did:") && !header.kid?.startsWith("#")) {
|
|
538
|
+
found.push({
|
|
539
|
+
error: "kid in header must start with either 'did:' or '#' when 'iss' value is a did",
|
|
540
|
+
method: "did",
|
|
541
|
+
valid: false
|
|
542
|
+
});
|
|
543
|
+
} else if (header.kid) {
|
|
544
|
+
found.push({
|
|
545
|
+
method: "did",
|
|
546
|
+
signer: {
|
|
547
|
+
alg: header.alg,
|
|
548
|
+
didUrl: header.kid.startsWith("did:") ? header.kid : `${payload.iss}${header.kid}`,
|
|
549
|
+
method: "did"
|
|
550
|
+
},
|
|
551
|
+
valid: true
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (header.jwk) {
|
|
556
|
+
found.push({
|
|
557
|
+
method: "jwk",
|
|
558
|
+
signer: {
|
|
559
|
+
alg: header.alg,
|
|
560
|
+
method: "jwk",
|
|
561
|
+
publicJwk: header.jwk
|
|
562
|
+
},
|
|
563
|
+
valid: true
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
const allowedFoundMethods = found.filter(
|
|
567
|
+
(candidate) => !allowedSignerMethods || allowedSignerMethods.includes(candidate.method)
|
|
568
|
+
);
|
|
569
|
+
const allowedValidMethods = allowedFoundMethods.filter(
|
|
570
|
+
(candidate) => candidate.valid
|
|
571
|
+
);
|
|
572
|
+
const firstAllowedValidMethod = allowedValidMethods[0];
|
|
573
|
+
if (firstAllowedValidMethod && firstAllowedValidMethod.valid) {
|
|
574
|
+
return firstAllowedValidMethod.signer;
|
|
575
|
+
}
|
|
576
|
+
if (allowedFoundMethods.length > 0) {
|
|
577
|
+
throw new JwtParseError(
|
|
578
|
+
`Unable to extract signer method from jwt. Found ${allowedFoundMethods.length} allowed signer method(s) but contained invalid configuration:
|
|
579
|
+
${allowedFoundMethods.map(
|
|
580
|
+
(candidate) => candidate.valid ? "" : `FAILED: method ${candidate.method} - ${candidate.error}`
|
|
581
|
+
).join("\n")}`
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
if (found.length > 0) {
|
|
585
|
+
throw new JwtParseError(
|
|
586
|
+
`Unable to extract signer method from jwt. Found ${found.length} signer method(s) that are not allowed:
|
|
587
|
+
${found.map(
|
|
588
|
+
(candidate) => candidate.valid ? `SUCCEEDED: method ${candidate.method}` : `FAILED: method ${candidate.method} - ${candidate.error}`
|
|
589
|
+
).join("\n")}`
|
|
590
|
+
);
|
|
591
|
+
}
|
|
592
|
+
if (!allowedSignerMethods || allowedSignerMethods.includes("custom")) {
|
|
593
|
+
return {
|
|
594
|
+
alg: header.alg,
|
|
595
|
+
kid: header.kid,
|
|
596
|
+
method: "custom"
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
throw new JwtParseError(
|
|
600
|
+
"Unable to extract signer method from jwt. Found no signer methods and 'custom' signer method is not allowed."
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// src/crypto/jwt/decode-jwt.ts
|
|
605
|
+
function decodeJwt(options) {
|
|
606
|
+
const jwtParts = options.jwt.split(".");
|
|
607
|
+
if (jwtParts.length !== 3) {
|
|
608
|
+
throw new JwtParseError(
|
|
609
|
+
formatError(
|
|
610
|
+
"Unable to decode because Jwt is not a valid!",
|
|
611
|
+
options.errorMessagePrefix
|
|
612
|
+
)
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
let payloadJson;
|
|
616
|
+
try {
|
|
617
|
+
const payloadPart = jwtParts[1];
|
|
618
|
+
if (payloadPart === void 0) {
|
|
619
|
+
throw new JwtParseError(
|
|
620
|
+
formatError(
|
|
621
|
+
"Unable to decode because Jwt is not a valid!",
|
|
622
|
+
options.errorMessagePrefix
|
|
623
|
+
)
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
payloadJson = stringToJsonWithErrorHandling(
|
|
627
|
+
encodeToUtf8String(decodeBase64(payloadPart)),
|
|
628
|
+
formatError(
|
|
629
|
+
"Unable to parse jwt payload to JSON",
|
|
630
|
+
options.errorMessagePrefix
|
|
631
|
+
)
|
|
632
|
+
);
|
|
633
|
+
} catch (error) {
|
|
634
|
+
throw new JwtParseError(
|
|
635
|
+
formatError(
|
|
636
|
+
`Error parsing JWT. ${error instanceof Error ? error.message : ""}`,
|
|
637
|
+
options.errorMessagePrefix
|
|
638
|
+
)
|
|
639
|
+
);
|
|
640
|
+
}
|
|
641
|
+
const signaturePart = jwtParts[2];
|
|
642
|
+
if (signaturePart === void 0) {
|
|
643
|
+
throw new JwtParseError(
|
|
644
|
+
formatError(
|
|
645
|
+
"Unable to decode because Jwt is not a valid!",
|
|
646
|
+
options.errorMessagePrefix
|
|
647
|
+
)
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
const { header } = decodeJwtHeader({
|
|
651
|
+
errorMessagePrefix: options.errorMessagePrefix,
|
|
652
|
+
headerSchema: options.headerSchema,
|
|
653
|
+
jwt: options.jwt
|
|
654
|
+
});
|
|
655
|
+
const payload = parseWithErrorHandling(
|
|
656
|
+
options.payloadSchema ?? zJwtPayload,
|
|
657
|
+
payloadJson,
|
|
658
|
+
formatError("Invalid JWT payload", options.errorMessagePrefix)
|
|
659
|
+
);
|
|
660
|
+
return {
|
|
661
|
+
compact: options.jwt,
|
|
662
|
+
header,
|
|
663
|
+
payload,
|
|
664
|
+
signature: signaturePart
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// src/date.ts
|
|
669
|
+
function dateToSeconds(date) {
|
|
670
|
+
const milliseconds = date?.getTime() ?? Date.now();
|
|
671
|
+
return Math.floor(milliseconds / 1e3);
|
|
672
|
+
}
|
|
673
|
+
function addSecondsToDate(date, seconds) {
|
|
674
|
+
return new Date(date.getTime() + seconds * 1e3);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// src/crypto/jwt/verify-jwt.ts
|
|
678
|
+
async function verifyJwt(options) {
|
|
679
|
+
const errorMessage = options.errorMessage ?? "Error during verification of jwt.";
|
|
680
|
+
let signerJwk;
|
|
681
|
+
try {
|
|
682
|
+
const result = await options.verifyJwtCallback(options.signer, {
|
|
683
|
+
compact: options.compact,
|
|
684
|
+
header: options.header,
|
|
685
|
+
payload: options.payload
|
|
686
|
+
});
|
|
687
|
+
if (!result.verified) {
|
|
688
|
+
throw new JwtVerificationError(errorMessage);
|
|
689
|
+
}
|
|
690
|
+
signerJwk = result.signerJwk;
|
|
691
|
+
} catch (error) {
|
|
692
|
+
if (error instanceof JwtVerificationError) {
|
|
693
|
+
throw error;
|
|
694
|
+
}
|
|
695
|
+
throw new JwtVerificationError(errorMessage, { cause: error });
|
|
696
|
+
}
|
|
697
|
+
const nowInSeconds = dateToSeconds(options.now ?? /* @__PURE__ */ new Date());
|
|
698
|
+
const skewInSeconds = options.allowedSkewInSeconds ?? 0;
|
|
699
|
+
const timeBasedValidation = options.skipTimeBasedValidation !== void 0 ? !options.skipTimeBasedValidation : true;
|
|
700
|
+
if (timeBasedValidation && options.payload.nbf && nowInSeconds < options.payload.nbf - skewInSeconds) {
|
|
701
|
+
throw new JwtVerificationError(
|
|
702
|
+
`${errorMessage} jwt 'nbf' is in the future`
|
|
703
|
+
);
|
|
704
|
+
}
|
|
705
|
+
if (timeBasedValidation && options.payload.exp && nowInSeconds > options.payload.exp + skewInSeconds) {
|
|
706
|
+
throw new JwtVerificationError(`${errorMessage} jwt 'exp' is in the past`);
|
|
707
|
+
}
|
|
708
|
+
if (options.expectedAudience) {
|
|
709
|
+
const { aud } = options.payload;
|
|
710
|
+
if (Array.isArray(aud) && !aud.includes(options.expectedAudience) || typeof aud === "string" && aud !== options.expectedAudience) {
|
|
711
|
+
throw new JwtVerificationError(
|
|
712
|
+
`${errorMessage} jwt 'aud' does not match expected value.`
|
|
713
|
+
);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
if (options.expectedIssuer && options.expectedIssuer !== options.payload.iss) {
|
|
717
|
+
throw new JwtVerificationError(
|
|
718
|
+
`${errorMessage} jwt 'iss' does not match expected value.`
|
|
719
|
+
);
|
|
720
|
+
}
|
|
721
|
+
if (options.expectedNonce && options.expectedNonce !== options.payload.nonce) {
|
|
722
|
+
throw new JwtVerificationError(
|
|
723
|
+
`${errorMessage} jwt 'nonce' does not match expected value.`
|
|
724
|
+
);
|
|
725
|
+
}
|
|
726
|
+
if (options.expectedSubject && options.expectedSubject !== options.payload.sub) {
|
|
727
|
+
throw new JwtVerificationError(
|
|
728
|
+
`${errorMessage} jwt 'sub' does not match expected value.`
|
|
729
|
+
);
|
|
730
|
+
}
|
|
731
|
+
if (options.requiredClaims) {
|
|
732
|
+
for (const claim of options.requiredClaims) {
|
|
733
|
+
if (!options.payload[claim]) {
|
|
734
|
+
throw new JwtVerificationError(
|
|
735
|
+
`${errorMessage} jwt '${claim}' is missing.`
|
|
736
|
+
);
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
return {
|
|
741
|
+
signer: {
|
|
742
|
+
...options.signer,
|
|
743
|
+
publicJwk: signerJwk
|
|
744
|
+
}
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// src/crypto/jwt/z-jwe.ts
|
|
749
|
+
import { z as z4 } from "zod";
|
|
750
|
+
var zCompactJwe = z4.string().regex(
|
|
751
|
+
/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/,
|
|
752
|
+
{
|
|
753
|
+
message: "Not a valid compact jwe"
|
|
754
|
+
}
|
|
755
|
+
);
|
|
756
|
+
|
|
757
|
+
// src/fetcher.ts
|
|
758
|
+
var defaultFetcher = fetch;
|
|
759
|
+
function createFetcher(fetcher = defaultFetcher) {
|
|
760
|
+
return (input, init) => fetcher(
|
|
761
|
+
input,
|
|
762
|
+
init ? {
|
|
763
|
+
...init,
|
|
764
|
+
body: init.body instanceof URLSearchParams ? init.body.toString() : init.body
|
|
765
|
+
} : void 0
|
|
766
|
+
).catch((error) => {
|
|
767
|
+
throw new Error(
|
|
768
|
+
`Unknown error occurred during fetch to '${String(input)}'
|
|
769
|
+
Cause: ${error instanceof Error ? error.message : String(error)}`
|
|
770
|
+
);
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
var hasStatusOrThrow = (status, customError) => async (res) => {
|
|
774
|
+
if (Array.isArray(status) ? !status.includes(res.status) : res.status !== status) {
|
|
775
|
+
const ErrorClass = customError ?? UnexpectedStatusCodeError;
|
|
776
|
+
throw new ErrorClass({
|
|
777
|
+
message: `Http request failed. Expected ${status}, got ${res.status}, url: ${res.url}`,
|
|
778
|
+
reason: await parseRawHttpResponse(res),
|
|
779
|
+
// Pass the response body as reason so the original error can surface
|
|
780
|
+
statusCode: res.status
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
return res;
|
|
784
|
+
};
|
|
785
|
+
var parseRawHttpResponse = (response) => response.headers.get("content-type")?.includes("application/json") ? response.json() : response.text();
|
|
786
|
+
|
|
787
|
+
// src/url.ts
|
|
788
|
+
function objectToQueryParams(object) {
|
|
789
|
+
const params = new URLSearchParams();
|
|
790
|
+
for (const [key, value] of Object.entries(object)) {
|
|
791
|
+
if (value != null) {
|
|
792
|
+
params.append(
|
|
793
|
+
key,
|
|
794
|
+
typeof value === "object" ? JSON.stringify(value) : String(value)
|
|
795
|
+
);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
return params;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
// src/validation.ts
|
|
802
|
+
import z5 from "zod";
|
|
803
|
+
var zItwSupportedSignatureAlg = z5.enum([
|
|
211
804
|
"ES256",
|
|
212
805
|
"ES384",
|
|
213
806
|
"ES512",
|
|
@@ -215,7 +808,7 @@ var zItwSupportedSignatureAlg = z.enum([
|
|
|
215
808
|
"PS384",
|
|
216
809
|
"PS512"
|
|
217
810
|
]);
|
|
218
|
-
var zHttpMethod =
|
|
811
|
+
var zHttpMethod = z5.enum([
|
|
219
812
|
"GET",
|
|
220
813
|
"POST",
|
|
221
814
|
"PUT",
|
|
@@ -265,28 +858,6 @@ function dispatchByVersion(version, handlers) {
|
|
|
265
858
|
}
|
|
266
859
|
return handler();
|
|
267
860
|
}
|
|
268
|
-
|
|
269
|
-
// src/index.ts
|
|
270
|
-
import {
|
|
271
|
-
HashAlgorithm,
|
|
272
|
-
calculateJwkThumbprint,
|
|
273
|
-
decodeJwt
|
|
274
|
-
} from "@openid4vc/oauth2";
|
|
275
|
-
import {
|
|
276
|
-
ContentType,
|
|
277
|
-
JsonParseError,
|
|
278
|
-
addSecondsToDate,
|
|
279
|
-
createFetcher,
|
|
280
|
-
dateToSeconds,
|
|
281
|
-
decodeBase64,
|
|
282
|
-
decodeUtf8String,
|
|
283
|
-
encodeToBase64Url,
|
|
284
|
-
encodeToUtf8String,
|
|
285
|
-
objectToQueryParams,
|
|
286
|
-
parseIfJson,
|
|
287
|
-
setGlobalConfig,
|
|
288
|
-
stringToJsonWithErrorHandling
|
|
289
|
-
} from "@openid4vc/utils";
|
|
290
861
|
export {
|
|
291
862
|
CLOCK_SKEW_TOLERANCE_SECONDS,
|
|
292
863
|
CONTENT_TYPES,
|
|
@@ -297,7 +868,10 @@ export {
|
|
|
297
868
|
ItWalletSpecsVersion,
|
|
298
869
|
ItWalletSpecsVersionError,
|
|
299
870
|
JsonParseError,
|
|
871
|
+
JwtParseError,
|
|
872
|
+
JwtVerificationError,
|
|
300
873
|
MAX_IAT_AGE_SECONDS,
|
|
874
|
+
MAX_JTI_LENGTH,
|
|
301
875
|
UnexpectedStatusCodeError,
|
|
302
876
|
ValidationError,
|
|
303
877
|
addSecondsToDate,
|
|
@@ -307,6 +881,7 @@ export {
|
|
|
307
881
|
dateToSeconds,
|
|
308
882
|
decodeBase64,
|
|
309
883
|
decodeJwt,
|
|
884
|
+
decodeJwtHeader,
|
|
310
885
|
decodeUtf8String,
|
|
311
886
|
dispatchByVersion,
|
|
312
887
|
encodeToBase64Url,
|
|
@@ -315,15 +890,27 @@ export {
|
|
|
315
890
|
formatZodError,
|
|
316
891
|
hasConfigVersion,
|
|
317
892
|
hasStatusOrThrow,
|
|
893
|
+
jwtHeaderFromJwtSigner,
|
|
894
|
+
jwtSignerFromJwt,
|
|
318
895
|
objectToQueryParams,
|
|
319
896
|
parseIfJson,
|
|
320
897
|
parseRawHttpResponse,
|
|
321
898
|
parseWithErrorHandling,
|
|
322
899
|
serializeAttrs,
|
|
323
|
-
setGlobalConfig,
|
|
324
900
|
stringToJsonWithErrorHandling,
|
|
901
|
+
verifyJwt,
|
|
325
902
|
verifyJwtIatOrThrow,
|
|
903
|
+
zAlgValueNotNone,
|
|
904
|
+
zCertificateChain,
|
|
905
|
+
zCompactJwe,
|
|
906
|
+
zCompactJwt,
|
|
326
907
|
zHttpMethod,
|
|
327
|
-
zItwSupportedSignatureAlg
|
|
908
|
+
zItwSupportedSignatureAlg,
|
|
909
|
+
zJwk,
|
|
910
|
+
zJwkSet,
|
|
911
|
+
zJwtConfirmationPayload,
|
|
912
|
+
zJwtHeader,
|
|
913
|
+
zJwtPayload,
|
|
914
|
+
zTrustChain
|
|
328
915
|
};
|
|
329
916
|
//# sourceMappingURL=index.mjs.map
|