@sphereon/ssi-sdk.presentation-exchange 0.33.1-next.3 → 0.33.1-next.68

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
@@ -1,27 +1,599 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
4
+ var __commonJS = (cb, mod) => function __require() {
5
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
6
+ };
7
+
8
+ // plugin.schema.json
9
+ var require_plugin_schema = __commonJS({
10
+ "plugin.schema.json"(exports, module) {
11
+ module.exports = {
12
+ IDidAuthSiopOpAuthenticator: {
13
+ components: {
14
+ schemas: {
15
+ IGetSiopSessionArgs: {
16
+ type: "object",
17
+ properties: {
18
+ sessionId: {
19
+ type: "string"
20
+ },
21
+ additionalProperties: false
22
+ },
23
+ required: ["sessionId"],
24
+ description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.getSessionForSiop } "
25
+ },
26
+ IRegisterSiopSessionArgs: {
27
+ type: "object",
28
+ properties: {
29
+ identifier: {
30
+ type: "object",
31
+ properties: {
32
+ did: {
33
+ type: "string"
34
+ },
35
+ alias: {
36
+ type: "string"
37
+ },
38
+ provider: {
39
+ type: "string"
40
+ },
41
+ controllerKeyId: {
42
+ type: "string"
43
+ },
44
+ keys: {
45
+ type: "array",
46
+ items: {
47
+ type: "object",
48
+ properties: {
49
+ additionalProperties: true
50
+ }
51
+ }
52
+ },
53
+ services: {
54
+ type: "array",
55
+ items: {
56
+ type: "object",
57
+ properties: {
58
+ additionalProperties: true
59
+ }
60
+ }
61
+ }
62
+ },
63
+ additionalProperties: false,
64
+ required: ["did", "provider", "keys", "services"]
65
+ },
66
+ sessionId: {
67
+ type: "string"
68
+ },
69
+ expiresIn: {
70
+ type: "number"
71
+ },
72
+ additionalProperties: false
73
+ },
74
+ required: ["identifier"],
75
+ description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.registerSessionForSiop } "
76
+ },
77
+ IRemoveSiopSessionArgs: {
78
+ type: "object",
79
+ properties: {
80
+ sessionId: {
81
+ type: "string"
82
+ },
83
+ additionalProperties: false
84
+ },
85
+ required: ["sessionId"],
86
+ description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.removeSessionForSiop } "
87
+ },
88
+ IAuthenticateWithSiopArgs: {
89
+ type: "object",
90
+ properties: {
91
+ sessionId: {
92
+ type: "string"
93
+ },
94
+ stateId: {
95
+ type: "string"
96
+ },
97
+ redirectUrl: {
98
+ type: "string"
99
+ },
100
+ additionalProperties: false
101
+ },
102
+ required: ["sessionId", "stateId", "redirectUrl"],
103
+ description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.authenticateWithSiop } "
104
+ },
105
+ IResponse: {
106
+ type: "object",
107
+ properties: {
108
+ status: {
109
+ type: "number"
110
+ },
111
+ additionalProperties: true
112
+ },
113
+ required: ["status"],
114
+ description: "Result of {@link DidAuthSiopOpAuthenticator.authenticateWithSiop & DidAuthSiopOpAuthenticator.sendSiopAuthenticationResponse } "
115
+ },
116
+ IGetSiopAuthenticationRequestFromRpArgs: {
117
+ type: "object",
118
+ properties: {
119
+ sessionId: {
120
+ type: "string"
121
+ },
122
+ stateId: {
123
+ type: "string"
124
+ },
125
+ redirectUrl: {
126
+ type: "string"
127
+ },
128
+ additionalProperties: false
129
+ },
130
+ required: ["sessionId", "stateId", "redirectUrl"],
131
+ description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.getSiopAuthenticationRequestFromRP } "
132
+ },
133
+ ParsedAuthenticationRequestURI: {
134
+ type: "object",
135
+ properties: {
136
+ jwt: {
137
+ type: "string"
138
+ },
139
+ requestPayload: {
140
+ type: "object",
141
+ properties: {
142
+ additionalProperties: true
143
+ }
144
+ },
145
+ registration: {
146
+ type: "object",
147
+ properties: {
148
+ additionalProperties: true
149
+ }
150
+ },
151
+ additionalProperties: false
152
+ },
153
+ required: ["jwt", "requestPayload", "registration"],
154
+ description: "Result of {@link DidAuthSiopOpAuthenticator.getSiopAuthenticationRequestFromRP } "
155
+ },
156
+ IGetSiopAuthenticationRequestDetailsArgs: {
157
+ type: "object",
158
+ properties: {
159
+ sessionId: {
160
+ type: "string"
161
+ },
162
+ verifiedAuthenticationRequest: {
163
+ type: "object",
164
+ properties: {
165
+ additionalProperties: true
166
+ }
167
+ },
168
+ credentialFilter: {
169
+ type: "object",
170
+ properties: {
171
+ additionalProperties: true
172
+ }
173
+ },
174
+ additionalProperties: false
175
+ },
176
+ required: ["sessionId", "verifiedAuthenticationRequest"],
177
+ description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.getSiopAuthenticationRequestDetails } "
178
+ },
179
+ IAuthRequestDetails: {
180
+ type: "object",
181
+ properties: {
182
+ id: {
183
+ type: "string"
184
+ },
185
+ alsoKnownAs: {
186
+ type: "array",
187
+ items: {
188
+ type: "string"
189
+ }
190
+ },
191
+ vpResponseOpts: {
192
+ type: "object",
193
+ properties: {
194
+ additionalProperties: true
195
+ }
196
+ },
197
+ additionalProperties: false
198
+ },
199
+ required: ["id", "vpResponseOpts"],
200
+ description: "Result of {@link DidAuthSiopOpAuthenticator.getSiopAuthenticationRequestDetails } "
201
+ },
202
+ IVerifySiopAuthenticationRequestUriArgs: {
203
+ type: "object",
204
+ properties: {
205
+ sessionId: {
206
+ type: "string"
207
+ },
208
+ ParsedAuthenticationRequestURI: {
209
+ type: "object",
210
+ properties: {
211
+ additionalProperties: true
212
+ }
213
+ },
214
+ additionalProperties: false
215
+ },
216
+ required: ["sessionId", "ParsedAuthenticationRequestURI"],
217
+ description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.verifySiopAuthenticationRequestURI } "
218
+ },
219
+ VerifiedAuthorizationRequest: {
220
+ type: "object",
221
+ properties: {
222
+ payload: {
223
+ type: "object",
224
+ properties: {
225
+ additionalProperties: true
226
+ }
227
+ },
228
+ presentationDefinitions: {
229
+ type: "object",
230
+ properties: {
231
+ additionalProperties: true
232
+ }
233
+ },
234
+ verifyOpts: {
235
+ type: "object",
236
+ properties: {
237
+ additionalProperties: true
238
+ }
239
+ },
240
+ additionalProperties: false
241
+ },
242
+ required: ["payload", "verifyOpts"],
243
+ description: "Result of {@link DidAuthSiopOpAuthenticator.verifySiopAuthenticationRequestURI } "
244
+ },
245
+ ISendSiopAuthenticationResponseArgs: {
246
+ type: "object",
247
+ properties: {
248
+ sessionId: {
249
+ type: "string"
250
+ },
251
+ verifiedAuthenticationRequest: {
252
+ type: "object",
253
+ properties: {
254
+ additionalProperties: true
255
+ }
256
+ },
257
+ verifiablePresentationResponse: {
258
+ type: "object",
259
+ properties: {
260
+ additionalProperties: true
261
+ }
262
+ },
263
+ additionalProperties: false
264
+ },
265
+ required: ["sessionId", "verifiedAuthenticationRequest"],
266
+ description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.sendSiopAuthenticationResponse } "
267
+ }
268
+ },
269
+ methods: {
270
+ getSessionForSiop: {
271
+ description: "Get SIOP session",
272
+ arguments: {
273
+ $ref: "#/components/schemas/IGetSiopSessionArgs"
274
+ },
275
+ returnType: "object"
276
+ },
277
+ registerSessionForSiop: {
278
+ description: "Register SIOP session",
279
+ arguments: {
280
+ $ref: "#/components/schemas/IRegisterSiopSessionArgs"
281
+ },
282
+ returnType: "object"
283
+ },
284
+ removeSessionForSiop: {
285
+ description: "Remove SIOP session",
286
+ arguments: {
287
+ $ref: "#/components/schemas/IRemoveSiopSessionArgs"
288
+ },
289
+ returnType: "boolean"
290
+ },
291
+ authenticateWithSiop: {
292
+ description: "Authenticate using DID Auth SIOP",
293
+ arguments: {
294
+ $ref: "#/components/schemas/IAuthenticateWithSiopArgs"
295
+ },
296
+ returnType: {
297
+ $ref: "#/components/schemas/Response"
298
+ }
299
+ },
300
+ getSiopAuthenticationRequestFromRP: {
301
+ description: "Get authentication request from RP",
302
+ arguments: {
303
+ $ref: "#/components/schemas/IGetSiopAuthenticationRequestFromRpArgs"
304
+ },
305
+ returnType: {
306
+ $ref: "#/components/schemas/ParsedAuthenticationRequestURI"
307
+ }
308
+ },
309
+ getSiopAuthenticationRequestDetails: {
310
+ description: "Get authentication request details",
311
+ arguments: {
312
+ $ref: "#/components/schemas/IGetSiopAuthenticationRequestDetailsArgs"
313
+ },
314
+ returnType: {
315
+ $ref: "#/components/schemas/IAuthRequestDetails"
316
+ }
317
+ },
318
+ verifySiopAuthenticationRequestURI: {
319
+ description: "Verify authentication request URI",
320
+ arguments: {
321
+ $ref: "#/components/schemas/IVerifySiopAuthenticationRequestUriArgs"
322
+ },
323
+ returnType: {
324
+ $ref: "#/components/schemas/VerifiedAuthorizationRequest"
325
+ }
326
+ },
327
+ sendSiopAuthenticationResponse: {
328
+ description: "Send authentication response",
329
+ arguments: {
330
+ $ref: "#/components/schemas/ISendSiopAuthenticationResponseArgs"
331
+ },
332
+ returnType: {
333
+ $ref: "#/components/schemas/IRequiredContext"
334
+ }
335
+ }
336
+ }
337
+ }
338
+ }
339
+ };
340
+ }
341
+ });
342
+
343
+ // src/agent/PresentationExchange.ts
344
+ import { PEX } from "@sphereon/pex";
345
+ import { CredentialMapper, JWT_PROOF_TYPE_2020 } from "@sphereon/ssi-types";
346
+ import { toDIDs } from "@sphereon/ssi-sdk-ext.did-utils";
347
+ import { verifiableCredentialForRoleFilter } from "@sphereon/ssi-sdk.credential-store";
348
+ var PresentationExchange = class {
349
+ static {
350
+ __name(this, "PresentationExchange");
351
+ }
352
+ schema = schema.IDidAuthSiopOpAuthenticator;
353
+ pex = new PEX();
354
+ methods = {
355
+ pexValidateDefinition: this.pexValidateDefinition.bind(this),
356
+ pexDefinitionVersion: this.pexDefinitionVersion.bind(this),
357
+ pexDefinitionFilterCredentials: this.pexDefinitionFilterCredentials.bind(this),
358
+ pexDefinitionFilterCredentialsPerInputDescriptor: this.pexDefinitionFilterCredentialsPerInputDescriptor.bind(this)
359
+ };
360
+ constructor(opts) {
361
+ }
362
+ async pexValidateDefinition(args) {
363
+ const { definition } = args;
364
+ const invalids = [];
365
+ try {
366
+ const result = PEX.validateDefinition(definition);
367
+ const validations = Array.isArray(result) ? result : [
368
+ result
369
+ ];
370
+ invalids.push(...validations.filter((v) => v.status === "error"));
371
+ } catch (error) {
372
+ invalids.push({
373
+ status: "error",
374
+ message: typeof error === "string" ? error : typeof error === "object" && "message" in error ? error.message : "unknown error",
375
+ tag: "validation"
376
+ });
377
+ }
378
+ if (invalids.length > 0) {
379
+ throw Error(`Invalid definition. ${invalids.map((v) => v.message).toString()}`);
380
+ }
381
+ return true;
382
+ }
383
+ async pexDefinitionVersion(presentationDefinition) {
384
+ return PEX.definitionVersionDiscovery(presentationDefinition);
385
+ }
386
+ async pexDefinitionFilterCredentials(args, context) {
387
+ const credentials = await this.pexFilterCredentials(args.credentialFilterOpts, context);
388
+ const holderDIDs = args.holderDIDs ? toDIDs(args.holderDIDs) : toDIDs(await context.agent.dataStoreORMGetIdentifiers());
389
+ const selectResults = this.pex.selectFrom(args.presentationDefinition, credentials ?? [], {
390
+ ...args,
391
+ holderDIDs,
392
+ limitDisclosureSignatureSuites: args.limitDisclosureSignatureSuites ?? [
393
+ "BbsBlsSignature2020"
394
+ ]
395
+ });
396
+ return {
397
+ id: args.presentationDefinition.id,
398
+ selectResults,
399
+ filteredCredentials: selectResults.verifiableCredential?.map((vc) => CredentialMapper.storedCredentialToOriginalFormat(vc)) ?? []
400
+ };
401
+ }
402
+ async pexDefinitionFilterCredentialsPerInputDescriptor(args, context) {
403
+ const origDefinition = args.presentationDefinition;
404
+ const credentials = await this.pexFilterCredentials(args.credentialFilterOpts ?? {}, context);
405
+ const holderDIDs = args.holderDIDs ? toDIDs(args.holderDIDs) : toDIDs(await context.agent.dataStoreORMGetIdentifiers());
406
+ const limitDisclosureSignatureSuites = args.limitDisclosureSignatureSuites;
407
+ const promises = /* @__PURE__ */ new Map();
408
+ origDefinition.input_descriptors.forEach((inputDescriptor) => {
409
+ const presentationDefinition = {
410
+ id: inputDescriptor.id,
411
+ input_descriptors: [
412
+ inputDescriptor
413
+ ]
414
+ };
415
+ const credentialRole = args.credentialFilterOpts.credentialRole;
416
+ promises.set(inputDescriptor, this.pexDefinitionFilterCredentials({
417
+ credentialFilterOpts: {
418
+ credentialRole,
419
+ verifiableCredentials: credentials
420
+ },
421
+ // @ts-ignore
422
+ presentationDefinition,
423
+ holderDIDs,
424
+ limitDisclosureSignatureSuites
425
+ }, context));
426
+ });
427
+ await Promise.all(promises.values());
428
+ const result = [];
429
+ for (const entry of promises.entries()) {
430
+ result.push({
431
+ ...await entry[1],
432
+ inputDescriptor: entry[0]
433
+ });
434
+ }
435
+ return result;
436
+ }
437
+ async pexFilterCredentials(filterOpts, context) {
438
+ if (filterOpts.verifiableCredentials && filterOpts.verifiableCredentials.length > 0) {
439
+ return filterOpts.verifiableCredentials;
440
+ }
441
+ const filter = verifiableCredentialForRoleFilter(filterOpts.credentialRole, filterOpts.filter);
442
+ const uniqueCredentials = await context.agent.crsGetUniqueCredentials({
443
+ filter
444
+ });
445
+ return uniqueCredentials.map((uniqueVC) => {
446
+ const vc = uniqueVC.uniformVerifiableCredential;
447
+ const proof = Array.isArray(vc.proof) ? vc.proof : [
448
+ vc.proof
449
+ ];
450
+ const jwtProof = proof.find((p) => p?.type === JWT_PROOF_TYPE_2020);
451
+ return jwtProof ? jwtProof.jwt : vc;
452
+ });
453
+ }
454
+ };
455
+
456
+ // src/functions.ts
457
+ import { isManagedIdentifierDidOpts, isManagedIdentifierDidResult, isManagedIdentifierX5cResult } from "@sphereon/ssi-sdk-ext.identifier-resolution";
458
+ import { CredentialMapper as CredentialMapper2 } from "@sphereon/ssi-types";
459
+ async function createPEXPresentationSignCallback(args, context) {
460
+ function determineProofFormat(determineArgs) {
461
+ const { format, presentationDefinition, presentation } = determineArgs;
462
+ var formatOptions = format ?? presentationDefinition.format ?? args.format;
463
+ if (!formatOptions && presentationDefinition.input_descriptors.length == 1 && "format" in presentationDefinition.input_descriptors[0]) {
464
+ formatOptions = presentationDefinition.input_descriptors[0].format;
465
+ }
466
+ if (!formatOptions) {
467
+ if (CredentialMapper2.isSdJwtDecodedCredentialPayload(presentation.decodedPayload)) {
468
+ return "vc+sd-jwt";
469
+ } else if (CredentialMapper2.isMsoMdocDecodedPresentation(presentation.decodedPayload)) {
470
+ return "mso_mdoc";
471
+ } else if (CredentialMapper2.isW3cPresentation(presentation.decodedPayload)) {
472
+ if (typeof presentation.signedPayload === "string") {
473
+ return "jwt";
474
+ }
475
+ return "lds";
476
+ }
477
+ return "jwt";
478
+ } else if (typeof formatOptions === "string") {
479
+ return formatOptions;
480
+ }
481
+ const formats = new Set(Object.keys(formatOptions).map((form) => form.includes("ldp") ? "lds" : form.includes("vc+sd-jwt") ? "vc+sd-jwt" : "jwt"));
482
+ if (formats.size === 1) {
483
+ return formats.values().next().value;
484
+ }
485
+ formats.keys().next();
486
+ if (formats.has("vc+sd-jwt")) {
487
+ return "vc+sd-jwt";
488
+ } else if (formats.has("jwt")) {
489
+ return "jwt";
490
+ }
491
+ return "lds";
492
+ }
493
+ __name(determineProofFormat, "determineProofFormat");
494
+ return async ({ presentation, domain, presentationDefinition, format, challenge }) => {
495
+ const proofFormat = determineProofFormat({
496
+ format,
497
+ presentationDefinition,
498
+ presentation
499
+ });
500
+ const { idOpts } = args;
501
+ const CLOCK_SKEW = 120;
502
+ if (args.skipDidResolution && isManagedIdentifierDidOpts(idOpts)) {
503
+ idOpts.offlineWhenNoDIDRegistered = true;
504
+ }
505
+ if ("compactSdJwtVc" in presentation) {
506
+ if (proofFormat !== "vc+sd-jwt") {
507
+ return Promise.reject(Error(`presentation payload does not match proof format ${proofFormat}`));
508
+ }
509
+ const presentationResult = await context.agent.createSdJwtPresentation({
510
+ ...idOpts?.method === "oid4vci-issuer" && {
511
+ holder: idOpts?.issuer
512
+ },
513
+ presentation: presentation.compactSdJwtVc,
514
+ kb: {
515
+ payload: {
516
+ ...presentation.kbJwt?.payload,
517
+ iat: presentation.kbJwt?.payload?.iat ?? Math.floor(Date.now() / 1e3 - CLOCK_SKEW),
518
+ nonce: challenge ?? presentation.kbJwt?.payload?.nonce,
519
+ aud: presentation.kbJwt?.payload?.aud ?? domain ?? args.domain
520
+ }
521
+ }
522
+ });
523
+ return CredentialMapper2.storedPresentationToOriginalFormat(presentationResult.presentation);
524
+ } else {
525
+ const resolution = await context.agent.identifierManagedGet(idOpts);
526
+ if (proofFormat === "vc+sd-jwt") {
527
+ return Promise.reject(Error(`presentation payload does not match proof format ${proofFormat}`));
528
+ }
529
+ let header;
530
+ if (!presentation.holder) {
531
+ presentation.holder = resolution.issuer;
532
+ }
533
+ if (proofFormat === "jwt") {
534
+ header = {
535
+ ...(isManagedIdentifierDidResult(resolution) || isManagedIdentifierX5cResult(resolution)) && resolution.kid && {
536
+ kid: resolution.kid
537
+ },
538
+ ...isManagedIdentifierX5cResult(resolution) && {
539
+ jwk: resolution.jwk
540
+ }
541
+ };
542
+ if (presentation.verifier || !presentation.aud) {
543
+ presentation.aud = Array.isArray(presentation.verifier) ? presentation.verifier : presentation.verifier ?? domain ?? args.domain;
544
+ delete presentation.verifier;
545
+ }
546
+ if (!presentation.nbf) {
547
+ if (presentation.issuanceDate) {
548
+ const converted = Date.parse(presentation.issuanceDate);
549
+ if (!isNaN(converted)) {
550
+ presentation.nbf = Math.floor(converted / 1e3);
551
+ }
552
+ } else {
553
+ presentation.nbf = Math.floor(Date.now() / 1e3 - CLOCK_SKEW);
554
+ }
555
+ }
556
+ if (!presentation.iat) {
557
+ presentation.iat = presentation.nbf;
558
+ }
559
+ if (!presentation.exp) {
560
+ if (presentation.expirationDate) {
561
+ const converted = Date.parse(presentation.expirationDate);
562
+ if (!isNaN(converted)) {
563
+ presentation.exp = Math.floor(converted / 1e3);
564
+ }
565
+ } else {
566
+ presentation.exp = presentation.nbf + 600 + CLOCK_SKEW;
567
+ }
568
+ }
569
+ if (!presentation.vp) {
570
+ presentation.vp = {};
571
+ }
572
+ if (!presentation.vp.holder) {
573
+ presentation.vp.holder = presentation.holder;
574
+ }
575
+ }
576
+ const vp = await context.agent.createVerifiablePresentation({
577
+ presentation,
578
+ removeOriginalFields: false,
579
+ keyRef: resolution.kmsKeyRef,
580
+ // domain: domain ?? args.domain, // handled above, and did-jwt-vc creates an array even for 1 entry
581
+ challenge: challenge ?? args.challenge,
582
+ fetchRemoteContexts: args.fetchRemoteContexts !== false,
583
+ proofFormat,
584
+ header
585
+ });
586
+ return CredentialMapper2.storedPresentationToOriginalFormat(vp);
587
+ }
588
+ };
589
+ }
590
+ __name(createPEXPresentationSignCallback, "createPEXPresentationSignCallback");
591
+
592
+ // src/index.ts
593
+ var schema = require_plugin_schema();
594
+ export {
595
+ PresentationExchange,
596
+ createPEXPresentationSignCallback,
597
+ schema
15
598
  };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.PresentationExchange = exports.schema = void 0;
18
- /**
19
- * @public
20
- */
21
- const schema = require('../plugin.schema.json');
22
- exports.schema = schema;
23
- var PresentationExchange_1 = require("./agent/PresentationExchange");
24
- Object.defineProperty(exports, "PresentationExchange", { enumerable: true, get: function () { return PresentationExchange_1.PresentationExchange; } });
25
- __exportStar(require("./types/IPresentationExchange"), exports);
26
- __exportStar(require("./functions"), exports);
27
599
  //# sourceMappingURL=index.js.map