forked from jsonwebtoken/jsonwebtoken.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
1070 lines (815 loc) · 34.1 KB
/
Copy patheditor.js
File metadata and controls
1070 lines (815 loc) · 34.1 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
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const chaiArrays = require('chai-arrays');
const express = require('express');
const { importPKCS8 } = require('jose/key/import')
const { CompactSign } = require('jose/jws/compact/sign')
const _ = require('lodash');
const utils = require('./utils.js');
const defaultTokens =
require('esm')(module)('../../src/editor/default-tokens.js').default;
const jwks = require('./jwks.json');
const { isVisible, selectAll } = utils;
chai.use(chaiAsPromised);
chai.use(chaiArrays);
const expect = chai.expect;
const typingDelay = 0;
const tokenProcessingWait = 300;
const tokenEditorThrottleWait = 1200;
describe('Editor', function() {
this.timeout(75000);
before(utils.launchBrowser);
after(utils.closeBrowser);
it('Displays editor when clicking on navbar', async function() {
await this.page.click('a[href="#debugger-io"]');
// Wait for scroll
await this.page.waitFor(3000);
expect(await this.page.$eval('#debugger-io', isVisible)).to.be.true;
});
it('HS256 should be selected by default', async function() {
const selected = await this.page.$eval('#algorithm-select', select => {
return select.options[select.selectedIndex].value;
});
expect(selected).to.equal('HS256');
});
it('Default selected token should say something ' +
'about secret length', async function() {
const secret = this.page.$eval('input[name="secret"]',
secretInput => secretInput.value);
return expect(secret).to.eventually.include('256');
});
it('Should select default tokens when no changes have ' +
'been made', async function() {
try {
await this.page.select('#algorithm-select', 'HS256');
const algs = await this.page.$eval('#algorithm-select', select => {
return Array.prototype.map.call(select.options, opt => opt.value);
});
for(const alg of algs) {
await this.page.select('#algorithm-select', alg);
const token = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue();
});
expect(defaultTokens[alg.toLowerCase()].token).to.equal(token);
}
} finally {
await this.page.select('#algorithm-select', 'HS256');
}
});
it('Should display a tooltip for a claim', async function() {
await this.page.select('#algorithm-select', 'HS384');
await this.page.mouse.move(0, 0);
function tippyVisible(element) {
return element._tippy.state.visible;
}
expect(await this.page.$eval('[data-tippy]', tippyVisible)).
to.be.false;
const iatPos = await this.page.evaluate(() => {
return window.test.payloadEditor.charCoords({
line: 4,
pos: 3
}, 'window');
});
await this.page.mouse.move(iatPos.left, iatPos.top);
// Wait for animation
await this.page.waitFor(2000);
expect(await this.page.$eval('[data-tippy]', tippyVisible))
.to.be.true;
});
it('Displays a valid token by default', async function() {
const valid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('valid-token') &&
status.textContent.indexOf('verified') !== -1;
});
expect(valid).to.be.true;
});
it('Shows invalid token when a valid token is edited ' +
'in the left pane', async function() {
await this.page.evaluate(() => {
let token = window.test.tokenEditor.getValue();
token += 'asdf23';
window.test.tokenEditor.setValue(token);
});
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
const invalid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('invalid-token') &&
status.textContent.indexOf('invalid') !== -1;
});
expect(invalid).to.be.true;
});
it('Updates the token when the header is edited', async function() {
const oldToken = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue()
});
await this.page.click('.js-header');
await selectAll.call(this);
const header = {
alg: 'HS256',
typ: 'JWT',
test: 'test'
};
await this.page.keyboard.type(JSON.stringify(header, null, 2), {
delay: typingDelay
});
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
await this.page.waitFor(tokenEditorThrottleWait);
const newToken = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue();
});
expect(newToken).to.not.equal(oldToken);
const valid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('valid-token') &&
status.textContent.indexOf('verified') !== -1;
});
expect(valid).to.be.true;
});
it('Updates the token when the payload is edited', async function() {
const oldToken = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue()
});
await this.page.click('.js-payload');
await selectAll.call(this);
const payload = {
"sub": "1234567890",
"name": "John Doe",
"admin": true,
"iat": 1516239022,
"test": "test"
};
await this.page.keyboard.type(JSON.stringify(payload, null, 2), {
delay: typingDelay
});
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
await this.page.waitFor(tokenEditorThrottleWait);
const newToken = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue()
});
expect(newToken).to.not.equal(oldToken);
const valid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('valid-token') &&
status.textContent.indexOf('verified') !== -1;
});
expect(valid).to.be.true;
});
it('Selects algorithm when header is edited', async function() {
const selectedBefore =
await this.page.$eval('#algorithm-select', select => {
return select.options[select.selectedIndex].value;
});
await this.page.click('.js-header');
await selectAll.call(this);
const header = {
alg: 'HS384',
typ: 'JWT',
};
await this.page.keyboard.type(JSON.stringify(header, null, 2), {
delay: typingDelay
});
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
await this.page.waitFor(tokenEditorThrottleWait);
const selectedAfter =
await this.page.$eval('#algorithm-select', select => {
return select.options[select.selectedIndex].value;
});
expect(selectedBefore).to.not.equal(selectedAfter);
expect(selectedAfter).to.equal('HS384');
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
await this.page.waitFor(tokenEditorThrottleWait);
const valid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('valid-token') &&
status.textContent.indexOf('verified') !== -1;
});
expect(valid).to.be.true;
});
it('Should never revert to a default token after a non-default token ' +
'is generated', async function() {
await this.page.select('#algorithm-select', 'HS256');
await this.page.click('.js-payload');
await selectAll.call(this);
const payload = {
sub: 'test'
};
await this.page.keyboard.type(JSON.stringify(payload, null, 2), {
delay: typingDelay
});
const algs = await this.page.$eval('#algorithm-select', select => {
return Array.prototype.map.call(select.options, opt => opt.value);
});
for(const alg of algs) {
await this.page.select('#algorithm-select', alg);
}
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
const payloadInEditor = await this.page.evaluate(() => {
return JSON.parse(window.test.payloadEditor.getValue());
});
expect(payload).to.deep.equal(payloadInEditor);
});
describe('HMAC', function() {
before(async function() {
await this.page.select('#algorithm-select', 'HS256');
});
it('Updates the token when the secret changes', async function() {
const oldToken = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue()
});
const secretInput = await this.page.$('input[name="secret"]');
await secretInput.type('asdfasdf');
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
const newToken = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue()
});
expect(oldToken).to.not.equal(newToken);
});
it('Updates the token when the Base64 checkbox changes', async function() {
const oldToken = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue()
});
await this.page.click('#is-base64-encoded');
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
let newToken = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue()
});
expect(oldToken).to.not.equal(newToken);
await this.page.click('#is-base64-encoded');
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
newToken = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue()
});
expect(oldToken).to.equal(newToken);
});
describe('HS256/384/512', function() {
const algs = Object.keys(defaultTokens).filter(alg => alg.includes('hs'));
for(const alg of algs) {
it(`Decodes ${alg.toUpperCase()} tokens`, async function() {
const secretInput = await this.page.$('input[name="secret"]');
await secretInput.click();
await selectAll.call(this);
await secretInput.type(defaultTokens[alg].secret);
await this.page.click('.js-input');
await selectAll.call(this);
await this.page.keyboard.type(defaultTokens[alg].token);
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
await this.page.waitFor(tokenEditorThrottleWait);
const valid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('valid-token') &&
status.textContent.indexOf('verified') !== -1;
});
expect(valid).to.be.true;
});
const bits = parseInt(alg.substr(2));
it(`Considers less-than-${bits}-bit secrets weak`,
async function() {
let secret = _.pad('', (bits / 8) - 1, 'test');
const secretInput = await this.page.$('input[name="secret"]');
await secretInput.click();
await selectAll.call(this);
await secretInput.type(secret, {
delay: typingDelay
});
// Wait for animations
await this.page.waitFor(500);
let tooltipVisible =
await this.page.$eval('input[name="secret"]', input => {
return input._tippy.state.visible;
});
expect(tooltipVisible).to.be.true;
secret += 'test';
await secretInput.click();
await selectAll.call(this);
await secretInput.type(secret, {
delay: typingDelay
});
// Wait for animations
await this.page.waitFor(500);
tooltipVisible =
await this.page.$eval('input[name="secret"]', input => {
return input._tippy.state.visible;
});
expect(tooltipVisible).to.be.false;
});
}
});
it('Signs tokens with an empty secret', async function() {
const secretInput = await this.page.$('input[name="secret"]');
await secretInput.click();
await selectAll.call(this);
await this.page.keyboard.press('Delete');
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
const valid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('valid-token') &&
status.textContent.indexOf('verified') !== -1;
});
expect(valid).to.be.true;
});
});
describe('Public-key', function() {
describe('Decodes RS/ES/PS tokens', function() {
const algs = Object.keys(defaultTokens).filter(alg => !alg.includes('hs'));
for(const alg of algs) {
for (const format of ['pem', 'jwk']) {
it(`${alg.toUpperCase()} using a ${format.toUpperCase()} key`, async function() {
await this.page.click('.js-input');
await selectAll.call(this);
await this.page.keyboard.type(defaultTokens[alg].token);
const secretInput = await this.page.$('textarea[name="public-key"]');
await secretInput.click();
await selectAll.call(this);
let publicKey = defaultTokens[alg].publicKey;
if (format === 'jwk') {
publicKey = JSON.stringify(defaultTokens[alg].jwk)
}
await secretInput.type(publicKey);
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
await this.page.waitFor(tokenEditorThrottleWait);
const valid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('valid-token') &&
status.textContent.indexOf('verified') !== -1;
});
expect(valid).to.be.true;
});
}
}
});
describe('Encodes RS/ES/PS tokens', function() {
const algs = Object.keys(defaultTokens).filter(alg => !alg.includes('hs'));
for(const alg of algs) {
for (const format of ['pem', 'jwk']) {
it(`${alg.toUpperCase()} using a ${format.toUpperCase()} key`, async function() {
await this.page.select('#algorithm-select', alg.toUpperCase());
let publicKey = defaultTokens[alg].publicKey;
if (format === 'jwk') {
publicKey = JSON.stringify(defaultTokens[alg].jwk)
}
let privateKey = defaultTokens[alg].privateKey;
if (format === 'jwk') {
privateKey = JSON.stringify(defaultTokens[alg].jwk)
}
await this.page.click('textarea[name="public-key"]');
await selectAll.call(this);
await this.page.keyboard.type(publicKey, {
delay: typingDelay
});
await this.page.click('textarea[name="private-key"]');
await selectAll.call(this);
await this.page.keyboard.type(privateKey, {
delay: typingDelay
});
await this.page.evaluate(token => {
window.test.tokenEditor.setValue(token);
}, defaultTokens[alg].token);
await this.page.select('#algorithm-select', alg.toUpperCase());
await this.page.click('.js-header');
await selectAll.call(this);
await this.page.keyboard.type(JSON.stringify({
alg: alg.toUpperCase(),
typ: 'JWT'
}, null, 2), {
delay: typingDelay
});
await this.page.click('.js-payload');
await selectAll.call(this);
await this.page.keyboard.type(JSON.stringify({
sub: 'test'
}, null, 2), {
delay: typingDelay
});
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
await this.page.waitFor(tokenEditorThrottleWait);
const newToken = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue();
});
expect(newToken).to.not.be.empty;
const valid = await this.page.$eval('.validation-status',
status => {
return status.classList.contains('valid-token') &&
status.textContent.indexOf('verified') !== -1;
});
expect(valid).to.be.true;
});
}
}
});
describe('Should download public-keys when possible', function() {
before(async function() {
this.app = express();
this.app.get('/.well-known/openid-configuration', (req, res) => {
res.set('Access-Control-Allow-Origin', '*');
res.json({
jwks_uri: 'http://localhost:3000/.well-known/jwks.json'
});
});
this.app.get('/.well-known/jwks.json', (req, res) => {
res.set('Access-Control-Allow-Origin', '*');
res.json(jwks);
});
this.server = this.app.listen(3000);
await this.page.select('#algorithm-select', 'RS256');
});
beforeEach(async function() {
const publicKeyInput = await this.page.$('textarea[name="public-key"]');
await publicKeyInput.click();
await selectAll.call(this);
await this.page.keyboard.press('Delete');
});
after(function() {
this.server.close();
});
it('iss URL + .well-known', async function() {
const key = await importPKCS8(defaultTokens.rs256.privateKey, 'RS256');
const token = await new CompactSign(Buffer.from(JSON.stringify({
sub: 'test',
iss: 'http://localhost:3000/'
}))).setProtectedHeader({
alg: 'RS256',
typ: 'JWT',
kid: '1'
}).sign(key);
await this.page.click('.js-input');
await selectAll.call(this);
await this.page.keyboard.type(token);
await this.page.waitFor(2000);
await this.page.waitFor(tokenEditorThrottleWait);
const publicKey = await this.page.$eval('textarea[name="public-key"]',
publicKeyElement => publicKeyElement.value);
expect(jwks.keys[0]).to.contain(JSON.parse(publicKey))
});
it('jku', async function() {
//this.timeout(20000);
const key = await importPKCS8(defaultTokens.rs256.privateKey, 'RS256');
const token = await new CompactSign(Buffer.from(JSON.stringify({
sub: 'test'
}))).setProtectedHeader({
alg: 'RS256',
typ: 'JWT',
kid: '1',
jku: 'http://localhost:3000/.well-known/jwks.json'
}).sign(key);
await this.page.click('.js-input');
await selectAll.call(this);
await this.page.keyboard.type(token);
await this.page.waitFor(2000);
await this.page.waitFor(tokenEditorThrottleWait);
const publicKey = await this.page.$eval('textarea[name="public-key"]',
publicKeyElement => publicKeyElement.value);
expect(jwks.keys[0]).to.contain(JSON.parse(publicKey))
});
it('x5c', async function() {
//this.timeout(35000);
const key = await importPKCS8(defaultTokens.rs256.privateKey, 'RS256');
const token = await new CompactSign(Buffer.from(JSON.stringify({
sub: 'test'
}))).setProtectedHeader({
alg: 'RS256',
typ: 'JWT',
x5c: jwks.keys[0].x5c
}).sign(key);
await this.page.click('.js-input');
await selectAll.call(this);
await this.page.keyboard.type(token);
await this.page.waitFor(2000);
const publicKey = await this.page.$eval('textarea[name="public-key"]',
publicKeyElement => publicKeyElement.value);
expect(publicKey).to.include('-----BEGIN CERTIFICATE-----\nMIIDBjCCAe4CCQDOaPo3zzlhlzANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJV\nUzETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\ncyBQdHkgTHRkMB4XDTE5MDYwNTEwMDg0OVoXDTIwMDYwNDEwMDg0OVowRTELMAkG\nA1UEBhMCVVMxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0\nIFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\nAJ88orNWY3zQdGwYChTEr75E7cJbwbGiau0ucAPpM3lTlaVVsJFnVYWuLN/FzP6W\nv8q+O2r+/s91U5rw0cgB3Gk/dsIURBaS7/XI+ZU3iUom8q/zK5v2LYwmVVoGjmCI\ncK18Ci6j6/9dYp1rAJHyMrbx1k8WWBHFy4AFxblLmkt7hfYBIjUMMxk1Nb9BapKk\nwa+AfJ1txwjeO11LtLfGNHvpX+LODsUGsFg+/Sff+Xd0ctL21dwJtRbRiYibzsEb\nCH1QoQ6WErU3B0wjKrb1m1ei9dQVpKcxl0luB7+N6mvhkmDg9kFOvDG+faEpNjgf\ngbTi6SaH5mxhBoL5sMgiPTMCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAU4Nx25sl\niX8gD10Oik+RpBpHmsuZBU6q3nqYQKMlVdKFVeuptdqNjnVZY0WSSYvAUdtZ1kCu\nkMxjhvPb+LVaCkfOfXEuSqyjN55C1vDLpCV0xMb3irltYaWRIeHkqlRxRmoS+569\nZ/qg+JqZnCuEc6CeLLkzhVq+zagJkJs3evpSmofrY4qzJ1s1rygqSUhyS5NoyGAX\nCHba+m+7qMuSC3yOEPfiuAh9h6D/RltGi1iQ9Yqi4DoKlW9mfKGXbDn8thkGNdc7\n6yffilC8B/7Q8d5Btox/fE9yNI4YVhUEo+MkvUMnT1KS9fbw0o6fTjSa0D2oaHSO\nyBSs097rzC1PIw==\n-----END CERTIFICATE-----');
});
});
it('Clears the token when the header is edited and there ' +
'is no private key', async function() {
await this.page.select('#algorithm-select', 'RS256');
const secretInput = await this.page.$('textarea[name="private-key"]');
await secretInput.click();
await selectAll.call(this);
await this.page.keyboard.press('Delete');
await this.page.click('.js-header');
await selectAll.call(this);
const header = {
alg: 'RS256',
typ: 'JWT',
test: 'test'
};
await this.page.keyboard.type(JSON.stringify(header, null, 2), {
delay: typingDelay
});
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
const token = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue();
});
expect(token).to.be.empty;
});
it('Clears the token when the payload is edited and there ' +
'is no private key', async function() {
await this.page.select('#algorithm-select', 'RS256');
const secretInput = await this.page.$('textarea[name="private-key"]');
await secretInput.click();
await selectAll.call(this);
await this.page.keyboard.press('Delete');
await this.page.click('.js-payload');
await selectAll.call(this);
const payload = {
sub: 'test'
};
await this.page.keyboard.type(JSON.stringify(payload, null, 2), {
delay: typingDelay
});
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
const token = await this.page.evaluate(() => {
return window.test.tokenEditor.getValue();
});
expect(token).to.be.empty;
});
it('Marks token as invalid when there is no public key', async function() {
//this.timeout(20000);
await this.page.waitFor(tokenEditorThrottleWait);
await this.page.select('#algorithm-select', 'RS256');
await this.page.click('.js-input');
await selectAll.call(this);
await this.page.keyboard.type(defaultTokens['rs256'].token);
const secretInput = await this.page.$('textarea[name="public-key"]');
await secretInput.click();
await selectAll.call(this);
await secretInput.type(defaultTokens['rs256'].publicKey);
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
await this.page.waitFor(tokenEditorThrottleWait);
const valid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('valid-token') &&
status.textContent.indexOf('verified') !== -1;
});
expect(valid).to.be.true;
await secretInput.click();
await selectAll.call(this);
await this.page.keyboard.press('Delete');
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
await this.page.waitFor(tokenEditorThrottleWait);
const invalid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('invalid-token') &&
status.textContent.indexOf('invalid') !== -1;
});
expect(invalid).to.be.true;
});
it('Marks token as invalid when the public key is wrong', async function() {
//this.timeout(20000);
await this.page.select('#algorithm-select', 'RS256');
await this.page.click('.js-input');
await selectAll.call(this);
await this.page.keyboard.type(defaultTokens['rs256'].token);
const secretInput = await this.page.$('textarea[name="public-key"]');
await secretInput.click();
await selectAll.call(this);
await secretInput.type(defaultTokens['rs256'].publicKey);
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
await this.page.waitFor(tokenEditorThrottleWait);
const valid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('valid-token') &&
status.textContent.indexOf('verified') !== -1;
});
expect(valid).to.be.true;
await secretInput.click();
await this.page.keyboard.type('sdfasdf389972389', {
delay: typingDelay
});
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
const invalid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('invalid-token') &&
status.textContent.indexOf('invalid') !== -1;
});
expect(invalid).to.be.true;
});
it('Marks token as valid when the public key is OK and private ' +
'key is wrong', async function() {
//this.timeout(30000);
await this.page.select('#algorithm-select', 'RS256');
const secretInput = await this.page.$('textarea[name="public-key"]');
await secretInput.click();
await selectAll.call(this);
await secretInput.type(defaultTokens['rs256'].publicKey, {
delay: typingDelay
});
const privateKeyInput = await this.page.$('textarea[name="private-key"]');
await privateKeyInput.click();
await selectAll.call(this);
const head = defaultTokens['rs256'].privateKey.slice(0, 20);
const tail = defaultTokens['rs256'].privateKey.slice(20);
await privateKeyInput.type(`${head}sadfasdf${tail}`, {
delay: typingDelay
});
await this.page.click('.js-input');
await selectAll.call(this);
await this.page.keyboard.type(defaultTokens['rs256'].token, {
delay: typingDelay
});
await this.page.waitFor(1000);
const valid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('valid-token') &&
status.textContent.indexOf('verified') !== -1;
});
expect(valid).to.be.true;
});
it('Marks token as valid when the public key is OK and private ' +
'key is missing', async function() {
//this.timeout(30000);
await this.page.select('#algorithm-select', 'RS256');
const secretInput = await this.page.$('textarea[name="public-key"]');
await secretInput.click();
await selectAll.call(this);
await secretInput.type(defaultTokens['rs256'].publicKey);
const privateKeyInput = await this.page.$('textarea[name="private-key"]');
await privateKeyInput.click();
await selectAll.call(this);
await this.page.keyboard.press('Delete');
await this.page.click('.js-input');
await selectAll.call(this);
await this.page.keyboard.type(defaultTokens['rs256'].token);
await this.page.waitFor(1000);
const valid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('valid-token') &&
status.textContent.indexOf('verified') !== -1;
});
expect(valid).to.be.true;
});
});
it('Updates the header when the token algorithm ' +
'is changed', async function() {
await this.page.select('#algorithm-select', 'HS256');
await this.page.click('.js-input');
await selectAll.call(this);
await this.page.keyboard.type(defaultTokens.hs256.token);
await this.page.select('#algorithm-select', 'HS384');
const header = await this.page.evaluate(() => {
return JSON.parse(window.test.headerEditor.getValue());
});
expect(header.alg).to.equal('HS384');
});
it('Marks token as invalid when "alg" is "none"', async function() {
//this.timeout(20000);
await this.page.click('.js-input');
await selectAll.call(this);
await this.page.keyboard.type('eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.');
// Wait for token processing.
await this.page.waitFor(tokenProcessingWait);
const invalid = await this.page.$eval('.validation-status', status => {
return status.classList.contains('invalid-token') &&
status.textContent.indexOf('invalid') !== -1;
});
expect(invalid).to.be.true;
});
it('Does NOT save the last edited token', async function() {
await this.page.select('#algorithm-select', 'HS256');
const secretInput = await this.page.$('input[name="secret"]');
await secretInput.click();
await selectAll.call(this);
await secretInput.type('secret-test', {
delay: typingDelay
});
await this.page.click('.js-payload');
await selectAll.call(this);
const payload = {
sub: 'test'
};
await this.page.keyboard.type(JSON.stringify(payload, null, 2), {
delay: typingDelay
});
await this.page.reload();
const storedPayload = await this.page.evaluate(() => {
return JSON.parse(window.test.payloadEditor.getValue());
});
expect(storedPayload).to.not.deep.equal(payload);
});
describe('JWT share button', function() {
it('Copies an HMAC token to the clipboard (no secret)', async function() {
await this.page.select('#algorithm-select', 'HS256');
const secretInput = await this.page.$('input[name="secret"]');
await secretInput.click();
await selectAll.call(this);
await secretInput.type('secret-test', {
delay: typingDelay
});
await this.page.click('.js-payload');
await selectAll.call(this);
const payload = {
sub: 'test'
};
await this.page.keyboard.type(JSON.stringify(payload, null, 2), {
delay: typingDelay
});
await this.page.waitFor(tokenEditorThrottleWait);
const shareJwtButton = await this.page.$('.website-share button');
await shareJwtButton.click();
const srcToken = await this.page.evaluate(() =>
window.test.tokenEditor.getValue());
// We cannot read the clipboard in headless Chrome, so we have a special
// harness in the code that stores this value. See:
// https://github.com/GoogleChrome/puppeteer/issues/2147
const copiedUrl = await this.page.evaluate(() =>
window.test.shareJwtCopiedUrl);
const newPage = await this.browser.newPage();
await newPage.goto(
copiedUrl.replace('https://jwt.io', 'http://localhost:8000'));
const destToken = await newPage.evaluate(() =>
window.test.tokenEditor.getValue());
const destSecret = await newPage.$eval('input[name="secret"]', input =>
input.value);
expect(srcToken).to.equal(destToken);
expect(destSecret).to.not.equal('secret-test');
});
it('Copies an RSA token to the clipboard (with public-key)',
async function() {
//this.timeout(30000);
await this.page.select('#algorithm-select', 'RS256');
await this.page.click('.js-input');
await selectAll.call(this);
await this.page.keyboard.type(defaultTokens['rs256'].token, {
delay: typingDelay
});
const pubKeyInput = await this.page.$('textarea[name="public-key"]');
await pubKeyInput.click();
await selectAll.call(this);
await pubKeyInput.type(defaultTokens['rs256'].publicKey, {
delay: typingDelay
});
const privateKeyInput =
await this.page.$('textarea[name="private-key"]');
await privateKeyInput.click();
await selectAll.call(this);
await privateKeyInput.type(defaultTokens['rs256'].privateKey, {
delay: typingDelay
});
await this.page.click('.js-payload');
await selectAll.call(this);
const payload = {
sub: 'test'
};
await this.page.keyboard.type(JSON.stringify(payload, null, 2), {
delay: typingDelay
});
await this.page.waitFor(tokenEditorThrottleWait);
const shareJwtButton = await this.page.$('.website-share button');
await shareJwtButton.click();