|
1 | 1 | import json |
| 2 | +import warnings |
| 3 | + |
| 4 | +import pytest |
2 | 5 |
|
3 | 6 | from jose import jwk |
4 | 7 | from jose import jws |
5 | 8 | from jose.constants import ALGORITHMS |
6 | 9 | from jose.exceptions import JWSError |
7 | 10 |
|
8 | | -import pytest |
| 11 | +try: |
| 12 | + from jose.backends.cryptography_backend import CryptographyRSAKey |
| 13 | +except ImportError: |
| 14 | + CryptographyRSAKey = None |
9 | 15 |
|
10 | 16 |
|
11 | 17 | @pytest.fixture |
@@ -291,15 +297,21 @@ def test_wrong_key(self, payload): |
291 | 297 | with pytest.raises(JWSError): |
292 | 298 | jws.verify(token, rsa_public_key, ALGORITHMS.HS256) |
293 | 299 |
|
294 | | - def test_private_verify(self, payload): |
| 300 | + @pytest.mark.pycrypto |
| 301 | + @pytest.mark.pycryptodome |
| 302 | + @pytest.mark.skipif(CryptographyRSAKey is None, reason="Cryptography backend outright fails verification") |
| 303 | + def test_private_verify_raises_warning(self, payload): |
295 | 304 | token = jws.sign(payload, rsa_private_key, algorithm='RS256') |
296 | 305 |
|
297 | 306 | # verify with public |
298 | | - dec = jws.verify(token, rsa_public_key, algorithms='RS256') |
| 307 | + jws.verify(token, rsa_public_key, algorithms='RS256') |
299 | 308 |
|
300 | | - with pytest.raises(JWSError): |
301 | | - # verify with private does not work |
302 | | - dec = jws.verify(token, rsa_private_key, algorithms='RS256') |
| 309 | + with warnings.catch_warnings(record=True) as w: |
| 310 | + # verify with private raises warning |
| 311 | + jws.verify(token, rsa_private_key, algorithms='RS256') |
| 312 | + |
| 313 | + assert ("Attempting to verify a message with a private key. " |
| 314 | + "This is not recommended.") == str(w[-1].message) |
303 | 315 |
|
304 | 316 |
|
305 | 317 | ec_private_key = """-----BEGIN EC PRIVATE KEY----- |
|
0 commit comments