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

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
@@ -336,6 +336,7 @@ var plugin_schema_default = {
336
336
  import { AuthorizationResponseStateStatus, decodeUriAsJson } from "@sphereon/did-auth-siop";
337
337
  import { getAgentResolver as getAgentResolver2 } from "@sphereon/ssi-sdk-ext.did-utils";
338
338
  import { shaHasher as defaultHasher2 } from "@sphereon/ssi-sdk.core";
339
+ import { validate as isValidUUID } from "uuid";
339
340
  import { CredentialMapper as CredentialMapper2 } from "@sphereon/ssi-types";
340
341
  import { DcqlQuery } from "dcql";
341
342
 
@@ -351,7 +352,7 @@ function getRequestVersion(rpOptions) {
351
352
  if (Array.isArray(rpOptions.supportedVersions) && rpOptions.supportedVersions.length > 0) {
352
353
  return rpOptions.supportedVersions[0];
353
354
  }
354
- return SupportedVersion.JWT_VC_PRESENTATION_PROFILE_v1;
355
+ return SupportedVersion.OID4VP_v1;
355
356
  }
356
357
  __name(getRequestVersion, "getRequestVersion");
357
358
  function getWellKnownDIDVerifyCallback(siopIdentifierOpts, context) {
@@ -366,6 +367,33 @@ function getWellKnownDIDVerifyCallback(siopIdentifierOpts, context) {
366
367
  };
367
368
  }
368
369
  __name(getWellKnownDIDVerifyCallback, "getWellKnownDIDVerifyCallback");
370
+ function getDcqlQueryLookupCallback(context) {
371
+ async function dcqlQueryLookup(queryId, version, tenantId) {
372
+ const result = await context.agent.pdmGetDefinitions({
373
+ filter: [
374
+ {
375
+ queryId,
376
+ ...tenantId && {
377
+ tenantId
378
+ },
379
+ ...version && {
380
+ version
381
+ }
382
+ },
383
+ {
384
+ id: queryId
385
+ }
386
+ ]
387
+ });
388
+ if (result && result.length > 0) {
389
+ return result[0].query;
390
+ }
391
+ return Promise.reject(Error(`No dcql query found for queryId ${queryId}`));
392
+ }
393
+ __name(dcqlQueryLookup, "dcqlQueryLookup");
394
+ return dcqlQueryLookup;
395
+ }
396
+ __name(getDcqlQueryLookupCallback, "getDcqlQueryLookupCallback");
369
397
  function getPresentationVerificationCallback(idOpts, context) {
370
398
  async function presentationVerificationCallback(args, presentationSubmission) {
371
399
  if (CredentialMapper.isSdJwtEncoded(args)) {
@@ -405,27 +433,8 @@ function getPresentationVerificationCallback(idOpts, context) {
405
433
  }
406
434
  __name(getPresentationVerificationCallback, "getPresentationVerificationCallback");
407
435
  async function createRPBuilder(args) {
408
- const { rpOpts, pexOpts, context } = args;
436
+ const { rpOpts, context } = args;
409
437
  const { identifierOpts } = rpOpts;
410
- let definition = args.definition;
411
- let dcqlQuery = args.dcql;
412
- if (!definition && pexOpts && pexOpts.queryId) {
413
- const presentationDefinitionItems = await context.agent.pdmGetDefinitions({
414
- filter: [
415
- {
416
- queryId: pexOpts.queryId,
417
- version: pexOpts.version,
418
- tenantId: pexOpts.tenantId
419
- }
420
- ]
421
- });
422
- if (presentationDefinitionItems.length > 0) {
423
- const presentationDefinitionItem = presentationDefinitionItems[0];
424
- if (!dcqlQuery && presentationDefinitionItem.dcqlPayload) {
425
- dcqlQuery = presentationDefinitionItem.dcqlPayload.dcqlQuery;
426
- }
427
- }
428
- }
429
438
  const didMethods = identifierOpts.supportedDIDMethods ?? await getAgentDIDMethods(context);
430
439
  const eventEmitter = rpOpts.eventEmitter ?? new EventEmitter();
431
440
  const defaultClientMetadata = {
@@ -480,16 +489,15 @@ async function createRPBuilder(args) {
480
489
  const builder = RP.builder({
481
490
  requestVersion: getRequestVersion(rpOpts)
482
491
  }).withScope("openid", PropertyTarget.REQUEST_OBJECT).withResponseMode(rpOpts.responseMode ?? ResponseMode.POST).withResponseType(ResponseType.VP_TOKEN, PropertyTarget.REQUEST_OBJECT).withSupportedVersions(rpOpts.supportedVersions ?? [
483
- SupportedVersion.JWT_VC_PRESENTATION_PROFILE_v1,
484
- SupportedVersion.SIOPv2_ID1,
485
- SupportedVersion.SIOPv2_D11
492
+ SupportedVersion.OID4VP_v1,
493
+ SupportedVersion.SIOPv2_OID4VP_D28
486
494
  ]).withEventEmitter(eventEmitter).withSessionManager(rpOpts.sessionManager ?? new InMemoryRPSessionManager(eventEmitter)).withClientMetadata(rpOpts.clientMetadataOpts ?? defaultClientMetadata, PropertyTarget.REQUEST_OBJECT).withVerifyJwtCallback(rpOpts.verifyJwtCallback ? rpOpts.verifyJwtCallback : getVerifyJwtCallback({
487
495
  resolver,
488
496
  verifyOpts: {
489
497
  wellknownDIDVerifyCallback: getWellKnownDIDVerifyCallback(rpOpts.identifierOpts, context),
490
498
  checkLinkedDomain: "if_present"
491
499
  }
492
- }, context)).withRevocationVerification(RevocationVerification.NEVER).withPresentationVerification(getPresentationVerificationCallback(identifierOpts.idOpts, context));
500
+ }, context)).withDcqlQueryLookup(getDcqlQueryLookupCallback(context)).withRevocationVerification(RevocationVerification.NEVER).withPresentationVerification(getPresentationVerificationCallback(identifierOpts.idOpts, context));
493
501
  const oidfOpts = identifierOpts.oidfOpts;
494
502
  if (oidfOpts && isExternalIdentifierOIDFEntityIdOpts(oidfOpts)) {
495
503
  builder.withEntityId(oidfOpts.identifier, PropertyTarget.REQUEST_OBJECT);
@@ -502,9 +510,6 @@ async function createRPBuilder(args) {
502
510
  if (hasher) {
503
511
  builder.withHasher(hasher);
504
512
  }
505
- if (dcqlQuery) {
506
- builder.withDcqlQuery(dcqlQuery);
507
- }
508
513
  if (rpOpts.responseRedirectUri) {
509
514
  builder.withResponseRedirectUri(rpOpts.responseRedirectUri);
510
515
  }
@@ -577,17 +582,16 @@ var RPInstance = class {
577
582
  __name(this, "RPInstance");
578
583
  }
579
584
  _rp;
580
- _pexOptions;
585
+ _presentationOptions;
581
586
  _rpOptions;
582
587
  constructor({ rpOpts, pexOpts }) {
583
588
  this._rpOptions = rpOpts;
584
- this._pexOptions = pexOpts;
589
+ this._presentationOptions = pexOpts;
585
590
  }
586
591
  async get(context) {
587
592
  if (!this._rp) {
588
593
  const builder = await createRPBuilder({
589
594
  rpOpts: this._rpOptions,
590
- pexOpts: this._pexOptions,
591
595
  context
592
596
  });
593
597
  this._rp = builder.build();
@@ -597,20 +601,8 @@ var RPInstance = class {
597
601
  get rpOptions() {
598
602
  return this._rpOptions;
599
603
  }
600
- get pexOptions() {
601
- return this._pexOptions;
602
- }
603
- hasDefinition() {
604
- return this.definitionId !== void 0;
605
- }
606
- get definitionId() {
607
- return this.pexOptions?.queryId;
608
- }
609
- async getPresentationDefinition(context) {
610
- return this.definitionId ? await context.agent.pexStoreGetDefinition({
611
- definitionId: this.definitionId,
612
- tenantId: this.pexOptions?.tenantId
613
- }) : void 0;
604
+ get presentationOptions() {
605
+ return this._presentationOptions;
614
606
  }
615
607
  async createAuthorizationRequestURI(createArgs, context) {
616
608
  const { correlationId, queryId, claims, requestByReferenceURI, responseURI, responseURIType, callback } = createArgs;
@@ -727,6 +719,7 @@ var SIOPv2RP = class _SIOPv2RP {
727
719
  }
728
720
  async createAuthorizationRequestURI(createArgs, context) {
729
721
  return await this.getRPInstance({
722
+ createWhenNotPresent: true,
730
723
  responseRedirectURI: createArgs.responseRedirectURI,
731
724
  ...createArgs.useQueryIdInstance === true && {
732
725
  queryId: createArgs.queryId
@@ -735,6 +728,7 @@ var SIOPv2RP = class _SIOPv2RP {
735
728
  }
736
729
  async createAuthorizationRequestPayloads(createArgs, context) {
737
730
  return await this.getRPInstance({
731
+ createWhenNotPresent: true,
738
732
  queryId: createArgs.queryId
739
733
  }, context).then((rp) => rp.createAuthorizationRequest(createArgs, context)).then(async (request) => {
740
734
  const authRequest = {
@@ -747,11 +741,13 @@ var SIOPv2RP = class _SIOPv2RP {
747
741
  }
748
742
  async siopGetRequestState(args, context) {
749
743
  return await this.getRPInstance({
744
+ createWhenNotPresent: false,
750
745
  queryId: args.queryId
751
746
  }, context).then((rp) => rp.get(context).then((rp2) => rp2.sessionManager.getRequestStateByCorrelationId(args.correlationId, args.errorOnNotFound)));
752
747
  }
753
748
  async siopGetResponseState(args, context) {
754
749
  const rpInstance = await this.getRPInstance({
750
+ createWhenNotPresent: false,
755
751
  queryId: args.queryId
756
752
  }, context);
757
753
  const authorizationResponseState = await rpInstance.get(context).then((rp) => rp.sessionManager.getResponseStateByCorrelationId(args.correlationId, args.errorOnNotFound));
@@ -759,23 +755,24 @@ var SIOPv2RP = class _SIOPv2RP {
759
755
  return void 0;
760
756
  }
761
757
  const responseState = authorizationResponseState;
762
- if (responseState.status === AuthorizationResponseStateStatus.VERIFIED && args.includeVerifiedData && args.includeVerifiedData !== VerifiedDataMode.NONE) {
758
+ if (responseState.status === AuthorizationResponseStateStatus.VERIFIED) {
763
759
  let hasher;
764
760
  if (CredentialMapper2.isSdJwtEncoded(responseState.response.payload.vp_token) && (!rpInstance.rpOptions.credentialOpts?.hasher || typeof rpInstance.rpOptions.credentialOpts?.hasher !== "function")) {
765
761
  hasher = defaultHasher2;
766
762
  }
767
- const presentationDecoded = CredentialMapper2.decodeVerifiablePresentation(
768
- responseState.response.payload.vp_token,
769
- //todo: later we want to conditionally pass in options for mdl-mdoc here
770
- hasher
771
- );
772
- switch (args.includeVerifiedData) {
773
- case VerifiedDataMode.VERIFIED_PRESENTATION:
774
- responseState.response.payload.verifiedData = this.presentationOrClaimsFrom(presentationDecoded);
775
- break;
776
- case VerifiedDataMode.CREDENTIAL_SUBJECT_FLATTENED:
777
- const allClaims = {};
778
- for (const credential of this.presentationOrClaimsFrom(presentationDecoded).verifiableCredential || []) {
763
+ const vpToken = responseState.response.payload.vp_token && JSON.parse(responseState.response.payload.vp_token);
764
+ const claims = [];
765
+ for (const [key, value] of Object.entries(vpToken)) {
766
+ const presentationDecoded = CredentialMapper2.decodeVerifiablePresentation(
767
+ value,
768
+ //todo: later we want to conditionally pass in options for mdl-mdoc here
769
+ hasher
770
+ );
771
+ console.log(`presentationDecoded: ${JSON.stringify(presentationDecoded)}`);
772
+ const allClaims = {};
773
+ const presentationOrClaims = this.presentationOrClaimsFrom(presentationDecoded);
774
+ if ("verifiableCredential" in presentationOrClaims) {
775
+ for (const credential of presentationOrClaims.verifiableCredential) {
779
776
  const vc = credential;
780
777
  const schemaValidationResult = await context.agent.cvVerifySchema({
781
778
  credential,
@@ -791,24 +788,47 @@ var SIOPv2RP = class _SIOPv2RP {
791
788
  if (!("id" in allClaims)) {
792
789
  allClaims["id"] = credentialSubject.id;
793
790
  }
794
- Object.entries(credentialSubject).forEach(([key, value]) => {
795
- if (!(key in allClaims)) {
796
- allClaims[key] = value;
791
+ Object.entries(credentialSubject).forEach(([key2, value2]) => {
792
+ if (!(key2 in allClaims)) {
793
+ allClaims[key2] = value2;
797
794
  }
798
795
  });
796
+ claims.push({
797
+ id: key,
798
+ type: vc.type[0],
799
+ claims: allClaims
800
+ });
799
801
  }
800
- responseState.verifiedData = allClaims;
801
- break;
802
+ } else {
803
+ claims.push({
804
+ id: key,
805
+ type: presentationDecoded.decodedPayload.vct,
806
+ claims: presentationOrClaims
807
+ });
808
+ }
802
809
  }
810
+ responseState.verifiedData = {
811
+ ...responseState.response.payload.vp_token && {
812
+ authorization_response: {
813
+ vp_token: typeof responseState.response.payload.vp_token === "string" ? JSON.parse(responseState.response.payload.vp_token) : responseState.response.payload.vp_token
814
+ }
815
+ },
816
+ ...claims.length > 0 && {
817
+ credential_claims: claims
818
+ }
819
+ };
803
820
  }
804
821
  return responseState;
805
822
  }
806
- presentationOrClaimsFrom = /* @__PURE__ */ __name((presentationDecoded) => CredentialMapper2.isSdJwtDecodedCredential(presentationDecoded) ? presentationDecoded.decodedPayload : CredentialMapper2.toUniformPresentation(presentationDecoded), "presentationOrClaimsFrom");
823
+ presentationOrClaimsFrom = /* @__PURE__ */ __name((presentationDecoded) => {
824
+ return CredentialMapper2.isSdJwtDecodedCredential(presentationDecoded) ? presentationDecoded.decodedPayload : CredentialMapper2.toUniformPresentation(presentationDecoded);
825
+ }, "presentationOrClaimsFrom");
807
826
  async siopUpdateRequestState(args, context) {
808
827
  if (args.state !== "authorization_request_created") {
809
828
  throw Error(`Only 'authorization_request_created' status is supported for this method at this point`);
810
829
  }
811
830
  return await this.getRPInstance({
831
+ createWhenNotPresent: false,
812
832
  queryId: args.queryId
813
833
  }, context).then((rp) => rp.get(context).then(async (rp2) => {
814
834
  await rp2.signalAuthRequestRetrieved({
@@ -820,6 +840,7 @@ var SIOPv2RP = class _SIOPv2RP {
820
840
  }
821
841
  async siopDeleteState(args, context) {
822
842
  return await this.getRPInstance({
843
+ createWhenNotPresent: false,
823
844
  queryId: args.queryId
824
845
  }, context).then((rp) => rp.get(context).then((rp2) => rp2.sessionManager.deleteStateForCorrelationId(args.correlationId))).then(() => true);
825
846
  }
@@ -829,12 +850,13 @@ var SIOPv2RP = class _SIOPv2RP {
829
850
  }
830
851
  const authResponse = typeof args.authorizationResponse === "string" ? decodeUriAsJson(args.authorizationResponse) : args.authorizationResponse;
831
852
  return await this.getRPInstance({
853
+ createWhenNotPresent: false,
832
854
  queryId: args.queryId
833
855
  }, context).then((rp) => rp.get(context).then((rp2) => rp2.verifyAuthorizationResponse(authResponse, {
834
856
  correlationId: args.correlationId,
835
- ...args.dcqlQuery ? {
857
+ ...args.dcqlQuery && {
836
858
  dcqlQuery: args.dcqlQuery
837
- } : {},
859
+ },
838
860
  audience: args.audience
839
861
  })));
840
862
  }
@@ -873,9 +895,37 @@ var SIOPv2RP = class _SIOPv2RP {
873
895
  }
874
896
  return void 0;
875
897
  }
876
- async getRPInstance({ queryId, responseRedirectURI }, context) {
877
- const instanceId = queryId ?? _SIOPv2RP._DEFAULT_OPTS_KEY;
878
- if (!this.instances.has(instanceId)) {
898
+ async getRPInstance({ createWhenNotPresent, queryId, responseRedirectURI }, context) {
899
+ let rpInstanceId = _SIOPv2RP._DEFAULT_OPTS_KEY;
900
+ let rpInstance;
901
+ if (queryId) {
902
+ if (this.instances.has(queryId)) {
903
+ rpInstanceId = queryId;
904
+ rpInstance = this.instances.get(rpInstanceId);
905
+ } else if (isValidUUID(queryId)) {
906
+ try {
907
+ const pd = await context.agent.pdmGetDefinition({
908
+ itemId: queryId
909
+ });
910
+ if (this.instances.has(pd.queryId)) {
911
+ rpInstanceId = pd.queryId;
912
+ rpInstance = this.instances.get(rpInstanceId);
913
+ }
914
+ } catch (ignore) {
915
+ }
916
+ }
917
+ if (createWhenNotPresent) {
918
+ rpInstanceId = queryId;
919
+ } else {
920
+ rpInstance = this.instances.get(rpInstanceId);
921
+ }
922
+ } else {
923
+ rpInstance = this.instances.get(rpInstanceId);
924
+ }
925
+ if (!rpInstance) {
926
+ if (!createWhenNotPresent) {
927
+ return Promise.reject(`No RP instance found for key ${rpInstanceId}`);
928
+ }
879
929
  const instanceOpts = this.getInstanceOpts(queryId);
880
930
  const rpOpts = await this.getRPOptions(context, {
881
931
  queryId,
@@ -897,12 +947,12 @@ var SIOPv2RP = class _SIOPv2RP {
897
947
  resolverResolution: true
898
948
  });
899
949
  }
900
- this.instances.set(instanceId, new RPInstance({
950
+ rpInstance = new RPInstance({
901
951
  rpOpts,
902
952
  pexOpts: instanceOpts
903
- }));
953
+ });
954
+ this.instances.set(rpInstanceId, rpInstance);
904
955
  }
905
- const rpInstance = this.instances.get(instanceId);
906
956
  if (responseRedirectURI) {
907
957
  rpInstance.rpOptions.responseRedirectUri = responseRedirectURI;
908
958
  }
@@ -944,37 +994,28 @@ var SIOPv2RP = class _SIOPv2RP {
944
994
  }
945
995
  return options;
946
996
  }
947
- getInstanceOpts(definitionId) {
997
+ getInstanceOpts(queryId) {
948
998
  if (!this.opts.instanceOpts) return void 0;
949
- const instanceOpt = definitionId ? this.opts.instanceOpts.find((i) => i.queryId === definitionId) : void 0;
950
- return instanceOpt ?? this.getDefaultOptions(definitionId);
999
+ const instanceOpt = queryId ? this.opts.instanceOpts.find((i) => i.queryId === queryId) : void 0;
1000
+ return instanceOpt ?? this.getDefaultOptions(queryId);
951
1001
  }
952
- getDefaultOptions(definitionId) {
1002
+ getDefaultOptions(queryId) {
953
1003
  if (!this.opts.instanceOpts) return void 0;
954
1004
  const defaultOptions = this.opts.instanceOpts.find((i) => i.queryId === "default");
955
1005
  if (defaultOptions) {
956
1006
  const clonedOptions = {
957
1007
  ...defaultOptions
958
1008
  };
959
- if (definitionId !== void 0) {
960
- clonedOptions.queryId = definitionId;
1009
+ if (queryId !== void 0) {
1010
+ clonedOptions.queryId = queryId;
961
1011
  }
962
1012
  return clonedOptions;
963
1013
  }
964
1014
  return void 0;
965
1015
  }
966
1016
  };
967
-
968
- // src/types/ISIOPv2RP.ts
969
- var VerifiedDataMode = /* @__PURE__ */ (function(VerifiedDataMode2) {
970
- VerifiedDataMode2["NONE"] = "none";
971
- VerifiedDataMode2["VERIFIED_PRESENTATION"] = "vp";
972
- VerifiedDataMode2["CREDENTIAL_SUBJECT_FLATTENED"] = "cs-flat";
973
- return VerifiedDataMode2;
974
- })({});
975
1017
  export {
976
1018
  SIOPv2RP,
977
- VerifiedDataMode,
978
1019
  plugin_schema_default as schema
979
1020
  };
980
1021
  //# sourceMappingURL=index.js.map