sea_old.js
35.5 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
/* global Gun,describe,expect,it,beforeEach */
/*eslint max-len: ["error", 95, { "ignoreComments": true }]*/
/*eslint semi: ["error", "always", { "omitLastInOneLineBlock": true}]*/
/*eslint object-curly-spacing: ["error", "never"]*/
/*eslint node/no-deprecated-api: [error, {ignoreModuleItems: ["new buffer.Buffer()"]}] */
var root;
(function(env){
root = env.window ? env.window : global;
env.window && root.localStorage && root.localStorage.clear();
try{ require('fs').unlinkSync('data.json') }catch(e){}
//root.Gun = root.Gun || require('../gun');
if(root.Gun){
root.Gun = root.Gun;
} else {
var expect = global.expect = require("./expect");
root.Gun = require('../gun');
Gun.serve = require('../lib/serve');
//require('./s3');
//require('./uws');
//require('./wsp/server');
require('../lib/file');
require('../sea');
}
}(this));
;(function(){
const SEA = Gun.SEA
if(!SEA){ return }
const { Buffer, EasyIndexedDB } = SEA
const seaIndexedDb = new SEA.EasyIndexedDB('SEA', 'GunDB', 1)
const checkIndexedDB = (key, prop, resolve_) => {
const doIt = (resolve, reject) => seaIndexedDb.get(key, prop)
.then(resolve).catch(reject)
if (resolve_) {
doIt(resolve_, (e) => { throw e })
} else {
return new Promise(doIt)
}
}
const setIndexedDB = (key, auth, resolve_) => {
const doIt = (resolve, reject) => seaIndexedDb.put(key, { auth })
.then(resolve).catch(reject)
if (resolve_) {
doIt(resolve_, (e) => { throw e })
} else {
return new Promise(doIt)
}
}
SEA && describe('SEA', function(){
console.log('TODO: SEA! THIS IS AN EARLY ALPHA!!!');
var alias = 'dude';
var pass = 'my secret password';
var userKeys = ['pub', 'priv'];
var clearText = 'My precious secret!';
var encKeys = ['ct', 'iv', 's'];
const type = 'Promise' // TODO: this is leftover...
it('proof', function(done){
var check = function(proof){
expect(proof).to.not.be(undefined);
expect(proof).to.not.be('');
done();
};
// proof - generates PBKDF2 hash from user's alias and password
// which is then used to decrypt user's auth record
SEA.proof(pass, Gun.text.random(64)).then(check).catch(done);
});
it('pair', function(done){
var check = function(key){
expect(key).to.not.be(undefined);
expect(key).to.not.be('');
expect(key).to.have.keys(userKeys);
userKeys.map(function(fld){
expect(key[fld]).to.not.be(undefined);
expect(key[fld]).to.not.be('');
});
done();
};
// pair - generates ECDH key pair (for new user when created)
SEA.pair().then(check).catch(done);
});
it('keyid', function(done){
SEA.pair().then(function(key){
var check = function(keyid){
expect(keyid).to.not.be(undefined);
expect(keyid).to.not.be('');
expect(keyid.length).to.eql(16);
done();
};
// keyid - creates 8 byte KeyID from public key
SEA.keyid(key.pub).then(check);
}).catch(function(e){done(e)});
});
it('enc', function(done){
SEA.pair().then(function(key){
var check = function(jsonSecret){
expect(jsonSecret).to.not.be(undefined);
expect(jsonSecret).to.not.be('');
expect(jsonSecret).to.not.eql(clearText);
var objSecret = JSON.parse(jsonSecret);
expect(objSecret).to.have.keys(encKeys);
encKeys.map(function(key){
expect(objSecret[key]).to.not.be(undefined);
expect(objSecret[key]).to.not.be('');
});
done();
};
// en - encrypts JSON data using user's private or derived ECDH key
SEA.enc(clearText, key.priv).then(check);
}).catch(function(e){done(e)});
});
it('sign', function(done){
SEA.pair().then(function(key){
var check = function(signature){
expect(signature).to.not.be(undefined);
expect(signature).to.not.be('');
expect(signature).to.not.eql(key.pub);
done();
};
// sign - calculates signature for data using user's private ECDH key
SEA.sign(key.pub, key).then(check);
}).catch(function(e){done(e)});
});
it('verify', function(done){
SEA.pair().then(function(key){
var check = function(ok){
expect(ok).to.not.be(undefined);
expect(ok).to.not.be('');
expect(ok).to.be(true);
done();
};
// sign - calculates signature for data using user's private ECDH key
SEA.sign(key.pub, key).then(function(signature){
SEA.verify(key.pub, key.pub, signature).then(check);
});
}).catch(function(e){done(e)});
});
it('dec', function(done){
SEA.pair().then(function(key){
var check = function(decText){
expect(decText).to.not.be(undefined);
expect(decText).to.not.be('');
expect(decText).to.be.eql(clearText);
done();
};
SEA.enc(clearText, key.priv).then(function(jsonSecret){
// de - decrypts JSON data using user's private or derived ECDH key
SEA.dec(jsonSecret, key.priv).then(check);
});
}).catch(function(e){done(e)});
});
it('derive', function(done){
SEA.pair().then(function(txKey){
return SEA.pair().then(function(rxKey){
return {tx: txKey, rx: rxKey};
});
}).then(function(keys){
var check = function(shared){
expect(shared).to.not.be(undefined);
expect(shared).to.not.be('');
[keys.rx.pub, keys.rx.priv, keys.tx.pub, keys.tx.priv]
.map(function(val){
expect(shared).to.not.eql(val);
});
done();
};
// derive - provides shared secret for both receiver and sender
// which can be used to encrypt or sign data
SEA.derive(keys.rx.pub, keys.tx).then(check);
}).catch(function(e){done(e)});
});
it('write', function(done){
SEA.pair().then(function(key){
SEA.sign(key.pub, key).then(function(signature){
var check = function(result){
var parts;
try{
expect(result).to.not.be(undefined);
expect(result).to.not.be('');
expect(result.slice(0, 4)).to.eql('SEA[');
parts = JSON.parse(result.slice(3));
expect(parts).to.not.be(undefined);
expect(parts[0]).to.be.eql(key.pub);
// expect(parts[1]).to.be.eql(signature);
}catch(e){ return done(e) }
SEA.verify(key.pub, key.pub, parts[1]).then(function(flag){
expect(flag).to.be.true;
done();
});
};
// write - wraps data to 'SEA["data","signature"]'
SEA.write(key.pub, key).then(check);
});
}).catch(function(e){done(e)});
});
it('read', function(done){
SEA.pair().then(function(key){
var check = function(result){
expect(result).to.not.be(undefined);
expect(result).to.not.be('');
expect(result).to.be.equal(key.pub);
done();
};
SEA.sign(key.pub, key).then(function(signature){
SEA.write(key.pub, key).then(function(signed){
// read - unwraps data from 'SEA["data","signature"]'
SEA.read(signed, key.pub).then(check);
});
});
}).catch(function(e){done(e)});
});
});
Gun().user && describe('Gun', function(){
describe('User', function(){
console.log('TODO: User! THIS IS AN EARLY ALPHA!!!');
var alias = 'dude';
var pass = 'my secret password';
var gun = Gun();
var user = gun.user();
Gun.log.off = true; // Supress all console logging
const throwOutUser = (wipeStorageData, done) => {
// Get rid of authenticated Gun user
var user = gun.back(-1)._.user;
// TODO: is this correct way to 'logout' user from Gun.User ?
[ 'alias', 'sea', 'pub' ].forEach(function(key){
delete user._[key];
});
user._.is = user.is = {};
if(wipeStorageData){
// ... and persisted session
sessionStorage.removeItem('remember');
sessionStorage.removeItem('alias');
if (typeof done === 'function') {
seaIndexedDb.wipe().then(done)
return
} else {
return seaIndexedDb.wipe()
}
}
return Promise.resolve()
}
const type = 'Promise' // TODO: this is leftover...
// Simulate browser reload
beforeEach((done) => { throwOutUser(true, done) })
describe('create', function(){
it('new', function(done){
var check = function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.have.keys([ 'ok', 'pub' ]);
}catch(e){ done(e); return }
done();
};
// Gun.user.create - creates new user
user.create(alias+type, pass).then(check).catch(done);
});
it('conflict', function(done){
Gun.log.off = true; // Supress all console logging
var check = function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.have.key('err');
expect(ack.err).not.to.be(undefined);
expect(ack.err).not.to.be('');
expect(ack.err.toLowerCase().indexOf('already created')).not.to.be(-1);
}catch(e){ done(e); return }
done();
};
// Gun.user.create - fails to create existing user
user.create(alias+type, pass).then(function(ack){
done('Failed to decline creating existing user!');
}).catch(check);
});
});
describe('auth', function(){
const checkStorage = (done, notStored) => () => {
const checkValue = (data, val) => {
if (notStored) {
expect(typeof data !== 'undefined' && data !== null && data !== '')
.to.not.eql(true)
} else {
expect(data).to.not.be(undefined)
expect(data).to.not.be('')
if (val) {
expect(data).to.eql(val)
}
}
}
const alias = root.sessionStorage.getItem('user')
checkValue(alias)
checkValue(root.sessionStorage.getItem('remember'))
if (alias) {
checkIndexedDB(alias, 'auth').then((auth) => {
checkValue(auth)
done()
}).catch(done)
} else {
done()
}
}
it('login', function(done){
var check = function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.not.have.key('err');
}catch(e){ done(e); return }
done();
};
// Gun.user.auth - authenticates existing user
user.auth(alias+type, pass).then(check).catch(done);
});
it('wrong password', function(done){
var check = function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.have.key('err');
expect(ack.err).to.not.be(undefined);
expect(ack.err).to.not.be('');
expect(ack.err.toLowerCase().indexOf('failed to decrypt secret'))
.not.to.be(-1);
}catch(e){ done(e); return }
done();
};
user.auth(alias+type, pass+'not').then(function(ack){
done('Unexpected login success!');
}).catch(check);
});
it('unknown alias', function(done){
var check = function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.have.key('err');
expect(ack.err).to.not.be(undefined);
expect(ack.err).to.not.be('');
expect(ack.err.toLowerCase().indexOf('no user')).not.to.be(-1);
}catch(e){ done(e); return }
done();
};
user.auth(alias+type+'not', pass).then(function(ack){
done('Unexpected login success!');
}).catch(check);
});
it('new password', function(done){
var check = function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.not.have.key('err');
}catch(e){ done(e); return }
done();
};
// Gun.user.auth - with newpass props sets new password
user.auth(alias+type, pass, {newpass: pass+' new'}).then(check)
.catch(done);
});
it('failed new password', function(done){
var check = function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.have.key('err');
expect(ack.err).to.not.be(undefined);
expect(ack.err).to.not.be('');
expect(ack.err.toLowerCase().indexOf('failed to decrypt secret'))
.not.to.be(-1);
}catch(e){ done(e); return }
done();
};
user.auth(alias+type, pass+'not', {newpass: pass+' new'})
.then(function(ack){
done('Unexpected password change success!');
}).catch(check);
});
it('without PIN auth session stored', function(done){
user.auth(alias+type, pass+' new').then(checkStorage(done)).catch(done);
});
it('with PIN auth session stored', function(done){
user.auth(alias+type, pass+' new', { pin: 'PIN' })
.then(checkStorage(done)).catch(done)
})
it('without PIN and zero validity no auth session storing', function(done){
user.recall(0).then(function(){
user.auth(alias+type, pass+' new')
.then(checkStorage(done, true)).catch(done);
});
});
it('with PIN and zero validity no auth session storing', function(done){
user.recall(0).then(function(){
user.auth(alias+type, pass+' new', {pin: 'PIN'})
.then(checkStorage(done, true)).catch(done);
});
});
});
describe('leave', function(){
it('valid session', function(done){
var check = function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.not.have.key('err');
expect(ack).to.have.key('ok');
expect(gun.back(-1)._.user._).to.not.have.keys([ 'sea', 'pub' ]);
// expect(gun.back(-1)._.user).to.not.be.ok();
}catch(e){ done(e); return }
done();
};
var usr = alias+type+'leave';
user.create(usr, pass).then(function(ack){
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.have.keys([ 'ok', 'pub' ]);
user.auth(usr, pass).then(function(usr){
try{
expect(usr).to.not.be(undefined);
expect(usr).to.not.be('');
expect(usr).to.not.have.key('err');
expect(usr).to.have.key('put');
}catch(e){ done(e); return }
// Gun.user.leave - performs logout for authenticated user
user.leave().then(check).catch(done);
}).catch(done);
}).catch(done);
});
it('no session', function(done){
var check = function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.not.have.key('err');
expect(ack).to.have.key('ok');
}catch(e){ done(e); return }
done();
};
expect(gun.back(-1)._.user).to.not.have.keys([ 'sea', 'pub' ]);
user.leave().then(check).catch(done);
});
});
describe('delete', function(){
var usr = alias+type+'del';
var createUser = function(a, p){
return user.create(a, p).then(function(ack){
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.have.keys([ 'ok', 'pub' ]);
return ack;
});
};
var check = function(done){
return function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.not.have.key('err');
expect(ack).to.have.key('ok');
expect(gun.back(-1)._.user).to.not.have.keys([ 'sea', 'pub' ]);
}catch(e){ done(e); return }
done();
};
};
it('existing authenticated user', function(done){
createUser(usr, pass).then(function(){
user.auth(usr, pass).then(function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.not.have.key('err');
expect(ack).to.have.key('put');
}catch(e){ done(e); return }
// Gun.user.delete - deletes existing user account
user.delete(usr, pass).then(check(done)).catch(done);
}).catch(done);
}).catch(done);
});
it('unauthenticated existing user', function(done){
createUser(usr, pass).catch(function(){})
.then(function(){
user.delete(usr, pass).then(check(done)).catch(done);
});
});
it('non-existing user', function(done){
var notFound = function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.not.have.key('put');
expect(ack).to.have.key('err');
expect(ack.err.toLowerCase().indexOf('no user')).not.to.be(-1);
}catch(e){ done(e); return }
done();
};
user.delete('someone', 'password guess').then(function(){
done('Unexpectedly deleted guessed user!');
}).catch(notFound);
});
});
describe('recall (from IndexedDB)', function(){
var doCheck = function(done, hasPin, wantAck){
expect(typeof done).to.be('function');
return function(ack){
var user = root.sessionStorage.getItem('user');
var sRemember = root.sessionStorage.getItem('remember');
expect(user).to.not.be(undefined);
expect(user).to.not.be('');
expect(sRemember).to.not.be(undefined);
expect(sRemember).to.not.be('');
var ret;
if(wantAck && ack){
['err', 'pub', 'sea', 'alias', 'put'].forEach(function(key){
if(typeof ack[key] !== 'undefined'){
(ret = ret || {})[key] = ack[key];
}
});
}
// NOTE: done can be Promise returning function
return !hasPin || !wantAck || !ack ? done(ret)
: new Promise(function(resolve){
checkIndexedDB(ack.alias, 'auth', function(auth){
expect(auth).to.not.be(undefined);
expect(auth).to.not.be('');
resolve(done(wantAck && Object.assign(ret || {}, {auth: auth})));
});
});
};
};
// This re-constructs 'remember-me' data modified by manipulate func
var manipulateStorage = function(manipulate, pin){
expect(typeof manipulate).to.be('function');
// We'll use Gun internal User data
var usr = gun.back(-1)._.user;
expect(usr).to.not.be(undefined);
expect(usr).to.have.key('_');
expect(usr._).to.have.keys(['pub', 'sea']);
// ... to validate 'remember' data
pin = pin && Buffer.from(pin, 'utf8').toString('base64');
return !pin ? Promise.resolve(sessionStorage.getItem('remember'))
: new Promise(function(resolve){
checkIndexedDB(usr._.alias, 'auth', resolve);
}).then(function(remember){
return SEA.read(remember, usr._.pub).then(function(props){
return !pin ? props
: SEA.dec(props, pin);
});
}).then(function(props){
try{ props && (props = JSON.parse(props)) }catch(e){} //eslint-disable-line no-empty
return props;
}).then(manipulate).then(function(props){
expect(props).to.not.be(undefined);
expect(props).to.not.be('');
var keys = {pub: usr._.pub, priv: usr._.sea.priv};
return SEA.write(JSON.stringify(props), keys)
.then(function(remember){
return !pin ? sessionStorage.setItem('remember', remember)
: SEA.enc(remember, pin).then(function(encauth){
return new Promise(function(resolve){
setIndexedDB(usr._.alias, encauth, resolve);
});
});
});
});
};
it('with PIN auth session stores', function(done){
var doAction = function(){
user.auth(alias+type, pass+' new', {pin: 'PIN'})
.then(doCheck(done, true)).catch(done);
};
user.recall().then(doAction).catch(done);
});
it('without PIN auth session stores', function(done){
var doAction = function(){
user.auth(alias+type, pass+' new').then(doCheck(done));
};
user.leave().then(function(){
user.recall().then(doAction).catch(done);
}).catch(done);
});
it('no validity no session storing', function(done){
var doAction = function(){
user.auth(alias+type, pass+' new').then(doCheck(done)).catch(done);
};
user.recall(0).then(doAction).catch(done);
});
it('with validity but no PIN stores using random PIN', function(done){
var doAction = function(){
user.auth(alias+type, pass+' new').then(doCheck(done)).catch(done);
};
user.recall(12 * 60).then(doAction)
.catch(done);
});
it('validity and auth with PIN but storage empty', function(done){
user.auth(alias+type, pass+' new').then(function(usr){
var sUser;
var sRemember;
try{
expect(usr).to.not.be(undefined);
expect(usr).to.not.be('');
expect(usr).to.not.have.key('err');
expect(usr).to.have.key('put');
sUser = root.sessionStorage.getItem('user');
expect(sUser).to.be(alias+type);
sRemember = root.sessionStorage.getItem('remember');
expect(sRemember).to.not.be(undefined);
expect(sRemember).to.not.be('');
}catch(e){ done(e); return }
user.leave().then(function(ack){
try{
expect(ack).to.have.key('ok');
expect(gun.back(-1)._.user).to.not.have.keys([ 'sea', 'pub' ]);
expect(root.sessionStorage.getItem('user')).to.not.be(sUser);
expect(root.sessionStorage.getItem('remember')).to.not.be(sRemember);
}catch(e){ done(e); return }
// Restore but leave IndexedDB empty
root.sessionStorage.setItem('user', sUser);
root.sessionStorage.setItem('remember', sRemember);
user.recall(12 * 60).then(
doCheck(function(ack){
expect(ack).to.have.key('err');
expect(ack.err.toLowerCase().indexOf('no session')).to.not.be(-1);
checkIndexedDB(alias+type, 'auth', function(auth){
expect((typeof auth !== 'undefined' && auth !== null && auth !== ''))
.to.not.eql(true);
done();
});
}, false, true))
.catch(done);
}).catch(done);
}).catch(done);
});
it('valid session bootstrap', function(done){
var sUser;
var sRemember;
var iAuth;
user.auth(alias+type, pass+' new', {pin: 'PIN'}).then(function(usr){
try{
expect(usr).to.not.be(undefined);
expect(usr).to.not.be('');
expect(usr).to.not.have.key('err');
expect(usr).to.have.key('put');
expect(root.sessionStorage.getItem('user')).to.be(alias+type);
expect(root.sessionStorage.getItem('remember')).to.not.be(undefined);
expect(root.sessionStorage.getItem('remember')).to.not.be('');
sUser = root.sessionStorage.getItem('user');
sRemember = root.sessionStorage.getItem('remember');
}catch(e){ done(e); return }
return new Promise(function(resolve){
checkIndexedDB(sUser, 'auth', function(auth){ resolve(iAuth = auth) });
});
}).then(function(){
return user.leave().then(function(ack){
try{
expect(ack).to.have.key('ok');
expect(gun.back(-1)._.user).to.not.have.keys([ 'sea', 'pub' ]);
expect(root.sessionStorage.getItem('user')).to.not.be(sUser);
expect(root.sessionStorage.getItem('remember')).to.not.be(sRemember);
}catch(e){ done(e); return }
return new Promise(function(resolve){
checkIndexedDB(sUser, 'auth', function(auth){
expect(auth).to.not.be(iAuth);
resolve();
});
});
}).then(function(){
root.sessionStorage.setItem('user', sUser);
root.sessionStorage.setItem('remember', sRemember);
return new Promise(function(resolve){
setIndexedDB(sUser, iAuth, resolve);
});
}).then(function(){
user.recall(12 * 60).then(doCheck(done))
.catch(done);
}).catch(done);
}).catch(done);
});
it('valid session bootstrap using alias & PIN', function(done){
let sRemember
user.recall(12 * 60).then(function(){
return user.auth(alias+type, pass+' new', {pin: 'PIN'});
}).then(doCheck(function(ack){
// Let's save remember props
var sUser = root.sessionStorage.getItem('user');
sRemember = root.sessionStorage.getItem('remember')
var iAuth = ack.auth;
return new Promise(function(resolve){
checkIndexedDB(sUser, 'auth', function(auth){
iAuth = auth;
resolve(user.leave()); // Then logout user
});
}).then(function(ack){
try{
expect(ack).to.have.key('ok');
expect(gun.back(-1)._.user).to.not.have.keys([ 'sea', 'pub' ]);
expect(root.sessionStorage.getItem('user')).to.not.be(sUser);
expect(root.sessionStorage.getItem('remember')).to.not.be(sRemember);
}catch(e){ done(e); return }
return new Promise(function(resolve){
checkIndexedDB(sUser, 'auth', function(auth){
try{ expect(auth).to.not.be(iAuth) }catch(e){ done(e) }
// Then restore IndexedDB but skip sessionStorage remember
setIndexedDB(sUser, iAuth, function(){
root.sessionStorage.setItem('user', sUser);
resolve(ack);
});
});
});
});
}, true, true)).then(function(){
// Then try to recall authentication
return user.recall(12 * 60).then(function(props){
try{
expect(props).to.not.be(undefined);
expect(props).to.not.be('');
expect(props).to.have.key('err');
// Which fails to missing PIN
expect(props.err.toLowerCase()
.indexOf('missing pin')).not.to.be(-1);
}catch(e){ done(e); return }
root.sessionStorage.setItem('remember', sRemember)
// Ok, time to try auth with alias & PIN
return user.auth(alias+type, undefined, {pin: 'PIN'});
});
}).then(doCheck(function(usr){
try{
expect(usr).to.not.be(undefined);
expect(usr).to.not.be('');
expect(usr).to.not.have.key('err');
expect(usr).to.have.key('put');
}catch(e){ done(e); return }
// We've recalled authenticated session using alias & PIN!
done();
}, true, true)).catch(done);
});
it('valid session fails to bootstrap with alias & wrong PIN',
function(done){
user.recall(12 * 60).then(function(){
return user.auth(alias+type, pass+' new', {pin: 'PIN'});
}).then(doCheck(function(ack){
var sUser = root.sessionStorage.getItem('user');
var sRemember = root.sessionStorage.getItem('remember');
var iAuth = ack.auth;
return new Promise(function(resolve){
checkIndexedDB(sUser, 'auth', function(auth){
iAuth = auth;
resolve(user.leave()); // Then logout user
});
}).then(function(ack){
try{
expect(ack).to.have.key('ok');
expect(gun.back(-1)._.user).to.not.have.keys([ 'sea', 'pub' ]);
expect(root.sessionStorage.getItem('user')).to.not.be(sUser);
expect(root.sessionStorage.getItem('remember')).to.not.be(sRemember);
}catch(e){ done(e); return }
return new Promise(function(resolve){
checkIndexedDB(sUser, 'auth', function(auth){
try{ expect(auth).to.not.be(iAuth) }catch(e){ done(e) }
// Then restore IndexedDB auth data, skip sessionStorage
setIndexedDB(sUser, iAuth, function(){
root.sessionStorage.setItem('user', sUser);
resolve(ack);
});
});
});
});
}, true, true)).then(function(){
// Ok, time to try auth with alias & PIN
return user.auth(alias+type, undefined, {pin: 'PiN'});
}).then(function(){
done('Unexpected login success!');
}).catch(function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.have.key('err');
expect(ack.err.toLowerCase()
.indexOf('no session data for alias & pin')).not.to.be(-1);
}catch(e){ done(e); return }
// We've recalled authenticated session using alias & PIN!
done();
});
});
it('expired session fails to bootstrap', function(done){
var pin = 'PIN';
user.recall(60).then(function(){
return user.auth(alias+type, pass+' new', {pin: pin});
}).then(doCheck(function(){
// Storage data OK, let's back up time of auth to exp + 65 seconds
return manipulateStorage(function(props){
var ret = Object.assign({}, props, {iat: props.iat - 65 - props.exp});
return ret;
}, pin);
})).then(() => throwOutUser(false)) // Simulate browser reload
.then(() => user.recall(60).then((ack) => {
expect(ack).to.not.be(undefined)
expect(ack).to.not.be('')
expect(ack).to.not.have.keys([ 'pub', 'sea' ])
expect(ack).to.have.key('err')
expect(ack.err).to.not.be(undefined)
expect(ack.err).to.not.be('')
expect(ack.err.toLowerCase()
.indexOf('no session')).not.to.be(-1)
done()
})).catch(done)
});
it('changed password', function(done){
var pin = 'PIN';
var sUser;
var sRemember;
var iAuth;
user.recall(60).then(function(){
return user.auth(alias+type, pass+' new', {pin: pin});
}).then(function(usr){
try{
expect(usr).to.not.be(undefined);
expect(usr).to.not.be('');
expect(usr).to.not.have.key('err');
expect(usr).to.have.key('put');
sUser = root.sessionStorage.getItem('user');
expect(sUser).to.be(alias+type);
sRemember = root.sessionStorage.getItem('remember');
expect(sRemember).to.not.be(undefined);
expect(sRemember).to.not.be('');
}catch(e){ done(e); return }
return new Promise(function(resolve){
checkIndexedDB(sUser, 'auth', function(auth){ resolve(iAuth = auth) });
});
}).then(function(){
return user.leave().then(function(ack){
try{ expect(ack).to.have.key('ok') }catch(e){ done(e); return }
return user.auth(alias+type, pass+' new', {newpass: pass, pin: pin})
.then(function(usr){ expect(usr).to.not.have.key('err') });
}).then(() => user.leave().then((ack) => {
try {
expect(ack).to.have.key('ok')
} catch (e) { done(e); return }
return throwOutUser(false)
})).then(function(){
// Simulate browser reload
// Call back pre-update remember...
root.sessionStorage.setItem('user', sUser);
root.sessionStorage.setItem('remember', sRemember);
// ... and IndexedDB auth
return new Promise(function(resolve){
setIndexedDB(sUser, iAuth, resolve);
});
}).then(function(){
user.recall(60).then(function(props){
expect(props).to.not.be(undefined);
expect(props).to.not.be('');
expect(props).to.have.key('err');
expect(props.err).to.not.be(undefined);
expect(props.err).to.not.be('');
expect(props.err.toLowerCase()
.indexOf('failed to decrypt')).not.to.be(-1);
done();
}).catch(done);
}).catch(done);
}).catch(done);
});
it('recall hook session manipulation', function(done){
var pin = 'PIN';
var exp;
var hookFunc = function(props){
exp = props.exp * 2; // Doubles session expiration time
var ret = Object.assign({}, props, {exp: exp});
return new Promise(function(resolve){
resolve(ret); // Both callback & Promise methods here
});
};
user.recall(60, {hook: hookFunc}).then(function(){
return user.auth(alias+type, pass, {pin: pin});
}).then(function(){
return manipulateStorage(function(props){
expect(props).to.not.be(undefined);
expect(props).to.have.key('exp');
expect(props.exp).to.be(exp);
return props;
}, pin);
}).then(done).catch(done);
});
});
describe('alive', function(){
it('valid session', function(done){
var check = function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.not.have.key('err');
expect(ack).to.have.keys([ 'sea', 'pub' ]);
}catch(e){ done(e); return }
done();
};
var aliveUser = alias+type+'alive';
user.create(aliveUser, pass).then(function(ack){
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.have.keys([ 'ok', 'pub' ]);
user.auth(aliveUser, pass, {pin: 'PIN'}).then(function(usr){
try{
expect(usr).to.not.be(undefined);
expect(usr).to.not.be('');
expect(usr).to.not.have.key('err');
expect(usr).to.have.key('put');
}catch(e){ done(e); return }
// Gun.user.alive - keeps/checks User authentiation state
user.alive().then(check).catch(done);
}).catch(done);
}).catch(done);
});
it('expired session', function(done){
var check = function(ack){
try{
expect(ack).to.not.be(undefined);
expect(ack).to.not.be('');
expect(ack).to.not.have.keys([ 'sea', 'pub' ]);
expect(ack).to.have.key('err');
expect(ack.err.toLowerCase().indexOf('no session')).not.to.be(-1);
}catch(e){ done(e); return }
done();
};
user.leave().catch(function(){}).then(function(){
user.alive().then(function(){
done('Unexpected alive session!');
}).catch(check);
}).catch(done);
});
});
process.env.SEA_CHANNEL && describe('User channel', function(){
it.skip('create');
it.skip('add member');
});
Gun.log.off = false;
});
});
}());