Skip to content

Commit 2d7f481

Browse files
committed
Test warning for backends that support verifying with private keys
1 parent 53327c8 commit 2d7f481

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

tests/test_jws.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import json
2+
import warnings
3+
4+
import pytest
25

36
from jose import jwk
47
from jose import jws
58
from jose.constants import ALGORITHMS
69
from jose.exceptions import JWSError
710

8-
import pytest
11+
try:
12+
from jose.backends.cryptography_backend import CryptographyRSAKey
13+
except ImportError:
14+
CryptographyRSAKey = None
915

1016

1117
@pytest.fixture
@@ -291,15 +297,21 @@ def test_wrong_key(self, payload):
291297
with pytest.raises(JWSError):
292298
jws.verify(token, rsa_public_key, ALGORITHMS.HS256)
293299

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):
295304
token = jws.sign(payload, rsa_private_key, algorithm='RS256')
296305

297306
# verify with public
298-
dec = jws.verify(token, rsa_public_key, algorithms='RS256')
307+
jws.verify(token, rsa_public_key, algorithms='RS256')
299308

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)
303315

304316

305317
ec_private_key = """-----BEGIN EC PRIVATE KEY-----

0 commit comments

Comments
 (0)