|
10 | 10 | if sys.version_info >= (3, 0, 0): |
11 | 11 | unicode = str |
12 | 12 |
|
13 | | -from Crypto.PublicKey import RSA |
14 | | - |
15 | 13 |
|
16 | 14 | def utc_timestamp(): |
17 | 15 | return timegm(datetime.utcnow().utctimetuple()) |
@@ -175,30 +173,68 @@ def test_decode_with_expiration_with_leeway(self): |
175 | 173 | jwt.decode(jwt_message, secret, leeway=1) |
176 | 174 |
|
177 | 175 | def test_encode_decode_with_rsa_sha256(self): |
178 | | - with open('tests/testkey','r') as rsa_priv_file: |
179 | | - priv_rsakey = RSA.importKey(rsa_priv_file.read()) |
180 | | - jwt_message = jwt.encode(self.payload, priv_rsakey, algorithm='RS256') |
181 | | - with open('tests/testkey.pub','r') as rsa_pub_file: |
182 | | - pub_rsakey = RSA.importKey(rsa_pub_file.read()) |
183 | | - assert jwt.decode(jwt_message, pub_rsakey) |
| 176 | + try: |
| 177 | + from Crypto.PublicKey import RSA |
| 178 | + |
| 179 | + with open('tests/testkey','r') as rsa_priv_file: |
| 180 | + priv_rsakey = RSA.importKey(rsa_priv_file.read()) |
| 181 | + jwt_message = jwt.encode(self.payload, priv_rsakey, algorithm='RS256') |
| 182 | + |
| 183 | + with open('tests/testkey.pub','r') as rsa_pub_file: |
| 184 | + pub_rsakey = RSA.importKey(rsa_pub_file.read()) |
| 185 | + assert jwt.decode(jwt_message, pub_rsakey) |
| 186 | + except ImportError: |
| 187 | + pass |
184 | 188 |
|
185 | 189 | def test_encode_decode_with_rsa_sha384(self): |
186 | | - with open('tests/testkey','r') as rsa_priv_file: |
187 | | - priv_rsakey = RSA.importKey(rsa_priv_file.read()) |
188 | | - jwt_message = jwt.encode(self.payload, priv_rsakey, algorithm='RS384') |
189 | | - with open('tests/testkey.pub','r') as rsa_pub_file: |
190 | | - pub_rsakey = RSA.importKey(rsa_pub_file.read()) |
191 | | - assert jwt.decode(jwt_message, pub_rsakey) |
| 190 | + try: |
| 191 | + from Crypto.PublicKey import RSA |
192 | 192 |
|
193 | | - def test_encode_decode_with_rsa_sha512(self): |
194 | | - with open('tests/testkey','r') as rsa_priv_file: |
195 | | - priv_rsakey = RSA.importKey(rsa_priv_file.read()) |
196 | | - jwt_message = jwt.encode(self.payload, priv_rsakey, algorithm='RS512') |
197 | | - with open('tests/testkey.pub','r') as rsa_pub_file: |
198 | | - pub_rsakey = RSA.importKey(rsa_pub_file.read()) |
199 | | - assert jwt.decode(jwt_message, pub_rsakey) |
| 193 | + with open('tests/testkey','r') as rsa_priv_file: |
| 194 | + priv_rsakey = RSA.importKey(rsa_priv_file.read()) |
| 195 | + jwt_message = jwt.encode(self.payload, priv_rsakey, algorithm='RS384') |
200 | 196 |
|
| 197 | + with open('tests/testkey.pub','r') as rsa_pub_file: |
| 198 | + pub_rsakey = RSA.importKey(rsa_pub_file.read()) |
| 199 | + assert jwt.decode(jwt_message, pub_rsakey) |
| 200 | + except ImportError: |
| 201 | + pass |
201 | 202 |
|
| 203 | + def test_encode_decode_with_rsa_sha512(self): |
| 204 | + try: |
| 205 | + from Crypto.PublicKey import RSA |
| 206 | + |
| 207 | + with open('tests/testkey','r') as rsa_priv_file: |
| 208 | + priv_rsakey = RSA.importKey(rsa_priv_file.read()) |
| 209 | + jwt_message = jwt.encode(self.payload, priv_rsakey, algorithm='RS512') |
| 210 | + |
| 211 | + with open('tests/testkey.pub','r') as rsa_pub_file: |
| 212 | + pub_rsakey = RSA.importKey(rsa_pub_file.read()) |
| 213 | + assert jwt.decode(jwt_message, pub_rsakey) |
| 214 | + except ImportError: |
| 215 | + pass |
| 216 | + |
| 217 | + def test_crypto_related_signing_methods(self): |
| 218 | + try: |
| 219 | + import Crypto |
| 220 | + self.assertTrue('RS256' in jwt.signing_methods) |
| 221 | + self.assertTrue('RS384' in jwt.signing_methods) |
| 222 | + self.assertTrue('RS512' in jwt.signing_methods) |
| 223 | + except ImportError: |
| 224 | + self.assertFalse('RS256' in jwt.signing_methods) |
| 225 | + self.assertFalse('RS384' in jwt.signing_methods) |
| 226 | + self.assertFalse('RS512' in jwt.signing_methods) |
| 227 | + |
| 228 | + def test_crypto_related_verify_methods(self): |
| 229 | + try: |
| 230 | + import Crypto |
| 231 | + self.assertTrue('RS256' in jwt.verify_methods) |
| 232 | + self.assertTrue('RS384' in jwt.verify_methods) |
| 233 | + self.assertTrue('RS512' in jwt.verify_methods) |
| 234 | + except ImportError: |
| 235 | + self.assertFalse('RS256' in jwt.verify_methods) |
| 236 | + self.assertFalse('RS384' in jwt.verify_methods) |
| 237 | + self.assertFalse('RS512' in jwt.verify_methods) |
202 | 238 |
|
203 | 239 |
|
204 | 240 | if __name__ == '__main__': |
|
0 commit comments