@sphereon/ssi-sdk.siopv2-oid4vp-rp-auth 0.34.1-fix.182 → 0.34.1-fix.223

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
@@ -22,7 +22,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
24
  SIOPv2RP: () => SIOPv2RP,
25
- VerifiedDataMode: () => VerifiedDataMode,
26
25
  schema: () => plugin_schema_default
27
26
  });
28
27
  module.exports = __toCommonJS(index_exports);
@@ -362,6 +361,7 @@ var plugin_schema_default = {
362
361
  var import_did_auth_siop2 = require("@sphereon/did-auth-siop");
363
362
  var import_ssi_sdk_ext4 = require("@sphereon/ssi-sdk-ext.did-utils");
364
363
  var import_ssi_sdk2 = require("@sphereon/ssi-sdk.core");
364
+ var import_uuid2 = require("uuid");
365
365
  var import_ssi_types2 = require("@sphereon/ssi-types");
366
366
  var import_dcql = require("dcql");
367
367
 
@@ -377,7 +377,7 @@ function getRequestVersion(rpOptions) {
377
377
  if (Array.isArray(rpOptions.supportedVersions) && rpOptions.supportedVersions.length > 0) {
378
378
  return rpOptions.supportedVersions[0];
379
379
  }
380
- return import_did_auth_siop.SupportedVersion.JWT_VC_PRESENTATION_PROFILE_v1;
380
+ return import_did_auth_siop.SupportedVersion.OID4VP_v1;
381
381
  }
382
382
  __name(getRequestVersion, "getRequestVersion");
383
383
  function getWellKnownDIDVerifyCallback(siopIdentifierOpts, context) {
@@ -392,6 +392,33 @@ function getWellKnownDIDVerifyCallback(siopIdentifierOpts, context) {
392
392
  };
393
393
  }
394
394
  __name(getWellKnownDIDVerifyCallback, "getWellKnownDIDVerifyCallback");
395
+ function getDcqlQueryLookupCallback(context) {
396
+ async function dcqlQueryLookup(queryId, version, tenantId) {
397
+ const result = await context.agent.pdmGetDefinitions({
398
+ filter: [
399
+ {
400
+ queryId,
401
+ ...tenantId && {
402
+ tenantId
403
+ },
404
+ ...version && {
405
+ version
406
+ }
407
+ },
408
+ {
409
+ id: queryId
410
+ }
411
+ ]
412
+ });
413
+ if (result && result.length > 0) {
414
+ return result[0].query;
415
+ }
416
+ return Promise.reject(Error(`No dcql query found for queryId ${queryId}`));
417
+ }
418
+ __name(dcqlQueryLookup, "dcqlQueryLookup");
419
+ return dcqlQueryLookup;
420
+ }
421
+ __name(getDcqlQueryLookupCallback, "getDcqlQueryLookupCallback");
395
422
  function getPresentationVerificationCallback(idOpts, context) {
396
423
  async function presentationVerificationCallback(args, presentationSubmission) {
397
424
  if (import_ssi_types.CredentialMapper.isSdJwtEncoded(args)) {
@@ -431,27 +458,8 @@ function getPresentationVerificationCallback(idOpts, context) {
431
458
  }
432
459
  __name(getPresentationVerificationCallback, "getPresentationVerificationCallback");
433
460
  async function createRPBuilder(args) {
434
- const { rpOpts, pexOpts, context } = args;
461
+ const { rpOpts, context } = args;
435
462
  const { identifierOpts } = rpOpts;
436
- let definition = args.definition;
437
- let dcqlQuery = args.dcql;
438
- if (!definition && pexOpts && pexOpts.queryId) {
439
- const presentationDefinitionItems = await context.agent.pdmGetDefinitions({
440
- filter: [
441
- {
442
- queryId: pexOpts.queryId,
443
- version: pexOpts.version,
444
- tenantId: pexOpts.tenantId
445
- }
446
- ]
447
- });
448
- if (presentationDefinitionItems.length > 0) {
449
- const presentationDefinitionItem = presentationDefinitionItems[0];
450
- if (!dcqlQuery && presentationDefinitionItem.dcqlPayload) {
451
- dcqlQuery = presentationDefinitionItem.dcqlPayload.dcqlQuery;
452
- }
453
- }
454
- }
455
463
  const didMethods = identifierOpts.supportedDIDMethods ?? await (0, import_ssi_sdk_ext.getAgentDIDMethods)(context);
456
464
  const eventEmitter = rpOpts.eventEmitter ?? new import_events.EventEmitter();
457
465
  const defaultClientMetadata = {
@@ -506,16 +514,15 @@ async function createRPBuilder(args) {
506
514
  const builder = import_did_auth_siop.RP.builder({
507
515
  requestVersion: getRequestVersion(rpOpts)
508
516
  }).withScope("openid", import_did_auth_siop.PropertyTarget.REQUEST_OBJECT).withResponseMode(rpOpts.responseMode ?? import_did_auth_siop.ResponseMode.POST).withResponseType(import_did_auth_siop.ResponseType.VP_TOKEN, import_did_auth_siop.PropertyTarget.REQUEST_OBJECT).withSupportedVersions(rpOpts.supportedVersions ?? [
509
- import_did_auth_siop.SupportedVersion.JWT_VC_PRESENTATION_PROFILE_v1,
510
- import_did_auth_siop.SupportedVersion.SIOPv2_ID1,
511
- import_did_auth_siop.SupportedVersion.SIOPv2_D11
517
+ import_did_auth_siop.SupportedVersion.OID4VP_v1,
518
+ import_did_auth_siop.SupportedVersion.SIOPv2_OID4VP_D28
512
519
  ]).withEventEmitter(eventEmitter).withSessionManager(rpOpts.sessionManager ?? new import_did_auth_siop.InMemoryRPSessionManager(eventEmitter)).withClientMetadata(rpOpts.clientMetadataOpts ?? defaultClientMetadata, import_did_auth_siop.PropertyTarget.REQUEST_OBJECT).withVerifyJwtCallback(rpOpts.verifyJwtCallback ? rpOpts.verifyJwtCallback : getVerifyJwtCallback({
513
520
  resolver,
514
521
  verifyOpts: {
515
522
  wellknownDIDVerifyCallback: getWellKnownDIDVerifyCallback(rpOpts.identifierOpts, context),
516
523
  checkLinkedDomain: "if_present"
517
524
  }
518
- }, context)).withRevocationVerification(import_did_auth_siop.RevocationVerification.NEVER).withPresentationVerification(getPresentationVerificationCallback(identifierOpts.idOpts, context));
525
+ }, context)).withDcqlQueryLookup(getDcqlQueryLookupCallback(context)).withRevocationVerification(import_did_auth_siop.RevocationVerification.NEVER).withPresentationVerification(getPresentationVerificationCallback(identifierOpts.idOpts, context));
519
526
  const oidfOpts = identifierOpts.oidfOpts;
520
527
  if (oidfOpts && (0, import_ssi_sdk_ext2.isExternalIdentifierOIDFEntityIdOpts)(oidfOpts)) {
521
528
  builder.withEntityId(oidfOpts.identifier, import_did_auth_siop.PropertyTarget.REQUEST_OBJECT);
@@ -528,9 +535,6 @@ async function createRPBuilder(args) {
528
535
  if (hasher) {
529
536
  builder.withHasher(hasher);
530
537
  }
531
- if (dcqlQuery) {
532
- builder.withDcqlQuery(dcqlQuery);
533
- }
534
538
  if (rpOpts.responseRedirectUri) {
535
539
  builder.withResponseRedirectUri(rpOpts.responseRedirectUri);
536
540
  }
@@ -603,17 +607,16 @@ var RPInstance = class {
603
607
  __name(this, "RPInstance");
604
608
  }
605
609
  _rp;
606
- _pexOptions;
610
+ _presentationOptions;
607
611
  _rpOptions;
608
612
  constructor({ rpOpts, pexOpts }) {
609
613
  this._rpOptions = rpOpts;
610
- this._pexOptions = pexOpts;
614
+ this._presentationOptions = pexOpts;
611
615
  }
612
616
  async get(context) {
613
617
  if (!this._rp) {
614
618
  const builder = await createRPBuilder({
615
619
  rpOpts: this._rpOptions,
616
- pexOpts: this._pexOptions,
617
620
  context
618
621
  });
619
622
  this._rp = builder.build();
@@ -623,20 +626,8 @@ var RPInstance = class {
623
626
  get rpOptions() {
624
627
  return this._rpOptions;
625
628
  }
626
- get pexOptions() {
627
- return this._pexOptions;
628
- }
629
- hasDefinition() {
630
- return this.definitionId !== void 0;
631
- }
632
- get definitionId() {
633
- return this.pexOptions?.queryId;
634
- }
635
- async getPresentationDefinition(context) {
636
- return this.definitionId ? await context.agent.pexStoreGetDefinition({
637
- definitionId: this.definitionId,
638
- tenantId: this.pexOptions?.tenantId
639
- }) : void 0;
629
+ get presentationOptions() {
630
+ return this._presentationOptions;
640
631
  }
641
632
  async createAuthorizationRequestURI(createArgs, context) {
642
633
  const { correlationId, queryId, claims, requestByReferenceURI, responseURI, responseURIType, callback } = createArgs;
@@ -753,6 +744,7 @@ var SIOPv2RP = class _SIOPv2RP {
753
744
  }
754
745
  async createAuthorizationRequestURI(createArgs, context) {
755
746
  return await this.getRPInstance({
747
+ createWhenNotPresent: true,
756
748
  responseRedirectURI: createArgs.responseRedirectURI,
757
749
  ...createArgs.useQueryIdInstance === true && {
758
750
  queryId: createArgs.queryId
@@ -761,6 +753,7 @@ var SIOPv2RP = class _SIOPv2RP {
761
753
  }
762
754
  async createAuthorizationRequestPayloads(createArgs, context) {
763
755
  return await this.getRPInstance({
756
+ createWhenNotPresent: true,
764
757
  queryId: createArgs.queryId
765
758
  }, context).then((rp) => rp.createAuthorizationRequest(createArgs, context)).then(async (request) => {
766
759
  const authRequest = {
@@ -773,11 +766,13 @@ var SIOPv2RP = class _SIOPv2RP {
773
766
  }
774
767
  async siopGetRequestState(args, context) {
775
768
  return await this.getRPInstance({
769
+ createWhenNotPresent: false,
776
770
  queryId: args.queryId
777
771
  }, context).then((rp) => rp.get(context).then((rp2) => rp2.sessionManager.getRequestStateByCorrelationId(args.correlationId, args.errorOnNotFound)));
778
772
  }
779
773
  async siopGetResponseState(args, context) {
780
774
  const rpInstance = await this.getRPInstance({
775
+ createWhenNotPresent: false,
781
776
  queryId: args.queryId
782
777
  }, context);
783
778
  const authorizationResponseState = await rpInstance.get(context).then((rp) => rp.sessionManager.getResponseStateByCorrelationId(args.correlationId, args.errorOnNotFound));
@@ -785,23 +780,24 @@ var SIOPv2RP = class _SIOPv2RP {
785
780
  return void 0;
786
781
  }
787
782
  const responseState = authorizationResponseState;
788
- if (responseState.status === import_did_auth_siop2.AuthorizationResponseStateStatus.VERIFIED && args.includeVerifiedData && args.includeVerifiedData !== VerifiedDataMode.NONE) {
783
+ if (responseState.status === import_did_auth_siop2.AuthorizationResponseStateStatus.VERIFIED) {
789
784
  let hasher;
790
785
  if (import_ssi_types2.CredentialMapper.isSdJwtEncoded(responseState.response.payload.vp_token) && (!rpInstance.rpOptions.credentialOpts?.hasher || typeof rpInstance.rpOptions.credentialOpts?.hasher !== "function")) {
791
786
  hasher = import_ssi_sdk2.shaHasher;
792
787
  }
793
- const presentationDecoded = import_ssi_types2.CredentialMapper.decodeVerifiablePresentation(
794
- responseState.response.payload.vp_token,
795
- //todo: later we want to conditionally pass in options for mdl-mdoc here
796
- hasher
797
- );
798
- switch (args.includeVerifiedData) {
799
- case VerifiedDataMode.VERIFIED_PRESENTATION:
800
- responseState.response.payload.verifiedData = this.presentationOrClaimsFrom(presentationDecoded);
801
- break;
802
- case VerifiedDataMode.CREDENTIAL_SUBJECT_FLATTENED:
803
- const allClaims = {};
804
- for (const credential of this.presentationOrClaimsFrom(presentationDecoded).verifiableCredential || []) {
788
+ const vpToken = responseState.response.payload.vp_token && JSON.parse(responseState.response.payload.vp_token);
789
+ const claims = [];
790
+ for (const [key, value] of Object.entries(vpToken)) {
791
+ const presentationDecoded = import_ssi_types2.CredentialMapper.decodeVerifiablePresentation(
792
+ value,
793
+ //todo: later we want to conditionally pass in options for mdl-mdoc here
794
+ hasher
795
+ );
796
+ console.log(`presentationDecoded: ${JSON.stringify(presentationDecoded)}`);
797
+ const allClaims = {};
798
+ const presentationOrClaims = this.presentationOrClaimsFrom(presentationDecoded);
799
+ if ("verifiableCredential" in presentationOrClaims) {
800
+ for (const credential of presentationOrClaims.verifiableCredential) {
805
801
  const vc = credential;
806
802
  const schemaValidationResult = await context.agent.cvVerifySchema({
807
803
  credential,
@@ -817,24 +813,47 @@ var SIOPv2RP = class _SIOPv2RP {
817
813
  if (!("id" in allClaims)) {
818
814
  allClaims["id"] = credentialSubject.id;
819
815
  }
820
- Object.entries(credentialSubject).forEach(([key, value]) => {
821
- if (!(key in allClaims)) {
822
- allClaims[key] = value;
816
+ Object.entries(credentialSubject).forEach(([key2, value2]) => {
817
+ if (!(key2 in allClaims)) {
818
+ allClaims[key2] = value2;
823
819
  }
824
820
  });
821
+ claims.push({
822
+ id: key,
823
+ type: vc.type[0],
824
+ claims: allClaims
825
+ });
825
826
  }
826
- responseState.verifiedData = allClaims;
827
- break;
827
+ } else {
828
+ claims.push({
829
+ id: key,
830
+ type: presentationDecoded.decodedPayload.vct,
831
+ claims: presentationOrClaims
832
+ });
833
+ }
828
834
  }
835
+ responseState.verifiedData = {
836
+ ...responseState.response.payload.vp_token && {
837
+ authorization_response: {
838
+ vp_token: typeof responseState.response.payload.vp_token === "string" ? JSON.parse(responseState.response.payload.vp_token) : responseState.response.payload.vp_token
839
+ }
840
+ },
841
+ ...claims.length > 0 && {
842
+ credential_claims: claims
843
+ }
844
+ };
829
845
  }
830
846
  return responseState;
831
847
  }
832
- presentationOrClaimsFrom = /* @__PURE__ */ __name((presentationDecoded) => import_ssi_types2.CredentialMapper.isSdJwtDecodedCredential(presentationDecoded) ? presentationDecoded.decodedPayload : import_ssi_types2.CredentialMapper.toUniformPresentation(presentationDecoded), "presentationOrClaimsFrom");
848
+ presentationOrClaimsFrom = /* @__PURE__ */ __name((presentationDecoded) => {
849
+ return import_ssi_types2.CredentialMapper.isSdJwtDecodedCredential(presentationDecoded) ? presentationDecoded.decodedPayload : import_ssi_types2.CredentialMapper.toUniformPresentation(presentationDecoded);
850
+ }, "presentationOrClaimsFrom");
833
851
  async siopUpdateRequestState(args, context) {
834
852
  if (args.state !== "authorization_request_created") {
835
853
  throw Error(`Only 'authorization_request_created' status is supported for this method at this point`);
836
854
  }
837
855
  return await this.getRPInstance({
856
+ createWhenNotPresent: false,
838
857
  queryId: args.queryId
839
858
  }, context).then((rp) => rp.get(context).then(async (rp2) => {
840
859
  await rp2.signalAuthRequestRetrieved({
@@ -846,6 +865,7 @@ var SIOPv2RP = class _SIOPv2RP {
846
865
  }
847
866
  async siopDeleteState(args, context) {
848
867
  return await this.getRPInstance({
868
+ createWhenNotPresent: false,
849
869
  queryId: args.queryId
850
870
  }, context).then((rp) => rp.get(context).then((rp2) => rp2.sessionManager.deleteStateForCorrelationId(args.correlationId))).then(() => true);
851
871
  }
@@ -855,12 +875,13 @@ var SIOPv2RP = class _SIOPv2RP {
855
875
  }
856
876
  const authResponse = typeof args.authorizationResponse === "string" ? (0, import_did_auth_siop2.decodeUriAsJson)(args.authorizationResponse) : args.authorizationResponse;
857
877
  return await this.getRPInstance({
878
+ createWhenNotPresent: false,
858
879
  queryId: args.queryId
859
880
  }, context).then((rp) => rp.get(context).then((rp2) => rp2.verifyAuthorizationResponse(authResponse, {
860
881
  correlationId: args.correlationId,
861
- ...args.dcqlQuery ? {
882
+ ...args.dcqlQuery && {
862
883
  dcqlQuery: args.dcqlQuery
863
- } : {},
884
+ },
864
885
  audience: args.audience
865
886
  })));
866
887
  }
@@ -899,9 +920,37 @@ var SIOPv2RP = class _SIOPv2RP {
899
920
  }
900
921
  return void 0;
901
922
  }
902
- async getRPInstance({ queryId, responseRedirectURI }, context) {
903
- const instanceId = queryId ?? _SIOPv2RP._DEFAULT_OPTS_KEY;
904
- if (!this.instances.has(instanceId)) {
923
+ async getRPInstance({ createWhenNotPresent, queryId, responseRedirectURI }, context) {
924
+ let rpInstanceId = _SIOPv2RP._DEFAULT_OPTS_KEY;
925
+ let rpInstance;
926
+ if (queryId) {
927
+ if (this.instances.has(queryId)) {
928
+ rpInstanceId = queryId;
929
+ rpInstance = this.instances.get(rpInstanceId);
930
+ } else if ((0, import_uuid2.validate)(queryId)) {
931
+ try {
932
+ const pd = await context.agent.pdmGetDefinition({
933
+ itemId: queryId
934
+ });
935
+ if (this.instances.has(pd.queryId)) {
936
+ rpInstanceId = pd.queryId;
937
+ rpInstance = this.instances.get(rpInstanceId);
938
+ }
939
+ } catch (ignore) {
940
+ }
941
+ }
942
+ if (createWhenNotPresent) {
943
+ rpInstanceId = queryId;
944
+ } else {
945
+ rpInstance = this.instances.get(rpInstanceId);
946
+ }
947
+ } else {
948
+ rpInstance = this.instances.get(rpInstanceId);
949
+ }
950
+ if (!rpInstance) {
951
+ if (!createWhenNotPresent) {
952
+ return Promise.reject(`No RP instance found for key ${rpInstanceId}`);
953
+ }
905
954
  const instanceOpts = this.getInstanceOpts(queryId);
906
955
  const rpOpts = await this.getRPOptions(context, {
907
956
  queryId,
@@ -923,12 +972,12 @@ var SIOPv2RP = class _SIOPv2RP {
923
972
  resolverResolution: true
924
973
  });
925
974
  }
926
- this.instances.set(instanceId, new RPInstance({
975
+ rpInstance = new RPInstance({
927
976
  rpOpts,
928
977
  pexOpts: instanceOpts
929
- }));
978
+ });
979
+ this.instances.set(rpInstanceId, rpInstance);
930
980
  }
931
- const rpInstance = this.instances.get(instanceId);
932
981
  if (responseRedirectURI) {
933
982
  rpInstance.rpOptions.responseRedirectUri = responseRedirectURI;
934
983
  }
@@ -970,32 +1019,24 @@ var SIOPv2RP = class _SIOPv2RP {
970
1019
  }
971
1020
  return options;
972
1021
  }
973
- getInstanceOpts(definitionId) {
1022
+ getInstanceOpts(queryId) {
974
1023
  if (!this.opts.instanceOpts) return void 0;
975
- const instanceOpt = definitionId ? this.opts.instanceOpts.find((i) => i.queryId === definitionId) : void 0;
976
- return instanceOpt ?? this.getDefaultOptions(definitionId);
1024
+ const instanceOpt = queryId ? this.opts.instanceOpts.find((i) => i.queryId === queryId) : void 0;
1025
+ return instanceOpt ?? this.getDefaultOptions(queryId);
977
1026
  }
978
- getDefaultOptions(definitionId) {
1027
+ getDefaultOptions(queryId) {
979
1028
  if (!this.opts.instanceOpts) return void 0;
980
1029
  const defaultOptions = this.opts.instanceOpts.find((i) => i.queryId === "default");
981
1030
  if (defaultOptions) {
982
1031
  const clonedOptions = {
983
1032
  ...defaultOptions
984
1033
  };
985
- if (definitionId !== void 0) {
986
- clonedOptions.queryId = definitionId;
1034
+ if (queryId !== void 0) {
1035
+ clonedOptions.queryId = queryId;
987
1036
  }
988
1037
  return clonedOptions;
989
1038
  }
990
1039
  return void 0;
991
1040
  }
992
1041
  };
993
-
994
- // src/types/ISIOPv2RP.ts
995
- var VerifiedDataMode = /* @__PURE__ */ (function(VerifiedDataMode2) {
996
- VerifiedDataMode2["NONE"] = "none";
997
- VerifiedDataMode2["VERIFIED_PRESENTATION"] = "vp";
998
- VerifiedDataMode2["CREDENTIAL_SUBJECT_FLATTENED"] = "cs-flat";
999
- return VerifiedDataMode2;
1000
- })({});
1001
1042
  //# sourceMappingURL=index.cjs.map