-
Notifications
You must be signed in to change notification settings - Fork 33
/
tests.py
950 lines (762 loc) · 34.2 KB
/
tests.py
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
import json
import mock
import unittest
from base64 import b64encode
from copy import copy, deepcopy
from itertools import product
from time import time
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
import jose
rsa_key = RSA.generate(2048)
rsa_priv_key = {
'k': rsa_key.exportKey('PEM'),
}
rsa_pub_key = {
'k': rsa_key.publickey().exportKey('PEM'),
}
claims = {'john': 'cleese'}
def legacy_encrypt(claims, jwk, adata='', add_header=None, alg='RSA-OAEP',
enc='A128CBC-HS256', rng=get_random_bytes, compression=None, version=None):
# see https://github.com/Demonware/jose/pull/3/files
header = dict((add_header or {}).items() + [
('enc', enc), ('alg', alg)])
if version == 1:
claims = deepcopy(claims)
assert jose._TEMP_VER_KEY not in claims
claims[jose._TEMP_VER_KEY] = version
# promote the temp key to the header
assert jose._TEMP_VER_KEY not in header
header[jose._TEMP_VER_KEY] = version
plaintext = jose.json_encode(claims)
# compress (if required)
if compression is not None:
header['zip'] = compression
try:
(compress, _) = jose.COMPRESSION[compression]
except KeyError:
raise jose.Error(
'Unsupported compression algorithm: {}'.format(compression))
plaintext = compress(plaintext)
# body encryption/hash
((cipher, _), key_size), ((hash_fn, _), hash_mod) = jose.JWA[enc]
iv = rng(AES.block_size)
if version == 1:
encryption_key = rng(hash_mod.digest_size)
cipher_key = encryption_key[-hash_mod.digest_size/2:]
mac_key = encryption_key[:-hash_mod.digest_size/2]
else:
encryption_key = rng((key_size // 8) + hash_mod.digest_size)
cipher_key = encryption_key[:-hash_mod.digest_size]
mac_key = encryption_key[-hash_mod.digest_size:]
ciphertext = cipher(plaintext, cipher_key, iv)
hash = hash_fn(jose._jwe_hash_str(ciphertext, iv, adata, version), mac_key, hash_mod)
# cek encryption
(cipher, _), _ = jose.JWA[alg]
encryption_key_ciphertext = cipher(encryption_key, jwk)
return jose.JWE(*map(jose.b64encode_url,
(jose.json_encode(header),
encryption_key_ciphertext,
iv,
ciphertext,
jose.auth_tag(hash))))
class TestLegacyDecrypt(unittest.TestCase):
def test_jwe(self):
bad_key = {'k': RSA.generate(2048).exportKey('PEM')}
jwe = legacy_encrypt(claims, rsa_pub_key)
token = jose.serialize_compact(jwe)
jwt = jose.decrypt(jose.deserialize_compact(token), rsa_priv_key)
self.assertEqual(jwt.claims, claims)
self.assertNotIn(jose._TEMP_VER_KEY, claims)
# invalid key
try:
jose.decrypt(jose.deserialize_compact(token), bad_key)
self.fail()
except jose.Error as e:
self.assertEqual(e.message, 'Incorrect decryption.')
def test_version1(self):
bad_key = {'k': RSA.generate(2048).exportKey('PEM')}
jwe = legacy_encrypt(claims, rsa_pub_key, version=1)
token = jose.serialize_compact(jwe)
jwt = jose.decrypt(jose.deserialize_compact(token), rsa_priv_key)
self.assertEqual(jwt.claims, claims)
self.assertEqual(jwt.header.get(jose._TEMP_VER_KEY), 1)
class TestSerializeDeserialize(unittest.TestCase):
def test_serialize(self):
try:
jose.deserialize_compact('1.2.3.4')
self.fail()
except jose.Error as e:
self.assertEqual(e.message, 'Malformed JWT')
class TestJWE(unittest.TestCase):
encs = ('A128CBC-HS256', 'A192CBC-HS384', 'A256CBC-HS512')
algs = (('RSA-OAEP', rsa_key),)
def test_jwe(self):
bad_key = {'k': RSA.generate(2048).exportKey('PEM')}
for (alg, jwk), enc in product(self.algs, self.encs):
jwe = jose.encrypt(claims, rsa_pub_key, enc=enc, alg=alg)
# make sure the body can't be loaded as json (should be encrypted)
try:
json.loads(jose.b64decode_url(jwe.ciphertext))
self.fail()
except ValueError:
pass
token = jose.serialize_compact(jwe)
jwt = jose.legacy_decrypt(jose.deserialize_compact(token), rsa_priv_key)
self.assertNotIn(jose._TEMP_VER_KEY, claims)
self.assertEqual(jwt.claims, claims)
# invalid key
try:
jose.legacy_decrypt(jose.deserialize_compact(token), bad_key)
self.fail()
except jose.Error as e:
self.assertEqual(e.message, 'Incorrect decryption.')
def test_jwe_add_header(self):
add_header = {'foo': 'bar'}
for (alg, jwk), enc in product(self.algs, self.encs):
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key,
add_header=add_header))
jwt = jose.legacy_decrypt(jose.deserialize_compact(et), rsa_priv_key)
self.assertEqual(jwt.header['foo'], add_header['foo'])
def test_jwe_adata(self):
adata = '42'
for (alg, jwk), enc in product(self.algs, self.encs):
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key,
adata=adata))
jwt = jose.legacy_decrypt(jose.deserialize_compact(et), rsa_priv_key,
adata=adata)
# make sure signaures don't match when adata isn't passed in
try:
hdr, dt = jose.legacy_decrypt(jose.deserialize_compact(et),
rsa_priv_key)
self.fail()
except jose.Error as e:
self.assertEqual(e.message, 'Mismatched authentication tags')
self.assertEqual(jwt.claims, claims)
def test_jwe_invalid_base64(self):
try:
jose.legacy_decrypt('aaa', rsa_priv_key)
self.fail() # expecting error due to invalid base64
except jose.Error as e:
pass
self.assertEqual(
e.args[0],
'Unable to decode base64: Incorrect padding'
)
def test_jwe_no_error_with_exp_claim(self):
claims = {jose.CLAIM_EXPIRATION_TIME: int(time()) + 5}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
jose.legacy_decrypt(jose.deserialize_compact(et), rsa_priv_key)
def test_jwe_expired_error_with_exp_claim(self):
claims = {jose.CLAIM_EXPIRATION_TIME: int(time()) - 5}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
try:
jose.legacy_decrypt(jose.deserialize_compact(et), rsa_priv_key)
self.fail() # expecting expired token
except jose.Expired as e:
pass
self.assertEqual(
e.args[0],
'Token expired at {}'.format(
jose._format_timestamp(claims[jose.CLAIM_EXPIRATION_TIME])
)
)
def test_jwe_no_error_with_iat_claim(self):
claims = {jose.CLAIM_ISSUED_AT: int(time()) - 15}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
jose.legacy_decrypt(jose.deserialize_compact(et), rsa_priv_key,
expiry_seconds=20)
def test_jwe_expired_error_with_iat_claim(self):
expiry_seconds = 10
claims = {jose.CLAIM_ISSUED_AT: int(time()) - 15}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
try:
jose.legacy_decrypt(jose.deserialize_compact(et), rsa_priv_key,
expiry_seconds=expiry_seconds)
self.fail() # expecting expired token
except jose.Expired as e:
pass
expiration_time = claims[jose.CLAIM_ISSUED_AT] + expiry_seconds
self.assertEqual(
e.args[0],
'Token expired at {}'.format(
jose._format_timestamp(expiration_time)
)
)
def test_jwe_no_error_with_nbf_claim(self):
claims = {jose.CLAIM_NOT_BEFORE: int(time()) - 5}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
jose.legacy_decrypt(jose.deserialize_compact(et), rsa_priv_key)
def test_jwe_not_yet_valid_error_with_nbf_claim(self):
claims = {jose.CLAIM_NOT_BEFORE: int(time()) + 5}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
try:
jose.legacy_decrypt(jose.deserialize_compact(et), rsa_priv_key)
self.fail() # expecting not valid yet
except jose.NotYetValid as e:
pass
self.assertEqual(
e.args[0],
'Token not valid until {}'.format(
jose._format_timestamp(claims[jose.CLAIM_NOT_BEFORE])
)
)
def test_jwe_ignores_expired_token_if_validate_claims_is_false(self):
claims = {jose.CLAIM_EXPIRATION_TIME: int(time()) - 5}
et = jose.serialize_compact(jose.encrypt(claims, rsa_pub_key))
jose.legacy_decrypt(jose.deserialize_compact(et), rsa_priv_key,
validate_claims=False)
def test_format_timestamp(self):
self.assertEqual(
jose._format_timestamp(1403054056),
'2014-06-18T01:14:16Z'
)
def test_jwe_compression(self):
local_claims = copy(claims)
for v in xrange(1000):
local_claims['dummy_' + str(v)] = '0' * 100
jwe = jose.serialize_compact(jose.encrypt(local_claims, rsa_pub_key))
_, _, _, uncompressed_ciphertext, _ = jwe.split('.')
jwe = jose.serialize_compact(jose.encrypt(local_claims, rsa_pub_key,
compression='DEF'))
_, _, _, compressed_ciphertext, _ = jwe.split('.')
self.assertTrue(len(compressed_ciphertext) <
len(uncompressed_ciphertext))
jwt = jose.legacy_decrypt(jose.deserialize_compact(jwe), rsa_priv_key)
self.assertEqual(jwt.claims, local_claims)
def test_encrypt_invalid_compression_error(self):
try:
jose.encrypt(claims, rsa_pub_key, compression='BAD')
self.fail()
except jose.Error:
pass
def test_decrypt_invalid_compression_error(self):
jwe = jose.encrypt(claims, rsa_pub_key, compression='DEF')
header = jose.b64encode_url(
jose.json_encode(
{"alg": "RSA-OAEP", "enc": "A128CBC-HS256",
jose._TEMP_VER_KEY: jose._TEMP_VER, "zip": "BAD"}
)
)
try:
jose.legacy_decrypt(jose.JWE(*((header,) + (jwe[1:]))),
rsa_priv_key)
self.fail()
except jose.Error as e:
self.assertEqual(
e.message, 'Unsupported compression algorithm: BAD')
class TestSpecCompliantJWE(unittest.TestCase):
encs = ('A128CBC-HS256', 'A192CBC-HS384', 'A256CBC-HS512')
algs = (('RSA-OAEP', rsa_key),)
def test_jwe(self):
bad_key = {'k': RSA.generate(2048).exportKey('PEM')}
for (alg, jwk), enc in product(self.algs, self.encs):
jwe = jose.spec_compliant_encrypt(claims, rsa_pub_key, enc=enc, alg=alg)
# make sure the body can't be loaded as json (should be encrypted)
try:
json.loads(jose.b64decode_url(jwe.ciphertext))
self.fail()
except ValueError:
pass
token = jose.serialize_compact(jwe)
jwt = jose.spec_compliant_decrypt(jose.deserialize_compact(token), rsa_priv_key)
self.assertNotIn(jose._TEMP_VER_KEY, claims)
self.assertEqual(jwt.claims, claims)
# invalid key
try:
jose.spec_compliant_decrypt(jose.deserialize_compact(token), bad_key)
self.fail()
except jose.Error as e:
self.assertEqual(e.message, 'Incorrect decryption.')
def test_jwe_add_header(self):
add_header = {'foo': 'bar'}
for (alg, jwk), enc in product(self.algs, self.encs):
et = jose.serialize_compact(jose.spec_compliant_encrypt(claims, rsa_pub_key,
add_header=add_header))
jwt = jose.spec_compliant_decrypt(jose.deserialize_compact(et), rsa_priv_key)
self.assertEqual(jwt.header['foo'], add_header['foo'])
def test_jwe_invalid_base64(self):
try:
jose.spec_compliant_decrypt('aaa', rsa_priv_key)
self.fail() # expecting error due to invalid base64
except jose.Error as e:
pass
self.assertEqual(
e.args[0],
'Unable to decode base64: Incorrect padding'
)
def test_jwe_no_error_with_exp_claim(self):
claims = {jose.CLAIM_EXPIRATION_TIME: int(time()) + 5}
et = jose.serialize_compact(jose.spec_compliant_encrypt(claims, rsa_pub_key))
jose.spec_compliant_decrypt(jose.deserialize_compact(et), rsa_priv_key)
def test_jwe_expired_error_with_exp_claim(self):
claims = {jose.CLAIM_EXPIRATION_TIME: int(time()) - 5}
et = jose.serialize_compact(jose.spec_compliant_encrypt(claims, rsa_pub_key))
try:
jose.spec_compliant_decrypt(jose.deserialize_compact(et), rsa_priv_key)
self.fail() # expecting expired token
except jose.Expired as e:
pass
self.assertEqual(
e.args[0],
'Token expired at {}'.format(
jose._format_timestamp(claims[jose.CLAIM_EXPIRATION_TIME])
)
)
def test_jwe_no_error_with_iat_claim(self):
claims = {jose.CLAIM_ISSUED_AT: int(time()) - 15}
et = jose.serialize_compact(jose.spec_compliant_encrypt(claims, rsa_pub_key))
jose.spec_compliant_decrypt(jose.deserialize_compact(et), rsa_priv_key,
expiry_seconds=20)
def test_jwe_expired_error_with_iat_claim(self):
expiry_seconds = 10
claims = {jose.CLAIM_ISSUED_AT: int(time()) - 15}
et = jose.serialize_compact(jose.spec_compliant_encrypt(claims, rsa_pub_key))
try:
jose.spec_compliant_decrypt(jose.deserialize_compact(et), rsa_priv_key,
expiry_seconds=expiry_seconds)
self.fail() # expecting expired token
except jose.Expired as e:
pass
expiration_time = claims[jose.CLAIM_ISSUED_AT] + expiry_seconds
self.assertEqual(
e.args[0],
'Token expired at {}'.format(
jose._format_timestamp(expiration_time)
)
)
def test_jwe_no_error_with_nbf_claim(self):
claims = {jose.CLAIM_NOT_BEFORE: int(time()) - 5}
et = jose.serialize_compact(jose.spec_compliant_encrypt(claims, rsa_pub_key))
jose.spec_compliant_decrypt(jose.deserialize_compact(et), rsa_priv_key)
def test_jwe_not_yet_valid_error_with_nbf_claim(self):
claims = {jose.CLAIM_NOT_BEFORE: int(time()) + 5}
et = jose.serialize_compact(jose.spec_compliant_encrypt(claims, rsa_pub_key))
try:
jose.spec_compliant_decrypt(jose.deserialize_compact(et), rsa_priv_key)
self.fail() # expecting not valid yet
except jose.NotYetValid as e:
pass
self.assertEqual(
e.args[0],
'Token not valid until {}'.format(
jose._format_timestamp(claims[jose.CLAIM_NOT_BEFORE])
)
)
def test_jwe_ignores_expired_token_if_validate_claims_is_false(self):
claims = {jose.CLAIM_EXPIRATION_TIME: int(time()) - 5}
et = jose.serialize_compact(jose.spec_compliant_encrypt(claims, rsa_pub_key))
jose.spec_compliant_decrypt(jose.deserialize_compact(et), rsa_priv_key,
validate_claims=False)
def test_format_timestamp(self):
self.assertEqual(
jose._format_timestamp(1403054056),
'2014-06-18T01:14:16Z'
)
def test_jwe_compression(self):
local_claims = copy(claims)
for v in xrange(1000):
local_claims['dummy_' + str(v)] = '0' * 100
jwe = jose.serialize_compact(
jose.spec_compliant_encrypt(local_claims, rsa_pub_key)
)
_, _, _, uncompressed_ciphertext, _ = jwe.split('.')
jwe = jose.serialize_compact(
jose.spec_compliant_encrypt(local_claims, rsa_pub_key,
add_header={'zip': 'DEF'})
)
_, _, _, compressed_ciphertext, _ = jwe.split('.')
self.assertTrue(len(compressed_ciphertext) <
len(uncompressed_ciphertext))
jwt = jose.spec_compliant_decrypt(jose.deserialize_compact(jwe),
rsa_priv_key)
self.assertEqual(jwt.claims, local_claims)
def test_encrypt_invalid_compression_error(self):
try:
jose.spec_compliant_encrypt(claims, rsa_pub_key,
add_header={'zip':'BAD'})
self.fail()
except jose.Error:
pass
def test_decrypt_invalid_compression_error(self):
with mock.patch.dict(jose.COMPRESSION,
{'BAD': jose.COMPRESSION['DEF']}):
jwe = jose.spec_compliant_encrypt(claims, rsa_pub_key,
add_header={'zip': 'BAD'})
try:
jose.spec_compliant_decrypt(jwe, rsa_priv_key)
self.fail()
except jose.Error as e:
self.assertEqual(
e.message, 'Unsupported compression algorithm: BAD')
class TestJWS(unittest.TestCase):
def test_jws_sym(self):
algs = ('HS256', 'HS384', 'HS512',)
jwk = {'k': 'password'}
for alg in algs:
st = jose.serialize_compact(jose.sign(claims, jwk, alg=alg))
jwt = jose.verify(jose.deserialize_compact(st), jwk, alg)
self.assertEqual(jwt.claims, claims)
def test_jws_asym(self):
algs = ('RS256', 'RS384', 'RS512')
for alg in algs:
st = jose.serialize_compact(jose.sign(claims, rsa_priv_key,
alg=alg))
jwt = jose.verify(jose.deserialize_compact(st), rsa_pub_key, alg)
self.assertEqual(jwt.claims, claims)
def test_jws_signature_mismatch_error(self):
alg = 'HS256'
jwk = {'k': 'password'}
jws = jose.sign(claims, jwk, alg=alg)
try:
jose.verify(jose.JWS(jws.header, jws.payload, 'asd'), jwk, alg)
except jose.Error as e:
self.assertEqual(e.message, 'Mismatched signatures')
def test_jws_invalid_algorithm_error(self):
sign_alg = 'HS256'
verify_alg = 'RS256'
jwk = {'k': 'password'}
jws = jose.sign(claims, jwk, alg=sign_alg)
try:
jose.verify(jose.JWS(jws.header, jws.payload, 'asd'), jwk,
verify_alg)
except jose.Error as e:
self.assertEqual(e.message, 'Invalid algorithm')
PRIVATE_KEY = """-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEApqgUUxKXW4gVCHffi+u2nWqlYK6WBCPCNyhJzsauFbsilw0G
dU6BKUMIzrsKvm8wUxJVVSVH42dCZhLiT3yC85Eb6rrrYpdXzMkim9oPR1pG1lyg
3pJGcg4eFxd8S7xGeBELcANTmvLT0D1ka9Rs4iWImQDyQieXdglQWesIOYNSymaz
SzWrj3LZ5ihns6uFzx4ykisRVfK7TgOGGBl/53b0J8DxvjbHDFKNt4DgTF8eKP59
O4rKsEAv5LlsbN/MirvF6D1ZoKOXDCAvmC7MMXw8nYE9qwfgCXSH9VKnUtnVmDXL
za0loS4RIpkz1cVbHKOJlp7HH66rg5yOZek/mQIDAQABAoIBAAEQHWESQ0jgK1Is
gY6A6F9EqN1e/7HzEHANn7rj5YRZ9zSDbsEcyRIcTVgUNVNVnjdJbKXoYPcAV5oT
EMJ1BtjK2iS7IHk2gebaeZAI6gQIfV8spBIHWM+ta1+2VKKfBswJP8ttGgFo/xTa
72MIrdEbcC2ZpfH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g.QHgwlB0dCHXx1c-dnn7c0g.F04cKdz-M1_VSb25_kPwiAGBbVGE-Mh4OE"
"vrOsilQGc.vsO_UAlFRGIWlkFis5Xnng"
)
class TestDecryptCompatibility(unittest.TestCase):
def test_jwe_decrypt_compliant(self):
jwk = {'k': PRIVATE_KEY}
legacy_patch = mock.patch.object(
jose, 'legacy_decrypt', wraps=jose.legacy_decrypt
)
spec_patch = mock.patch.object(
jose, 'spec_compliant_decrypt', wraps=jose.spec_compliant_decrypt
)
with legacy_patch as legacy_mock, spec_patch as spec_mock:
jwt = jose.decrypt(
jose.deserialize_compact(SPEC_COMPLIANT_TOKEN), jwk
)
self.assertEqual(legacy_mock.call_count, 1)
self.assertEqual(spec_mock.call_count, 1)
self.assertEqual(jwt.claims, claims)
expected_header = {
'alg': 'RSA-OAEP',
'enc': 'A128CBC-HS256'
}
self.assertEqual(jwt.header, expected_header)
def test_jwe_decrypt_compliant_incorrect_jwk(self):
jwk_for_decrypt = {'k': RSA.generate(2048).exportKey('PEM')}
legacy_patch = mock.patch.object(
jose, 'legacy_decrypt', wraps=jose.legacy_decrypt
)
spec_patch = mock.patch.object(
jose, 'spec_compliant_decrypt', wraps=jose.spec_compliant_decrypt
)
with legacy_patch as legacy_mock, spec_patch as spec_mock:
with self.assertRaises(jose.Error) as decryption_error:
jose.decrypt(
jose.deserialize_compact(SPEC_COMPLIANT_TOKEN),
jwk_for_decrypt)
self.assertEqual(legacy_mock.call_count, 1)
self.assertEqual(spec_mock.call_count, 1)
self.assertEqual(decryption_error.exception.message,
"Incorrect decryption.")
def test_jwe_decrypt_compliant_expiry(self):
expiry_seconds = 10
claims = {jose.CLAIM_ISSUED_AT: int(time()) - 15}
jwe = jose.spec_compliant_encrypt(claims, rsa_pub_key)
legacy_patch = mock.patch.object(
jose, 'legacy_decrypt', wraps=jose.legacy_decrypt
)
spec_patch = mock.patch.object(
jose, 'spec_compliant_decrypt', wraps=jose.spec_compliant_decrypt
)
with legacy_patch as legacy_mock, spec_patch as spec_mock:
with self.assertRaises(jose.Expired) as expiry_error:
jose.decrypt(jwe, rsa_priv_key, expiry_seconds=expiry_seconds)
expiration_time = claims[jose.CLAIM_ISSUED_AT] + expiry_seconds
# when the error is expiry, we should not fall back to legacy.
self.assertEqual(legacy_mock.call_count, 1)
self.assertEqual(spec_mock.call_count, 1)
self.assertEqual(
expiry_error.exception.message,
'Token expired at {}'.format(
jose._format_timestamp(expiration_time)
)
)
def test_jwe_decrypt_compliant_not_before(self):
# not valid for another hour.
claim_not_before = int(time()) + 3600
claims = {jose.CLAIM_NOT_BEFORE: claim_not_before}
jwe = jose.spec_compliant_encrypt(claims, rsa_pub_key)
legacy_patch = mock.patch.object(
jose, 'legacy_decrypt', wraps=jose.legacy_decrypt
)
spec_patch = mock.patch.object(
jose, 'spec_compliant_decrypt', wraps=jose.spec_compliant_decrypt
)
with legacy_patch as legacy_mock, spec_patch as spec_mock:
with self.assertRaises(jose.NotYetValid) as not_valid_error:
jose.decrypt(jwe, rsa_priv_key)
# when the error is expiry, we should not fall back to legacy.
self.assertEqual(legacy_mock.call_count, 1)
self.assertEqual(spec_mock.call_count, 1)
self.assertEqual(
not_valid_error.exception.message,
'Token not valid until {}'.format(
jose._format_timestamp(claim_not_before)
)
)
def test_jwe_decrypt_legacy_v1(self):
jwk = {'k': PRIVATE_KEY}
legacy_patch = mock.patch.object(
jose, 'legacy_decrypt', wraps=jose.legacy_decrypt
)
spec_patch = mock.patch.object(
jose, 'spec_compliant_decrypt', wraps=jose.spec_compliant_decrypt
)
with legacy_patch as legacy_mock, spec_patch as spec_mock:
jwt = jose.decrypt(jose.deserialize_compact(LEGACY_V1_TOKEN), jwk)
self.assertEqual(legacy_mock.call_count, 1)
self.assertEqual(spec_mock.call_count, 0)
self.assertEqual(jwt.claims, claims)
expected_header = {
'alg': 'RSA-OAEP',
'enc': 'A128CBC-HS256',
'__v': 1
}
self.assertEqual(jwt.header, expected_header)
def test_jwe_decrypt_legacy_v1_expiry(self):
expiry_seconds = 10
claims = {jose.CLAIM_ISSUED_AT: int(time()) - 15}
jwe = jose.encrypt(claims, rsa_pub_key)
legacy_patch = mock.patch.object(
jose, 'legacy_decrypt', wraps=jose.legacy_decrypt
)
spec_patch = mock.patch.object(
jose, 'spec_compliant_decrypt', wraps=jose.spec_compliant_decrypt
)
with legacy_patch as legacy_mock, spec_patch as spec_mock:
with self.assertRaises(jose.Expired) as expiry_error:
jose.decrypt(jwe, rsa_priv_key, expiry_seconds=expiry_seconds)
expiration_time = claims[jose.CLAIM_ISSUED_AT] + expiry_seconds
self.assertEqual(legacy_mock.call_count, 1)
self.assertEqual(spec_mock.call_count, 0)
self.assertEqual(
expiry_error.exception.message,
'Token expired at {}'.format(
jose._format_timestamp(expiration_time)
)
)
def test_jwe_decrypt_legacy_v1_not_yet_valid(self):
# not valid for another hour.
claim_not_before = int(time()) + 3600
claims = {jose.CLAIM_NOT_BEFORE: claim_not_before}
jwe = jose.encrypt(claims, rsa_pub_key)
legacy_patch = mock.patch.object(
jose, 'legacy_decrypt', wraps=jose.legacy_decrypt
)
spec_patch = mock.patch.object(
jose, 'spec_compliant_decrypt', wraps=jose.spec_compliant_decrypt
)
with legacy_patch as legacy_mock, spec_patch as spec_mock:
with self.assertRaises(jose.NotYetValid) as not_valid_error:
jose.decrypt(jwe, rsa_priv_key)
self.assertEqual(legacy_mock.call_count, 1)
self.assertEqual(spec_mock.call_count, 0)
self.assertEqual(
not_valid_error.exception.message,
'Token not valid until {}'.format(
jose._format_timestamp(claim_not_before)
)
)
def test_jwe_decrypt_legacy_v1_incorrect_jwk(self):
jwk_for_decrypt = {'k': RSA.generate(2048).exportKey('PEM')}
legacy_patch = mock.patch.object(
jose, 'legacy_decrypt', wraps=jose.legacy_decrypt
)
spec_patch = mock.patch.object(
jose, 'spec_compliant_decrypt', wraps=jose.spec_compliant_decrypt
)
with legacy_patch as legacy_mock, spec_patch as spec_mock:
with self.assertRaises(jose.Error) as decryption_error:
jose.decrypt(
jose.deserialize_compact(LEGACY_V1_TOKEN),
jwk_for_decrypt)
self.assertEqual(legacy_mock.call_count, 1)
self.assertEqual(spec_mock.call_count, 1)
self.assertEqual(decryption_error.exception.message,
"Incorrect decryption.")
def test_jwe_decrypt_legacy_v1_without_temp_ver(self):
jwk = {'k': PRIVATE_KEY}
legacy_patch = mock.patch.object(
jose, 'legacy_decrypt', wraps=jose.legacy_decrypt
)
spec_patch = mock.patch.object(
jose, 'spec_compliant_decrypt', wraps=jose.spec_compliant_decrypt
)
legacy_legacy_temp_ver = jose.serialize_compact(
legacy_encrypt(claims, jwk)
)
with legacy_patch as legacy_mock, spec_patch as spec_mock:
jwt = jose.decrypt(
jose.deserialize_compact(legacy_legacy_temp_ver), jwk)
self.assertEqual(legacy_mock.call_count, 1)
self.assertEqual(spec_mock.call_count, 0)
self.assertEqual(jwt.claims, claims)
expected_header = {
'alg': 'RSA-OAEP',
'enc': 'A128CBC-HS256',
}
self.assertEqual(jwt.header, expected_header)
self.assertNotIn('__v', jwt.header)
def test_jwe_decrypt_legacy_v1_without_temp_ver_incorrect_jwk(self):
jwk_for_encrypt = {'k': PRIVATE_KEY}
legacy_legacy_temp_ver = jose.serialize_compact(
legacy_encrypt(claims, jwk_for_encrypt)
)
jwk_for_decrypt = {'k': RSA.generate(2048).exportKey('PEM')}
legacy_patch = mock.patch.object(
jose, 'legacy_decrypt', wraps=jose.legacy_decrypt
)
spec_patch = mock.patch.object(
jose, 'spec_compliant_decrypt', wraps=jose.spec_compliant_decrypt
)
with legacy_patch as legacy_mock, spec_patch as spec_mock:
with self.assertRaises(jose.Error) as decryption_error:
jose.decrypt(
jose.deserialize_compact(legacy_legacy_temp_ver),
jwk_for_decrypt)
self.assertEqual(legacy_mock.call_count, 1)
self.assertEqual(spec_mock.call_count, 1)
self.assertEqual(decryption_error.exception.message,
"Incorrect decryption.")
def test_jwe_decrypt_legacy_v1_without_temp_var_expiry(self):
expiry_seconds = 10
claims = {jose.CLAIM_ISSUED_AT: int(time()) - 15}
jwe = legacy_encrypt(claims, rsa_pub_key)
legacy_patch = mock.patch.object(
jose, 'legacy_decrypt', wraps=jose.legacy_decrypt
)
spec_patch = mock.patch.object(
jose, 'spec_compliant_decrypt', wraps=jose.spec_compliant_decrypt
)
with legacy_patch as legacy_mock, spec_patch as spec_mock:
with self.assertRaises(jose.Expired) as expiry_error:
jose.decrypt(jwe, rsa_priv_key, expiry_seconds=expiry_seconds)
expiration_time = claims[jose.CLAIM_ISSUED_AT] + expiry_seconds
self.assertEqual(legacy_mock.call_count, 1)
self.assertEqual(spec_mock.call_count, 0)
self.assertEqual(
expiry_error.exception.message,
'Token expired at {}'.format(
jose._format_timestamp(expiration_time)
)
)
def test_jwe_decrypt_legacy_v1_without_temp_ver_not_yet_valid(self):
# not valid for another hour.
claim_not_before = int(time()) + 3600
claims = {jose.CLAIM_NOT_BEFORE: claim_not_before}
jwe = legacy_encrypt(claims, rsa_pub_key)
legacy_patch = mock.patch.object(
jose, 'legacy_decrypt', wraps=jose.legacy_decrypt
)
spec_patch = mock.patch.object(
jose, 'spec_compliant_decrypt', wraps=jose.spec_compliant_decrypt
)
with legacy_patch as legacy_mock, spec_patch as spec_mock:
with self.assertRaises(jose.NotYetValid) as not_valid_error:
jose.decrypt(jwe, rsa_priv_key)
self.assertEqual(legacy_mock.call_count, 1)
self.assertEqual(spec_mock.call_count, 0)
self.assertEqual(
not_valid_error.exception.message,
'Token not valid until {}'.format(
jose._format_timestamp(claim_not_before)
)
)
class TestUtils(unittest.TestCase):
def test_b64encode_url_utf8(self):
istr = 'eric idle'.encode('utf8')
encoded = jose.b64encode_url(istr)
self.assertEqual(jose.b64decode_url(encoded), istr)
def test_b64encode_url_ascii(self):
istr = 'eric idle'
encoded = jose.b64encode_url(istr)
self.assertEqual(jose.b64decode_url(encoded), istr)
def test_b64encode_url(self):
istr = '{"alg": "RSA-OAEP", "enc": "A128CBC-HS256"}'
# sanity check
self.assertEqual(b64encode(istr)[-1], '=')
# actual test
self.assertNotEqual(jose.b64encode_url(istr), '=')
class TestJWA(unittest.TestCase):
def test_lookup(self):
impl = jose._JWA._impl
jose._JWA._impl = dict((k, k) for k in (
'HS256', 'RSA-OAEP', 'A128CBC', 'A128CBC'))
self.assertEqual(jose.JWA['HS256'], 'HS256')
self.assertEqual(jose.JWA['RSA-OAEP'], 'RSA-OAEP')
self.assertEqual(jose.JWA['A128CBC-HS256'],
('A128CBC', 'HS256'))
self.assertEqual(jose.JWA['A128CBC+HS256'],
('A128CBC', 'HS256'))
jose._JWA._impl = impl
def test_invalid_error(self):
try:
jose.JWA['bad']
self.fail()
except jose.Error as e:
self.assertTrue(e.message.startswith('Unsupported'))
if __name__ == '__main__':
unittest.main()