@pagopa/io-wallet-oid4vp 1.5.7 → 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.js CHANGED
@@ -39,7 +39,7 @@ __export(index_exports, {
39
39
  ParseAuthorizeRequestError: () => ParseAuthorizeRequestError,
40
40
  createAuthorizationRequest: () => createAuthorizationRequest,
41
41
  createAuthorizationResponse: () => createAuthorizationResponse3,
42
- createOpenid4vpAuthorizationResponse: () => import_openid4vp.createOpenid4vpAuthorizationResponse,
42
+ createOpenid4vpAuthorizationResponse: () => createOpenid4vpAuthorizationResponse,
43
43
  createParsedQrCode: () => createParsedQrCode,
44
44
  createX509HashClientId: () => createX509HashClientId,
45
45
  extractClientIdPrefix: () => extractClientIdPrefix,
@@ -71,7 +71,7 @@ module.exports = __toCommonJS(index_exports);
71
71
 
72
72
  // src/authorization-request/create-authorization-request.ts
73
73
  var import_io_wallet_oauth22 = require("@pagopa/io-wallet-oauth2");
74
- var import_io_wallet_utils2 = require("@pagopa/io-wallet-utils");
74
+ var import_io_wallet_utils3 = require("@pagopa/io-wallet-utils");
75
75
 
76
76
  // src/errors.ts
77
77
  var Oid4vpError = class extends Error {
@@ -188,6 +188,7 @@ function getLeafCertificate(certificateChain) {
188
188
  // src/authorization-request/z-authorization-request.ts
189
189
  var import_io_wallet_oauth2 = require("@pagopa/io-wallet-oauth2");
190
190
  var import_io_wallet_oid_federation = require("@pagopa/io-wallet-oid-federation");
191
+ var import_io_wallet_utils2 = require("@pagopa/io-wallet-utils");
191
192
  var import_zod = require("zod");
192
193
  var zOpenid4vpCredentialVerifierMetadata = import_io_wallet_oid_federation.itWalletCredentialVerifierMetadataV1_3.partial({
193
194
  application_type: true,
@@ -214,36 +215,40 @@ var zOpenid4vpAuthorizationRequestPayload = import_zod.z.looseObject({
214
215
  wallet_nonce: import_zod.z.string().optional()
215
216
  }).and(
216
217
  import_zod.z.object({
217
- ...import_io_wallet_oauth2.zJwtPayload.shape
218
+ ...import_io_wallet_utils2.zJwtPayload.shape
218
219
  })
219
220
  );
220
221
  var zOpenid4vpAuthorizationRequestHeaderBase = import_zod.z.object({
221
- alg: import_io_wallet_oauth2.zAlgValueNotNone,
222
+ alg: import_io_wallet_utils2.zAlgValueNotNone,
222
223
  kid: import_zod.z.string(),
223
224
  typ: import_io_wallet_oauth2.zSignedAuthorizationRequestJwtHeaderTyp
224
225
  });
225
226
  var zOpenid4vpAuthorizationRequestHeaderV1_0 = zOpenid4vpAuthorizationRequestHeaderBase.extend({
226
- trust_chain: import_io_wallet_oauth2.zTrustChain
227
+ trust_chain: import_io_wallet_utils2.zTrustChain
227
228
  }).loose();
228
229
  var zOpenid4vpAuthorizationRequestHeaderV1_3 = zOpenid4vpAuthorizationRequestHeaderBase.extend({
229
230
  // kid is only mandatory for openid_federation/legacy; x509_hash RPs use x5c instead (see getPublicKeyForVerification).
230
231
  kid: import_zod.z.string().optional(),
231
- trust_chain: import_io_wallet_oauth2.zTrustChain.optional(),
232
- x5c: import_io_wallet_oauth2.zCertificateChain.optional()
232
+ trust_chain: import_io_wallet_utils2.zTrustChain.optional(),
233
+ x5c: import_io_wallet_utils2.zCertificateChain.optional()
233
234
  }).loose();
234
235
 
235
236
  // src/authorization-request/create-authorization-request.ts
236
- var dispatchCreateAuthorizationRequest = (0, import_io_wallet_utils2.createVersionDispatcher)({
237
- [import_io_wallet_utils2.ItWalletSpecsVersion.V1_0]: async (o) => createAuthorizationRequestWithHeader(
237
+ var dispatchCreateAuthorizationRequest = (0, import_io_wallet_utils3.createVersionDispatcher)({
238
+ [import_io_wallet_utils3.ItWalletSpecsVersion.APTITUDE]: async (o) => createAuthorizationRequestWithHeader(
239
+ o,
240
+ zOpenid4vpAuthorizationRequestHeaderV1_3
241
+ ),
242
+ [import_io_wallet_utils3.ItWalletSpecsVersion.V1_0]: async (o) => createAuthorizationRequestWithHeader(
238
243
  o,
239
244
  zOpenid4vpAuthorizationRequestHeaderV1_0
240
245
  ),
241
- [import_io_wallet_utils2.ItWalletSpecsVersion.V1_3]: async (o) => createAuthorizationRequestWithHeader(
246
+ [import_io_wallet_utils3.ItWalletSpecsVersion.V1_3]: async (o) => createAuthorizationRequestWithHeader(
242
247
  o,
243
248
  zOpenid4vpAuthorizationRequestHeaderV1_3
244
249
  ),
245
250
  // V1_4 reuses the V1_3 JAR header schema; the conditional x5c rule is a normative 1.4.4 LTS backport.
246
- [import_io_wallet_utils2.ItWalletSpecsVersion.V1_4]: async (o) => createAuthorizationRequestWithHeader(
251
+ [import_io_wallet_utils3.ItWalletSpecsVersion.V1_4]: async (o) => createAuthorizationRequestWithHeader(
247
252
  o,
248
253
  zOpenid4vpAuthorizationRequestHeaderV1_3
249
254
  )
@@ -252,7 +257,7 @@ async function createAuthorizationRequest(options) {
252
257
  try {
253
258
  return await dispatchCreateAuthorizationRequest(options);
254
259
  } catch (error) {
255
- if (error instanceof import_io_wallet_utils2.ValidationError) {
260
+ if (error instanceof import_io_wallet_utils3.ValidationError) {
256
261
  throw new Oid4vpError(`Invalid authorization request: ${error.message}`);
257
262
  }
258
263
  throw error;
@@ -260,11 +265,11 @@ async function createAuthorizationRequest(options) {
260
265
  }
261
266
  async function createAuthorizationRequestWithHeader(options, headerSchema) {
262
267
  const { callbacks, jar, scheme = "openid4vp://" } = options;
263
- const authorizationRequestHeader = (0, import_io_wallet_utils2.parseWithErrorHandling)(headerSchema, {
264
- ...(0, import_io_wallet_oauth22.jwtHeaderFromJwtSigner)(jar.jwtSigner),
268
+ const authorizationRequestHeader = (0, import_io_wallet_utils3.parseWithErrorHandling)(headerSchema, {
269
+ ...(0, import_io_wallet_utils3.jwtHeaderFromJwtSigner)(jar.jwtSigner),
265
270
  typ: import_io_wallet_oauth22.signedAuthorizationRequestJwtHeaderTyp
266
271
  });
267
- const authorizationRequestPayload = (0, import_io_wallet_utils2.parseWithErrorHandling)(
272
+ const authorizationRequestPayload = (0, import_io_wallet_utils3.parseWithErrorHandling)(
268
273
  zOpenid4vpAuthorizationRequestPayload,
269
274
  options.authorizationRequestPayload
270
275
  );
@@ -300,13 +305,13 @@ function createAuthorizationRequestUrl(scheme, request) {
300
305
  const url = new URL(scheme);
301
306
  const searchParams = new URLSearchParams([
302
307
  ...url.searchParams.entries(),
303
- ...(0, import_io_wallet_utils2.objectToQueryParams)(request).entries()
308
+ ...(0, import_io_wallet_utils3.objectToQueryParams)(request).entries()
304
309
  ]);
305
310
  url.search = searchParams.toString();
306
311
  return url.toString();
307
312
  }
308
313
  function validateJarSignerForAuthorizationRequest(specsVersion, jwtSigner, payload) {
309
- if (specsVersion === import_io_wallet_utils2.ItWalletSpecsVersion.V1_0) {
314
+ if (specsVersion === import_io_wallet_utils3.ItWalletSpecsVersion.V1_0) {
310
315
  return;
311
316
  }
312
317
  const { prefix } = extractClientIdPrefix(payload.client_id);
@@ -323,8 +328,7 @@ function validateJarSignerForAuthorizationRequest(specsVersion, jwtSigner, paylo
323
328
  }
324
329
 
325
330
  // src/authorization-request/fetch-authorization-request.ts
326
- var import_oauth2 = require("@openid4vc/oauth2");
327
- var import_io_wallet_utils3 = require("@pagopa/io-wallet-utils");
331
+ var import_io_wallet_utils4 = require("@pagopa/io-wallet-utils");
328
332
 
329
333
  // src/authorization-request/validate-authorization-request.ts
330
334
  function validateAuthorizationRequestParams(params) {
@@ -461,7 +465,7 @@ function createParsedQrCode(authorizeRequestUrl) {
461
465
  };
462
466
  }
463
467
  async function fetchRequestObjectJwt(requestUri, options) {
464
- const fetch = (0, import_io_wallet_utils3.createFetcher)(options.fetch);
468
+ const fetch = (0, import_io_wallet_utils4.createFetcher)(options.fetch);
465
469
  const requestInit = {
466
470
  method: options.method.toUpperCase()
467
471
  };
@@ -482,7 +486,7 @@ async function fetchRequestObjectJwt(requestUri, options) {
482
486
  requestInit.body = formData.toString();
483
487
  }
484
488
  const response = await fetch(requestUri, requestInit);
485
- await (0, import_io_wallet_utils3.hasStatusOrThrow)(200, import_io_wallet_utils3.UnexpectedStatusCodeError)(response);
489
+ await (0, import_io_wallet_utils4.hasStatusOrThrow)(200, import_io_wallet_utils4.UnexpectedStatusCodeError)(response);
486
490
  return await response.text();
487
491
  }
488
492
  async function fetchAuthorizationRequest(options) {
@@ -520,7 +524,7 @@ async function fetchAuthorizationRequest(options) {
520
524
  sendBy: parsedQrCode.request ? "value" : "reference"
521
525
  };
522
526
  } catch (error) {
523
- if (error instanceof import_io_wallet_utils3.ValidationError || error instanceof import_oauth2.Oauth2JwtParseError || error instanceof Oid4vpError || error instanceof import_io_wallet_utils3.UnexpectedStatusCodeError) {
527
+ if (error instanceof import_io_wallet_utils4.ValidationError || error instanceof import_io_wallet_utils4.JwtParseError || error instanceof Oid4vpError || error instanceof import_io_wallet_utils4.UnexpectedStatusCodeError) {
524
528
  throw error;
525
529
  }
526
530
  throw new Oid4vpError(
@@ -530,9 +534,7 @@ async function fetchAuthorizationRequest(options) {
530
534
  }
531
535
 
532
536
  // src/authorization-request/parse-authorization-request.ts
533
- var import_oauth22 = require("@openid4vc/oauth2");
534
- var import_io_wallet_oauth23 = require("@pagopa/io-wallet-oauth2");
535
- var import_io_wallet_utils4 = require("@pagopa/io-wallet-utils");
537
+ var import_io_wallet_utils5 = require("@pagopa/io-wallet-utils");
536
538
  function getPublicKeyForVerification(options) {
537
539
  const { header, payload } = options;
538
540
  const { prefix: clientIdPrefix } = extractClientIdPrefix(payload.client_id);
@@ -569,18 +571,19 @@ function getPublicKeyForVerification(options) {
569
571
  }
570
572
  async function parseAuthorizeRequest(options) {
571
573
  try {
572
- const headerSchema = (0, import_io_wallet_utils4.dispatchByVersion)(options.config.itWalletSpecsVersion, {
573
- [import_io_wallet_utils4.ItWalletSpecsVersion.V1_0]: () => zOpenid4vpAuthorizationRequestHeaderV1_0,
574
- [import_io_wallet_utils4.ItWalletSpecsVersion.V1_3]: () => zOpenid4vpAuthorizationRequestHeaderV1_3,
575
- [import_io_wallet_utils4.ItWalletSpecsVersion.V1_4]: () => zOpenid4vpAuthorizationRequestHeaderV1_3
574
+ const headerSchema = (0, import_io_wallet_utils5.dispatchByVersion)(options.config.itWalletSpecsVersion, {
575
+ [import_io_wallet_utils5.ItWalletSpecsVersion.APTITUDE]: () => zOpenid4vpAuthorizationRequestHeaderV1_3,
576
+ [import_io_wallet_utils5.ItWalletSpecsVersion.V1_0]: () => zOpenid4vpAuthorizationRequestHeaderV1_0,
577
+ [import_io_wallet_utils5.ItWalletSpecsVersion.V1_3]: () => zOpenid4vpAuthorizationRequestHeaderV1_3,
578
+ [import_io_wallet_utils5.ItWalletSpecsVersion.V1_4]: () => zOpenid4vpAuthorizationRequestHeaderV1_3
576
579
  });
577
- const decoded = (0, import_io_wallet_oauth23.decodeJwt)({
580
+ const decoded = (0, import_io_wallet_utils5.decodeJwt)({
578
581
  errorMessagePrefix: "Error decoding authorization request JWT:",
579
582
  headerSchema,
580
583
  jwt: options.requestObjectJwt,
581
584
  payloadSchema: zOpenid4vpAuthorizationRequestPayload
582
585
  });
583
- if (options.config.itWalletSpecsVersion !== import_io_wallet_utils4.ItWalletSpecsVersion.V1_0) {
586
+ if (options.config.itWalletSpecsVersion !== import_io_wallet_utils5.ItWalletSpecsVersion.V1_0) {
584
587
  await validateAuthorizationRequestClientBinding({
585
588
  hash: options.callbacks?.hash,
586
589
  header: decoded.header,
@@ -605,7 +608,7 @@ async function parseAuthorizeRequest(options) {
605
608
  header: decoded.header,
606
609
  payload: decoded.payload
607
610
  });
608
- await (0, import_oauth22.verifyJwt)({
611
+ await (0, import_io_wallet_utils5.verifyJwt)({
609
612
  compact: options.requestObjectJwt,
610
613
  errorMessage: "Error verifying Request Object signature",
611
614
  header: decoded.header,
@@ -625,7 +628,7 @@ async function parseAuthorizeRequest(options) {
625
628
  }
626
629
  };
627
630
  } catch (error) {
628
- if (error instanceof import_io_wallet_utils4.ItWalletSpecsVersionError || error instanceof import_io_wallet_utils4.ValidationError || error instanceof import_oauth22.Oauth2JwtParseError || error instanceof Oid4vpError)
631
+ if (error instanceof import_io_wallet_utils5.ItWalletSpecsVersionError || error instanceof import_io_wallet_utils5.ValidationError || error instanceof import_io_wallet_utils5.JwtParseError || error instanceof Oid4vpError)
629
632
  throw error;
630
633
  throw new ParseAuthorizeRequestError(
631
634
  `Unexpected error during Request Object parsing: ${error instanceof Error ? error.message : String(error)}`
@@ -634,10 +637,10 @@ async function parseAuthorizeRequest(options) {
634
637
  }
635
638
 
636
639
  // src/authorization-response/create-authorization-response.ts
637
- var import_io_wallet_utils6 = require("@pagopa/io-wallet-utils");
640
+ var import_io_wallet_utils7 = require("@pagopa/io-wallet-utils");
638
641
 
639
642
  // src/authorization-response/build-jarm-response.ts
640
- var import_io_wallet_utils5 = require("@pagopa/io-wallet-utils");
643
+ var import_io_wallet_utils6 = require("@pagopa/io-wallet-utils");
641
644
 
642
645
  // src/jarm/jarm-extract-jwks.ts
643
646
  function extractEncryptionJwkFromJwks(jwks, {
@@ -693,8 +696,8 @@ async function buildJarmResponse(options) {
693
696
  const nonceBytes = await options.callbacks.generateRandom(32);
694
697
  const jweEncryptor = {
695
698
  alg,
696
- apu: (0, import_io_wallet_utils5.encodeToBase64Url)(nonceBytes),
697
- apv: (0, import_io_wallet_utils5.encodeToBase64Url)(options.requestObject.nonce),
699
+ apu: (0, import_io_wallet_utils6.encodeToBase64Url)(nonceBytes),
700
+ apv: (0, import_io_wallet_utils6.encodeToBase64Url)(options.requestObject.nonce),
698
701
  enc,
699
702
  kid: encryptionJwk.kid,
700
703
  method: "jwk",
@@ -773,17 +776,20 @@ async function createAuthorizationResponse2(options) {
773
776
  }
774
777
 
775
778
  // src/authorization-response/create-authorization-response.ts
776
- var dispatchCreateAuthorizationResponse = (0, import_io_wallet_utils6.createVersionDispatcher)({
777
- [import_io_wallet_utils6.ItWalletSpecsVersion.V1_0]: (o) => (
779
+ var dispatchCreateAuthorizationResponse = (0, import_io_wallet_utils7.createVersionDispatcher)({
780
+ [import_io_wallet_utils7.ItWalletSpecsVersion.APTITUDE]: (o) => createAuthorizationResponse2(
781
+ o
782
+ ),
783
+ [import_io_wallet_utils7.ItWalletSpecsVersion.V1_0]: (o) => (
778
784
  // V1_0 shares the v1.3 response logic — JWKS / enc resolution is identical.
779
785
  createAuthorizationResponse(
780
786
  o
781
787
  )
782
788
  ),
783
- [import_io_wallet_utils6.ItWalletSpecsVersion.V1_3]: (o) => createAuthorizationResponse(
789
+ [import_io_wallet_utils7.ItWalletSpecsVersion.V1_3]: (o) => createAuthorizationResponse(
784
790
  o
785
791
  ),
786
- [import_io_wallet_utils6.ItWalletSpecsVersion.V1_4]: (o) => createAuthorizationResponse2(
792
+ [import_io_wallet_utils7.ItWalletSpecsVersion.V1_4]: (o) => createAuthorizationResponse2(
787
793
  o
788
794
  )
789
795
  });
@@ -791,8 +797,218 @@ async function createAuthorizationResponse3(options) {
791
797
  return dispatchCreateAuthorizationResponse(options);
792
798
  }
793
799
 
800
+ // src/authorization-response/create-openid4vp-authorization-response.ts
801
+ var import_io_wallet_oauth23 = require("@pagopa/io-wallet-oauth2");
802
+ var import_io_wallet_utils8 = require("@pagopa/io-wallet-utils");
803
+ var jarmResponseModes = /* @__PURE__ */ new Set([
804
+ "jwt",
805
+ "query.jwt",
806
+ "fragment.jwt",
807
+ "form_post.jwt",
808
+ "direct_post.jwt",
809
+ "dc_api.jwt"
810
+ ]);
811
+ function assertValueSupported(options) {
812
+ const { actual, errorMessage, supported } = options;
813
+ const found = supported.find((value) => value === actual);
814
+ if (!found) {
815
+ throw new import_io_wallet_oauth23.Oauth2Error(errorMessage);
816
+ }
817
+ return found;
818
+ }
819
+ function assertClientJarmMetadataSupported(clientMetadata, serverMetadata) {
820
+ const {
821
+ authorization_encrypted_response_alg: encryptedAlg,
822
+ authorization_encrypted_response_enc: encryptedEnc,
823
+ authorization_signed_response_alg: signedAlg
824
+ } = clientMetadata;
825
+ if (encryptedAlg) {
826
+ assertValueSupported({
827
+ actual: encryptedAlg,
828
+ errorMessage: `Invalid authorization_encrypted_response_alg ${encryptedAlg}`,
829
+ supported: serverMetadata.authorization_encryption_alg_values_supported
830
+ });
831
+ }
832
+ if (encryptedEnc) {
833
+ assertValueSupported({
834
+ actual: encryptedEnc,
835
+ errorMessage: `Invalid authorization_encrypted_response_enc ${encryptedEnc}`,
836
+ supported: serverMetadata.authorization_encryption_enc_values_supported
837
+ });
838
+ }
839
+ if (signedAlg && serverMetadata.authorization_signed_response_alg_values_supported) {
840
+ assertValueSupported({
841
+ actual: signedAlg,
842
+ errorMessage: `Invalid authorization_signed_response_alg ${signedAlg}`,
843
+ supported: serverMetadata.authorization_signed_response_alg_values_supported
844
+ });
845
+ }
846
+ }
847
+ async function fetchJwks(jwksUrl, fetchCallback) {
848
+ if (!fetchCallback) {
849
+ throw new import_io_wallet_oauth23.Oauth2Error(
850
+ `Missing fetch callback required to resolve jwks_uri '${jwksUrl}'.`
851
+ );
852
+ }
853
+ const response = await fetchCallback(jwksUrl, {
854
+ headers: {
855
+ Accept: `${import_io_wallet_utils8.ContentType.JwkSet}, ${import_io_wallet_utils8.ContentType.Json};q=0.9`
856
+ }
857
+ });
858
+ await (0, import_io_wallet_utils8.hasStatusOrThrow)(200, import_io_wallet_utils8.UnexpectedStatusCodeError)(response);
859
+ return (0, import_io_wallet_utils8.parseWithErrorHandling)(
860
+ import_io_wallet_utils8.zJwkSet,
861
+ await response.json(),
862
+ `Validation of JWKs from jwks_uri '${jwksUrl}' failed`
863
+ );
864
+ }
865
+ async function createJarmAuthorizationResponse(options) {
866
+ const { callbacks, jarmAuthorizationResponse, jweEncryptor, jwtSigner } = options;
867
+ if (!jwtSigner && jweEncryptor) {
868
+ const { encryptionJwk: encryptionJwk2, jwe: jwe2 } = await callbacks.encryptJwe(
869
+ jweEncryptor,
870
+ JSON.stringify(jarmAuthorizationResponse)
871
+ );
872
+ return {
873
+ encryptionJwk: encryptionJwk2,
874
+ jarmAuthorizationResponseJwt: jwe2
875
+ };
876
+ }
877
+ if (jwtSigner && !jweEncryptor) {
878
+ const { jwt } = await callbacks.signJwt(jwtSigner, {
879
+ header: (0, import_io_wallet_utils8.jwtHeaderFromJwtSigner)(jwtSigner),
880
+ payload: jarmAuthorizationResponse
881
+ });
882
+ return {
883
+ jarmAuthorizationResponseJwt: jwt
884
+ };
885
+ }
886
+ if (!jwtSigner || !jweEncryptor) {
887
+ throw new import_io_wallet_oauth23.Oauth2Error(
888
+ "JWT signer and/or encryptor are required to create a JARM auth response."
889
+ );
890
+ }
891
+ const signed = await callbacks.signJwt(jwtSigner, {
892
+ header: (0, import_io_wallet_utils8.jwtHeaderFromJwtSigner)(jwtSigner),
893
+ payload: jarmAuthorizationResponse
894
+ });
895
+ const { encryptionJwk, jwe } = await callbacks.encryptJwe(
896
+ jweEncryptor,
897
+ signed.jwt
898
+ );
899
+ return {
900
+ encryptionJwk,
901
+ jarmAuthorizationResponseJwt: jwe
902
+ };
903
+ }
904
+ async function createOpenid4vpAuthorizationResponse(options) {
905
+ const { authorizationRequestPayload, jarm } = options;
906
+ const authorizationResponsePayload = {
907
+ ...options.authorizationResponsePayload,
908
+ state: authorizationRequestPayload.state
909
+ };
910
+ const { prefix: clientIdPrefix } = extractClientIdPrefix(
911
+ authorizationRequestPayload.client_id
912
+ );
913
+ if (authorizationRequestPayload.response_mode && jarmResponseModes.has(authorizationRequestPayload.response_mode) && !jarm) {
914
+ throw new import_io_wallet_oauth23.Oauth2Error(
915
+ `Missing jarm options for creating Jarm response with response mode '${authorizationRequestPayload.response_mode}'`
916
+ );
917
+ }
918
+ if (!jarm) {
919
+ return { authorizationResponsePayload };
920
+ }
921
+ if ((clientIdPrefix === "openid_federation" /* OPENID_FEDERATION */ || clientIdPrefix === "none" /* NONE */) && !options.clientMetadata) {
922
+ throw new import_io_wallet_oauth23.Oauth2Error(
923
+ "When OpenID Federation is used as the client id prefix (https/openid_federation), passing externally fetched and verified 'clientMetadata' to 'createOpenid4vpAuthorizationResponse' is required."
924
+ );
925
+ }
926
+ const clientMetadata = options.clientMetadata ?? authorizationRequestPayload.client_metadata;
927
+ if (!clientMetadata) {
928
+ throw new import_io_wallet_oauth23.Oauth2Error(
929
+ "Missing client metadata in the request params to assert Jarm metadata support."
930
+ );
931
+ }
932
+ const jwks = clientMetadata.jwks ? clientMetadata.jwks : clientMetadata.jwks_uri ? await fetchJwks(clientMetadata.jwks_uri, options.callbacks.fetch) : void 0;
933
+ if (!jwks) {
934
+ throw new import_io_wallet_oauth23.Oauth2Error(
935
+ "Missing 'jwks' or 'jwks_uri' in client metadata. Cannot extract encryption JWK."
936
+ );
937
+ }
938
+ assertClientJarmMetadataSupported(clientMetadata, jarm.serverMetadata);
939
+ const encryptionJwk = jarm.encryption?.jwk ?? extractEncryptionJwkFromJwks(jwks, {
940
+ supportedAlgValues: jarm.serverMetadata.authorization_encryption_alg_values_supported ?? (clientMetadata.authorization_encrypted_response_alg ? [clientMetadata.authorization_encrypted_response_alg] : void 0)
941
+ });
942
+ if (!encryptionJwk) {
943
+ throw new import_io_wallet_oauth23.Oauth2Error(
944
+ "No encryption JWK provided and could not extract encryption JWK from client metadata. Failed to create JARM response."
945
+ );
946
+ }
947
+ const enc = clientMetadata.encrypted_response_enc_values_supported ? jarm.serverMetadata.authorization_encryption_enc_values_supported.find(
948
+ (value) => clientMetadata.encrypted_response_enc_values_supported?.includes(
949
+ value
950
+ )
951
+ ) ?? clientMetadata.encrypted_response_enc_values_supported[0] ?? "A128GCM" : clientMetadata.authorization_encrypted_response_enc ?? "A128GCM";
952
+ assertValueSupported({
953
+ actual: enc,
954
+ errorMessage: `Invalid 'enc' value ${enc}. Supported values are ${jarm.serverMetadata.authorization_encryption_enc_values_supported.join(", ")}`,
955
+ supported: jarm.serverMetadata.authorization_encryption_enc_values_supported
956
+ });
957
+ const alg = encryptionJwk.alg ?? clientMetadata.authorization_encrypted_response_alg ?? "ECDH-ES";
958
+ assertValueSupported({
959
+ actual: alg,
960
+ errorMessage: `Invalid 'alg' value ${alg}. Supported values are ${jarm.serverMetadata.authorization_encryption_alg_values_supported.join(", ")}`,
961
+ supported: jarm.serverMetadata.authorization_encryption_alg_values_supported
962
+ });
963
+ if (clientMetadata.authorization_signed_response_alg && jarm.jwtSigner && clientMetadata.authorization_signed_response_alg !== jarm.jwtSigner.alg) {
964
+ throw new import_io_wallet_oauth23.Oauth2Error(
965
+ `Invalid signed response alg value ${jarm.jwtSigner.alg}. Expected ${clientMetadata.authorization_signed_response_alg}.`
966
+ );
967
+ }
968
+ const additionalJwtPayload = jarm.jwtSigner ? {
969
+ aud: jarm.audience ?? (() => {
970
+ throw new import_io_wallet_oauth23.Oauth2Error(
971
+ "Missing required aud in JARM configuration for creating OpenID4VP authorization response."
972
+ );
973
+ })(),
974
+ exp: jarm.expiresInSeconds ? (0, import_io_wallet_utils8.dateToSeconds)((0, import_io_wallet_utils8.addSecondsToDate)(/* @__PURE__ */ new Date(), jarm.expiresInSeconds)) : (0, import_io_wallet_utils8.dateToSeconds)((0, import_io_wallet_utils8.addSecondsToDate)(/* @__PURE__ */ new Date(), 600)),
975
+ iss: jarm.authorizationServer ?? (() => {
976
+ throw new import_io_wallet_oauth23.Oauth2Error(
977
+ "Missing required iss in JARM configuration for creating OpenID4VP authorization response."
978
+ );
979
+ })()
980
+ } : void 0;
981
+ const jarmResponsePayload = {
982
+ ...authorizationResponsePayload,
983
+ ...additionalJwtPayload
984
+ };
985
+ const { encryptionJwk: usedEncryptionJwk, jarmAuthorizationResponseJwt } = await createJarmAuthorizationResponse({
986
+ callbacks: {
987
+ encryptJwe: options.callbacks.encryptJwe,
988
+ signJwt: options.callbacks.signJwt
989
+ },
990
+ jarmAuthorizationResponse: jarmResponsePayload,
991
+ jweEncryptor: jarm.encryption ? {
992
+ alg,
993
+ apu: jarm.encryption.nonce ? (0, import_io_wallet_utils8.encodeToBase64Url)(jarm.encryption.nonce) : void 0,
994
+ apv: (0, import_io_wallet_utils8.encodeToBase64Url)(authorizationRequestPayload.nonce),
995
+ enc,
996
+ method: "jwk",
997
+ publicJwk: encryptionJwk
998
+ } : void 0,
999
+ jwtSigner: jarm.jwtSigner
1000
+ });
1001
+ return {
1002
+ authorizationResponsePayload: jarmResponsePayload,
1003
+ jarm: {
1004
+ encryptionJwk: usedEncryptionJwk,
1005
+ responseJwt: jarmAuthorizationResponseJwt
1006
+ }
1007
+ };
1008
+ }
1009
+
794
1010
  // src/authorization-response/fetch-authorization-response.ts
795
- var import_io_wallet_utils7 = require("@pagopa/io-wallet-utils");
1011
+ var import_io_wallet_utils9 = require("@pagopa/io-wallet-utils");
796
1012
 
797
1013
  // src/authorization-response/z-authorization-response.ts
798
1014
  var import_zod4 = __toESM(require("zod"));
@@ -828,7 +1044,7 @@ async function fetchAuthorizationResponse(options) {
828
1044
  ]
829
1045
  });
830
1046
  }
831
- const fetch = (0, import_io_wallet_utils7.createFetcher)(options.callbacks.fetch);
1047
+ const fetch = (0, import_io_wallet_utils9.createFetcher)(options.callbacks.fetch);
832
1048
  const authorizationResponseResult = await fetch(
833
1049
  options.presentationResponseUri,
834
1050
  {
@@ -836,17 +1052,17 @@ async function fetchAuthorizationResponse(options) {
836
1052
  response: options.authorizationResponseJarm
837
1053
  }),
838
1054
  headers: {
839
- [import_io_wallet_utils7.HEADERS.CONTENT_TYPE]: import_io_wallet_utils7.CONTENT_TYPES.FORM_URLENCODED
1055
+ [import_io_wallet_utils9.HEADERS.CONTENT_TYPE]: import_io_wallet_utils9.CONTENT_TYPES.FORM_URLENCODED
840
1056
  },
841
1057
  method: "POST"
842
1058
  }
843
1059
  );
844
- await (0, import_io_wallet_utils7.hasStatusOrThrow)(
1060
+ await (0, import_io_wallet_utils9.hasStatusOrThrow)(
845
1061
  200,
846
- import_io_wallet_utils7.UnexpectedStatusCodeError
1062
+ import_io_wallet_utils9.UnexpectedStatusCodeError
847
1063
  )(authorizationResponseResult);
848
1064
  const authorizationResponseResultJson = await authorizationResponseResult.json();
849
- const parsedAuthorizationResponseResult = (0, import_io_wallet_utils7.parseWithErrorHandling)(
1065
+ const parsedAuthorizationResponseResult = (0, import_io_wallet_utils9.parseWithErrorHandling)(
850
1066
  zOpenid4vpAuthorizationResponseResult,
851
1067
  authorizationResponseResultJson
852
1068
  );
@@ -864,7 +1080,7 @@ async function fetchAuthorizationResponse(options) {
864
1080
  }
865
1081
  return parsedAuthorizationResponseResult;
866
1082
  } catch (error) {
867
- if (error instanceof import_io_wallet_utils7.UnexpectedStatusCodeError || error instanceof import_io_wallet_utils7.ValidationError || error instanceof FetchAuthorizationResponseError) {
1083
+ if (error instanceof import_io_wallet_utils9.UnexpectedStatusCodeError || error instanceof import_io_wallet_utils9.ValidationError || error instanceof FetchAuthorizationResponseError) {
868
1084
  throw error;
869
1085
  }
870
1086
  throw new FetchAuthorizationResponseError(
@@ -874,19 +1090,18 @@ async function fetchAuthorizationResponse(options) {
874
1090
  }
875
1091
 
876
1092
  // src/authorization-response/parse-authorization-response.ts
877
- var import_io_wallet_utils11 = require("@pagopa/io-wallet-utils");
1093
+ var import_io_wallet_utils14 = require("@pagopa/io-wallet-utils");
878
1094
 
879
1095
  // src/jarm/parse-jarm-authorization-response.ts
880
- var import_io_wallet_oauth26 = require("@pagopa/io-wallet-oauth2");
881
- var import_io_wallet_utils10 = require("@pagopa/io-wallet-utils");
1096
+ var import_io_wallet_utils13 = require("@pagopa/io-wallet-utils");
882
1097
  var import_zod7 = __toESM(require("zod"));
883
1098
 
884
1099
  // src/vp-token/parse-vp-token.ts
885
- var import_io_wallet_utils8 = require("@pagopa/io-wallet-utils");
1100
+ var import_io_wallet_utils10 = require("@pagopa/io-wallet-utils");
886
1101
  function parseVpToken(vpToken) {
887
- return (0, import_io_wallet_utils8.parseWithErrorHandling)(
1102
+ return (0, import_io_wallet_utils10.parseWithErrorHandling)(
888
1103
  zVpToken,
889
- (0, import_io_wallet_utils8.parseIfJson)(vpToken),
1104
+ (0, import_io_wallet_utils10.parseIfJson)(vpToken),
890
1105
  "Could not parse dcql vp_token. Expected an object where the values are encoded presentations"
891
1106
  );
892
1107
  }
@@ -905,22 +1120,21 @@ function validateOpenid4vpAuthorizationResponsePayload(options) {
905
1120
  }
906
1121
 
907
1122
  // src/jarm/verify-jarm-authorization-response.ts
908
- var import_oauth23 = require("@openid4vc/oauth2");
909
- var import_io_wallet_oauth25 = require("@pagopa/io-wallet-oauth2");
910
- var import_io_wallet_utils9 = require("@pagopa/io-wallet-utils");
1123
+ var import_io_wallet_oauth24 = require("@pagopa/io-wallet-oauth2");
1124
+ var import_io_wallet_utils12 = require("@pagopa/io-wallet-utils");
911
1125
  var import_zod6 = __toESM(require("zod"));
912
1126
 
913
1127
  // src/jarm/z-jarm.ts
914
- var import_io_wallet_oauth24 = require("@pagopa/io-wallet-oauth2");
1128
+ var import_io_wallet_utils11 = require("@pagopa/io-wallet-utils");
915
1129
  var import_zod5 = require("zod");
916
1130
  var zJarmHeader = import_zod5.z.object({
917
- ...import_io_wallet_oauth24.zJwtHeader.shape,
1131
+ ...import_io_wallet_utils11.zJwtHeader.shape,
918
1132
  apu: import_zod5.z.string().optional(),
919
1133
  apv: import_zod5.z.string().optional(),
920
1134
  kid: import_zod5.z.string()
921
1135
  });
922
1136
  var zEncryptedJarmHeader = import_zod5.z.object({
923
- ...import_io_wallet_oauth24.zJwtHeader.shape,
1137
+ ...import_io_wallet_utils11.zJwtHeader.shape,
924
1138
  apu: import_zod5.z.string().optional(),
925
1139
  apv: import_zod5.z.string().optional(),
926
1140
  enc: import_zod5.z.string().optional(),
@@ -932,12 +1146,12 @@ var zJarmAuthorizationResponse = import_zod5.z.looseObject({
932
1146
  * aud: The client_id of the client the response is intended for
933
1147
  * exp: The expiration time of the JWT. A maximum JWT lifetime of 10 minutes is RECOMMENDED.
934
1148
  */
935
- ...import_io_wallet_oauth24.zJwtPayload.shape,
936
- ...import_io_wallet_oauth24.zJwtPayload.pick({ aud: true, exp: true, iss: true }).required().shape,
1149
+ ...import_io_wallet_utils11.zJwtPayload.shape,
1150
+ ...import_io_wallet_utils11.zJwtPayload.pick({ aud: true, exp: true, iss: true }).required().shape,
937
1151
  state: import_zod5.z.optional(import_zod5.z.string())
938
1152
  });
939
1153
  var zJarmAuthorizationResponseEncryptedOnly = import_zod5.z.looseObject({
940
- ...import_io_wallet_oauth24.zJwtPayload.shape,
1154
+ ...import_io_wallet_utils11.zJwtPayload.shape,
941
1155
  state: import_zod5.z.optional(import_zod5.z.string())
942
1156
  });
943
1157
 
@@ -955,7 +1169,7 @@ var decryptJarmAuthorizationResponseJwt = async (options) => {
955
1169
  jarmAuthorizationResponseJwt
956
1170
  } = options;
957
1171
  let encryptionJwk;
958
- const { header } = (0, import_io_wallet_oauth25.decodeJwtHeader)({
1172
+ const { header } = (0, import_io_wallet_utils12.decodeJwtHeader)({
959
1173
  headerSchema: zEncryptedJarmHeader,
960
1174
  jwt: jarmAuthorizationResponseJwt
961
1175
  });
@@ -967,7 +1181,7 @@ var decryptJarmAuthorizationResponseJwt = async (options) => {
967
1181
  jwk: encryptionJwk
968
1182
  });
969
1183
  if (!result.decrypted) {
970
- throw new import_io_wallet_oauth25.Oauth2Error("Failed to decrypt jarm auth response.");
1184
+ throw new import_io_wallet_oauth24.Oauth2Error("Failed to decrypt jarm auth response.");
971
1185
  }
972
1186
  return {
973
1187
  decryptionJwk: result.decryptionJwk,
@@ -980,7 +1194,7 @@ async function verifyJarmAuthorizationResponse(options) {
980
1194
  callbacks,
981
1195
  jarmAuthorizationResponseJwt
982
1196
  } = options;
983
- const requestDataIsEncrypted = import_io_wallet_oauth25.zCompactJwe.safeParse(
1197
+ const requestDataIsEncrypted = import_io_wallet_utils12.zCompactJwe.safeParse(
984
1198
  jarmAuthorizationResponseJwt
985
1199
  ).success;
986
1200
  const decryptedRequestData = requestDataIsEncrypted ? await decryptJarmAuthorizationResponseJwt({
@@ -988,23 +1202,23 @@ async function verifyJarmAuthorizationResponse(options) {
988
1202
  callbacks,
989
1203
  jarmAuthorizationResponseJwt
990
1204
  }) : { decryptionJwk: void 0, payload: jarmAuthorizationResponseJwt };
991
- const responseIsSigned = import_io_wallet_oauth25.zCompactJwt.safeParse(
1205
+ const responseIsSigned = import_io_wallet_utils12.zCompactJwt.safeParse(
992
1206
  decryptedRequestData.payload
993
1207
  ).success;
994
1208
  if (!requestDataIsEncrypted && !responseIsSigned) {
995
- throw new import_io_wallet_oauth25.Oauth2Error(
1209
+ throw new import_io_wallet_oauth24.Oauth2Error(
996
1210
  "Jarm Auth Response must be either encrypted, signed, or signed and encrypted."
997
1211
  );
998
1212
  }
999
1213
  let jarmAuthorizationResponse;
1000
1214
  if (responseIsSigned) {
1001
- const { header: jwsProtectedHeader, payload: jwsPayload } = (0, import_io_wallet_oauth25.decodeJwt)({
1215
+ const { header: jwsProtectedHeader, payload: jwsPayload } = (0, import_io_wallet_utils12.decodeJwt)({
1002
1216
  errorMessagePrefix: "Error decoding JARM authorization response JWT:",
1003
- headerSchema: import_zod6.default.object({ ...import_io_wallet_oauth25.zJwtHeader.shape, kid: import_zod6.default.string() }),
1217
+ headerSchema: import_zod6.default.object({ ...import_io_wallet_utils12.zJwtHeader.shape, kid: import_zod6.default.string() }),
1004
1218
  jwt: decryptedRequestData.payload
1005
1219
  });
1006
1220
  const response = zJarmAuthorizationResponse.parse(jwsPayload);
1007
- const jwtSigner = (0, import_oauth23.jwtSignerFromJwt)({
1221
+ const jwtSigner = (0, import_io_wallet_utils12.jwtSignerFromJwt)({
1008
1222
  header: jwsProtectedHeader,
1009
1223
  payload: jwsPayload
1010
1224
  });
@@ -1014,31 +1228,31 @@ async function verifyJarmAuthorizationResponse(options) {
1014
1228
  payload: jwsPayload
1015
1229
  });
1016
1230
  if (!verificationResult.verified) {
1017
- throw new import_io_wallet_oauth25.Oauth2Error("Jarm Auth Response is not valid.");
1231
+ throw new import_io_wallet_oauth24.Oauth2Error("Jarm Auth Response is not valid.");
1018
1232
  }
1019
1233
  const expectedAudience = authorizationRequestPayload.client_id;
1020
1234
  const expectedIssuer = authorizationRequestPayload.iss;
1021
1235
  if (response.aud !== expectedAudience) {
1022
- throw new import_io_wallet_oauth25.Oauth2Error(
1236
+ throw new import_io_wallet_oauth24.Oauth2Error(
1023
1237
  `Jarm Auth Response contains 'aud' value '${response.aud}', but expected '${expectedAudience}'.`
1024
1238
  );
1025
1239
  }
1026
1240
  if (expectedIssuer && response.iss !== expectedIssuer) {
1027
- throw new import_io_wallet_oauth25.Oauth2Error(
1241
+ throw new import_io_wallet_oauth24.Oauth2Error(
1028
1242
  `Jarm Auth Response contains 'iss' value '${response.iss}', but expected '${expectedIssuer}'.`
1029
1243
  );
1030
1244
  }
1031
1245
  const now = options.now ?? /* @__PURE__ */ new Date();
1032
1246
  const nowSeconds = Math.floor(now.getTime() / 1e3);
1033
1247
  if (response.exp < nowSeconds) {
1034
- throw new import_io_wallet_oauth25.Oauth2Error("Jarm Auth Response has expired.");
1248
+ throw new import_io_wallet_oauth24.Oauth2Error("Jarm Auth Response has expired.");
1035
1249
  }
1036
1250
  if (response.nbf !== void 0 && response.nbf > nowSeconds) {
1037
- throw new import_io_wallet_oauth25.Oauth2Error("Jarm Auth Response is not active yet.");
1251
+ throw new import_io_wallet_oauth24.Oauth2Error("Jarm Auth Response is not active yet.");
1038
1252
  }
1039
1253
  jarmAuthorizationResponse = response;
1040
1254
  } else {
1041
- const jsonRequestData = (0, import_io_wallet_utils9.stringToJsonWithErrorHandling)(
1255
+ const jsonRequestData = (0, import_io_wallet_utils12.stringToJsonWithErrorHandling)(
1042
1256
  decryptedRequestData.payload,
1043
1257
  "Unable to parse decrypted JARM JWE body to JSON"
1044
1258
  );
@@ -1057,8 +1271,8 @@ async function verifyJarmAuthorizationResponse(options) {
1057
1271
  // src/jarm/parse-jarm-authorization-response.ts
1058
1272
  async function parseJarmAuthorizationResponse(options) {
1059
1273
  const { authorizationRequestPayload, callbacks, jarmResponseJwt, now } = options;
1060
- const jarmAuthorizationResponseJwt = (0, import_io_wallet_utils10.parseWithErrorHandling)(
1061
- import_zod7.default.union([import_io_wallet_oauth26.zCompactJwt, import_io_wallet_oauth26.zCompactJwe]),
1274
+ const jarmAuthorizationResponseJwt = (0, import_io_wallet_utils13.parseWithErrorHandling)(
1275
+ import_zod7.default.union([import_io_wallet_utils13.zCompactJwt, import_io_wallet_utils13.zCompactJwe]),
1062
1276
  jarmResponseJwt,
1063
1277
  "Invalid jarm authorization response jwt."
1064
1278
  );
@@ -1068,11 +1282,11 @@ async function parseJarmAuthorizationResponse(options) {
1068
1282
  jarmAuthorizationResponseJwt,
1069
1283
  now
1070
1284
  });
1071
- const { header: jarmHeader } = (0, import_io_wallet_oauth26.decodeJwtHeader)({
1285
+ const { header: jarmHeader } = (0, import_io_wallet_utils13.decodeJwtHeader)({
1072
1286
  headerSchema: zJarmHeader,
1073
1287
  jwt: jarmAuthorizationResponseJwt
1074
1288
  });
1075
- const authorizationResponsePayload = (0, import_io_wallet_utils10.parseWithErrorHandling)(
1289
+ const authorizationResponsePayload = (0, import_io_wallet_utils13.parseWithErrorHandling)(
1076
1290
  zOpenid4vpAuthorizationResponse,
1077
1291
  verifiedJarmResponse.jarmAuthorizationResponse,
1078
1292
  "Failed to parse openid4vp authorization response."
@@ -1104,7 +1318,7 @@ async function parseAuthorizationResponse(options) {
1104
1318
  jarmResponseJwt: authorizationResponse.response
1105
1319
  });
1106
1320
  }
1107
- const authorizationResponsePayload = (0, import_io_wallet_utils11.parseWithErrorHandling)(
1321
+ const authorizationResponsePayload = (0, import_io_wallet_utils14.parseWithErrorHandling)(
1108
1322
  zOpenid4vpAuthorizationResponse,
1109
1323
  authorizationResponse,
1110
1324
  "Failed to parse openid4vp authorization response."
@@ -1119,9 +1333,6 @@ async function parseAuthorizationResponse(options) {
1119
1333
  expectedNonce: authorizationRequestPayload.nonce
1120
1334
  };
1121
1335
  }
1122
-
1123
- // src/index.ts
1124
- var import_openid4vp = require("@openid4vc/openid4vp");
1125
1336
  // Annotate the CommonJS export names for ESM import in node:
1126
1337
  0 && (module.exports = {
1127
1338
  ClientIdPrefix,