@pagopa/io-wallet-oid4vp 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.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,36 @@ 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.V1_0]: async (o) => createAuthorizationRequestWithHeader(
238
239
  o,
239
240
  zOpenid4vpAuthorizationRequestHeaderV1_0
240
241
  ),
241
- [import_io_wallet_utils2.ItWalletSpecsVersion.V1_3]: async (o) => createAuthorizationRequestWithHeader(
242
+ [import_io_wallet_utils3.ItWalletSpecsVersion.V1_3]: async (o) => createAuthorizationRequestWithHeader(
242
243
  o,
243
244
  zOpenid4vpAuthorizationRequestHeaderV1_3
244
245
  ),
245
246
  // 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(
247
+ [import_io_wallet_utils3.ItWalletSpecsVersion.V1_4]: async (o) => createAuthorizationRequestWithHeader(
247
248
  o,
248
249
  zOpenid4vpAuthorizationRequestHeaderV1_3
249
250
  )
@@ -252,7 +253,7 @@ async function createAuthorizationRequest(options) {
252
253
  try {
253
254
  return await dispatchCreateAuthorizationRequest(options);
254
255
  } catch (error) {
255
- if (error instanceof import_io_wallet_utils2.ValidationError) {
256
+ if (error instanceof import_io_wallet_utils3.ValidationError) {
256
257
  throw new Oid4vpError(`Invalid authorization request: ${error.message}`);
257
258
  }
258
259
  throw error;
@@ -260,11 +261,11 @@ async function createAuthorizationRequest(options) {
260
261
  }
261
262
  async function createAuthorizationRequestWithHeader(options, headerSchema) {
262
263
  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),
264
+ const authorizationRequestHeader = (0, import_io_wallet_utils3.parseWithErrorHandling)(headerSchema, {
265
+ ...(0, import_io_wallet_utils3.jwtHeaderFromJwtSigner)(jar.jwtSigner),
265
266
  typ: import_io_wallet_oauth22.signedAuthorizationRequestJwtHeaderTyp
266
267
  });
267
- const authorizationRequestPayload = (0, import_io_wallet_utils2.parseWithErrorHandling)(
268
+ const authorizationRequestPayload = (0, import_io_wallet_utils3.parseWithErrorHandling)(
268
269
  zOpenid4vpAuthorizationRequestPayload,
269
270
  options.authorizationRequestPayload
270
271
  );
@@ -300,13 +301,13 @@ function createAuthorizationRequestUrl(scheme, request) {
300
301
  const url = new URL(scheme);
301
302
  const searchParams = new URLSearchParams([
302
303
  ...url.searchParams.entries(),
303
- ...(0, import_io_wallet_utils2.objectToQueryParams)(request).entries()
304
+ ...(0, import_io_wallet_utils3.objectToQueryParams)(request).entries()
304
305
  ]);
305
306
  url.search = searchParams.toString();
306
307
  return url.toString();
307
308
  }
308
309
  function validateJarSignerForAuthorizationRequest(specsVersion, jwtSigner, payload) {
309
- if (specsVersion === import_io_wallet_utils2.ItWalletSpecsVersion.V1_0) {
310
+ if (specsVersion === import_io_wallet_utils3.ItWalletSpecsVersion.V1_0) {
310
311
  return;
311
312
  }
312
313
  const { prefix } = extractClientIdPrefix(payload.client_id);
@@ -323,8 +324,7 @@ function validateJarSignerForAuthorizationRequest(specsVersion, jwtSigner, paylo
323
324
  }
324
325
 
325
326
  // 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");
327
+ var import_io_wallet_utils4 = require("@pagopa/io-wallet-utils");
328
328
 
329
329
  // src/authorization-request/validate-authorization-request.ts
330
330
  function validateAuthorizationRequestParams(params) {
@@ -461,7 +461,7 @@ function createParsedQrCode(authorizeRequestUrl) {
461
461
  };
462
462
  }
463
463
  async function fetchRequestObjectJwt(requestUri, options) {
464
- const fetch = (0, import_io_wallet_utils3.createFetcher)(options.fetch);
464
+ const fetch = (0, import_io_wallet_utils4.createFetcher)(options.fetch);
465
465
  const requestInit = {
466
466
  method: options.method.toUpperCase()
467
467
  };
@@ -482,7 +482,7 @@ async function fetchRequestObjectJwt(requestUri, options) {
482
482
  requestInit.body = formData.toString();
483
483
  }
484
484
  const response = await fetch(requestUri, requestInit);
485
- await (0, import_io_wallet_utils3.hasStatusOrThrow)(200, import_io_wallet_utils3.UnexpectedStatusCodeError)(response);
485
+ await (0, import_io_wallet_utils4.hasStatusOrThrow)(200, import_io_wallet_utils4.UnexpectedStatusCodeError)(response);
486
486
  return await response.text();
487
487
  }
488
488
  async function fetchAuthorizationRequest(options) {
@@ -520,7 +520,7 @@ async function fetchAuthorizationRequest(options) {
520
520
  sendBy: parsedQrCode.request ? "value" : "reference"
521
521
  };
522
522
  } 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) {
523
+ 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
524
  throw error;
525
525
  }
526
526
  throw new Oid4vpError(
@@ -530,9 +530,7 @@ async function fetchAuthorizationRequest(options) {
530
530
  }
531
531
 
532
532
  // 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");
533
+ var import_io_wallet_utils5 = require("@pagopa/io-wallet-utils");
536
534
  function getPublicKeyForVerification(options) {
537
535
  const { header, payload } = options;
538
536
  const { prefix: clientIdPrefix } = extractClientIdPrefix(payload.client_id);
@@ -569,18 +567,18 @@ function getPublicKeyForVerification(options) {
569
567
  }
570
568
  async function parseAuthorizeRequest(options) {
571
569
  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
570
+ const headerSchema = (0, import_io_wallet_utils5.dispatchByVersion)(options.config.itWalletSpecsVersion, {
571
+ [import_io_wallet_utils5.ItWalletSpecsVersion.V1_0]: () => zOpenid4vpAuthorizationRequestHeaderV1_0,
572
+ [import_io_wallet_utils5.ItWalletSpecsVersion.V1_3]: () => zOpenid4vpAuthorizationRequestHeaderV1_3,
573
+ [import_io_wallet_utils5.ItWalletSpecsVersion.V1_4]: () => zOpenid4vpAuthorizationRequestHeaderV1_3
576
574
  });
577
- const decoded = (0, import_io_wallet_oauth23.decodeJwt)({
575
+ const decoded = (0, import_io_wallet_utils5.decodeJwt)({
578
576
  errorMessagePrefix: "Error decoding authorization request JWT:",
579
577
  headerSchema,
580
578
  jwt: options.requestObjectJwt,
581
579
  payloadSchema: zOpenid4vpAuthorizationRequestPayload
582
580
  });
583
- if (options.config.itWalletSpecsVersion !== import_io_wallet_utils4.ItWalletSpecsVersion.V1_0) {
581
+ if (options.config.itWalletSpecsVersion !== import_io_wallet_utils5.ItWalletSpecsVersion.V1_0) {
584
582
  await validateAuthorizationRequestClientBinding({
585
583
  hash: options.callbacks?.hash,
586
584
  header: decoded.header,
@@ -605,7 +603,7 @@ async function parseAuthorizeRequest(options) {
605
603
  header: decoded.header,
606
604
  payload: decoded.payload
607
605
  });
608
- await (0, import_oauth22.verifyJwt)({
606
+ await (0, import_io_wallet_utils5.verifyJwt)({
609
607
  compact: options.requestObjectJwt,
610
608
  errorMessage: "Error verifying Request Object signature",
611
609
  header: decoded.header,
@@ -625,7 +623,7 @@ async function parseAuthorizeRequest(options) {
625
623
  }
626
624
  };
627
625
  } 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)
626
+ 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
627
  throw error;
630
628
  throw new ParseAuthorizeRequestError(
631
629
  `Unexpected error during Request Object parsing: ${error instanceof Error ? error.message : String(error)}`
@@ -634,10 +632,10 @@ async function parseAuthorizeRequest(options) {
634
632
  }
635
633
 
636
634
  // src/authorization-response/create-authorization-response.ts
637
- var import_io_wallet_utils6 = require("@pagopa/io-wallet-utils");
635
+ var import_io_wallet_utils7 = require("@pagopa/io-wallet-utils");
638
636
 
639
637
  // src/authorization-response/build-jarm-response.ts
640
- var import_io_wallet_utils5 = require("@pagopa/io-wallet-utils");
638
+ var import_io_wallet_utils6 = require("@pagopa/io-wallet-utils");
641
639
 
642
640
  // src/jarm/jarm-extract-jwks.ts
643
641
  function extractEncryptionJwkFromJwks(jwks, {
@@ -693,8 +691,8 @@ async function buildJarmResponse(options) {
693
691
  const nonceBytes = await options.callbacks.generateRandom(32);
694
692
  const jweEncryptor = {
695
693
  alg,
696
- apu: (0, import_io_wallet_utils5.encodeToBase64Url)(nonceBytes),
697
- apv: (0, import_io_wallet_utils5.encodeToBase64Url)(options.requestObject.nonce),
694
+ apu: (0, import_io_wallet_utils6.encodeToBase64Url)(nonceBytes),
695
+ apv: (0, import_io_wallet_utils6.encodeToBase64Url)(options.requestObject.nonce),
698
696
  enc,
699
697
  kid: encryptionJwk.kid,
700
698
  method: "jwk",
@@ -773,17 +771,17 @@ async function createAuthorizationResponse2(options) {
773
771
  }
774
772
 
775
773
  // 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) => (
774
+ var dispatchCreateAuthorizationResponse = (0, import_io_wallet_utils7.createVersionDispatcher)({
775
+ [import_io_wallet_utils7.ItWalletSpecsVersion.V1_0]: (o) => (
778
776
  // V1_0 shares the v1.3 response logic — JWKS / enc resolution is identical.
779
777
  createAuthorizationResponse(
780
778
  o
781
779
  )
782
780
  ),
783
- [import_io_wallet_utils6.ItWalletSpecsVersion.V1_3]: (o) => createAuthorizationResponse(
781
+ [import_io_wallet_utils7.ItWalletSpecsVersion.V1_3]: (o) => createAuthorizationResponse(
784
782
  o
785
783
  ),
786
- [import_io_wallet_utils6.ItWalletSpecsVersion.V1_4]: (o) => createAuthorizationResponse2(
784
+ [import_io_wallet_utils7.ItWalletSpecsVersion.V1_4]: (o) => createAuthorizationResponse2(
787
785
  o
788
786
  )
789
787
  });
@@ -791,8 +789,218 @@ async function createAuthorizationResponse3(options) {
791
789
  return dispatchCreateAuthorizationResponse(options);
792
790
  }
793
791
 
792
+ // src/authorization-response/create-openid4vp-authorization-response.ts
793
+ var import_io_wallet_oauth23 = require("@pagopa/io-wallet-oauth2");
794
+ var import_io_wallet_utils8 = require("@pagopa/io-wallet-utils");
795
+ var jarmResponseModes = /* @__PURE__ */ new Set([
796
+ "jwt",
797
+ "query.jwt",
798
+ "fragment.jwt",
799
+ "form_post.jwt",
800
+ "direct_post.jwt",
801
+ "dc_api.jwt"
802
+ ]);
803
+ function assertValueSupported(options) {
804
+ const { actual, errorMessage, supported } = options;
805
+ const found = supported.find((value) => value === actual);
806
+ if (!found) {
807
+ throw new import_io_wallet_oauth23.Oauth2Error(errorMessage);
808
+ }
809
+ return found;
810
+ }
811
+ function assertClientJarmMetadataSupported(clientMetadata, serverMetadata) {
812
+ const {
813
+ authorization_encrypted_response_alg: encryptedAlg,
814
+ authorization_encrypted_response_enc: encryptedEnc,
815
+ authorization_signed_response_alg: signedAlg
816
+ } = clientMetadata;
817
+ if (encryptedAlg) {
818
+ assertValueSupported({
819
+ actual: encryptedAlg,
820
+ errorMessage: `Invalid authorization_encrypted_response_alg ${encryptedAlg}`,
821
+ supported: serverMetadata.authorization_encryption_alg_values_supported
822
+ });
823
+ }
824
+ if (encryptedEnc) {
825
+ assertValueSupported({
826
+ actual: encryptedEnc,
827
+ errorMessage: `Invalid authorization_encrypted_response_enc ${encryptedEnc}`,
828
+ supported: serverMetadata.authorization_encryption_enc_values_supported
829
+ });
830
+ }
831
+ if (signedAlg && serverMetadata.authorization_signed_response_alg_values_supported) {
832
+ assertValueSupported({
833
+ actual: signedAlg,
834
+ errorMessage: `Invalid authorization_signed_response_alg ${signedAlg}`,
835
+ supported: serverMetadata.authorization_signed_response_alg_values_supported
836
+ });
837
+ }
838
+ }
839
+ async function fetchJwks(jwksUrl, fetchCallback) {
840
+ if (!fetchCallback) {
841
+ throw new import_io_wallet_oauth23.Oauth2Error(
842
+ `Missing fetch callback required to resolve jwks_uri '${jwksUrl}'.`
843
+ );
844
+ }
845
+ const response = await fetchCallback(jwksUrl, {
846
+ headers: {
847
+ Accept: `${import_io_wallet_utils8.ContentType.JwkSet}, ${import_io_wallet_utils8.ContentType.Json};q=0.9`
848
+ }
849
+ });
850
+ await (0, import_io_wallet_utils8.hasStatusOrThrow)(200, import_io_wallet_utils8.UnexpectedStatusCodeError)(response);
851
+ return (0, import_io_wallet_utils8.parseWithErrorHandling)(
852
+ import_io_wallet_utils8.zJwkSet,
853
+ await response.json(),
854
+ `Validation of JWKs from jwks_uri '${jwksUrl}' failed`
855
+ );
856
+ }
857
+ async function createJarmAuthorizationResponse(options) {
858
+ const { callbacks, jarmAuthorizationResponse, jweEncryptor, jwtSigner } = options;
859
+ if (!jwtSigner && jweEncryptor) {
860
+ const { encryptionJwk: encryptionJwk2, jwe: jwe2 } = await callbacks.encryptJwe(
861
+ jweEncryptor,
862
+ JSON.stringify(jarmAuthorizationResponse)
863
+ );
864
+ return {
865
+ encryptionJwk: encryptionJwk2,
866
+ jarmAuthorizationResponseJwt: jwe2
867
+ };
868
+ }
869
+ if (jwtSigner && !jweEncryptor) {
870
+ const { jwt } = await callbacks.signJwt(jwtSigner, {
871
+ header: (0, import_io_wallet_utils8.jwtHeaderFromJwtSigner)(jwtSigner),
872
+ payload: jarmAuthorizationResponse
873
+ });
874
+ return {
875
+ jarmAuthorizationResponseJwt: jwt
876
+ };
877
+ }
878
+ if (!jwtSigner || !jweEncryptor) {
879
+ throw new import_io_wallet_oauth23.Oauth2Error(
880
+ "JWT signer and/or encryptor are required to create a JARM auth response."
881
+ );
882
+ }
883
+ const signed = await callbacks.signJwt(jwtSigner, {
884
+ header: (0, import_io_wallet_utils8.jwtHeaderFromJwtSigner)(jwtSigner),
885
+ payload: jarmAuthorizationResponse
886
+ });
887
+ const { encryptionJwk, jwe } = await callbacks.encryptJwe(
888
+ jweEncryptor,
889
+ signed.jwt
890
+ );
891
+ return {
892
+ encryptionJwk,
893
+ jarmAuthorizationResponseJwt: jwe
894
+ };
895
+ }
896
+ async function createOpenid4vpAuthorizationResponse(options) {
897
+ const { authorizationRequestPayload, jarm } = options;
898
+ const authorizationResponsePayload = {
899
+ ...options.authorizationResponsePayload,
900
+ state: authorizationRequestPayload.state
901
+ };
902
+ const { prefix: clientIdPrefix } = extractClientIdPrefix(
903
+ authorizationRequestPayload.client_id
904
+ );
905
+ if (authorizationRequestPayload.response_mode && jarmResponseModes.has(authorizationRequestPayload.response_mode) && !jarm) {
906
+ throw new import_io_wallet_oauth23.Oauth2Error(
907
+ `Missing jarm options for creating Jarm response with response mode '${authorizationRequestPayload.response_mode}'`
908
+ );
909
+ }
910
+ if (!jarm) {
911
+ return { authorizationResponsePayload };
912
+ }
913
+ if ((clientIdPrefix === "openid_federation" /* OPENID_FEDERATION */ || clientIdPrefix === "none" /* NONE */) && !options.clientMetadata) {
914
+ throw new import_io_wallet_oauth23.Oauth2Error(
915
+ "When OpenID Federation is used as the client id prefix (https/openid_federation), passing externally fetched and verified 'clientMetadata' to 'createOpenid4vpAuthorizationResponse' is required."
916
+ );
917
+ }
918
+ const clientMetadata = options.clientMetadata ?? authorizationRequestPayload.client_metadata;
919
+ if (!clientMetadata) {
920
+ throw new import_io_wallet_oauth23.Oauth2Error(
921
+ "Missing client metadata in the request params to assert Jarm metadata support."
922
+ );
923
+ }
924
+ const jwks = clientMetadata.jwks ? clientMetadata.jwks : clientMetadata.jwks_uri ? await fetchJwks(clientMetadata.jwks_uri, options.callbacks.fetch) : void 0;
925
+ if (!jwks) {
926
+ throw new import_io_wallet_oauth23.Oauth2Error(
927
+ "Missing 'jwks' or 'jwks_uri' in client metadata. Cannot extract encryption JWK."
928
+ );
929
+ }
930
+ assertClientJarmMetadataSupported(clientMetadata, jarm.serverMetadata);
931
+ const encryptionJwk = jarm.encryption?.jwk ?? extractEncryptionJwkFromJwks(jwks, {
932
+ supportedAlgValues: jarm.serverMetadata.authorization_encryption_alg_values_supported ?? (clientMetadata.authorization_encrypted_response_alg ? [clientMetadata.authorization_encrypted_response_alg] : void 0)
933
+ });
934
+ if (!encryptionJwk) {
935
+ throw new import_io_wallet_oauth23.Oauth2Error(
936
+ "No encryption JWK provided and could not extract encryption JWK from client metadata. Failed to create JARM response."
937
+ );
938
+ }
939
+ const enc = clientMetadata.encrypted_response_enc_values_supported ? jarm.serverMetadata.authorization_encryption_enc_values_supported.find(
940
+ (value) => clientMetadata.encrypted_response_enc_values_supported?.includes(
941
+ value
942
+ )
943
+ ) ?? clientMetadata.encrypted_response_enc_values_supported[0] ?? "A128GCM" : clientMetadata.authorization_encrypted_response_enc ?? "A128GCM";
944
+ assertValueSupported({
945
+ actual: enc,
946
+ errorMessage: `Invalid 'enc' value ${enc}. Supported values are ${jarm.serverMetadata.authorization_encryption_enc_values_supported.join(", ")}`,
947
+ supported: jarm.serverMetadata.authorization_encryption_enc_values_supported
948
+ });
949
+ const alg = encryptionJwk.alg ?? clientMetadata.authorization_encrypted_response_alg ?? "ECDH-ES";
950
+ assertValueSupported({
951
+ actual: alg,
952
+ errorMessage: `Invalid 'alg' value ${alg}. Supported values are ${jarm.serverMetadata.authorization_encryption_alg_values_supported.join(", ")}`,
953
+ supported: jarm.serverMetadata.authorization_encryption_alg_values_supported
954
+ });
955
+ if (clientMetadata.authorization_signed_response_alg && jarm.jwtSigner && clientMetadata.authorization_signed_response_alg !== jarm.jwtSigner.alg) {
956
+ throw new import_io_wallet_oauth23.Oauth2Error(
957
+ `Invalid signed response alg value ${jarm.jwtSigner.alg}. Expected ${clientMetadata.authorization_signed_response_alg}.`
958
+ );
959
+ }
960
+ const additionalJwtPayload = jarm.jwtSigner ? {
961
+ aud: jarm.audience ?? (() => {
962
+ throw new import_io_wallet_oauth23.Oauth2Error(
963
+ "Missing required aud in JARM configuration for creating OpenID4VP authorization response."
964
+ );
965
+ })(),
966
+ 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)),
967
+ iss: jarm.authorizationServer ?? (() => {
968
+ throw new import_io_wallet_oauth23.Oauth2Error(
969
+ "Missing required iss in JARM configuration for creating OpenID4VP authorization response."
970
+ );
971
+ })()
972
+ } : void 0;
973
+ const jarmResponsePayload = {
974
+ ...authorizationResponsePayload,
975
+ ...additionalJwtPayload
976
+ };
977
+ const { encryptionJwk: usedEncryptionJwk, jarmAuthorizationResponseJwt } = await createJarmAuthorizationResponse({
978
+ callbacks: {
979
+ encryptJwe: options.callbacks.encryptJwe,
980
+ signJwt: options.callbacks.signJwt
981
+ },
982
+ jarmAuthorizationResponse: jarmResponsePayload,
983
+ jweEncryptor: jarm.encryption ? {
984
+ alg,
985
+ apu: jarm.encryption.nonce ? (0, import_io_wallet_utils8.encodeToBase64Url)(jarm.encryption.nonce) : void 0,
986
+ apv: (0, import_io_wallet_utils8.encodeToBase64Url)(authorizationRequestPayload.nonce),
987
+ enc,
988
+ method: "jwk",
989
+ publicJwk: encryptionJwk
990
+ } : void 0,
991
+ jwtSigner: jarm.jwtSigner
992
+ });
993
+ return {
994
+ authorizationResponsePayload: jarmResponsePayload,
995
+ jarm: {
996
+ encryptionJwk: usedEncryptionJwk,
997
+ responseJwt: jarmAuthorizationResponseJwt
998
+ }
999
+ };
1000
+ }
1001
+
794
1002
  // src/authorization-response/fetch-authorization-response.ts
795
- var import_io_wallet_utils7 = require("@pagopa/io-wallet-utils");
1003
+ var import_io_wallet_utils9 = require("@pagopa/io-wallet-utils");
796
1004
 
797
1005
  // src/authorization-response/z-authorization-response.ts
798
1006
  var import_zod4 = __toESM(require("zod"));
@@ -828,7 +1036,7 @@ async function fetchAuthorizationResponse(options) {
828
1036
  ]
829
1037
  });
830
1038
  }
831
- const fetch = (0, import_io_wallet_utils7.createFetcher)(options.callbacks.fetch);
1039
+ const fetch = (0, import_io_wallet_utils9.createFetcher)(options.callbacks.fetch);
832
1040
  const authorizationResponseResult = await fetch(
833
1041
  options.presentationResponseUri,
834
1042
  {
@@ -836,17 +1044,17 @@ async function fetchAuthorizationResponse(options) {
836
1044
  response: options.authorizationResponseJarm
837
1045
  }),
838
1046
  headers: {
839
- [import_io_wallet_utils7.HEADERS.CONTENT_TYPE]: import_io_wallet_utils7.CONTENT_TYPES.FORM_URLENCODED
1047
+ [import_io_wallet_utils9.HEADERS.CONTENT_TYPE]: import_io_wallet_utils9.CONTENT_TYPES.FORM_URLENCODED
840
1048
  },
841
1049
  method: "POST"
842
1050
  }
843
1051
  );
844
- await (0, import_io_wallet_utils7.hasStatusOrThrow)(
1052
+ await (0, import_io_wallet_utils9.hasStatusOrThrow)(
845
1053
  200,
846
- import_io_wallet_utils7.UnexpectedStatusCodeError
1054
+ import_io_wallet_utils9.UnexpectedStatusCodeError
847
1055
  )(authorizationResponseResult);
848
1056
  const authorizationResponseResultJson = await authorizationResponseResult.json();
849
- const parsedAuthorizationResponseResult = (0, import_io_wallet_utils7.parseWithErrorHandling)(
1057
+ const parsedAuthorizationResponseResult = (0, import_io_wallet_utils9.parseWithErrorHandling)(
850
1058
  zOpenid4vpAuthorizationResponseResult,
851
1059
  authorizationResponseResultJson
852
1060
  );
@@ -864,7 +1072,7 @@ async function fetchAuthorizationResponse(options) {
864
1072
  }
865
1073
  return parsedAuthorizationResponseResult;
866
1074
  } catch (error) {
867
- if (error instanceof import_io_wallet_utils7.UnexpectedStatusCodeError || error instanceof import_io_wallet_utils7.ValidationError || error instanceof FetchAuthorizationResponseError) {
1075
+ if (error instanceof import_io_wallet_utils9.UnexpectedStatusCodeError || error instanceof import_io_wallet_utils9.ValidationError || error instanceof FetchAuthorizationResponseError) {
868
1076
  throw error;
869
1077
  }
870
1078
  throw new FetchAuthorizationResponseError(
@@ -874,19 +1082,18 @@ async function fetchAuthorizationResponse(options) {
874
1082
  }
875
1083
 
876
1084
  // src/authorization-response/parse-authorization-response.ts
877
- var import_io_wallet_utils11 = require("@pagopa/io-wallet-utils");
1085
+ var import_io_wallet_utils14 = require("@pagopa/io-wallet-utils");
878
1086
 
879
1087
  // 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");
1088
+ var import_io_wallet_utils13 = require("@pagopa/io-wallet-utils");
882
1089
  var import_zod7 = __toESM(require("zod"));
883
1090
 
884
1091
  // src/vp-token/parse-vp-token.ts
885
- var import_io_wallet_utils8 = require("@pagopa/io-wallet-utils");
1092
+ var import_io_wallet_utils10 = require("@pagopa/io-wallet-utils");
886
1093
  function parseVpToken(vpToken) {
887
- return (0, import_io_wallet_utils8.parseWithErrorHandling)(
1094
+ return (0, import_io_wallet_utils10.parseWithErrorHandling)(
888
1095
  zVpToken,
889
- (0, import_io_wallet_utils8.parseIfJson)(vpToken),
1096
+ (0, import_io_wallet_utils10.parseIfJson)(vpToken),
890
1097
  "Could not parse dcql vp_token. Expected an object where the values are encoded presentations"
891
1098
  );
892
1099
  }
@@ -905,22 +1112,21 @@ function validateOpenid4vpAuthorizationResponsePayload(options) {
905
1112
  }
906
1113
 
907
1114
  // 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");
1115
+ var import_io_wallet_oauth24 = require("@pagopa/io-wallet-oauth2");
1116
+ var import_io_wallet_utils12 = require("@pagopa/io-wallet-utils");
911
1117
  var import_zod6 = __toESM(require("zod"));
912
1118
 
913
1119
  // src/jarm/z-jarm.ts
914
- var import_io_wallet_oauth24 = require("@pagopa/io-wallet-oauth2");
1120
+ var import_io_wallet_utils11 = require("@pagopa/io-wallet-utils");
915
1121
  var import_zod5 = require("zod");
916
1122
  var zJarmHeader = import_zod5.z.object({
917
- ...import_io_wallet_oauth24.zJwtHeader.shape,
1123
+ ...import_io_wallet_utils11.zJwtHeader.shape,
918
1124
  apu: import_zod5.z.string().optional(),
919
1125
  apv: import_zod5.z.string().optional(),
920
1126
  kid: import_zod5.z.string()
921
1127
  });
922
1128
  var zEncryptedJarmHeader = import_zod5.z.object({
923
- ...import_io_wallet_oauth24.zJwtHeader.shape,
1129
+ ...import_io_wallet_utils11.zJwtHeader.shape,
924
1130
  apu: import_zod5.z.string().optional(),
925
1131
  apv: import_zod5.z.string().optional(),
926
1132
  enc: import_zod5.z.string().optional(),
@@ -932,12 +1138,12 @@ var zJarmAuthorizationResponse = import_zod5.z.looseObject({
932
1138
  * aud: The client_id of the client the response is intended for
933
1139
  * exp: The expiration time of the JWT. A maximum JWT lifetime of 10 minutes is RECOMMENDED.
934
1140
  */
935
- ...import_io_wallet_oauth24.zJwtPayload.shape,
936
- ...import_io_wallet_oauth24.zJwtPayload.pick({ aud: true, exp: true, iss: true }).required().shape,
1141
+ ...import_io_wallet_utils11.zJwtPayload.shape,
1142
+ ...import_io_wallet_utils11.zJwtPayload.pick({ aud: true, exp: true, iss: true }).required().shape,
937
1143
  state: import_zod5.z.optional(import_zod5.z.string())
938
1144
  });
939
1145
  var zJarmAuthorizationResponseEncryptedOnly = import_zod5.z.looseObject({
940
- ...import_io_wallet_oauth24.zJwtPayload.shape,
1146
+ ...import_io_wallet_utils11.zJwtPayload.shape,
941
1147
  state: import_zod5.z.optional(import_zod5.z.string())
942
1148
  });
943
1149
 
@@ -955,7 +1161,7 @@ var decryptJarmAuthorizationResponseJwt = async (options) => {
955
1161
  jarmAuthorizationResponseJwt
956
1162
  } = options;
957
1163
  let encryptionJwk;
958
- const { header } = (0, import_io_wallet_oauth25.decodeJwtHeader)({
1164
+ const { header } = (0, import_io_wallet_utils12.decodeJwtHeader)({
959
1165
  headerSchema: zEncryptedJarmHeader,
960
1166
  jwt: jarmAuthorizationResponseJwt
961
1167
  });
@@ -967,7 +1173,7 @@ var decryptJarmAuthorizationResponseJwt = async (options) => {
967
1173
  jwk: encryptionJwk
968
1174
  });
969
1175
  if (!result.decrypted) {
970
- throw new import_io_wallet_oauth25.Oauth2Error("Failed to decrypt jarm auth response.");
1176
+ throw new import_io_wallet_oauth24.Oauth2Error("Failed to decrypt jarm auth response.");
971
1177
  }
972
1178
  return {
973
1179
  decryptionJwk: result.decryptionJwk,
@@ -980,7 +1186,7 @@ async function verifyJarmAuthorizationResponse(options) {
980
1186
  callbacks,
981
1187
  jarmAuthorizationResponseJwt
982
1188
  } = options;
983
- const requestDataIsEncrypted = import_io_wallet_oauth25.zCompactJwe.safeParse(
1189
+ const requestDataIsEncrypted = import_io_wallet_utils12.zCompactJwe.safeParse(
984
1190
  jarmAuthorizationResponseJwt
985
1191
  ).success;
986
1192
  const decryptedRequestData = requestDataIsEncrypted ? await decryptJarmAuthorizationResponseJwt({
@@ -988,23 +1194,23 @@ async function verifyJarmAuthorizationResponse(options) {
988
1194
  callbacks,
989
1195
  jarmAuthorizationResponseJwt
990
1196
  }) : { decryptionJwk: void 0, payload: jarmAuthorizationResponseJwt };
991
- const responseIsSigned = import_io_wallet_oauth25.zCompactJwt.safeParse(
1197
+ const responseIsSigned = import_io_wallet_utils12.zCompactJwt.safeParse(
992
1198
  decryptedRequestData.payload
993
1199
  ).success;
994
1200
  if (!requestDataIsEncrypted && !responseIsSigned) {
995
- throw new import_io_wallet_oauth25.Oauth2Error(
1201
+ throw new import_io_wallet_oauth24.Oauth2Error(
996
1202
  "Jarm Auth Response must be either encrypted, signed, or signed and encrypted."
997
1203
  );
998
1204
  }
999
1205
  let jarmAuthorizationResponse;
1000
1206
  if (responseIsSigned) {
1001
- const { header: jwsProtectedHeader, payload: jwsPayload } = (0, import_io_wallet_oauth25.decodeJwt)({
1207
+ const { header: jwsProtectedHeader, payload: jwsPayload } = (0, import_io_wallet_utils12.decodeJwt)({
1002
1208
  errorMessagePrefix: "Error decoding JARM authorization response JWT:",
1003
- headerSchema: import_zod6.default.object({ ...import_io_wallet_oauth25.zJwtHeader.shape, kid: import_zod6.default.string() }),
1209
+ headerSchema: import_zod6.default.object({ ...import_io_wallet_utils12.zJwtHeader.shape, kid: import_zod6.default.string() }),
1004
1210
  jwt: decryptedRequestData.payload
1005
1211
  });
1006
1212
  const response = zJarmAuthorizationResponse.parse(jwsPayload);
1007
- const jwtSigner = (0, import_oauth23.jwtSignerFromJwt)({
1213
+ const jwtSigner = (0, import_io_wallet_utils12.jwtSignerFromJwt)({
1008
1214
  header: jwsProtectedHeader,
1009
1215
  payload: jwsPayload
1010
1216
  });
@@ -1014,31 +1220,31 @@ async function verifyJarmAuthorizationResponse(options) {
1014
1220
  payload: jwsPayload
1015
1221
  });
1016
1222
  if (!verificationResult.verified) {
1017
- throw new import_io_wallet_oauth25.Oauth2Error("Jarm Auth Response is not valid.");
1223
+ throw new import_io_wallet_oauth24.Oauth2Error("Jarm Auth Response is not valid.");
1018
1224
  }
1019
1225
  const expectedAudience = authorizationRequestPayload.client_id;
1020
1226
  const expectedIssuer = authorizationRequestPayload.iss;
1021
1227
  if (response.aud !== expectedAudience) {
1022
- throw new import_io_wallet_oauth25.Oauth2Error(
1228
+ throw new import_io_wallet_oauth24.Oauth2Error(
1023
1229
  `Jarm Auth Response contains 'aud' value '${response.aud}', but expected '${expectedAudience}'.`
1024
1230
  );
1025
1231
  }
1026
1232
  if (expectedIssuer && response.iss !== expectedIssuer) {
1027
- throw new import_io_wallet_oauth25.Oauth2Error(
1233
+ throw new import_io_wallet_oauth24.Oauth2Error(
1028
1234
  `Jarm Auth Response contains 'iss' value '${response.iss}', but expected '${expectedIssuer}'.`
1029
1235
  );
1030
1236
  }
1031
1237
  const now = options.now ?? /* @__PURE__ */ new Date();
1032
1238
  const nowSeconds = Math.floor(now.getTime() / 1e3);
1033
1239
  if (response.exp < nowSeconds) {
1034
- throw new import_io_wallet_oauth25.Oauth2Error("Jarm Auth Response has expired.");
1240
+ throw new import_io_wallet_oauth24.Oauth2Error("Jarm Auth Response has expired.");
1035
1241
  }
1036
1242
  if (response.nbf !== void 0 && response.nbf > nowSeconds) {
1037
- throw new import_io_wallet_oauth25.Oauth2Error("Jarm Auth Response is not active yet.");
1243
+ throw new import_io_wallet_oauth24.Oauth2Error("Jarm Auth Response is not active yet.");
1038
1244
  }
1039
1245
  jarmAuthorizationResponse = response;
1040
1246
  } else {
1041
- const jsonRequestData = (0, import_io_wallet_utils9.stringToJsonWithErrorHandling)(
1247
+ const jsonRequestData = (0, import_io_wallet_utils12.stringToJsonWithErrorHandling)(
1042
1248
  decryptedRequestData.payload,
1043
1249
  "Unable to parse decrypted JARM JWE body to JSON"
1044
1250
  );
@@ -1057,8 +1263,8 @@ async function verifyJarmAuthorizationResponse(options) {
1057
1263
  // src/jarm/parse-jarm-authorization-response.ts
1058
1264
  async function parseJarmAuthorizationResponse(options) {
1059
1265
  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]),
1266
+ const jarmAuthorizationResponseJwt = (0, import_io_wallet_utils13.parseWithErrorHandling)(
1267
+ import_zod7.default.union([import_io_wallet_utils13.zCompactJwt, import_io_wallet_utils13.zCompactJwe]),
1062
1268
  jarmResponseJwt,
1063
1269
  "Invalid jarm authorization response jwt."
1064
1270
  );
@@ -1068,11 +1274,11 @@ async function parseJarmAuthorizationResponse(options) {
1068
1274
  jarmAuthorizationResponseJwt,
1069
1275
  now
1070
1276
  });
1071
- const { header: jarmHeader } = (0, import_io_wallet_oauth26.decodeJwtHeader)({
1277
+ const { header: jarmHeader } = (0, import_io_wallet_utils13.decodeJwtHeader)({
1072
1278
  headerSchema: zJarmHeader,
1073
1279
  jwt: jarmAuthorizationResponseJwt
1074
1280
  });
1075
- const authorizationResponsePayload = (0, import_io_wallet_utils10.parseWithErrorHandling)(
1281
+ const authorizationResponsePayload = (0, import_io_wallet_utils13.parseWithErrorHandling)(
1076
1282
  zOpenid4vpAuthorizationResponse,
1077
1283
  verifiedJarmResponse.jarmAuthorizationResponse,
1078
1284
  "Failed to parse openid4vp authorization response."
@@ -1104,7 +1310,7 @@ async function parseAuthorizationResponse(options) {
1104
1310
  jarmResponseJwt: authorizationResponse.response
1105
1311
  });
1106
1312
  }
1107
- const authorizationResponsePayload = (0, import_io_wallet_utils11.parseWithErrorHandling)(
1313
+ const authorizationResponsePayload = (0, import_io_wallet_utils14.parseWithErrorHandling)(
1108
1314
  zOpenid4vpAuthorizationResponse,
1109
1315
  authorizationResponse,
1110
1316
  "Failed to parse openid4vp authorization response."
@@ -1119,9 +1325,6 @@ async function parseAuthorizationResponse(options) {
1119
1325
  expectedNonce: authorizationRequestPayload.nonce
1120
1326
  };
1121
1327
  }
1122
-
1123
- // src/index.ts
1124
- var import_openid4vp = require("@openid4vc/openid4vp");
1125
1328
  // Annotate the CommonJS export names for ESM import in node:
1126
1329
  0 && (module.exports = {
1127
1330
  ClientIdPrefix,