@sphereon/ssi-sdk.oid4vci-holder 0.34.1-next.91 → 0.36.0

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.cjs CHANGED
@@ -141,9 +141,10 @@ var import_ssi_sdk_ext5 = require("@sphereon/ssi-sdk-ext.did-utils");
141
141
  var import_ssi_sdk_ext6 = require("@sphereon/ssi-sdk-ext.identifier-resolution");
142
142
  var import_ssi_sdk_ext7 = require("@sphereon/ssi-sdk-ext.key-utils");
143
143
  var import_ssi_sdk2 = require("@sphereon/ssi-sdk.core");
144
- var import_ssi_sdk3 = require("@sphereon/ssi-sdk.data-store");
144
+ var import_ssi_sdk3 = require("@sphereon/ssi-sdk.data-store-types");
145
145
  var import_ssi_types2 = require("@sphereon/ssi-types");
146
146
  var import_utils2 = require("@veramo/utils");
147
+ var import_cross_fetch = __toESM(require("cross-fetch"), 1);
147
148
  var import_did_jwt = require("did-jwt");
148
149
  var import_uuid2 = require("uuid");
149
150
 
@@ -183,6 +184,7 @@ var OID4VCIMachineStates = /* @__PURE__ */ (function(OID4VCIMachineStates2) {
183
184
  OID4VCIMachineStates2["selectCredentials"] = "selectCredentials";
184
185
  OID4VCIMachineStates2["transitionFromSelectingCredentials"] = "transitionFromSelectingCredentials";
185
186
  OID4VCIMachineStates2["verifyPin"] = "verifyPin";
187
+ OID4VCIMachineStates2["prepareAuthorizationRequest"] = "prepareAuthorizationRequest";
186
188
  OID4VCIMachineStates2["initiateAuthorizationRequest"] = "initiateAuthorizationRequest";
187
189
  OID4VCIMachineStates2["waitForAuthorizationResponse"] = "waitForAuthorizationResponse";
188
190
  OID4VCIMachineStates2["getCredentials"] = "getCredentials";
@@ -247,6 +249,7 @@ var OID4VCIMachineServices = /* @__PURE__ */ (function(OID4VCIMachineServices2)
247
249
  OID4VCIMachineServices2["getFederationTrust"] = "getFederationTrust";
248
250
  OID4VCIMachineServices2["addContactIdentity"] = "addContactIdentity";
249
251
  OID4VCIMachineServices2["createCredentialsToSelectFrom"] = "createCredentialsToSelectFrom";
252
+ OID4VCIMachineServices2["prepareAuthorizationRequest"] = "prepareAuthorizationRequest";
250
253
  OID4VCIMachineServices2["getIssuerBranding"] = "getIssuerBranding";
251
254
  OID4VCIMachineServices2["storeIssuerBranding"] = "storeIssuerBranding";
252
255
  OID4VCIMachineServices2["getCredentials"] = "getCredentials";
@@ -388,9 +391,7 @@ var oid4vciRequireAuthorizationGuard = /* @__PURE__ */ __name((ctx, _event) => {
388
391
  if (!openID4VCIClientState) {
389
392
  throw Error("Missing openID4VCI client state in context");
390
393
  }
391
- if (!openID4VCIClientState.authorizationURL) {
392
- return false;
393
- } else if (openID4VCIClientState.authorizationRequestOpts) {
394
+ if (openID4VCIClientState.authorizationURL && openID4VCIClientState.authorizationRequestOpts) {
394
395
  return !ctx.openID4VCIClientState?.authorizationCodeResponse;
395
396
  } else if (openID4VCIClientState.credentialOffer?.supportedFlows?.includes(import_oid4vci_common.AuthzFlowType.AUTHORIZATION_CODE_FLOW)) {
396
397
  return !ctx.openID4VCIClientState?.authorizationCodeResponse;
@@ -441,7 +442,6 @@ var createOID4VCIMachine = /* @__PURE__ */ __name((opts) => {
441
442
  onDone: {
442
443
  target: OID4VCIMachineStates.createCredentialsToSelectFrom,
443
444
  actions: (0, import_xstate.assign)({
444
- authorizationCodeURL: /* @__PURE__ */ __name((_ctx, _event) => _event.data.authorizationCodeURL, "authorizationCodeURL"),
445
445
  credentialBranding: /* @__PURE__ */ __name((_ctx, _event) => _event.data.credentialBranding ?? {}, "credentialBranding"),
446
446
  credentialsSupported: /* @__PURE__ */ __name((_ctx, _event) => _event.data.credentialsSupported, "credentialsSupported"),
447
447
  serverMetadata: /* @__PURE__ */ __name((_ctx, _event) => _event.data.serverMetadata, "serverMetadata"),
@@ -678,6 +678,10 @@ var createOID4VCIMachine = /* @__PURE__ */ __name((opts) => {
678
678
  target: OID4VCIMachineStates.startFirstPartApplicationFlow,
679
679
  cond: OID4VCIMachineGuards.isFirstPartyApplication
680
680
  },
681
+ {
682
+ target: OID4VCIMachineStates.prepareAuthorizationRequest,
683
+ cond: OID4VCIMachineGuards.requireAuthorizationGuard
684
+ },
681
685
  {
682
686
  target: OID4VCIMachineStates.initiateAuthorizationRequest,
683
687
  cond: OID4VCIMachineGuards.requireAuthorizationGuard
@@ -753,12 +757,16 @@ var createOID4VCIMachine = /* @__PURE__ */ __name((opts) => {
753
757
  target: OID4VCIMachineStates.startFirstPartApplicationFlow,
754
758
  cond: OID4VCIMachineGuards.isFirstPartyApplication
755
759
  },
760
+ {
761
+ target: OID4VCIMachineStates.prepareAuthorizationRequest,
762
+ cond: OID4VCIMachineGuards.requireAuthorizationGuard
763
+ },
756
764
  {
757
765
  target: OID4VCIMachineStates.verifyPin,
758
766
  cond: OID4VCIMachineGuards.requirePinGuard
759
767
  },
760
768
  {
761
- target: OID4VCIMachineStates.initiateAuthorizationRequest,
769
+ target: OID4VCIMachineStates.prepareAuthorizationRequest,
762
770
  cond: OID4VCIMachineGuards.requireAuthorizationGuard
763
771
  },
764
772
  {
@@ -766,6 +774,29 @@ var createOID4VCIMachine = /* @__PURE__ */ __name((opts) => {
766
774
  }
767
775
  ]
768
776
  },
777
+ [OID4VCIMachineStates.prepareAuthorizationRequest]: {
778
+ id: OID4VCIMachineStates.prepareAuthorizationRequest,
779
+ invoke: {
780
+ src: OID4VCIMachineServices.prepareAuthorizationRequest,
781
+ onDone: {
782
+ target: OID4VCIMachineStates.initiateAuthorizationRequest,
783
+ actions: (0, import_xstate.assign)({
784
+ authorizationCodeURL: /* @__PURE__ */ __name((_ctx, _event) => _event.data.authorizationCodeURL, "authorizationCodeURL"),
785
+ openID4VCIClientState: /* @__PURE__ */ __name((_ctx, _event) => _event.data.oid4vciClientState, "openID4VCIClientState")
786
+ })
787
+ },
788
+ onError: {
789
+ target: OID4VCIMachineStates.handleError,
790
+ actions: (0, import_xstate.assign)({
791
+ error: /* @__PURE__ */ __name((_ctx, _event) => ({
792
+ title: translate("oid4vci_machine_prepare_authorization_error_title"),
793
+ message: _event.data.message,
794
+ stack: _event.data.stack
795
+ }), "error")
796
+ })
797
+ }
798
+ }
799
+ },
769
800
  [OID4VCIMachineStates.initiateAuthorizationRequest]: {
770
801
  id: OID4VCIMachineStates.initiateAuthorizationRequest,
771
802
  on: {
@@ -1744,7 +1775,7 @@ var mapCredentialToAccept = /* @__PURE__ */ __name(async (args) => {
1744
1775
  });
1745
1776
  let uniformVerifiableCredential;
1746
1777
  if (import_ssi_types.CredentialMapper.isSdJwtDecodedCredential(wrappedVerifiableCredential.credential)) {
1747
- uniformVerifiableCredential = await (0, import_ssi_types.sdJwtDecodedCredentialToUniformCredential)(wrappedVerifiableCredential.credential);
1778
+ uniformVerifiableCredential = (0, import_ssi_types.sdJwtDecodedCredentialToUniformCredential)(wrappedVerifiableCredential.credential);
1748
1779
  } else if (import_ssi_types.CredentialMapper.isSdJwtEncoded(wrappedVerifiableCredential.credential)) {
1749
1780
  if (!hasher) {
1750
1781
  return Promise.reject("a hasher is required for encoded SD-JWT credentials");
@@ -1890,24 +1921,19 @@ var getCredentialConfigsSupportedBySingleTypeOrId = /* @__PURE__ */ __name(async
1890
1921
  }
1891
1922
  __name(createIdFromTypes, "createIdFromTypes");
1892
1923
  if (configurationId) {
1893
- const allSupported2 = client.getCredentialsSupported(false);
1924
+ const allSupported2 = client.getCredentialsSupported(void 0, format);
1894
1925
  return Object.fromEntries(Object.entries(allSupported2).filter(([id, supported]) => id === configurationId || supported.id === configurationId || createIdFromTypes(supported) === configurationId));
1895
1926
  }
1896
- if (!types && !client.credentialOffer) {
1897
- return Promise.reject(Error("openID4VCIClient has no credentialOffer and no types where provided"));
1927
+ if (!client.credentialOffer) {
1928
+ return Promise.reject(Error("openID4VCIClient has no credentialOffer"));
1898
1929
  }
1899
- if (!Array.isArray(format) && client.credentialOffer) {
1900
- if (client.version() > import_oid4vci_common3.OpenId4VCIVersion.VER_1_0_09 && typeof client.credentialOffer.credential_offer === "object" && "credentials" in client.credentialOffer.credential_offer) {
1901
- format = client.credentialOffer.credential_offer.credentials.filter((cred) => typeof cred !== "string").map((cred) => cred.format);
1902
- if (format?.length === 0) {
1903
- format = void 0;
1904
- }
1905
- }
1930
+ if (!types) {
1931
+ return Promise.reject(Error("openID4VCIClient has no types"));
1906
1932
  }
1907
1933
  const offerSupported = (0, import_oid4vci_common3.getSupportedCredentials)({
1908
- types: types ? [
1934
+ types: [
1909
1935
  types
1910
- ] : client.getCredentialOfferTypes(),
1936
+ ],
1911
1937
  format,
1912
1938
  version: client.version(),
1913
1939
  issuerMetadata: client.endpointMetadata.credentialIssuerMetadata
@@ -2151,7 +2177,6 @@ var startFirstPartApplicationMachine = /* @__PURE__ */ __name(async (args, conte
2151
2177
  }, "startFirstPartApplicationMachine");
2152
2178
 
2153
2179
  // src/agent/OID4VCIHolder.ts
2154
- var import_polyfill = require("cross-fetch/polyfill");
2155
2180
  var oid4vciHolderContextMethods = [
2156
2181
  "cmGetContacts",
2157
2182
  "cmGetContact",
@@ -2207,6 +2232,7 @@ var OID4VCIHolder = class _OID4VCIHolder {
2207
2232
  oid4vciHolderStart: this.oid4vciHolderStart.bind(this),
2208
2233
  oid4vciHolderGetIssuerMetadata: this.oid4vciHolderGetIssuerMetadata.bind(this),
2209
2234
  oid4vciHolderGetMachineInterpreter: this.oid4vciHolderGetMachineInterpreter.bind(this),
2235
+ oid4vciHolderPrepareAuthorizationRequest: this.oid4vciHolderPrepareAuthorizationRequest.bind(this),
2210
2236
  oid4vciHolderCreateCredentialsToSelectFrom: this.oid4vciHolderCreateCredentialsToSelectFrom.bind(this),
2211
2237
  oid4vciHolderGetContact: this.oid4vciHolderGetContact.bind(this),
2212
2238
  oid4vciHolderGetCredentials: this.oid4vciHolderGetCredentials.bind(this),
@@ -2311,6 +2337,7 @@ var OID4VCIHolder = class _OID4VCIHolder {
2311
2337
  stateNavigationListener: opts.firstPartyStateNavigationListener
2312
2338
  }, context),
2313
2339
  [OID4VCIMachineServices.createCredentialsToSelectFrom]: (args) => this.oid4vciHolderCreateCredentialsToSelectFrom(args, context),
2340
+ [OID4VCIMachineServices.prepareAuthorizationRequest]: (args) => this.oid4vciHolderPrepareAuthorizationRequest(args, context),
2314
2341
  [OID4VCIMachineServices.getContact]: (args) => this.oid4vciHolderGetContact(args, context),
2315
2342
  [OID4VCIMachineServices.getCredentials]: (args) => this.oid4vciHolderGetCredentials({
2316
2343
  accessTokenOpts: args.accessTokenOpts ?? opts.accessTokenOpts,
@@ -2359,7 +2386,7 @@ var OID4VCIHolder = class _OID4VCIHolder {
2359
2386
  ...this.defaultAuthorizationRequestOpts,
2360
2387
  ...args.authorizationRequestOpts
2361
2388
  };
2362
- authorizationRequestOpts.authorizationDetails = authorizationRequestOpts?.authorizationDetails ? (0, import_utils2.asArray)(authorizationRequestOpts.authorizationDetails).filter((detail) => typeof detail === "string" || this.vcFormatPreferences.includes(detail.format)) : void 0;
2389
+ authorizationRequestOpts.authorizationDetails = authorizationRequestOpts?.authorizationDetails ? (0, import_utils2.asArray)(authorizationRequestOpts.authorizationDetails) : void 0;
2363
2390
  if (!authorizationRequestOpts.redirectUri) {
2364
2391
  authorizationRequestOpts.redirectUri = _OID4VCIHolder.DEFAULT_MOBILE_REDIRECT_URI;
2365
2392
  }
@@ -2372,10 +2399,9 @@ var OID4VCIHolder = class _OID4VCIHolder {
2372
2399
  formats = Array.from(new Set(authFormats));
2373
2400
  }
2374
2401
  let oid4vciClient;
2375
- let types = void 0;
2376
2402
  let offer;
2377
2403
  if (requestData.existingClientState) {
2378
- oid4vciClient = await import_oid4vci_client3.OpenID4VCIClient.fromState({
2404
+ oid4vciClient = await import_oid4vci_client3.OpenID4VCIClientV1_0_15.fromState({
2379
2405
  state: requestData.existingClientState
2380
2406
  });
2381
2407
  offer = oid4vciClient.credentialOffer;
@@ -2392,50 +2418,78 @@ var OID4VCIHolder = class _OID4VCIHolder {
2392
2418
  }
2393
2419
  if (!offer) {
2394
2420
  logger.log(`Issuer url received (no credential offer): ${uri}`);
2395
- oid4vciClient = await import_oid4vci_client3.OpenID4VCIClient.fromCredentialIssuer({
2421
+ oid4vciClient = await import_oid4vci_client3.OpenID4VCIClientV1_0_15.fromCredentialIssuer({
2396
2422
  credentialIssuer: uri,
2397
2423
  authorizationRequest: authorizationRequestOpts,
2398
2424
  clientId: authorizationRequestOpts.clientId,
2399
- createAuthorizationRequestURL: requestData.createAuthorizationRequestURL ?? true
2425
+ createAuthorizationRequestURL: false
2400
2426
  });
2401
2427
  } else {
2402
2428
  logger.log(`Credential offer received: ${uri}`);
2403
- oid4vciClient = await import_oid4vci_client3.OpenID4VCIClient.fromURI({
2429
+ oid4vciClient = await import_oid4vci_client3.OpenID4VCIClientV1_0_15.fromURI({
2404
2430
  uri,
2405
2431
  authorizationRequest: authorizationRequestOpts,
2406
2432
  clientId: authorizationRequestOpts.clientId,
2407
- createAuthorizationRequestURL: requestData.createAuthorizationRequestURL ?? true
2433
+ createAuthorizationRequestURL: false
2408
2434
  });
2409
2435
  }
2410
2436
  }
2437
+ let configurationIds = [];
2411
2438
  if (offer) {
2412
- types = (0, import_oid4vci_common4.getTypesFromCredentialOffer)(offer.original_credential_offer);
2439
+ configurationIds = offer.original_credential_offer.credential_configuration_ids;
2413
2440
  } else {
2414
- types = (0, import_utils2.asArray)(authorizationRequestOpts.authorizationDetails).map((authReqOpts) => (0, import_oid4vci_common4.getTypesFromAuthorizationDetails)(authReqOpts) ?? []).filter((inner) => inner.length > 0);
2441
+ configurationIds = (0, import_utils2.asArray)(authorizationRequestOpts.authorizationDetails).map((authReqOpts) => authReqOpts.credential_configuration_id).filter((id) => !!id);
2415
2442
  }
2416
- const serverMetadata = await oid4vciClient.retrieveServerMetadata();
2417
2443
  const credentialsSupported = await getCredentialConfigsSupportedMerged({
2418
2444
  client: oid4vciClient,
2419
2445
  vcFormatPreferences: formats,
2420
- types
2446
+ configurationIds
2421
2447
  });
2448
+ const serverMetadata = await oid4vciClient.retrieveServerMetadata();
2422
2449
  const credentialBranding = await getCredentialBranding({
2423
2450
  credentialsSupported,
2424
2451
  context
2425
2452
  });
2426
- const authorizationCodeURL = oid4vciClient.authorizationURL;
2427
- if (authorizationCodeURL) {
2428
- logger.log(`authorization code URL ${authorizationCodeURL}`);
2429
- }
2430
2453
  const oid4vciClientState = JSON.parse(await oid4vciClient.exportState());
2431
2454
  return {
2432
- authorizationCodeURL,
2433
2455
  credentialBranding,
2434
2456
  credentialsSupported,
2435
2457
  serverMetadata,
2436
2458
  oid4vciClientState
2437
2459
  };
2438
2460
  }
2461
+ async oid4vciHolderPrepareAuthorizationRequest(args, context) {
2462
+ const { openID4VCIClientState, contact } = args;
2463
+ if (!openID4VCIClientState) {
2464
+ return Promise.reject(Error("Missing openID4VCI client state in context"));
2465
+ }
2466
+ const clientId = contact?.identities.map((identity) => {
2467
+ const connectionConfig = identity.connection?.config;
2468
+ if (connectionConfig && "clientId" in connectionConfig) {
2469
+ return connectionConfig.clientId;
2470
+ }
2471
+ return void 0;
2472
+ }).find((clientId2) => clientId2);
2473
+ if (!clientId) {
2474
+ return Promise.reject(Error(`Missing client id in contact's connectionConfig`));
2475
+ }
2476
+ const client = await import_oid4vci_client3.OpenID4VCIClient.fromState({
2477
+ state: openID4VCIClientState
2478
+ });
2479
+ const authorizationCodeURL = await client.createAuthorizationRequestUrl({
2480
+ authorizationRequest: {
2481
+ clientId
2482
+ }
2483
+ });
2484
+ if (authorizationCodeURL) {
2485
+ logger.log(`authorization code URL ${authorizationCodeURL}`);
2486
+ }
2487
+ return {
2488
+ authorizationCodeURL,
2489
+ // Needed, because the above createAuthorizationRequestUrl manipulates the state, adding pkce opts to the state
2490
+ oid4vciClientState: JSON.parse(await client.exportState())
2491
+ };
2492
+ }
2439
2493
  async oid4vciHolderCreateCredentialsToSelectFrom(args, context) {
2440
2494
  const { credentialBranding, locale, selectedCredentials, credentialsSupported } = args;
2441
2495
  logger.info(`Credentials supported ${Object.keys(credentialsSupported).join(", ")}`);
@@ -2507,7 +2561,7 @@ var OID4VCIHolder = class _OID4VCIHolder {
2507
2561
  if (!openID4VCIClientState) {
2508
2562
  return Promise.reject(Error("Missing openID4VCI client state in context"));
2509
2563
  }
2510
- const client = await import_oid4vci_client3.OpenID4VCIClient.fromState({
2564
+ const client = await import_oid4vci_client3.OpenID4VCIClientV1_0_15.fromState({
2511
2565
  state: openID4VCIClientState
2512
2566
  });
2513
2567
  const credentialsSupported = await getCredentialConfigsSupportedMerged({
@@ -2646,7 +2700,7 @@ var OID4VCIHolder = class _OID4VCIHolder {
2646
2700
  alias: credentialsToAccept[0].correlationId,
2647
2701
  origin: import_ssi_sdk3.IdentityOrigin.EXTERNAL,
2648
2702
  roles: [
2649
- import_ssi_sdk3.CredentialRole.ISSUER
2703
+ import_ssi_types2.CredentialRole.ISSUER
2650
2704
  ],
2651
2705
  identifier: {
2652
2706
  type: identifierType,
@@ -2681,7 +2735,7 @@ var OID4VCIHolder = class _OID4VCIHolder {
2681
2735
  }
2682
2736
  async oid4vciHolderGetIssuerBranding(args, context) {
2683
2737
  const { serverMetadata, contact } = args;
2684
- const issuerCorrelationId = contact?.identities.filter((identity) => identity.roles.includes(import_ssi_sdk3.CredentialRole.ISSUER)).map((identity) => identity.identifier.correlationId)[0];
2738
+ const issuerCorrelationId = contact?.identities.filter((identity) => identity.roles.includes(import_ssi_types2.CredentialRole.ISSUER)).map((identity) => identity.identifier.correlationId)[0];
2685
2739
  if (issuerCorrelationId) {
2686
2740
  const branding = await context.agent.ibGetIssuerBranding({
2687
2741
  filter: [
@@ -2711,7 +2765,7 @@ var OID4VCIHolder = class _OID4VCIHolder {
2711
2765
  if (!contact) {
2712
2766
  return Promise.reject(Error("Missing contact in context"));
2713
2767
  }
2714
- const issuerCorrelationId = contact?.identities.filter((identity) => identity.roles.includes(import_ssi_sdk3.CredentialRole.ISSUER)).map((identity) => identity.identifier.correlationId)[0];
2768
+ const issuerCorrelationId = contact?.identities.filter((identity) => identity.roles.includes(import_ssi_types2.CredentialRole.ISSUER)).map((identity) => identity.identifier.correlationId)[0];
2715
2769
  const branding = await context.agent.ibGetIssuerBranding({
2716
2770
  filter: [
2717
2771
  {
@@ -2913,7 +2967,7 @@ var OID4VCIHolder = class _OID4VCIHolder {
2913
2967
  rawDocument: (0, import_ssi_sdk3.ensureRawDocument)(persistCredential),
2914
2968
  kmsKeyRef,
2915
2969
  identifierMethod: method,
2916
- credentialRole: import_ssi_sdk3.CredentialRole.HOLDER,
2970
+ credentialRole: import_ssi_types2.CredentialRole.HOLDER,
2917
2971
  issuerCorrelationType: issuer?.startsWith("did:") ? import_ssi_sdk3.CredentialCorrelationType.DID : import_ssi_sdk3.CredentialCorrelationType.URL,
2918
2972
  issuerCorrelationId: issuer,
2919
2973
  subjectCorrelationType,