@sphereon/ssi-sdk.siopv2-oid4vp-rp-auth 0.33.1-next.3 → 0.33.1-next.73

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,26 +1,990 @@
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/SIOPv2RP.ts
344
+ import { AuthorizationResponseStateStatus, decodeUriAsJson } from "@sphereon/did-auth-siop";
345
+ import { getAgentResolver as getAgentResolver2 } from "@sphereon/ssi-sdk-ext.did-utils";
346
+ import { CredentialMapper as CredentialMapper2 } from "@sphereon/ssi-types";
347
+
348
+ // src/functions.ts
349
+ import { InMemoryRPSessionManager, PassBy, PropertyTarget, ResponseMode, ResponseType, RevocationVerification, RP, Scope, SubjectType, SupportedVersion } from "@sphereon/did-auth-siop";
350
+ import { SigningAlgo } from "@sphereon/oid4vc-common";
351
+ import { getAgentDIDMethods, getAgentResolver } from "@sphereon/ssi-sdk-ext.did-utils";
352
+ import { isExternalIdentifierOIDFEntityIdOpts, isManagedIdentifierDidOpts, isManagedIdentifierDidResult, isManagedIdentifierX5cOpts } from "@sphereon/ssi-sdk-ext.identifier-resolution";
353
+ import { CredentialMapper } from "@sphereon/ssi-types";
354
+ import { EventEmitter } from "events";
355
+ import { defaultHasher } from "@sphereon/ssi-sdk.core";
356
+ function getRequestVersion(rpOptions) {
357
+ if (Array.isArray(rpOptions.supportedVersions) && rpOptions.supportedVersions.length > 0) {
358
+ return rpOptions.supportedVersions[0];
359
+ }
360
+ return SupportedVersion.JWT_VC_PRESENTATION_PROFILE_v1;
361
+ }
362
+ __name(getRequestVersion, "getRequestVersion");
363
+ function getWellKnownDIDVerifyCallback(siopIdentifierOpts, context) {
364
+ return siopIdentifierOpts.wellknownDIDVerifyCallback ? siopIdentifierOpts.wellknownDIDVerifyCallback : async (args) => {
365
+ const result = await context.agent.cvVerifyCredential({
366
+ credential: args.credential,
367
+ fetchRemoteContexts: true
368
+ });
369
+ return {
370
+ verified: result.result
371
+ };
372
+ };
373
+ }
374
+ __name(getWellKnownDIDVerifyCallback, "getWellKnownDIDVerifyCallback");
375
+ function getPresentationVerificationCallback(idOpts, context) {
376
+ async function presentationVerificationCallback(args, presentationSubmission) {
377
+ if (CredentialMapper.isSdJwtEncoded(args)) {
378
+ const result2 = await context.agent.verifySdJwtPresentation({
379
+ presentation: args,
380
+ kb: true
381
+ });
382
+ return {
383
+ verified: !!result2.payload
384
+ };
385
+ }
386
+ if (CredentialMapper.isMsoMdocOid4VPEncoded(args)) {
387
+ if (context.agent.mdocOid4vpRPVerify === void 0) {
388
+ return Promise.reject("ImDLMdoc agent plugin must be enabled to support MsoMdoc types");
389
+ }
390
+ if (presentationSubmission !== void 0 && presentationSubmission !== null) {
391
+ const verifyResult = await context.agent.mdocOid4vpRPVerify({
392
+ vp_token: args,
393
+ presentation_submission: presentationSubmission
394
+ });
395
+ return {
396
+ verified: !verifyResult.error
397
+ };
398
+ }
399
+ throw Error(`mdocOid4vpRPVerify(...) method requires a presentation submission`);
400
+ }
401
+ const result = await context.agent.verifyPresentation({
402
+ presentation: args,
403
+ fetchRemoteContexts: true,
404
+ domain: (await context.agent.identifierManagedGet(idOpts)).kid?.split("#")[0]
405
+ });
406
+ return {
407
+ verified: result.verified
408
+ };
409
+ }
410
+ __name(presentationVerificationCallback, "presentationVerificationCallback");
411
+ return presentationVerificationCallback;
412
+ }
413
+ __name(getPresentationVerificationCallback, "getPresentationVerificationCallback");
414
+ async function createRPBuilder(args) {
415
+ const { rpOpts, pexOpts, context } = args;
416
+ const { identifierOpts } = rpOpts;
417
+ let definition = args.definition;
418
+ let dcqlQuery = args.dcql;
419
+ if (!definition && pexOpts && pexOpts.definitionId) {
420
+ const presentationDefinitionItems = await context.agent.pdmGetDefinitions({
421
+ filter: [
422
+ {
423
+ definitionId: pexOpts.definitionId,
424
+ version: pexOpts.version,
425
+ tenantId: pexOpts.tenantId
426
+ }
427
+ ]
428
+ });
429
+ if (presentationDefinitionItems.length > 0) {
430
+ const presentationDefinitionItem = presentationDefinitionItems[0];
431
+ definition = presentationDefinitionItem.definitionPayload;
432
+ if (!dcqlQuery && presentationDefinitionItem.dcqlPayload) {
433
+ dcqlQuery = presentationDefinitionItem.dcqlPayload;
434
+ }
435
+ }
436
+ }
437
+ const didMethods = identifierOpts.supportedDIDMethods ?? await getAgentDIDMethods(context);
438
+ const eventEmitter = rpOpts.eventEmitter ?? new EventEmitter();
439
+ const defaultClientMetadata = {
440
+ // FIXME: All of the below should be configurable. Some should come from builder, some should be determined by the agent.
441
+ // For now it is either preconfigured or everything passed in as a single object
442
+ idTokenSigningAlgValuesSupported: [
443
+ SigningAlgo.EDDSA,
444
+ SigningAlgo.ES256,
445
+ SigningAlgo.ES256K
446
+ ],
447
+ requestObjectSigningAlgValuesSupported: [
448
+ SigningAlgo.EDDSA,
449
+ SigningAlgo.ES256,
450
+ SigningAlgo.ES256K
451
+ ],
452
+ responseTypesSupported: [
453
+ ResponseType.ID_TOKEN
454
+ ],
455
+ client_name: "Sphereon",
456
+ vpFormatsSupported: {
457
+ jwt_vc: {
458
+ alg: [
459
+ "EdDSA",
460
+ "ES256K"
461
+ ]
462
+ },
463
+ jwt_vp: {
464
+ alg: [
465
+ "ES256K",
466
+ "EdDSA"
467
+ ]
468
+ }
469
+ },
470
+ scopesSupported: [
471
+ Scope.OPENID_DIDAUTHN
472
+ ],
473
+ subjectTypesSupported: [
474
+ SubjectType.PAIRWISE
475
+ ],
476
+ subject_syntax_types_supported: didMethods.map((method) => `did:${method}`),
477
+ passBy: PassBy.VALUE
478
+ };
479
+ const resolver = rpOpts.identifierOpts.resolveOpts?.resolver ?? getAgentResolver(context, {
480
+ resolverResolution: true,
481
+ localResolution: true,
482
+ uniresolverResolution: rpOpts.identifierOpts.resolveOpts?.noUniversalResolverFallback !== true
483
+ });
484
+ let hasher = rpOpts.credentialOpts?.hasher;
485
+ if (!rpOpts.credentialOpts?.hasher || typeof rpOpts.credentialOpts?.hasher !== "function") {
486
+ hasher = defaultHasher;
487
+ }
488
+ const builder = RP.builder({
489
+ requestVersion: getRequestVersion(rpOpts)
490
+ }).withScope("openid", PropertyTarget.REQUEST_OBJECT).withResponseMode(rpOpts.responseMode ?? ResponseMode.POST).withResponseType(ResponseType.VP_TOKEN, PropertyTarget.REQUEST_OBJECT).withSupportedVersions(rpOpts.supportedVersions ?? [
491
+ SupportedVersion.JWT_VC_PRESENTATION_PROFILE_v1,
492
+ SupportedVersion.SIOPv2_ID1,
493
+ SupportedVersion.SIOPv2_D11
494
+ ]).withEventEmitter(eventEmitter).withSessionManager(rpOpts.sessionManager ?? new InMemoryRPSessionManager(eventEmitter)).withClientMetadata(rpOpts.clientMetadataOpts ?? defaultClientMetadata, PropertyTarget.REQUEST_OBJECT).withVerifyJwtCallback(rpOpts.verifyJwtCallback ? rpOpts.verifyJwtCallback : getVerifyJwtCallback({
495
+ resolver,
496
+ verifyOpts: {
497
+ wellknownDIDVerifyCallback: getWellKnownDIDVerifyCallback(rpOpts.identifierOpts, context),
498
+ checkLinkedDomain: "if_present"
499
+ }
500
+ }, context)).withRevocationVerification(RevocationVerification.NEVER).withPresentationVerification(getPresentationVerificationCallback(identifierOpts.idOpts, context));
501
+ const oidfOpts = identifierOpts.oidfOpts;
502
+ if (oidfOpts && isExternalIdentifierOIDFEntityIdOpts(oidfOpts)) {
503
+ builder.withEntityId(oidfOpts.identifier, PropertyTarget.REQUEST_OBJECT).withClientIdScheme("entity_id", PropertyTarget.REQUEST_OBJECT);
504
+ } else {
505
+ const resolution = await context.agent.identifierManagedGet(identifierOpts.idOpts);
506
+ builder.withClientId(resolution.issuer ?? (isManagedIdentifierDidResult(resolution) ? resolution.did : resolution.jwkThumbprint), PropertyTarget.REQUEST_OBJECT).withClientIdScheme(resolution.clientIdScheme ?? identifierOpts.idOpts.clientIdScheme, PropertyTarget.REQUEST_OBJECT);
507
+ }
508
+ if (hasher) {
509
+ builder.withHasher(hasher);
510
+ }
511
+ if (definition) {
512
+ builder.withPresentationDefinition({
513
+ definition
514
+ }, PropertyTarget.REQUEST_OBJECT);
515
+ }
516
+ if (dcqlQuery) {
517
+ builder.withDcqlQuery(dcqlQuery);
518
+ }
519
+ if (rpOpts.responseRedirectUri) {
520
+ builder.withResponseRedirectUri(rpOpts.responseRedirectUri);
521
+ }
522
+ const createJwtCallback = signCallback(rpOpts.identifierOpts.idOpts, context);
523
+ builder.withCreateJwtCallback(createJwtCallback);
524
+ return builder;
525
+ }
526
+ __name(createRPBuilder, "createRPBuilder");
527
+ function signCallback(idOpts, context) {
528
+ return async (jwtIssuer, jwt, kid) => {
529
+ if (!(isManagedIdentifierDidOpts(idOpts) || isManagedIdentifierX5cOpts(idOpts))) {
530
+ return Promise.reject(Error(`JWT issuer method ${jwtIssuer.method} not yet supported`));
531
+ }
532
+ const result = await context.agent.jwtCreateJwsCompactSignature({
533
+ // FIXME fix cose-key inference
534
+ // @ts-ignore
535
+ issuer: {
536
+ identifier: idOpts.identifier,
537
+ kmsKeyRef: idOpts.kmsKeyRef,
538
+ noIdentifierInHeader: false
539
+ },
540
+ // FIXME fix JWK key_ops
541
+ // @ts-ignore
542
+ protectedHeader: jwt.header,
543
+ payload: jwt.payload
544
+ });
545
+ return result.jwt;
546
+ };
547
+ }
548
+ __name(signCallback, "signCallback");
549
+ function getVerifyJwtCallback(_opts, context) {
550
+ return async (_jwtVerifier, jwt) => {
551
+ const result = await context.agent.jwtVerifyJwsSignature({
552
+ jws: jwt.raw
553
+ });
554
+ console.log(result.message);
555
+ return !result.error;
556
+ };
557
+ }
558
+ __name(getVerifyJwtCallback, "getVerifyJwtCallback");
559
+ function getSigningAlgo(type) {
560
+ switch (type) {
561
+ case "Ed25519":
562
+ return SigningAlgo.EDDSA;
563
+ case "Secp256k1":
564
+ return SigningAlgo.ES256K;
565
+ case "Secp256r1":
566
+ return SigningAlgo.ES256;
567
+ // @ts-ignore
568
+ case "RSA":
569
+ return SigningAlgo.RS256;
570
+ default:
571
+ throw Error("Key type not yet supported");
572
+ }
573
+ }
574
+ __name(getSigningAlgo, "getSigningAlgo");
575
+
576
+ // src/RPInstance.ts
577
+ import { v4 as uuidv4 } from "uuid";
578
+ import { ensureManagedIdentifierResult, isManagedIdentifierDidResult as isManagedIdentifierDidResult2, isManagedIdentifierX5cResult } from "@sphereon/ssi-sdk-ext.identifier-resolution";
579
+ var RPInstance = class {
580
+ static {
581
+ __name(this, "RPInstance");
582
+ }
583
+ _rp;
584
+ _pexOptions;
585
+ _rpOptions;
586
+ constructor({ rpOpts, pexOpts }) {
587
+ this._rpOptions = rpOpts;
588
+ this._pexOptions = pexOpts;
589
+ }
590
+ async get(context) {
591
+ if (!this._rp) {
592
+ const builder = await createRPBuilder({
593
+ rpOpts: this._rpOptions,
594
+ pexOpts: this._pexOptions,
595
+ context
596
+ });
597
+ this._rp = builder.build();
598
+ }
599
+ return this._rp;
600
+ }
601
+ get rpOptions() {
602
+ return this._rpOptions;
603
+ }
604
+ get pexOptions() {
605
+ return this._pexOptions;
606
+ }
607
+ hasDefinition() {
608
+ return this.definitionId !== void 0;
609
+ }
610
+ get definitionId() {
611
+ return this.pexOptions?.definitionId;
612
+ }
613
+ async getPresentationDefinition(context) {
614
+ return this.definitionId ? await context.agent.pexStoreGetDefinition({
615
+ definitionId: this.definitionId,
616
+ tenantId: this.pexOptions?.tenantId
617
+ }) : void 0;
618
+ }
619
+ async createAuthorizationRequestURI(createArgs, context) {
620
+ const { correlationId, claims, requestByReferenceURI, responseURI, responseURIType } = createArgs;
621
+ const nonce = createArgs.nonce ?? uuidv4();
622
+ const state = createArgs.state ?? correlationId;
623
+ let jwtIssuer;
624
+ const idOpts = this.rpOptions.identifierOpts.idOpts;
625
+ const resolution = await ensureManagedIdentifierResult(idOpts, context);
626
+ if (isManagedIdentifierDidResult2(resolution)) {
627
+ jwtIssuer = {
628
+ didUrl: resolution.kid,
629
+ method: "did",
630
+ alg: getSigningAlgo(resolution.key.type)
631
+ };
632
+ } else if (isManagedIdentifierX5cResult(resolution)) {
633
+ if (!resolution.issuer) {
634
+ return Promise.reject("missing issuer in idOpts");
635
+ }
636
+ jwtIssuer = {
637
+ issuer: resolution.issuer,
638
+ x5c: resolution.x5c,
639
+ method: "x5c",
640
+ alg: getSigningAlgo(resolution.key.type)
641
+ };
642
+ } else {
643
+ return Promise.reject(Error(`JWT issuer method ${resolution.method} not yet supported`));
644
+ }
645
+ return await this.get(context).then((rp) => rp.createAuthorizationRequestURI({
646
+ version: getRequestVersion(this.rpOptions),
647
+ correlationId,
648
+ nonce,
649
+ state,
650
+ claims,
651
+ requestByReferenceURI,
652
+ responseURI,
653
+ responseURIType,
654
+ jwtIssuer
655
+ }));
656
+ }
657
+ async createAuthorizationRequest(createArgs, context) {
658
+ const { correlationId, claims, requestByReferenceURI, responseURI, responseURIType } = createArgs;
659
+ const nonce = createArgs.nonce ?? uuidv4();
660
+ const state = createArgs.state ?? correlationId;
661
+ const idOpts = this.rpOptions.identifierOpts.idOpts;
662
+ const resolution = await ensureManagedIdentifierResult(idOpts, context);
663
+ let jwtIssuer;
664
+ if (isManagedIdentifierX5cResult(resolution) && resolution.issuer) {
665
+ jwtIssuer = {
666
+ method: resolution.method,
667
+ alg: getSigningAlgo(resolution.key.type),
668
+ x5c: resolution.x5c,
669
+ issuer: resolution.issuer
670
+ };
671
+ } else if (isManagedIdentifierDidResult2(resolution)) {
672
+ jwtIssuer = {
673
+ method: resolution.method,
674
+ alg: getSigningAlgo(resolution.key.type),
675
+ didUrl: resolution.did
676
+ };
677
+ } else {
678
+ return Promise.reject(Error("Only did & x5c supported at present"));
679
+ }
680
+ return await this.get(context).then((rp) => rp.createAuthorizationRequest({
681
+ version: getRequestVersion(this.rpOptions),
682
+ correlationId,
683
+ nonce,
684
+ state,
685
+ claims,
686
+ requestByReferenceURI,
687
+ responseURIType,
688
+ responseURI,
689
+ jwtIssuer
690
+ }));
691
+ }
692
+ };
693
+
694
+ // src/agent/SIOPv2RP.ts
695
+ import { shaHasher as defaultHasher2 } from "@sphereon/ssi-sdk.core";
696
+ var SIOPv2RP = class _SIOPv2RP {
697
+ static {
698
+ __name(this, "SIOPv2RP");
699
+ }
700
+ opts;
701
+ static _DEFAULT_OPTS_KEY = "_default";
702
+ instances = /* @__PURE__ */ new Map();
703
+ schema = schema.IDidAuthSiopOpAuthenticator;
704
+ methods = {
705
+ siopCreateAuthRequestURI: this.createAuthorizationRequestURI.bind(this),
706
+ siopCreateAuthRequestPayloads: this.createAuthorizationRequestPayloads.bind(this),
707
+ siopGetAuthRequestState: this.siopGetRequestState.bind(this),
708
+ siopGetAuthResponseState: this.siopGetResponseState.bind(this),
709
+ siopUpdateAuthRequestState: this.siopUpdateRequestState.bind(this),
710
+ siopDeleteAuthState: this.siopDeleteState.bind(this),
711
+ siopVerifyAuthResponse: this.siopVerifyAuthResponse.bind(this),
712
+ siopImportDefinitions: this.siopImportDefinitions.bind(this),
713
+ siopGetRedirectURI: this.siopGetRedirectURI.bind(this)
714
+ };
715
+ constructor(opts) {
716
+ this.opts = opts;
717
+ }
718
+ setDefaultOpts(rpDefaultOpts, context) {
719
+ this.opts.defaultOpts = rpDefaultOpts;
720
+ if (!this.opts.defaultOpts.identifierOpts.resolveOpts?.resolver || typeof this.opts.defaultOpts.identifierOpts.resolveOpts.resolver.resolve !== "function") {
721
+ this.opts.defaultOpts.identifierOpts.resolveOpts = {
722
+ ...this.opts.defaultOpts.identifierOpts.resolveOpts,
723
+ resolver: getAgentResolver2(context, {
724
+ uniresolverResolution: true,
725
+ resolverResolution: true,
726
+ localResolution: true
727
+ })
728
+ };
729
+ }
730
+ }
731
+ async createAuthorizationRequestURI(createArgs, context) {
732
+ return await this.getRPInstance({
733
+ definitionId: createArgs.definitionId,
734
+ responseRedirectURI: createArgs.responseRedirectURI
735
+ }, context).then((rp) => rp.createAuthorizationRequestURI(createArgs, context)).then((URI) => URI.encodedUri);
736
+ }
737
+ async createAuthorizationRequestPayloads(createArgs, context) {
738
+ return await this.getRPInstance({
739
+ definitionId: createArgs.definitionId
740
+ }, context).then((rp) => rp.createAuthorizationRequest(createArgs, context)).then(async (request) => {
741
+ const authRequest = {
742
+ authorizationRequest: request.payload,
743
+ requestObject: await request.requestObjectJwt(),
744
+ requestObjectDecoded: await request.requestObject?.getPayload()
745
+ };
746
+ return authRequest;
747
+ });
748
+ }
749
+ async siopGetRequestState(args, context) {
750
+ return await this.getRPInstance({
751
+ definitionId: args.definitionId
752
+ }, context).then((rp) => rp.get(context).then((rp2) => rp2.sessionManager.getRequestStateByCorrelationId(args.correlationId, args.errorOnNotFound)));
753
+ }
754
+ async siopGetResponseState(args, context) {
755
+ const rpInstance = await this.getRPInstance({
756
+ definitionId: args.definitionId
757
+ }, context);
758
+ const authorizationResponseState = await rpInstance.get(context).then((rp) => rp.sessionManager.getResponseStateByCorrelationId(args.correlationId, args.errorOnNotFound));
759
+ if (authorizationResponseState === void 0) {
760
+ return void 0;
761
+ }
762
+ const responseState = authorizationResponseState;
763
+ if (responseState.status === AuthorizationResponseStateStatus.VERIFIED && args.includeVerifiedData && args.includeVerifiedData !== VerifiedDataMode.NONE) {
764
+ let hasher;
765
+ if (CredentialMapper2.isSdJwtEncoded(responseState.response.payload.vp_token) && (!rpInstance.rpOptions.credentialOpts?.hasher || typeof rpInstance.rpOptions.credentialOpts?.hasher !== "function")) {
766
+ hasher = defaultHasher2;
767
+ }
768
+ const presentationDecoded = CredentialMapper2.decodeVerifiablePresentation(
769
+ responseState.response.payload.vp_token,
770
+ //todo: later we want to conditionally pass in options for mdl-mdoc here
771
+ hasher
772
+ );
773
+ switch (args.includeVerifiedData) {
774
+ case VerifiedDataMode.VERIFIED_PRESENTATION:
775
+ responseState.response.payload.verifiedData = this.presentationOrClaimsFrom(presentationDecoded);
776
+ break;
777
+ case VerifiedDataMode.CREDENTIAL_SUBJECT_FLATTENED:
778
+ const allClaims = {};
779
+ for (const credential of this.presentationOrClaimsFrom(presentationDecoded).verifiableCredential || []) {
780
+ const vc = credential;
781
+ const schemaValidationResult = await context.agent.cvVerifySchema({
782
+ credential,
783
+ hasher,
784
+ validationPolicy: rpInstance.rpOptions.verificationPolicies?.schemaValidation
785
+ });
786
+ if (!schemaValidationResult.result) {
787
+ responseState.status = AuthorizationResponseStateStatus.ERROR;
788
+ responseState.error = new Error(schemaValidationResult.error);
789
+ return responseState;
790
+ }
791
+ const credentialSubject = vc.credentialSubject;
792
+ if (!("id" in allClaims)) {
793
+ allClaims["id"] = credentialSubject.id;
794
+ }
795
+ Object.entries(credentialSubject).forEach(([key, value]) => {
796
+ if (!(key in allClaims)) {
797
+ allClaims[key] = value;
798
+ }
799
+ });
800
+ }
801
+ responseState.verifiedData = allClaims;
802
+ break;
803
+ }
804
+ }
805
+ return responseState;
806
+ }
807
+ presentationOrClaimsFrom = /* @__PURE__ */ __name((presentationDecoded) => CredentialMapper2.isSdJwtDecodedCredential(presentationDecoded) ? presentationDecoded.decodedPayload : CredentialMapper2.toUniformPresentation(presentationDecoded), "presentationOrClaimsFrom");
808
+ async siopUpdateRequestState(args, context) {
809
+ if (args.state !== "sent") {
810
+ throw Error(`Only 'sent' status is supported for this method at this point`);
811
+ }
812
+ return await this.getRPInstance({
813
+ definitionId: args.definitionId
814
+ }, context).then((rp) => rp.get(context).then(async (rp2) => {
815
+ await rp2.signalAuthRequestRetrieved({
816
+ correlationId: args.correlationId,
817
+ error: args.error ? new Error(args.error) : void 0
818
+ });
819
+ return await rp2.sessionManager.getRequestStateByCorrelationId(args.correlationId, true);
820
+ }));
821
+ }
822
+ async siopDeleteState(args, context) {
823
+ return await this.getRPInstance({
824
+ definitionId: args.definitionId
825
+ }, context).then((rp) => rp.get(context).then((rp2) => rp2.sessionManager.deleteStateForCorrelationId(args.correlationId))).then(() => true);
826
+ }
827
+ async siopVerifyAuthResponse(args, context) {
828
+ if (!args.authorizationResponse) {
829
+ throw Error("No SIOPv2 Authorization Response received");
830
+ }
831
+ const authResponse = typeof args.authorizationResponse === "string" ? decodeUriAsJson(args.authorizationResponse) : args.authorizationResponse;
832
+ return await this.getRPInstance({
833
+ definitionId: args.definitionId
834
+ }, context).then((rp) => rp.get(context).then((rp2) => rp2.verifyAuthorizationResponse(authResponse, {
835
+ correlationId: args.correlationId,
836
+ ...args.presentationDefinitions && !args.dcqlQuery ? {
837
+ presentationDefinitions: args.presentationDefinitions
838
+ } : {},
839
+ ...args.dcqlQuery ? {
840
+ dcqlQuery: args.dcqlQuery
841
+ } : {},
842
+ audience: args.audience
843
+ })));
844
+ }
845
+ async siopImportDefinitions(args, context) {
846
+ const { definitions, tenantId, version, versionControlMode } = args;
847
+ await Promise.all(definitions.map(async (definitionPair) => {
848
+ const definitionPayload = definitionPair.definitionPayload;
849
+ await context.agent.pexValidateDefinition({
850
+ definition: definitionPayload
851
+ });
852
+ console.log(`persisting definition ${definitionPayload.id} / ${definitionPayload.name} with versionControlMode ${versionControlMode}`);
853
+ return context.agent.pdmPersistDefinition({
854
+ definitionItem: {
855
+ tenantId,
856
+ version,
857
+ definitionPayload,
858
+ dcqlPayload: definitionPair.dcqlPayload
859
+ },
860
+ opts: {
861
+ versionControlMode
862
+ }
863
+ });
864
+ }));
865
+ }
866
+ async siopGetRedirectURI(args, context) {
867
+ const instanceId = args.definitionId ?? _SIOPv2RP._DEFAULT_OPTS_KEY;
868
+ if (this.instances.has(instanceId)) {
869
+ const rpInstance = this.instances.get(instanceId);
870
+ if (rpInstance !== void 0) {
871
+ const rp = await rpInstance.get(context);
872
+ return rp.getResponseRedirectUri({
873
+ correlation_id: args.correlationId,
874
+ correlationId: args.correlationId,
875
+ ...args.state && {
876
+ state: args.state
877
+ }
878
+ });
879
+ }
880
+ }
881
+ return void 0;
882
+ }
883
+ async getRPInstance({ definitionId, responseRedirectURI }, context) {
884
+ const instanceId = definitionId ?? _SIOPv2RP._DEFAULT_OPTS_KEY;
885
+ if (!this.instances.has(instanceId)) {
886
+ const instanceOpts = this.getInstanceOpts(definitionId);
887
+ const rpOpts = await this.getRPOptions(context, {
888
+ definitionId,
889
+ responseRedirectURI
890
+ });
891
+ if (!rpOpts.identifierOpts.resolveOpts?.resolver || typeof rpOpts.identifierOpts.resolveOpts.resolver.resolve !== "function") {
892
+ if (!rpOpts.identifierOpts?.resolveOpts) {
893
+ rpOpts.identifierOpts = {
894
+ ...rpOpts.identifierOpts
895
+ };
896
+ rpOpts.identifierOpts.resolveOpts = {
897
+ ...rpOpts.identifierOpts.resolveOpts
898
+ };
899
+ }
900
+ console.log("Using agent DID resolver for RP instance with definition id " + definitionId);
901
+ rpOpts.identifierOpts.resolveOpts.resolver = getAgentResolver2(context, {
902
+ uniresolverResolution: true,
903
+ localResolution: true,
904
+ resolverResolution: true
905
+ });
906
+ }
907
+ this.instances.set(instanceId, new RPInstance({
908
+ rpOpts,
909
+ pexOpts: instanceOpts
910
+ }));
911
+ }
912
+ const rpInstance = this.instances.get(instanceId);
913
+ if (responseRedirectURI) {
914
+ rpInstance.rpOptions.responseRedirectUri = responseRedirectURI;
915
+ }
916
+ return rpInstance;
917
+ }
918
+ async getRPOptions(context, opts) {
919
+ const { definitionId, responseRedirectURI } = opts;
920
+ const options = this.getInstanceOpts(definitionId)?.rpOpts ?? this.opts.defaultOpts;
921
+ if (!options) {
922
+ throw Error(`Could not get specific nor default options for definition ${definitionId}`);
923
+ }
924
+ if (this.opts.defaultOpts) {
925
+ if (!options.identifierOpts) {
926
+ options.identifierOpts = this.opts.defaultOpts?.identifierOpts;
927
+ } else {
928
+ if (!options.identifierOpts.idOpts) {
929
+ options.identifierOpts.idOpts = this.opts.defaultOpts.identifierOpts.idOpts;
930
+ }
931
+ if (!options.identifierOpts.supportedDIDMethods) {
932
+ options.identifierOpts.supportedDIDMethods = this.opts.defaultOpts.identifierOpts.supportedDIDMethods;
933
+ }
934
+ if (!options.supportedVersions) {
935
+ options.supportedVersions = this.opts.defaultOpts.supportedVersions;
936
+ }
937
+ }
938
+ if (!options.identifierOpts.resolveOpts || typeof options.identifierOpts.resolveOpts.resolver?.resolve !== "function") {
939
+ options.identifierOpts.resolveOpts = {
940
+ ...this.opts.defaultOpts.identifierOpts.resolveOpts,
941
+ resolver: this.opts.defaultOpts.identifierOpts?.resolveOpts?.resolver ?? getAgentResolver2(context, {
942
+ localResolution: true,
943
+ resolverResolution: true,
944
+ uniresolverResolution: true
945
+ })
946
+ };
947
+ }
948
+ }
949
+ if (responseRedirectURI !== void 0 && responseRedirectURI !== options.responseRedirectUri) {
950
+ options.responseRedirectUri = responseRedirectURI;
951
+ }
952
+ return options;
953
+ }
954
+ getInstanceOpts(definitionId) {
955
+ if (!this.opts.instanceOpts) return void 0;
956
+ const instanceOpt = definitionId ? this.opts.instanceOpts.find((i) => i.definitionId === definitionId) : void 0;
957
+ return instanceOpt ?? this.getDefaultOptions(definitionId);
958
+ }
959
+ getDefaultOptions(definitionId) {
960
+ if (!this.opts.instanceOpts) return void 0;
961
+ const defaultOptions = this.opts.instanceOpts.find((i) => i.definitionId === "default");
962
+ if (defaultOptions) {
963
+ const clonedOptions = {
964
+ ...defaultOptions
965
+ };
966
+ if (definitionId !== void 0) {
967
+ clonedOptions.definitionId = definitionId;
968
+ }
969
+ return clonedOptions;
970
+ }
971
+ return void 0;
972
+ }
973
+ };
974
+
975
+ // src/types/ISIOPv2RP.ts
976
+ var VerifiedDataMode = /* @__PURE__ */ function(VerifiedDataMode2) {
977
+ VerifiedDataMode2["NONE"] = "none";
978
+ VerifiedDataMode2["VERIFIED_PRESENTATION"] = "vp";
979
+ VerifiedDataMode2["CREDENTIAL_SUBJECT_FLATTENED"] = "cs-flat";
980
+ return VerifiedDataMode2;
981
+ }({});
982
+
983
+ // src/index.ts
984
+ var schema = require_plugin_schema();
985
+ export {
986
+ SIOPv2RP,
987
+ VerifiedDataMode,
988
+ schema
15
989
  };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.SIOPv2RP = exports.schema = void 0;
18
- /**
19
- * @public
20
- */
21
- const schema = require('../plugin.schema.json');
22
- exports.schema = schema;
23
- var SIOPv2RP_1 = require("./agent/SIOPv2RP");
24
- Object.defineProperty(exports, "SIOPv2RP", { enumerable: true, get: function () { return SIOPv2RP_1.SIOPv2RP; } });
25
- __exportStar(require("./types/ISIOPv2RP"), exports);
26
990
  //# sourceMappingURL=index.js.map