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