forked from jpadilla/pyjwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_algorithms.py
More file actions
167 lines (118 loc) · 6.37 KB
/
test_algorithms.py
File metadata and controls
167 lines (118 loc) · 6.37 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
import base64
import hashlib
from jwt.algorithms import Algorithm, HMACAlgorithm
from .compat import unittest
from .utils import ensure_bytes, ensure_unicode
try:
from cryptography.hazmat.primitives import hashes
from jwt.algorithms import RSAAlgorithm, ECAlgorithm
has_crypto = True
except ImportError:
has_crypto = False
class TestAlgorithms(unittest.TestCase):
def setUp(self): # noqa
pass
def test_algorithm_should_throw_exception_if_prepare_key_not_impl(self):
algo = Algorithm()
with self.assertRaises(NotImplementedError):
algo.prepare_key('test')
def test_algorithm_should_throw_exception_if_sign_not_impl(self):
algo = Algorithm()
with self.assertRaises(NotImplementedError):
algo.sign('message', 'key')
def test_algorithm_should_throw_exception_if_verify_not_impl(self):
algo = Algorithm()
with self.assertRaises(NotImplementedError):
algo.verify('message', 'key', 'signature')
def test_hmac_should_reject_nonstring_key(self):
algo = HMACAlgorithm(hashlib.sha256())
with self.assertRaises(TypeError) as context:
algo.prepare_key(object())
exception = context.exception
self.assertEqual(str(exception), 'Expecting a string- or bytes-formatted key.')
def test_hmac_should_accept_unicode_key(self):
algo = HMACAlgorithm(hashlib.sha256())
algo.prepare_key(ensure_unicode('awesome'))
@unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
def test_rsa_should_parse_pem_public_key(self):
algo = RSAAlgorithm(hashes.SHA256())
with open('tests/keys/testkey2_rsa.pub.pem', 'r') as pem_key:
algo.prepare_key(pem_key.read())
@unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
def test_rsa_should_accept_unicode_key(self):
algo = RSAAlgorithm(hashes.SHA256())
with open('tests/keys/testkey_rsa', 'r') as rsa_key:
algo.prepare_key(ensure_unicode(rsa_key.read()))
@unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
def test_rsa_should_reject_non_string_key(self):
algo = RSAAlgorithm(hashes.SHA256())
with self.assertRaises(TypeError):
algo.prepare_key(None)
@unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
def test_rsa_verify_should_return_false_if_signature_invalid(self):
algo = RSAAlgorithm(hashes.SHA256())
jwt_message = ensure_bytes('Hello World!')
jwt_sig = base64.b64decode(ensure_bytes(
'yS6zk9DBkuGTtcBzLUzSpo9gGJxJFOGvUqN01iLhWHrzBQ9ZEz3+Ae38AXp'
'10RWwscp42ySC85Z6zoN67yGkLNWnfmCZSEv+xqELGEvBJvciOKsrhiObUl'
'2mveSc1oeO/2ujkGDkkkJ2epn0YliacVjZF5+/uDmImUfAAj8lzjnHlzYix'
'sn5jGz1H07jYYbi9diixN8IUhXeTafwFg02IcONhum29V40Wu6O5tAKWlJX'
'fHJnNUzAEUOXS0WahHVb57D30pcgIji9z923q90p5c7E2cU8V+E1qe8NdCA'
'APCDzZZ9zQ/dgcMVaBrGrgimrcLbPjueOKFgSO+SSjIElKA=='))
jwt_sig = jwt_sig + ensure_bytes('123') # Signature is now invalid
with open('tests/keys/testkey_rsa.pub', 'r') as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
self.assertFalse(result)
@unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
def test_rsa_verify_should_return_true_if_signature_valid(self):
algo = RSAAlgorithm(hashes.SHA256())
jwt_message = ensure_bytes('Hello World!')
jwt_sig = base64.b64decode(ensure_bytes(
'yS6zk9DBkuGTtcBzLUzSpo9gGJxJFOGvUqN01iLhWHrzBQ9ZEz3+Ae38AXp'
'10RWwscp42ySC85Z6zoN67yGkLNWnfmCZSEv+xqELGEvBJvciOKsrhiObUl'
'2mveSc1oeO/2ujkGDkkkJ2epn0YliacVjZF5+/uDmImUfAAj8lzjnHlzYix'
'sn5jGz1H07jYYbi9diixN8IUhXeTafwFg02IcONhum29V40Wu6O5tAKWlJX'
'fHJnNUzAEUOXS0WahHVb57D30pcgIji9z923q90p5c7E2cU8V+E1qe8NdCA'
'APCDzZZ9zQ/dgcMVaBrGrgimrcLbPjueOKFgSO+SSjIElKA=='))
with open('tests/keys/testkey_rsa.pub', 'r') as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
self.assertTrue(result)
@unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
def test_ec_should_reject_non_string_key(self):
algo = ECAlgorithm(hashes.SHA256())
with self.assertRaises(TypeError):
algo.prepare_key(None)
@unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
def test_ec_should_accept_unicode_key(self):
algo = ECAlgorithm(hashes.SHA256())
with open('tests/keys/testkey_ec', 'r') as ec_key:
algo.prepare_key(ensure_unicode(ec_key.read()))
@unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
def test_ec_verify_should_return_false_if_signature_invalid(self):
algo = ECAlgorithm(hashes.SHA256())
jwt_message = ensure_bytes('Hello World!')
jwt_sig = base64.b64decode(ensure_bytes(
'MIGIAkIB9vYz+inBL8aOTA4auYz/zVuig7TT1bQgKROIQX9YpViHkFa4DT5'
'5FuFKn9XzVlk90p6ldEj42DC9YecXHbC2t+cCQgCicY+8f3f/KCNtWK7cif'
'6vdsVwm6Lrjs0Ag6ZqCf+olN11hVt1qKBC4lXppqB1gNWEmNQaiz1z2QRyc'
'zJ8hSJmbw=='))
jwt_sig = ensure_bytes('123') # Signature is now invalid
with open('tests/keys/testkey_ec.pub', 'r') as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
self.assertFalse(result)
@unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
def test_ec_verify_should_return_true_if_signature_valid(self):
algo = ECAlgorithm(hashes.SHA256())
jwt_message = ensure_bytes('Hello World!')
jwt_sig = base64.b64decode(ensure_bytes(
'MIGIAkIB9vYz+inBL8aOTA4auYz/zVuig7TT1bQgKROIQX9YpViHkFa4DT5'
'5FuFKn9XzVlk90p6ldEj42DC9YecXHbC2t+cCQgCicY+8f3f/KCNtWK7cif'
'6vdsVwm6Lrjs0Ag6ZqCf+olN11hVt1qKBC4lXppqB1gNWEmNQaiz1z2QRyc'
'zJ8hSJmbw=='))
with open('tests/keys/testkey_ec.pub', 'r') as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
self.assertTrue(result)