generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathconnect.spec.ts
896 lines (779 loc) · 34.3 KB
/
connect.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
import { expect } from 'chai';
import sinon from 'sinon';
import { CryptoUtils } from '@web5/crypto';
import { type BearerDid, DidDht, DidJwk, PortableDid } from '@web5/dids';
import { Convert } from '@web5/common';
import {
Oidc,
type Web5ConnectAuthRequest,
type Web5ConnectAuthResponse,
} from '../src/oidc.js';
import { PlatformAgentTestHarness } from '../src/test-harness.js';
import { TestAgent } from './utils/test-agent.js';
import { testDwnUrl } from './utils/test-config.js';
import { BearerIdentity, DwnInterface, DwnMessage, DwnProtocolDefinition, WalletConnect } from '../src/index.js';
import { RecordsPermissionScope } from '@tbd54566975/dwn-sdk-js';
import { DwnInterfaceName, DwnMethodName } from '@tbd54566975/dwn-sdk-js';
describe('web5 connect', function () {
this.timeout(20000);
/** The temporary DID that web5 connect created on behalf of the client */
let clientEphemeralBearerDid: BearerDid;
let clientEphemeralPortableDid: PortableDid;
/** The real tenant (identity) of the DWN that the provider had chosen to connect */
let providerIdentity: BearerIdentity;
/** The new DID created for the delegate which it will impersonate in the future */
let delegateBearerDid: BearerDid;
let delegatePortableDid: PortableDid;
/** The real tenant (identity) of the DWN that the provider is using and selecting */
let providerIdentityBearerDid: BearerDid;
const providerIdentityPortableDid = {
uri : 'did:dht:ksbkpsjytbm7kh6hnt3xi91t6to98zndtrrxzsqz9y87m5qztyqo',
document : {
'@context' : 'https://www.w3.org/ns/did/v1',
id : 'did:dht:ksbkpsjytbm7kh6hnt3xi91t6to98zndtrrxzsqz9y87m5qztyqo',
verificationMethod : [
{
id : 'did:dht:ksbkpsjytbm7kh6hnt3xi91t6to98zndtrrxzsqz9y87m5qztyqo#0',
type : 'JsonWebKey',
controller:
'did:dht:ksbkpsjytbm7kh6hnt3xi91t6to98zndtrrxzsqz9y87m5qztyqo',
publicKeyJwk: {
crv : 'Ed25519',
kty : 'OKP' as const,
x : 'VYKm2SCIV9Vz3BRy-v5R9GHz3EOJCPvZ1_gP1e3XiB0',
kid : 'cyvOypa6k-4ffsRWcza37s5XVOh1kO9ICUeo1ZxHVM8',
alg : 'EdDSA',
},
},
],
authentication: [
'did:dht:ksbkpsjytbm7kh6hnt3xi91t6to98zndtrrxzsqz9y87m5qztyqo#0',
],
assertionMethod: [
'did:dht:ksbkpsjytbm7kh6hnt3xi91t6to98zndtrrxzsqz9y87m5qztyqo#0',
],
capabilityDelegation: [
'did:dht:ksbkpsjytbm7kh6hnt3xi91t6to98zndtrrxzsqz9y87m5qztyqo#0',
],
capabilityInvocation: [
'did:dht:ksbkpsjytbm7kh6hnt3xi91t6to98zndtrrxzsqz9y87m5qztyqo#0',
],
},
metadata : {},
privateKeys : [
{
crv : 'Ed25519',
d : 'hdSIwbQwVD-fNOVEgt-k3mMl44Ip1iPi58Ex6VDGxqY',
kty : 'OKP' as const,
x : 'VYKm2SCIV9Vz3BRy-v5R9GHz3EOJCPvZ1_gP1e3XiB0',
kid : 'cyvOypa6k-4ffsRWcza37s5XVOh1kO9ICUeo1ZxHVM8',
alg : 'EdDSA',
},
],
};
let permissionGrants: any[] = [
{
reply : { status: { code: 202, detail: 'Accepted' } },
message : {
recordId : 'bafyreifzveddv32ea3tzybpgphxvdvmk2qtgdi7ykt5atgo76m426jqp3m',
descriptor : {
interface : 'Records',
method : 'Write',
protocol : 'https://tbd.website/dwn/permissions',
protocolPath : 'grant',
recipient:
'did:dht:pfm8f6w57srtci1k3spp73dqgk5eo3afkimtyi4zcqc5hg1ui5mo',
dataCid:
'bafkreibesnbudco6hhuj5m4lc3jktvd2pd4ew4uypsiq66xxuaec4jwt7e',
dataSize : 156,
dateCreated : '2024-08-02T06:36:37.675594Z',
messageTimestamp : '2024-08-02T06:36:37.675594Z',
dataFormat : 'application/json',
},
contextId:
'bafyreifzveddv32ea3tzybpgphxvdvmk2qtgdi7ykt5atgo76m426jqp3m',
authorization: {
signature: {
payload:
'eyJyZWNvcmRJZCI6ImJhZnlyZWlmenZlZGR2MzJlYTN0enlicGdwaHh2ZHZtazJxdGdkaTd5a3Q1YXRnbzc2bTQyNmpxcDNtIiwiZGVzY3JpcHRvckNpZCI6ImJhZnlyZWlid3Y1ajVhbHlmbmV0Mmh6NTNoYWRsZnF6eG1vNzVsZHYyeml5cGp4enlmN2ZuZWMyYnV1IiwiY29udGV4dElkIjoiYmFmeXJlaWZ6dmVkZHYzMmVhM3R6eWJwZ3BoeHZkdm1rMnF0Z2RpN3lrdDVhdGdvNzZtNDI2anFwM20ifQ',
signatures: [
{
protected:
'eyJraWQiOiJkaWQ6ZGh0OjFxbmdkZ2RlMzE2NHB1MTU3eDRyZWlqcWlzYm1yN2R4OG5raWNpOXltdG56ZWsxaWJpMXkjMCIsImFsZyI6IkVkRFNBIn0',
signature:
'7xiNZGsb8dlom2tCSdjUQgkBsAm6XSRt6i4cNS6NDDSkCGjVr79TB7tF5VQdtwMJCrDpKtSmXQ0eEN4j2dWMAQ',
},
],
},
},
},
messageCid: 'bafyreievjytxn2qbfwg4fthnsrjnob3mm2j2haar6revl723v7q2up5g5i',
},
];
const protocolDefinition: DwnProtocolDefinition = {
protocol : 'http://profile-protocol.xyz',
published : true,
types : {
profile: {
schema : 'http://profile-protocol.xyz/schema/profile',
dataFormats : ['application/json'],
},
},
structure: {
profile: {
$actions: [
{
who : 'anyone',
can : ['create', 'update'],
},
],
},
},
};
const permissionScopes: RecordsPermissionScope[] = [
{
interface : 'Records' as any,
method : 'Write' as any,
protocol : 'http://profile-protocol.xyz',
},
{
interface : 'Records' as any,
method : 'Query' as any,
protocol : 'http://profile-protocol.xyz',
},
{
interface : 'Records' as any,
method : 'Read' as any,
protocol : 'http://profile-protocol.xyz',
},
];
let testHarness: PlatformAgentTestHarness;
let authRequest: Web5ConnectAuthRequest;
let authRequestJwt: string;
let authRequestJwe: string;
let authResponse: Web5ConnectAuthResponse;
let authResponseJwt: string;
let authResponseJwe: string;
let sharedECDHPrivateKey: Uint8Array;
const authRequestEncryptionKey = CryptoUtils.randomBytes(32);
const encryptionNonce = CryptoUtils.randomBytes(24);
const randomPin = '9999';
before(async () => {
providerIdentityBearerDid = await DidDht.import({
portableDid: providerIdentityPortableDid,
});
sinon.stub(DidDht, 'create').resolves(providerIdentityBearerDid);
testHarness = await PlatformAgentTestHarness.setup({
agentClass : TestAgent,
agentStores : 'memory',
});
await testHarness.createAgentDid();
sinon.restore();
providerIdentity = await testHarness.createIdentity({
name : 'MrProvider',
testDwnUrls : [testDwnUrl],
});
clientEphemeralBearerDid = await DidJwk.create();
clientEphemeralPortableDid = await clientEphemeralBearerDid.export();
delegateBearerDid = await DidJwk.create();
delegatePortableDid = await delegateBearerDid.export();
});
after(async () => {
sinon.restore();
await testHarness.clearStorage();
await testHarness.closeStorage();
});
afterEach(() => {
sinon.restore();
});
describe('client authrequest phase', function () {
// it('should create a code challenge', async () => {
// const result = await Oidc.generateCodeChallenge();
// expect(result.codeChallengeBytes).to.be.instanceOf(Uint8Array);
// expect(result.codeChallengeBase64Url).to.be.a('string');
// });
it('should create an authrequest with the code challenge and client did', async () => {
const randomBytesStub = sinon
.stub(CryptoUtils, 'randomBytes')
.returns(authRequestEncryptionKey);
const callbackUrl = Oidc.buildOidcUrl({
baseURL : 'http://localhost:3000',
endpoint : 'callback',
});
const options = {
client_id : clientEphemeralPortableDid.uri,
scope : 'openid did:jwk',
// code_challenge : Convert.uint8Array(codeChallenge).toBase64Url(),
// code_challenge_method : 'S256' as const,
permissionRequests : [{ protocolDefinition, permissionScopes }],
redirect_uri : callbackUrl,
};
authRequest = await Oidc.createAuthRequest(options);
expect(authRequest).to.include(options);
expect(authRequest.nonce).to.be.a('string');
expect(authRequest.state).to.be.a('string');
expect(authRequest.redirect_uri).to.equal(
'http://localhost:3000/callback'
);
});
it('should construct a signed jwt of an authrequest', async () => {
authRequestJwt = await Oidc.signJwt({
did : clientEphemeralBearerDid,
data : authRequest,
});
expect(authRequestJwt).to.be.a('string');
});
it('should encrypt an authrequest using the code challenge', async () => {
authRequestJwe = await Oidc.encryptAuthRequest({
jwt : authRequestJwt,
encryptionKey : authRequestEncryptionKey
});
expect(authRequestJwe).to.be.a('string');
expect(authRequestJwe.split('.')).to.have.lengthOf(5);
});
});
describe('provider authresponse phase', function () {
it('should get authrequest from server, decrypt and verify the jwt', async () => {
const fetchStub = sinon
.stub(globalThis, 'fetch')
.onFirstCall()
.resolves({
text: sinon.stub().resolves(authRequestJwe),
} as any);
fetchStub.callThrough();
const authorizeUrl = Oidc.buildOidcUrl({
baseURL : 'http://localhost:3000',
endpoint : 'authorize',
authParam : '12345',
});
expect(authorizeUrl).to.equal(
'http://localhost:3000/authorize/12345.jwt'
);
const result = await Oidc.getAuthRequest(
authorizeUrl,
Convert.uint8Array(authRequestEncryptionKey).toBase64Url()
);
expect(result).to.deep.equal(authRequest);
});
// TODO: waiting for DWN feature complete
it('should create permission grants for each selected did', async () => {
const results = await Oidc.createPermissionGrants(
providerIdentity.did.uri,
delegateBearerDid,
testHarness.agent,
permissionScopes
);
const scopesRequested = permissionScopes.length;
expect(results).to.have.lengthOf(scopesRequested);
expect(results[0]).to.be.a('object');
});
it('should create the authresponse which includes the permissionGrants, nonce, private key material', async () => {
const options = {
iss : providerIdentity.did.uri,
sub : delegateBearerDid.uri,
aud : authRequest.client_id,
nonce : authRequest.nonce,
delegateGrants : permissionGrants,
delegatePortableDid,
};
authResponse = await Oidc.createResponseObject(options);
expect(authResponse).to.include(options);
expect(authResponse.iat).to.be.a('number');
expect(authResponse.exp).to.be.a('number');
expect(authResponse.exp - authResponse.iat).to.equal(600);
});
it('should sign the authresponse with its provider did', async () => {
authResponseJwt = await Oidc.signJwt({
did : delegateBearerDid,
data : authResponse,
});
expect(authResponseJwt).to.be.a('string');
});
it('should derive a valid ECDH private key for both provider and client which is identical', async () => {
const providerECDHDerivedPrivateKey = await Oidc.deriveSharedKey(
delegateBearerDid,
clientEphemeralBearerDid.document
);
const clientECDHDerivedPrivateKey = await Oidc.deriveSharedKey(
clientEphemeralBearerDid,
delegateBearerDid.document
);
expect(providerECDHDerivedPrivateKey).to.be.instanceOf(Uint8Array);
expect(providerECDHDerivedPrivateKey.length).to.be.greaterThan(0);
expect(clientECDHDerivedPrivateKey).to.be.instanceOf(Uint8Array);
expect(clientECDHDerivedPrivateKey.length).to.be.greaterThan(0);
expect(
Convert.uint8Array(providerECDHDerivedPrivateKey).toHex()
).to.equal(Convert.uint8Array(clientECDHDerivedPrivateKey).toHex());
// doesnt matter client and provider are the same
sharedECDHPrivateKey = clientECDHDerivedPrivateKey;
});
it('should encrypt the jwt authresponse to pass back to the client', async () => {
const randomBytesStub = sinon
.stub(CryptoUtils, 'randomBytes')
.returns(encryptionNonce);
authResponseJwe = Oidc.encryptAuthResponse({
jwt : authResponseJwt,
encryptionKey : sharedECDHPrivateKey,
randomPin,
delegateDidKeyId : delegateBearerDid.document.verificationMethod![0].id,
});
expect(authResponseJwe).to.be.a('string');
expect(randomBytesStub.calledOnce).to.be.true;
});
it('should send the encrypted jwe authresponse to the server', async () => {
sinon.stub(Oidc, 'createPermissionGrants').resolves(permissionGrants as any);
sinon.stub(CryptoUtils, 'randomBytes').returns(encryptionNonce);
sinon.stub(DidJwk, 'create').resolves(delegateBearerDid);
const formEncodedRequest = new URLSearchParams({
id_token : authResponseJwe,
state : authRequest.state,
}).toString();
const callbackUrl = Oidc.buildOidcUrl({
baseURL : 'http://localhost:3000',
endpoint : 'callback',
});
expect(callbackUrl).to.equal('http://localhost:3000/callback');
const fetchSpy = sinon.spy(globalThis, 'fetch').withArgs(
callbackUrl,
sinon.match({
method : 'POST',
headers : {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: formEncodedRequest,
})
);
const selectedDid = providerIdentity.did.uri;
await Oidc.submitAuthResponse(
selectedDid,
authRequest,
randomPin,
testHarness.agent
);
expect(fetchSpy.calledOnce).to.be.true;
});
});
describe('client pin entry final phase', function () {
it('should get the authresponse from server and decrypt the jwe using the pin', async () => {
const result = await Oidc.decryptAuthResponse(
clientEphemeralBearerDid,
authResponseJwe,
randomPin
);
expect(result).to.be.a('string');
expect(result).to.equal(authResponseJwt);
});
it('should fail decrypting the jwe if the wrong pin is entered', async () => {
try {
await Oidc.decryptAuthResponse(
clientEphemeralBearerDid,
authResponseJwe,
'87383837583757835737537734783'
);
} catch (e: any) {
expect(e).to.be.instanceOf(Error);
expect(e.message).to.include('invalid tag');
}
});
it('should validate the jwt and parse it into an object', async () => {
const result = (await Oidc.verifyJwt({
jwt: authResponseJwt,
})) as Web5ConnectAuthResponse;
expect(result).to.be.a('object');
expect(result.delegateGrants).to.have.length.above(0);
});
});
describe('end to end client test', function () {
it('should complete the whole connect flow with the correct pin', async function () {
const fetchStub = sinon.stub(globalThis, 'fetch');
const onWalletUriReadySpy = sinon.spy();
sinon.stub(DidJwk, 'create').resolves(clientEphemeralBearerDid);
const par = {
expires_in : 3600000,
request_uri : 'http://localhost:3000/connect/authorize/xyz.jwt',
};
const parResponse = new Response(JSON.stringify(par), {
status : 200,
headers : { 'Content-type': 'application/json' },
});
const authResponse = new Response(authResponseJwe, {
status : 200,
headers : { 'Content-Type': 'application/x-www-form-urlencoded' },
});
fetchStub.onFirstCall().resolves(parResponse);
fetchStub.callThrough();
fetchStub.onThirdCall().resolves(authResponse);
fetchStub.callThrough();
const results = await WalletConnect.initClient({
walletUri : 'http://localhost:3000/',
connectServerUrl : 'http://localhost:3000/connect',
permissionRequests : [
{
protocolDefinition : {} as any,
permissionScopes : {} as any,
},
],
onWalletUriReady : (uri) => onWalletUriReadySpy(uri),
validatePin : async () => randomPin,
});
expect(fetchStub.firstCall.args[0]).to.equal(
'http://localhost:3000/connect/par'
);
expect(onWalletUriReadySpy.calledOnce).to.be.true;
expect(onWalletUriReadySpy.firstCall.args[0]).to.match(
new RegExp(
'http:\\/\\/[\\w.-]+:\\d+\\/\\?request_uri=http%3A%2F%2F[\\w.-]+%3A(\\d+|%24%7Bport%7D)%2Fconnect%2Fauthorize%2F[\\w.-]+\\.jwt&encryption_key=.+',
'i'
)
);
expect(fetchStub.thirdCall.args[0]).to.match(
new RegExp('^http:\\/\\/localhost:3000\\/connect\\/token\\/.+\\.jwt$')
);
expect(results).to.be.an('object');
expect(results?.delegateGrants).to.be.an('array');
expect(results?.delegatePortableDid).to.be.an('object');
});
});
describe('submitAuthResponse', () => {
it('should not attempt to configure the protocol if it already exists', async () => {
// scenario: the wallet gets a request for a protocol that it already has configured
// the wallet should not attempt to re-configure, but instead ensure that the protocol is
// sent to the remote DWN for the requesting client to be able to sync it down later
sinon.stub(Oidc, 'createPermissionGrants').resolves(permissionGrants as any);
sinon.stub(CryptoUtils, 'randomBytes').returns(encryptionNonce);
sinon.stub(DidJwk, 'create').resolves(delegateBearerDid);
const callbackUrl = Oidc.buildOidcUrl({
baseURL : 'http://localhost:3000',
endpoint : 'callback',
});
const options = {
client_id : clientEphemeralPortableDid.uri,
scope : 'openid did:jwk',
// code_challenge : Convert.uint8Array(codeChallenge).toBase64Url(),
// code_challenge_method : 'S256' as const,
permissionRequests : [{ protocolDefinition, permissionScopes }],
redirect_uri : callbackUrl,
};
authRequest = await Oidc.createAuthRequest(options);
// stub the processDwnRequest method to return a protocol entry
const protocolMessage = {} as DwnMessage[DwnInterface.ProtocolsConfigure];
// spy send request
const sendRequestSpy = sinon.stub(testHarness.agent, 'sendDwnRequest').resolves({
messageCid : '',
reply : { status: { code: 202, detail: 'OK' } }
});
const processDwnRequestStub = sinon
.stub(testHarness.agent, 'processDwnRequest')
.resolves({ messageCid: '', reply: { status: { code: 200, detail: 'OK' }, entries: [ protocolMessage ]} });
// call submitAuthResponse
await Oidc.submitAuthResponse(
providerIdentity.did.uri,
authRequest,
randomPin,
testHarness.agent
);
// expect the process request to only be called once for ProtocolsQuery
expect(processDwnRequestStub.callCount).to.equal(1);
expect(processDwnRequestStub.firstCall.args[0].messageType).to.equal(DwnInterface.ProtocolsQuery);
// send request should be called once as a ProtocolsConfigure
expect(sendRequestSpy.callCount).to.equal(1);
expect(sendRequestSpy.firstCall.args[0].messageType).to.equal(DwnInterface.ProtocolsConfigure);
});
it('should configure the protocol if it does not exist', async () => {
// scenario: the wallet gets a request for a protocol that it does not have configured
// the wallet should attempt to configure the protocol and then send the protocol to the remote DWN
// looks for a response of 404, empty entries array or missing entries array
sinon.stub(Oidc, 'createPermissionGrants').resolves(permissionGrants as any);
sinon.stub(CryptoUtils, 'randomBytes').returns(encryptionNonce);
sinon.stub(DidJwk, 'create').resolves(delegateBearerDid);
const callbackUrl = Oidc.buildOidcUrl({
baseURL : 'http://localhost:3000',
endpoint : 'callback',
});
const options = {
client_id : clientEphemeralPortableDid.uri,
scope : 'openid did:jwk',
// code_challenge : Convert.uint8Array(codeChallenge).toBase64Url(),
// code_challenge_method : 'S256' as const,
permissionRequests : [{ protocolDefinition, permissionScopes }],
redirect_uri : callbackUrl,
};
authRequest = await Oidc.createAuthRequest(options);
// spy send request
const sendRequestSpy = sinon.stub(testHarness.agent, 'sendDwnRequest').resolves({
messageCid : '',
reply : { status: { code: 202, detail: 'OK' } }
});
const processDwnRequestStub = sinon
.stub(testHarness.agent, 'processDwnRequest')
.resolves({ messageCid: '', reply: { status: { code: 200, detail: 'OK' }, entries: [ ] } });
// call submitAuthResponse
await Oidc.submitAuthResponse(
providerIdentity.did.uri,
authRequest,
randomPin,
testHarness.agent
);
// expect the process request to be called for query and configure
expect(processDwnRequestStub.callCount).to.equal(2);
expect(processDwnRequestStub.firstCall.args[0].messageType).to.equal(DwnInterface.ProtocolsQuery);
expect(processDwnRequestStub.secondCall.args[0].messageType).to.equal(DwnInterface.ProtocolsConfigure);
// send request should be called once as a ProtocolsConfigure
expect(sendRequestSpy.callCount).to.equal(1);
expect(sendRequestSpy.firstCall.args[0].messageType).to.equal(DwnInterface.ProtocolsConfigure);
// reset the spys
processDwnRequestStub.resetHistory();
sendRequestSpy.resetHistory();
// processDwnRequestStub should resolve a 200 with no entires
processDwnRequestStub.resolves({ messageCid: '', reply: { status: { code: 200, detail: 'OK' } } });
// call submitAuthResponse
await Oidc.submitAuthResponse(
providerIdentity.did.uri,
authRequest,
randomPin,
testHarness.agent
);
// expect the process request to be called for query and configure
expect(processDwnRequestStub.callCount).to.equal(2);
expect(processDwnRequestStub.firstCall.args[0].messageType).to.equal(DwnInterface.ProtocolsQuery);
expect(processDwnRequestStub.secondCall.args[0].messageType).to.equal(DwnInterface.ProtocolsConfigure);
// send request should be called once as a ProtocolsConfigure
expect(sendRequestSpy.callCount).to.equal(1);
expect(sendRequestSpy.firstCall.args[0].messageType).to.equal(DwnInterface.ProtocolsConfigure);
});
it('should fail if the send request fails for newly configured protocol', async () => {
sinon.stub(Oidc, 'createPermissionGrants').resolves(permissionGrants as any);
sinon.stub(CryptoUtils, 'randomBytes').returns(encryptionNonce);
sinon.stub(DidJwk, 'create').resolves(delegateBearerDid);
const callbackUrl = Oidc.buildOidcUrl({
baseURL : 'http://localhost:3000',
endpoint : 'callback',
});
const options = {
client_id : clientEphemeralPortableDid.uri,
scope : 'openid did:jwk',
// code_challenge : Convert.uint8Array(codeChallenge).toBase64Url(),
// code_challenge_method : 'S256' as const,
permissionRequests : [{ protocolDefinition, permissionScopes }],
redirect_uri : callbackUrl,
};
authRequest = await Oidc.createAuthRequest(options);
// spy send request
const sendRequestSpy = sinon.stub(testHarness.agent, 'sendDwnRequest').resolves({
reply : { status: { code: 500, detail: 'Internal Server Error' } },
messageCid : ''
});
// return without any entries
const processDwnRequestStub = sinon
.stub(testHarness.agent, 'processDwnRequest')
.resolves({ messageCid: '', reply: { status: { code: 200, detail: 'OK' } } });
try {
// call submitAuthResponse
await Oidc.submitAuthResponse(
providerIdentity.did.uri,
authRequest,
randomPin,
testHarness.agent
);
expect.fail('should have thrown an error');
} catch (error: any) {
expect(error.message).to.equal('Could not send protocol: Internal Server Error');
expect(sendRequestSpy.callCount).to.equal(1);
}
});
it('should fail if the send request fails for existing protocol', async () => {
sinon.stub(Oidc, 'createPermissionGrants').resolves(permissionGrants as any);
sinon.stub(CryptoUtils, 'randomBytes').returns(encryptionNonce);
sinon.stub(DidJwk, 'create').resolves(delegateBearerDid);
const callbackUrl = Oidc.buildOidcUrl({
baseURL : 'http://localhost:3000',
endpoint : 'callback',
});
const options = {
client_id : clientEphemeralPortableDid.uri,
scope : 'openid did:jwk',
// code_challenge : Convert.uint8Array(codeChallenge).toBase64Url(),
// code_challenge_method : 'S256' as const,
permissionRequests : [{ protocolDefinition, permissionScopes }],
redirect_uri : callbackUrl,
};
authRequest = await Oidc.createAuthRequest(options);
// stub the processDwnRequest method to return a protocol entry
const protocolMessage = {} as DwnMessage[DwnInterface.ProtocolsConfigure];
// spy send request
const sendRequestSpy = sinon.stub(testHarness.agent, 'sendDwnRequest').resolves({
reply : { status: { code: 500, detail: 'Internal Server Error' } },
messageCid : ''
});
// mock returning the protocol entry
const processDwnRequestStub = sinon
.stub(testHarness.agent, 'processDwnRequest')
.resolves({ messageCid: '', reply: { status: { code: 200, detail: 'OK' }, entries: [ protocolMessage ] } });
try {
// call submitAuthResponse
await Oidc.submitAuthResponse(
providerIdentity.did.uri,
authRequest,
randomPin,
testHarness.agent
);
expect.fail('should have thrown an error');
} catch (error: any) {
expect(error.message).to.equal('Could not send protocol: Internal Server Error');
expect(processDwnRequestStub.callCount).to.equal(1);
expect(sendRequestSpy.callCount).to.equal(1);
}
});
it('should throw if protocol could not be fetched at all', async () => {
sinon.stub(Oidc, 'createPermissionGrants').resolves(permissionGrants as any);
sinon.stub(CryptoUtils, 'randomBytes').returns(encryptionNonce);
sinon.stub(DidJwk, 'create').resolves(delegateBearerDid);
const callbackUrl = Oidc.buildOidcUrl({
baseURL : 'http://localhost:3000',
endpoint : 'callback',
});
const options = {
client_id : clientEphemeralPortableDid.uri,
scope : 'openid did:jwk',
// code_challenge : Convert.uint8Array(codeChallenge).toBase64Url(),
// code_challenge_method : 'S256' as const,
permissionRequests : [{ protocolDefinition, permissionScopes }],
redirect_uri : callbackUrl,
};
authRequest = await Oidc.createAuthRequest(options);
// spy send request
const sendRequestSpy = sinon.stub(testHarness.agent, 'sendDwnRequest').resolves({
reply : { status: { code: 500, detail: 'Internal Server Error' } },
messageCid : ''
});
// mock returning the protocol entry
const processDwnRequestStub = sinon
.stub(testHarness.agent, 'processDwnRequest')
.resolves({ messageCid: '', reply: { status: { code: 500, detail: 'Some Error'}, } });
try {
// call submitAuthResponse
await Oidc.submitAuthResponse(
providerIdentity.did.uri,
authRequest,
randomPin,
testHarness.agent
);
expect.fail('should have thrown an error');
} catch (error: any) {
expect(error.message).to.equal('Could not fetch protocol: Some Error');
expect(processDwnRequestStub.callCount).to.equal(1);
expect(sendRequestSpy.callCount).to.equal(0);
}
});
it('should throw if a grant that is included in the request does not match the protocol definition', async () => {
sinon.stub(Oidc, 'createPermissionGrants').resolves(permissionGrants as any);
sinon.stub(CryptoUtils, 'randomBytes').returns(encryptionNonce);
sinon.stub(DidJwk, 'create').resolves(delegateBearerDid);
const callbackUrl = Oidc.buildOidcUrl({
baseURL : 'http://localhost:3000',
endpoint : 'callback',
});
const mismatchedScopes = [...permissionScopes];
mismatchedScopes[0].protocol = 'http://profile-protocol.xyz/other';
const options = {
client_id : clientEphemeralPortableDid.uri,
scope : 'openid did:jwk',
// code_challenge : Convert.uint8Array(codeChallenge).toBase64Url(),
// code_challenge_method : 'S256' as const,
permissionRequests : [{ protocolDefinition, permissionScopes }],
redirect_uri : callbackUrl,
};
authRequest = await Oidc.createAuthRequest(options);
try {
// call submitAuthResponse
await Oidc.submitAuthResponse(
providerIdentity.did.uri,
authRequest,
randomPin,
testHarness.agent
);
expect.fail('should have thrown an error');
} catch (error: any) {
expect(error.message).to.equal('All permission scopes must match the protocol uri they are provided with.');
}
});
});
describe('createPermissionRequestForProtocol', () => {
it('should add sync permissions to all requests', async () => {
const protocol:DwnProtocolDefinition = {
published : true,
protocol : 'https://exmaple.org/protocols/social',
types : {
note: {
schema : 'https://example.org/schemas/note',
dataFormats : [ 'application/json', 'text/plain' ],
}
},
structure: {
note: {}
}
};
const permissionRequests = WalletConnect.createPermissionRequestForProtocol({
definition: protocol, permissions: []
});
expect(permissionRequests.protocolDefinition).to.deep.equal(protocol);
expect(permissionRequests.permissionScopes.length).to.equal(4); // only includes the sync permissions + protocol query permission
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Messages && scope.method === DwnMethodName.Read)).to.not.be.undefined;
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Messages && scope.method === DwnMethodName.Query)).to.not.be.undefined;
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Messages && scope.method === DwnMethodName.Subscribe)).to.not.be.undefined;
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Protocols && scope.method === DwnMethodName.Query)).to.not.be.undefined;
});
it('should add requested permissions to the request', async () => {
const protocol:DwnProtocolDefinition = {
published : true,
protocol : 'https://exmaple.org/protocols/social',
types : {
note: {
schema : 'https://example.org/schemas/note',
dataFormats : [ 'application/json', 'text/plain' ],
}
},
structure: {
note: {}
}
};
const permissionRequests = WalletConnect.createPermissionRequestForProtocol({
definition: protocol, permissions: ['write', 'read']
});
expect(permissionRequests.protocolDefinition).to.deep.equal(protocol);
// the 3 sync permissions plus the 2 requested permissions, and a protocol query permission
expect(permissionRequests.permissionScopes.length).to.equal(6);
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Records && scope.method === DwnMethodName.Read)).to.not.be.undefined;
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Records && scope.method === DwnMethodName.Write)).to.not.be.undefined;
});
it('supports requesting `read`, `write`, `delete`, `query`, `subscribe` and `configure` permissions', async () => {
const protocol:DwnProtocolDefinition = {
published : true,
protocol : 'https://exmaple.org/protocols/social',
types : {
note: {
schema : 'https://example.org/schemas/note',
dataFormats : [ 'application/json', 'text/plain' ],
}
},
structure: {
note: {}
}
};
const permissionRequests = WalletConnect.createPermissionRequestForProtocol({
definition: protocol, permissions: ['write', 'read', 'delete', 'query', 'subscribe', 'configure']
});
expect(permissionRequests.protocolDefinition).to.deep.equal(protocol);
// the 3 sync permissions plus the 5 requested permissions
expect(permissionRequests.permissionScopes.length).to.equal(10);
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Records && scope.method === DwnMethodName.Read)).to.not.be.undefined;
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Records && scope.method === DwnMethodName.Write)).to.not.be.undefined;
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Records && scope.method === DwnMethodName.Delete)).to.not.be.undefined;
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Records && scope.method === DwnMethodName.Query)).to.not.be.undefined;
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Records && scope.method === DwnMethodName.Subscribe)).to.not.be.undefined;
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Protocols && scope.method === DwnMethodName.Query)).to.not.be.undefined;
expect(permissionRequests.permissionScopes.find(scope => scope.interface === DwnInterfaceName.Protocols && scope.method === DwnMethodName.Configure)).to.not.be.undefined;
});
});
});