-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
1134 lines (997 loc) · 46.9 KB
/
main.dart
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// You can find the full repository for this example at https://github.com/seald/seald-sdk-demo-app-flutter/
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:path/path.dart' as path;
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
import 'package:seald_sdk_flutter/seald_sdk.dart';
import 'package:seald_sdk_flutter_example/ssks_backend.dart';
import 'package:seald_sdk_flutter_example/credentials.dart';
void main() {
runApp(const MyApp());
}
class BlinkingWidget extends StatefulWidget {
const BlinkingWidget({super.key});
@override
BlinkingWidgetState createState() => BlinkingWidgetState();
}
class BlinkingWidgetState extends State<BlinkingWidget> {
bool _isVisible = true;
@override
void initState() {
super.initState();
// start the blinking animation
Timer.periodic(const Duration(milliseconds: 500), (timer) {
setState(() {
_isVisible = !_isVisible;
});
});
}
@override
Widget build(BuildContext context) {
return AnimatedOpacity(
opacity: _isVisible ? 1.0 : 0.0,
duration: const Duration(milliseconds: 50),
child: Container(
width: 20.0,
height: 20.0,
color: Colors.red,
),
);
}
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
Uint8List randomBuffer(int length) {
final random = Random.secure();
final buffer = Uint8List(length);
for (var i = 0; i < length; ++i) {
buffer[i] = random.nextInt(256);
}
return buffer;
}
String randomString(int length) {
const chars = "abcdefghijklmnopqrstuvwxyz";
var random = Random();
return List.generate(length, (_) => chars[random.nextInt(chars.length)])
.join();
}
String getRegistrationJwt() {
// Create a json web token
final JWT jwt = JWT({
"iss": testCredentials["jwt_shared_secret_id"]!,
"jti": base64.encode(randomBuffer(16)),
"iat": DateTime.now().millisecondsSinceEpoch ~/ 1000,
"scopes": [3],
"join_team": true
});
return jwt.sign(SecretKey(testCredentials["jwt_shared_secret"]!),
algorithm: JWTAlgorithm.HS256);
}
String getConnectorJwt(customUserId) {
// Create a json web token
final JWT jwt = JWT({
"iss": testCredentials["jwt_shared_secret_id"]!,
"jti": base64.encode(randomBuffer(16)),
"iat": DateTime.now().millisecondsSinceEpoch ~/ 1000,
"scopes": [4],
"connector_add": {
"type": "AP",
"value": "$customUserId@${testCredentials["app_id"]}"
}
});
return jwt.sign(SecretKey(testCredentials["jwt_shared_secret"]!),
algorithm: JWTAlgorithm.HS256);
}
void assertEqual(actual, expected) {
if (actual != expected) {
print('Assert fail: expected $expected, got $actual');
throw AssertionError('Assertion failed');
}
}
void assertListEquals(actual, expected) {
if (!listEquals(actual, expected)) {
print('Assert fail: expected $expected, got $actual');
throw AssertionError('Assertion failed');
}
}
void assertNotEqual(actual, expected) {
if (actual == expected) {
print('Assert fail: expected to be not equal to $expected, got $actual');
throw AssertionError('Assertion failed');
}
}
typedef ErrorAssertion = void Function(Object err);
void assertThrows(Function func, ErrorAssertion assertion) {
try {
func();
} catch (err) {
assertion(err);
return; // Got expected error
}
print('Assert fail: expected function to throw, but succeeded');
throw AssertionError('Assertion failed');
}
Future<void> assertThrowsAsync(
Future<void> Function() func, ErrorAssertion assertion) async {
try {
await func();
} catch (err) {
assertion(err);
return; // Got expected error
}
print('Assert fail: expected async function to throw, but succeeded');
throw AssertionError('Assertion failed');
}
Future<bool> testSealdSdk() async {
print('Starting SDK tests...');
try {
// The SealdSDK uses a local database. This database should be written to a permanent directory.
// For Flutter apps, the recommended path is the one returned by `path_provider.getApplicationDocumentsDirectory();
final Directory tmpDir =
await path_provider.getApplicationDocumentsDirectory();
final Directory dbDir = Directory(path.join(tmpDir.path, 'seald-test-db'));
// The Seald SDK uses a local database that will persist on disk.
// When instantiating a SealdSDK, it is highly recommended to set a symmetric key to encrypt this database.
// In an actual app, it should be generated at signup,
// either on the server and retrieved from your backend at login,
// or on the client-side directly and stored in the system's keychain.
// WARNING: This should be a cryptographically random buffer of 64 bytes. This random generation is NOT good enough.
Uint8List databaseEncryptionKey = randomBuffer(64);
// This demo expects a clean database path to create it's own data, so we need to clean what previous runs left.
// In a real app, it should never be done.
if (dbDir.existsSync()) {
dbDir.deleteSync(recursive: true);
}
// Seald uses JWT to manage licenses and identity.
// JWTs should be generated by your backend, and sent to the user at signup.
// The JWT secretId and secret can be generated from your administration dashboard.
// They should NEVER be on client side.
// However, as this is a demo without a backend, we will use them on the frontend.
// JWT documentation: https://docs.seald.io/en/sdk/guides/jwt.html
// identity documentation: https://docs.seald.io/en/sdk/guides/4-identities.html
// let's instantiate 3 SealdSDK. They will correspond to 3 users that will exchange messages.
final SealdSdk sdk1 = SealdSdk(
apiURL: testCredentials["api_url"]!,
appId: testCredentials["app_id"]!,
databasePath: Directory(path.join(dbDir.path, 'sdk1')).path,
databaseEncryptionKey: databaseEncryptionKey,
logLevel: -1,
instanceName: "Dart-Instance-1",
);
final SealdSdk sdk2 = SealdSdk(
apiURL: testCredentials["api_url"]!,
appId: testCredentials["app_id"]!,
databasePath: Directory(path.join(dbDir.path, 'sdk2')).path,
databaseEncryptionKey: databaseEncryptionKey,
logLevel: -1,
instanceName: "Dart-Instance-2",
);
final SealdSdk sdk3 = SealdSdk(
apiURL: testCredentials["api_url"]!,
appId: testCredentials["app_id"]!,
databasePath: Directory(path.join(dbDir.path, 'sdk3')).path,
databaseEncryptionKey: databaseEncryptionKey,
logLevel: -1,
instanceName: "Dart-Instance-3",
);
// retrieve info about current user before creating a user should return null
final SealdAccountInfo? retrieveNoAccount = sdk1.getCurrentAccountInfo();
assertEqual(retrieveNoAccount, null);
// Create the 3 accounts. Again, the signupJWT should be generated by your backend
final SealdAccountInfo user1AccountInfo = await sdk1.createAccountAsync(
getRegistrationJwt(),
displayName: "Dart-demo-user-1",
deviceName: "Dart-demo-device-1");
final SealdAccountInfo user2AccountInfo = await sdk2.createAccountAsync(
getRegistrationJwt(),
displayName: "Dart-demo-user-2",
deviceName: "Dart-demo-device-2");
final SealdAccountInfo user3AccountInfo = await sdk3.createAccountAsync(
getRegistrationJwt(),
displayName: "Dart-demo-user-3",
deviceName: "Dart-demo-device-3");
// retrieve info about current user after creating a user should return account info:
final SealdAccountInfo? retrieveAccountInfo = sdk1.getCurrentAccountInfo();
assertNotEqual(retrieveAccountInfo, null);
assertEqual(retrieveAccountInfo?.userId, user1AccountInfo.userId);
assertEqual(retrieveAccountInfo?.deviceId, user1AccountInfo.deviceId);
assertNotEqual(retrieveAccountInfo?.deviceExpires, 0);
assertEqual(
retrieveAccountInfo?.deviceExpires, user1AccountInfo.deviceExpires);
// Create group: https://docs.seald.io/sdk/guides/5-groups.html
final String groupId = await sdk1.createGroupAsync(
groupName: "group-1",
members: [user1AccountInfo.userId],
admins: [user1AccountInfo.userId]);
// Manage group members and admins
// Add user2 as group member
await sdk1
.addGroupMembersAsync(groupId, membersToAdd: [user2AccountInfo.userId]);
// user1 add user3 as group member and group admin
await sdk1.addGroupMembersAsync(groupId,
membersToAdd: [user3AccountInfo.userId],
adminsToSet: [user3AccountInfo.userId]);
// user3 can remove user2
await sdk3.removeGroupMembersAsync(groupId,
membersToRemove: [user2AccountInfo.userId]);
// user3 can remove user1 from admins
await sdk3.setGroupAdminsAsync(groupId,
addToAdmins: [], removeFromAdmins: [user1AccountInfo.userId]);
// Create encryption session: https://docs.seald.io/sdk/guides/6-encryption-sessions.html
// user1, user2, and group as recipients
// Default rights for the session creator (if included as recipients without RecipientRights) read = true, forward = true, revoke = true
// Default rights for any other recipient: read = true, forward = true, revoke = false
final List<SealdRecipientWithRights> recipientsES1 = [
SealdRecipientWithRights(
id: user1AccountInfo.userId,
),
SealdRecipientWithRights(
id: user2AccountInfo.userId,
),
SealdRecipientWithRights(
id: groupId,
)
];
final SealdEncryptionSession es1SDK1 =
await sdk1.createEncryptionSessionAsync(recipientsES1,
useCache: false); // user1, user2, and group as recipients
assertEqual(es1SDK1.retrievalDetails.flow,
SealdEncryptionSessionRetrievalFlow.created);
// Using two-man-rule accesses
// Add TMR accesses to the session, then, retrieve the session using it.
// Create TMR a recipient
String authFactorType = "EM";
String authFactorValue = "tmr-em-flutter-${randomString(5)}@test.com";
// WARNING: This should be a cryptographically random buffer of 64 bytes. This random generation is NOT good enough.
Uint8List overEncryptionKey = randomBuffer(64);
// Add the TMR access
final String addedTMRId =
await es1SDK1.addTmrAccessAsync(SealdTmrRecipientWithRights(
type: authFactorType,
value: authFactorValue,
overEncryptionKey: overEncryptionKey,
));
assertEqual(addedTMRId.length, 36);
// Retrieve the TMR JWT
SealdSsksTMRPlugin ssksPlugin = SealdSsksTMRPlugin(
ssksURL: testCredentials["ssks_url"]!,
appId: testCredentials["app_id"]!,
logLevel: -1,
instanceName: "TMRPlugin1",
);
SsksBackend yourCompanyDummyBackend = SsksBackend(
testCredentials["ssks_url"]!,
testCredentials["app_id"]!,
testCredentials["ssks_backend_app_key"]!);
// The app backend creates an SSKS authentication session.
// This is the first time that this email is authenticating onto SSKS, so `mustAuthenticate` would be false, but we force auth because we want to convert TMR accesses.
ChallengeSendResponse authSession = await yourCompanyDummyBackend.challengeSend(
authFactorValue, authFactorType, authFactorValue, true, true,
fakeOtp:
true // `fakeOtp` is only on the staging server, to force the challenge to be 'aaaaaaaa'. In production, you cannot use this.
);
assertEqual(authSession.mustAuthenticate, true);
// Retrieve a JWT associated with the authentication factor from SSKS
SealdSsksTMRPluginGetFactorTokenResponse tmrJWT =
await ssksPlugin.getAuthFactorTokenAsync(
authSession.sessionId, authFactorType, authFactorValue,
challenge: testCredentials["ssks_tmr_challenge"]!);
// Retrieve the encryption session using the JWT
final SealdEncryptionSession tmrES =
await sdk2.retrieveEncryptionSessionByTmrAsync(
tmrJWT.token, es1SDK1.id, overEncryptionKey);
assertEqual(tmrES.retrievalDetails.flow,
SealdEncryptionSessionRetrievalFlow.viaTmrAccess);
// Convert the TMR accesses
SealdConvertTmrAccessesResult conversionResult =
await sdk2.convertTmrAccessesAsync(tmrJWT.token, overEncryptionKey);
assertEqual(conversionResult.status, "ok");
assertEqual(conversionResult.converted.length, 1);
// After conversion, sdk2 can retrieve the encryption session directly.
final SealdEncryptionSession classicES =
await sdk2.retrieveEncryptionSessionAsync(
sessionId: es1SDK1.id,
useCache: false,
lookupProxyKey: false,
lookupGroupKey: false);
assertEqual(classicES.retrievalDetails.flow,
SealdEncryptionSessionRetrievalFlow.direct);
// Using proxy sessions: https://docs.seald.io/sdk/guides/proxy-sessions.html
// Create proxy sessions: user1 needs to be a recipient of this session in order to be able to add it as a proxy session
final SealdEncryptionSession proxySession1 =
await sdk1.createEncryptionSessionAsync([
SealdRecipientWithRights(
id: user1AccountInfo.userId,
),
SealdRecipientWithRights(
id: user3AccountInfo.userId,
)
]);
await es1SDK1.addProxySessionAsync(proxySession1.id);
// user1 needs to be a recipient of this session in order to be able to add it as a proxy session
final SealdEncryptionSession proxySession2 =
await sdk1.createEncryptionSessionAsync([
SealdRecipientWithRights(
id: user1AccountInfo.userId,
),
SealdRecipientWithRights(
id: user2AccountInfo.userId,
)
]);
await es1SDK1.addProxySessionAsync(proxySession2.id);
// The SealdEncryptionSession object can encrypt and decrypt for user1
const String initialString = "a message that needs to be encrypted!";
final String encryptedMessage =
await es1SDK1.encryptMessageAsync(initialString);
final String decryptedMessage =
await es1SDK1.decryptMessageAsync(encryptedMessage);
assertEqual(decryptedMessage, initialString);
// user1 can parse/retrieve the SealdEncryptionSession from the encrypted message
final String es1SDK1RetrieveFromMessId =
parseSessionId(message: encryptedMessage);
assertEqual(es1SDK1RetrieveFromMessId, es1SDK1.id);
final SealdEncryptionSession es1SDK1RetrieveFromMess =
await sdk1.retrieveEncryptionSessionAsync(
message: encryptedMessage, useCache: false);
assertEqual(es1SDK1RetrieveFromMess.id, es1SDK1.id);
assertEqual(es1SDK1RetrieveFromMess.retrievalDetails.flow,
SealdEncryptionSessionRetrievalFlow.direct);
final String decryptedMessageFromMess =
await es1SDK1RetrieveFromMess.decryptMessageAsync(encryptedMessage);
assertEqual(decryptedMessageFromMess, initialString);
// Encrypt/Decrypt file from bytes
final Uint8List clearFileBytes = randomBuffer(1024);
final Uint8List encryptedFileFromBytes =
await es1SDK1.encryptFileAsync(clearFileBytes, 'test.bin');
final SealdClearFile decryptedFileFromBytes =
await es1SDK1.decryptFileAsync(encryptedFileFromBytes);
assertListEquals(clearFileBytes, decryptedFileFromBytes.fileContent);
// Create a test file on disk that we will encrypt/decrypt
final Directory directory =
await path_provider.getApplicationDocumentsDirectory();
await removeAllFilesInDirectory(directory.path);
final String testFilePath = "${directory.path}/test.txt";
final File testFile = File(testFilePath);
await testFile.writeAsString(initialString);
// Encrypt the test file. Resulting file will be written alongside the source file, with `.seald` extension added
final String encryptedFilePath =
await es1SDK1.encryptFileFromPathAsync(testFilePath);
assertEqual(encryptedFilePath, "${directory.path}/test.txt.seald");
// user1 can retrieve the encryptionSession directly from the encrypted file
final String es1SDK1RetrieveFromFileId =
parseSessionId(message: encryptedMessage);
assertEqual(es1SDK1RetrieveFromFileId, es1SDK1.id);
final SealdEncryptionSession es1SDK1RetrieveFromFile =
await sdk1.retrieveEncryptionSessionAsync(
filePath: encryptedFilePath, useCache: false);
assertEqual(es1SDK1RetrieveFromFile.id, es1SDK1.id);
assertEqual(es1SDK1RetrieveFromFile.retrievalDetails.flow,
SealdEncryptionSessionRetrievalFlow.direct);
// user1 can retrieve the SealdEncryptionSession from the encrypted file
// The decrypted file will be named with the name it had at encryption. Any renaming of the encrypted file will be ignore.
// NOTE: In this example, the decrypted file will have `(1)` suffix to avoid overwriting the original clear file.
final String decryptedFilePath = await es1SDK1RetrieveFromFile
.decryptFileFromPathAsync(encryptedFilePath);
assertEqual(decryptedFilePath, "${directory.path}/test (1).txt");
final File decryptedFile = File(decryptedFilePath);
assertEqual(await decryptedFile.exists(), true);
final String decryptedFileAsString = await decryptedFile.readAsString();
assertEqual(decryptedFileAsString, initialString);
// user1 can parse/retrieve the SealdEncryptionSession from the encrypted file bytes
final File encryptedFile = File(encryptedFilePath);
final Uint8List encryptedFileBytes = await encryptedFile.readAsBytes();
final String es1SDK1RetrieveFromFileBytesId =
parseSessionId(message: encryptedMessage);
assertEqual(es1SDK1RetrieveFromFileBytesId, es1SDK1.id);
final SealdEncryptionSession es1SDK1RetrieveFromFileBytes =
await sdk1.retrieveEncryptionSessionAsync(
fileBytes: encryptedFileBytes, useCache: false);
assertEqual(es1SDK1RetrieveFromFileBytes.id, es1SDK1.id);
assertEqual(es1SDK1RetrieveFromFileBytes.retrievalDetails.flow,
SealdEncryptionSessionRetrievalFlow.direct);
final String decryptedMessageFromFileBytes =
await es1SDK1RetrieveFromFileBytes
.decryptMessageAsync(encryptedMessage);
assertEqual(decryptedMessageFromFileBytes, initialString);
// user2 can retrieve the SealdEncryptionSession from the session ID.
final SealdEncryptionSession es1SDK2 = await sdk2
.retrieveEncryptionSessionAsync(sessionId: es1SDK1.id, useCache: false);
assertEqual(es1SDK2.retrievalDetails.flow,
SealdEncryptionSessionRetrievalFlow.direct);
final String decryptedMessageSDK2 =
await es1SDK2.decryptMessageAsync(encryptedMessage);
assertEqual(decryptedMessageSDK2, initialString);
// user3 cannot retrieve the SealdEncryptionSession with lookupGroupKey set to false.
await assertThrowsAsync(
() async => sdk3.retrieveEncryptionSessionAsync(
message: encryptedMessage,
useCache: false,
lookupGroupKey: false), (Object err) {
final SealdException sealdErr = err as SealdException;
assertEqual(sealdErr.code, "NO_TOKEN_FOR_YOU");
assertEqual(sealdErr.id, "GOSDK_NO_TOKEN_FOR_YOU");
assertEqual(sealdErr.description, "Can't decipher this session");
});
// user3 can retrieve the SealdEncryptionSession from the encrypted message through the group.
final SealdEncryptionSession es1SDK3FromGroup =
await sdk3.retrieveEncryptionSessionAsync(
message: encryptedMessage, useCache: false);
assertEqual(es1SDK3FromGroup.retrievalDetails.flow,
SealdEncryptionSessionRetrievalFlow.viaGroup);
assertEqual(es1SDK3FromGroup.retrievalDetails.groupId, groupId);
final String decryptedMessageSDK3 =
await es1SDK3FromGroup.decryptMessageAsync(encryptedMessage);
assertEqual(decryptedMessageSDK3, initialString);
// user3 removes all members of "group-1". A group without member is deleted.
await sdk3.removeGroupMembersAsync(groupId,
membersToRemove: [user1AccountInfo.userId, user3AccountInfo.userId]);
// user3 could retrieve the previous encryption session only because "group-1" was set as recipient.
// As the group was deleted, it can no longer access it.
// user3 still has the encryption session in its cache, but we can disable it.
await assertThrowsAsync(
() async => sdk3.retrieveEncryptionSessionAsync(
message: encryptedMessage, useCache: false), (Object err) {
final SealdException sealdErr = err as SealdException;
assertEqual(sealdErr.code, "NO_TOKEN_FOR_YOU");
assertEqual(sealdErr.id, "GOSDK_NO_TOKEN_FOR_YOU");
assertEqual(sealdErr.description, "Can't decipher this session");
});
// user3 can still retrieve the session via proxy
final SealdEncryptionSession es1SDK3FromProxy =
await sdk3.retrieveEncryptionSessionAsync(
message: encryptedMessage, useCache: false, lookupProxyKey: true);
assertEqual(es1SDK3FromProxy.retrievalDetails.flow,
SealdEncryptionSessionRetrievalFlow.viaProxy);
assertEqual(
es1SDK3FromProxy.retrievalDetails.proxySessionId, proxySession1.id);
// user2 adds user3 as recipient of the encryption session.
final List<SealdRecipientWithRights> addRecipient3 = [
SealdRecipientWithRights(
id: user3AccountInfo.userId,
)
];
final Map<String, SealdActionStatus> asMapAdd =
await es1SDK2.addRecipientsAsync(addRecipient3);
assertEqual(asMapAdd.length, 1);
assertEqual(asMapAdd.containsKey(user3AccountInfo.deviceId),
true); // Note that addRecipient return DeviceId, not UserId
assertEqual(asMapAdd[user3AccountInfo.deviceId]!.success, true);
// user3 can now retrieve it without group or proxy.
final SealdEncryptionSession es1SDK3 =
await sdk3.retrieveEncryptionSessionAsync(
sessionId: es1SDK1.id,
useCache: false,
lookupProxyKey: false,
lookupGroupKey: false);
final String decryptedMessageAfterAdd =
await es1SDK3.decryptMessageAsync(encryptedMessage);
assertEqual(decryptedMessageAfterAdd, initialString);
// user1 revokes user3 and proxy1 from the encryption session.
final SealdRevokeResult resultRevoke = await es1SDK1.revokeRecipientsAsync(
recipientsIds: [user3AccountInfo.userId],
proxySessionsIds: [proxySession1.id]);
assertEqual(resultRevoke.recipients.length, 1);
assertEqual(
resultRevoke.recipients.containsKey(user3AccountInfo.userId), true);
assertEqual(
resultRevoke.recipients[user3AccountInfo.userId]!.success, true);
assertEqual(resultRevoke.proxySessions.length, 1);
assertEqual(resultRevoke.proxySessions.containsKey(proxySession1.id), true);
assertEqual(resultRevoke.proxySessions[proxySession1.id]!.success, true);
// user3 cannot retrieve the session anymore, even with proxy or group
await assertThrowsAsync(
() async => sdk3.retrieveEncryptionSessionAsync(
message: encryptedMessage,
useCache: false,
lookupGroupKey: true,
lookupProxyKey: true), (Object err) {
final SealdException sealdErr = err as SealdException;
assertEqual(sealdErr.code, "NO_TOKEN_FOR_YOU");
assertEqual(sealdErr.id, "GOSDK_NO_TOKEN_FOR_YOU");
assertEqual(sealdErr.description, "Can't decipher this session");
});
// user1 revokes all other recipients from the session
final SealdRevokeResult resultRevokeOthers = await es1SDK1
.revokeOthersAsync(); // revoke user2 + group (user3 is already revoked) + proxy2
assertEqual(resultRevokeOthers.recipients.length, 2);
assertEqual(
resultRevokeOthers.recipients.containsKey(user2AccountInfo.userId),
true);
assertEqual(
resultRevokeOthers.recipients[user2AccountInfo.userId]!.success, true);
assertEqual(resultRevokeOthers.recipients.containsKey(groupId), true);
assertEqual(resultRevokeOthers.recipients[groupId]!.success, true);
assertEqual(resultRevokeOthers.proxySessions.length, 1);
assertEqual(
resultRevokeOthers.proxySessions.containsKey(proxySession2.id), true);
assertEqual(
resultRevokeOthers.proxySessions[proxySession2.id]!.success, true);
// user2 cannot retrieve the session anymore
await assertThrowsAsync(
() async => sdk2.retrieveEncryptionSessionAsync(
message: encryptedMessage, useCache: false), (Object err) {
final SealdException sealdErr = err as SealdException;
assertEqual(sealdErr.code, "NO_TOKEN_FOR_YOU");
assertEqual(sealdErr.id, "GOSDK_NO_TOKEN_FOR_YOU");
assertEqual(sealdErr.description, "Can't decipher this session");
});
// user1 revokes all. It can no longer retrieve it.
final SealdRevokeResult revokeRevokeAll =
await es1SDK1.revokeAllAsync(); // only user1 is left
assertEqual(revokeRevokeAll.recipients.length, 1);
assertEqual(
revokeRevokeAll.recipients.containsKey(user1AccountInfo.userId), true);
assertEqual(
revokeRevokeAll.recipients[user1AccountInfo.userId]!.success, true);
assertEqual(revokeRevokeAll.proxySessions.length, 0);
// user1 cannot retrieve anymore
await assertThrowsAsync(
() async => sdk1.retrieveEncryptionSessionAsync(
message: encryptedMessage, useCache: false), (Object err) {
final SealdException sealdErr = err as SealdException;
assertEqual(sealdErr.code, "NO_TOKEN_FOR_YOU");
assertEqual(sealdErr.id, "GOSDK_NO_TOKEN_FOR_YOU");
assertEqual(sealdErr.description, "Can't decipher this session");
});
// Create additional data for user1
final List<SealdRecipientWithRights> recipientsES234 = [
SealdRecipientWithRights(
id: user1AccountInfo.userId,
)
];
final SealdEncryptionSession es2SDK1 = await sdk1
.createEncryptionSessionAsync(recipientsES234, useCache: false);
const String anotherMessage = "nobody should read that!";
final String secondEncryptedMessage =
await es2SDK1.encryptMessageAsync(anotherMessage);
final SealdEncryptionSession es3SDK1 = await sdk1
.createEncryptionSessionAsync(recipientsES234, useCache: false);
final SealdEncryptionSession es4SDK1 = await sdk1
.createEncryptionSessionAsync(recipientsES234, useCache: false);
// user1 can retrieveMultiple
final List<SealdEncryptionSession> encryptionSessions = await sdk1
.retrieveMultipleEncryptionSessionsAsync(
[es2SDK1.id, es3SDK1.id, es4SDK1.id],
useCache: false);
assertEqual(encryptionSessions.length, 3);
assertEqual(encryptionSessions[0].id, es2SDK1.id);
assertEqual(encryptionSessions[1].id, es3SDK1.id);
assertEqual(encryptionSessions[2].id, es4SDK1.id);
// user1 can renew its key, and still decrypt old messages
final preparedRenewal = await sdk1.prepareRenewAsync();
// `preparedRenewal` Can be stored on SSKS as a new identity. That way, a backup will be available is the renewKeys fail.
await sdk1.renewKeysAsync(preparedRenewal: preparedRenewal);
final SealdEncryptionSession es2SDK1AfterRenew = await sdk1
.retrieveEncryptionSessionAsync(sessionId: es2SDK1.id, useCache: false);
final String decryptedMessageAfterRenew =
await es2SDK1AfterRenew.decryptMessageAsync(secondEncryptedMessage);
assertEqual(decryptedMessageAfterRenew, anotherMessage);
// CONNECTORS https://docs.seald.io/en/sdk/guides/jwt.html#adding-a-userid
// we can add a custom userId using a JWT
const String customConnectorJWTValue = "user1-custom-id";
await sdk1.pushJWTAsync(getConnectorJwt(customConnectorJWTValue));
final List<SealdConnector> connectors = await sdk1.listConnectorsAsync();
assertEqual(connectors.length, 1);
assertEqual(connectors[0].state, "VO");
assertEqual(connectors[0].type, "AP");
assertEqual(connectors[0].sealdId, user1AccountInfo.userId);
assertEqual(connectors[0].value,
"$customConnectorJWTValue@${testCredentials['app_id']}");
// Retrieve connector by its id
final SealdConnector retrieveConnector =
await sdk1.retrieveConnectorAsync(connectors[0].id);
assertEqual(retrieveConnector.sealdId, user1AccountInfo.userId);
assertEqual(retrieveConnector.state, "VO");
assertEqual(retrieveConnector.type, "AP");
assertEqual(retrieveConnector.value,
"$customConnectorJWTValue@${testCredentials['app_id']}");
// Retrieve connectors from a user id.
final List<SealdConnector> connectorsFromSealdId =
await sdk1.getConnectorsFromSealdIdAsync(user1AccountInfo.userId);
assertEqual(connectorsFromSealdId.length, 1);
assertEqual(connectorsFromSealdId[0].state, "VO");
assertEqual(connectorsFromSealdId[0].type, "AP");
assertEqual(connectorsFromSealdId[0].sealdId, user1AccountInfo.userId);
assertEqual(connectorsFromSealdId[0].value,
"$customConnectorJWTValue@${testCredentials['app_id']}");
// Get sealdId of a user from a connector
final List<String> sealdIds = await sdk2.getSealdIdsFromConnectorsAsync([
SealdConnectorTypeValue(
type: "AP",
value: "$customConnectorJWTValue@${testCredentials['app_id']}")
]);
assertEqual(sealdIds.length, 1);
assertEqual(sealdIds[0], user1AccountInfo.userId);
// user1 can remove a connector
await sdk1.removeConnectorAsync(connectors[0].id);
// verify that no connector left
final List<SealdConnector> connectorListAfterRevoke =
await sdk1.listConnectorsAsync();
assertEqual(connectorListAfterRevoke.length, 0);
// user1 can export its identity
final Uint8List exportIdentity = sdk1.exportIdentity();
// We can instantiate a new SealdSDK, import the exported identity
final SealdSdk sdk1Exported = SealdSdk(
apiURL: testCredentials["api_url"]!,
appId: testCredentials["app_id"]!,
databasePath: Directory(path.join(dbDir.path, 'sdk1exported')).path,
databaseEncryptionKey: databaseEncryptionKey,
logLevel: -1,
instanceName: "Dart1Exported",
);
await sdk1Exported.importIdentityAsync(exportIdentity);
// SDK with imported identity can decrypt
final SealdEncryptionSession es2SDK1Exported =
await sdk1Exported.retrieveEncryptionSessionAsync(
message: secondEncryptedMessage, useCache: false);
final String clearMessageExportedIdentity =
await es2SDK1Exported.decryptMessageAsync(secondEncryptedMessage);
assertEqual(clearMessageExportedIdentity, anotherMessage);
sdk1Exported.close();
// user1 can create sub identity
final SealdCreateSubIdentityResponse subIdentity =
await sdk1.createSubIdentityAsync(deviceName: "SUB-deviceName");
assertNotEqual(subIdentity.deviceId, "");
// first device needs to reencrypt for the new device
await sdk1.massReencryptAsync(subIdentity.deviceId);
// We can instantiate a new SealdSDK, import the sub-device identity
final SealdSdk sdk1SubDevice = SealdSdk(
apiURL: testCredentials["api_url"]!,
appId: testCredentials["app_id"]!,
databasePath: Directory(path.join(dbDir.path, 'sdk1SubDevice')).path,
databaseEncryptionKey: databaseEncryptionKey,
logLevel: -1,
instanceName: "sdk1SubDevice",
);
await sdk1SubDevice.importIdentityAsync(subIdentity.backupKey);
// sub device can decrypt
final SealdEncryptionSession es2SDK1SubDevice =
await sdk1SubDevice.retrieveEncryptionSessionAsync(
message: secondEncryptedMessage, useCache: false);
final String clearMessageSubdIdentity =
await es2SDK1SubDevice.decryptMessageAsync(secondEncryptedMessage);
assertEqual(clearMessageSubdIdentity, anotherMessage);
sdk1SubDevice.close();
// Get and Check sigchain hash
final SealdGetSigchainResponse user1LastSigchainHash =
await sdk1.getSigchainHashAsync(user1AccountInfo.userId);
assertEqual(user1LastSigchainHash.position, 2);
final SealdGetSigchainResponse user1FirstSigchainHash =
await sdk2.getSigchainHashAsync(user1AccountInfo.userId, position: 0);
assertEqual(user1FirstSigchainHash.position, 0);
final SealdCheckSigchainResponse lastHashCheck =
await sdk2.checkSigchainHashAsync(
user1AccountInfo.userId, user1LastSigchainHash.hash);
assertEqual(lastHashCheck.found, true);
assertEqual(lastHashCheck.position, 2);
assertEqual(lastHashCheck.lastPosition, 2);
final SealdCheckSigchainResponse firstHashCheck =
await sdk1.checkSigchainHashAsync(
user1AccountInfo.userId, user1FirstSigchainHash.hash);
assertEqual(firstHashCheck.found, true);
assertEqual(firstHashCheck.position, 0);
assertEqual(firstHashCheck.lastPosition, 2);
final SealdCheckSigchainResponse badPositionCheck =
await sdk2.checkSigchainHashAsync(
user1AccountInfo.userId, user1FirstSigchainHash.hash,
position: 1);
assertEqual(badPositionCheck.found, false);
// For badPositionCheck, position cannot be asserted as it is not set when the hash is not found.
assertEqual(badPositionCheck.lastPosition, 2);
// Group TMR temporary keys
// First, create a group to test on. sdk1 create a TMR temporary key to this group, sdk2 will join.
final String groupTMRId = await sdk1.createGroupAsync(
groupName: "group-TMR-1",
members: [user1AccountInfo.userId],
admins: [user1AccountInfo.userId]);
// WARNING: This should be a cryptographically random buffer of 64 bytes. This random generation is NOT good enough.
Uint8List gTMRRawOverEncryptionKey = randomBuffer(64);
// We defined a two man rule recipient earlier. We will use it again.
// The authentication factor is defined by `authFactorType` and `authFactorValue`.
// Also we already have the TMR JWT associated with it: `tmrJWT.token`
final SealdGroupTMRTemporaryKey gTMRTKCreated =
await sdk1.createGroupTMRTemporaryKeyAsync(groupTMRId, authFactorType,
authFactorValue, gTMRRawOverEncryptionKey);
final SealdListedGroupTMRTemporaryKey gTMRTKListed =
await sdk1.listGroupTMRTemporaryKeysAsync(groupTMRId);
assertEqual(gTMRTKListed.nbPage, 1);
assertEqual(gTMRTKListed.keys[0].id, gTMRTKCreated.id);
final SealdListedGroupTMRTemporaryKey gTMRTKSearched =
await sdk2.searchGroupTMRTemporaryKeysAsync(tmrJWT.token);
assertEqual(gTMRTKSearched.nbPage, 1);
assertEqual(gTMRTKSearched.keys[0].id, gTMRTKCreated.id);
await sdk2.convertGroupTMRTemporaryKeyAsync(
groupTMRId, gTMRTKCreated.id, tmrJWT.token, gTMRRawOverEncryptionKey);
await sdk1.deleteGroupTMRTemporaryKeyAsync(groupTMRId, gTMRTKCreated.id);
// Heartbeat can be used to check if proxies and firewalls are configured properly so that the app can reach Seald's servers.
await sdk1.heartbeatAsync();
sdk1.close();
sdk2.close();
sdk3.close();
print('SDK tests success!');
return true;
} catch (err, stack) {
print('SDK tests failed');
print(err);
print(stack);
return false;
}
}
Future<bool> testSealdSsksPassword() async {
print('Starting testSealdSsksPassword tests...');
try {
// Simulating a Seald identity with random data, for a simpler example.
Uint8List dummyIdentity =
randomBuffer(64); // should be: sdk.exportIdentity()
SealdSsksPasswordPlugin ssksPlugin = SealdSsksPasswordPlugin(
ssksURL: testCredentials["ssks_url"]!,
appId: testCredentials["app_id"]!,
logLevel: -1,
instanceName: "PasswordPlugin",
);
// Test with standard password
String userIdPassword = "user-${randomString(11)}";
String userPassword = randomString(12);
// Saving the identity with a password
String ssksId1 = await ssksPlugin.saveIdentityFromPasswordAsync(
userIdPassword, userPassword, dummyIdentity);
assertNotEqual(ssksId1, "");
// Retrieving the identity with the password
Uint8List retrievedIdentityPassword = await ssksPlugin
.retrieveIdentityFromPasswordAsync(userIdPassword, userPassword);
assertListEquals(retrievedIdentityPassword, dummyIdentity);
// Changing the password
String newPassword = "newPassword";
String ssksId1b = await ssksPlugin.changeIdentityPasswordAsync(
userIdPassword, userPassword, newPassword);
assertNotEqual(ssksId1b, ssksId1);
// The previous password does not work anymore
await assertThrowsAsync(
() async => ssksPlugin.retrieveIdentityFromPasswordAsync(
userIdPassword, userPassword), (Object err) {
final SealdException sealdErr = err as SealdException;
assertEqual(sealdErr.code, "SSKSPASSWORD_CANNOT_FIND_IDENTITY");
});
// Retrieving with the new password works
Uint8List retrievedIdentityNewPassword = await ssksPlugin
.retrieveIdentityFromPasswordAsync(userIdPassword, newPassword);
assertListEquals(retrievedIdentityNewPassword, dummyIdentity);
// Test with raw keys
String userIdRawKeys = "user-${randomString(11)}";
String rawStorageKey = randomString(32);
Uint8List rawEncryptionKey = randomBuffer(64);
// Saving identity with raw keys
String ssksId2 = await ssksPlugin.saveIdentityFromRawKeysAsync(
userIdRawKeys, rawStorageKey, rawEncryptionKey, dummyIdentity);
assertNotEqual(ssksId2, "");
// Retrieving the identity with raw keys
Uint8List retrievedIdentityRawKeys =
await ssksPlugin.retrieveIdentityFromRawKeysAsync(
userIdRawKeys, rawStorageKey, rawEncryptionKey);
assertListEquals(retrievedIdentityRawKeys, dummyIdentity);
// Deleting the identity by saving an empty `Data`
String ssksId2b = ssksPlugin.saveIdentityFromRawKeys(
userIdRawKeys, rawStorageKey, rawEncryptionKey, Uint8List(0));
assertEqual(ssksId2b, ssksId2);
// After deleting the identity, cannot retrieve anymore
await assertThrowsAsync(
() async => ssksPlugin.retrieveIdentityFromRawKeysAsync(
userIdRawKeys, rawStorageKey, rawEncryptionKey), (Object err) {
final SealdException sealdErr = err as SealdException;
assertEqual(sealdErr.code, "SSKSPASSWORD_CANNOT_FIND_IDENTITY");
});
print('SsksPassword tests success!');
return true;
} catch (err, stack) {
print('SsksPassword tests failed');
print(err);
print(stack);
return false;
}
}
Future<bool> testSealdSsksTMR() async {
print('Starting testSealdSsksTMR tests...');
try {
// rawTMRSymKey is a secret, generated and stored by your _backend_, unique for the user.
// It can be retrieved by client-side when authenticated (usually as part of signup/sign-in call response).
// This *MUST* be a cryptographically random Uint8List of 64 bytes.
Uint8List rawTMRSymKey = randomBuffer(64);
SsksBackend yourCompanyDummyBackend = SsksBackend(
testCredentials["ssks_url"]!,
testCredentials["app_id"]!,
testCredentials["ssks_backend_app_key"]!);
// First, we need to simulate a user. For a simpler example, we will use random data.
// userId is the ID of the user in your app.
String userId = "user-${randomString(11)}";
// userIdentity is the user's exported identity that you want to store on SSKS
Uint8List dummyIdentity =
randomBuffer(64); // should be: sdk.exportIdentity()
SealdSsksTMRPlugin ssksPlugin = SealdSsksTMRPlugin(
ssksURL: testCredentials["ssks_url"]!,
appId: testCredentials["app_id"]!,
logLevel: -1,
instanceName: "TMRPlugin1",
);
// Define an authentication factor: the user's email address.
// An authentication factor type can be an email `EM` or a phone number `SMS`
String userEM = "email-${randomString(15)}@test.com";
// The app backend creates an SSKS authentication session to save the identity.
// This is the first time that this email is storing an identity, so `must_authenticate` is false.
ChallengeSendResponse authSessionSave = await yourCompanyDummyBackend
.challengeSend(userId, "EM", userEM, true, false,
fakeOtp:
true // `fakeOtp` is only on the staging server, to force the challenge to be 'aaaaaaaa'. In production, you cannot use this.
);
assertEqual(authSessionSave.mustAuthenticate, false);
// Saving the identity. No challenge necessary because `must_authenticate` is false.
SealdSsksTMRPluginSaveIdentityResponse saveIdentityRes1 =
await ssksPlugin.saveIdentityAsync(authSessionSave.sessionId, "EM",
userEM, rawTMRSymKey, dummyIdentity);
assertNotEqual(saveIdentityRes1.ssksId, "");
assertEqual(saveIdentityRes1.authenticatedSessionId, null);
// The app backend creates another session to retrieve the identity.
// The identity is already saved, so `must_authenticate` is true.
ChallengeSendResponse authSessionRetrieve = await yourCompanyDummyBackend
.challengeSend(userId, "EM", userEM, true, false,
fakeOtp:
true // `fakeOtp` is only on the staging server, to force the challenge to be 'aaaaaaaa'. In production, you cannot use this.
);
assertEqual(authSessionRetrieve.mustAuthenticate, true);
SealdSsksTMRPluginRetrieveIdentityResponse retrieveNotAuth =
await ssksPlugin.retrieveIdentityAsync(
authSessionRetrieve.sessionId, "EM", userEM, rawTMRSymKey,
challenge: testCredentials["ssks_tmr_challenge"]!);
assertEqual(retrieveNotAuth.shouldRenewKey, true);
assertListEquals(retrieveNotAuth.identity, dummyIdentity);
// If initial key has been saved without being fully authenticated, you should renew the user's private key, and save it again.
// await sdk1.renewKeysAsync();
// Let's simulate the renew with another random identity
Uint8List identitySecondKey = randomBuffer(64);
// to save the newly renewed identityon the server, you can use the `authenticatedSessionId` from the response to `retrieveIdentityAsync`, with no challenge
SealdSsksTMRPluginSaveIdentityResponse saveIdentityRes2 =
await ssksPlugin.saveIdentityAsync(
retrieveNotAuth.authenticatedSessionId,
"EM",
userEM,
rawTMRSymKey,
identitySecondKey);
assertEqual(saveIdentityRes2.ssksId, saveIdentityRes1.ssksId);
assertEqual(saveIdentityRes2.authenticatedSessionId, null);
// And now let's retrieve this new saved identity
ChallengeSendResponse authSessionRetrieve2 = await yourCompanyDummyBackend
.challengeSend(userId, "EM", userEM, false, false,
fakeOtp:
true // `fakeOtp` is only on the staging server, to force the challenge to be 'aaaaaaaa'. In production, you cannot use this.
);