Skip to content

Commit 6a3865b

Browse files
committed
Test warning for backends that support verifying with private keys
1 parent 74cdac5 commit 6a3865b

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

tests/test_jws.py

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

36
from jose import jwk
47
from jose import jws
8+
from jose.backends import RSAKey
59
from jose.constants import ALGORITHMS
610
from jose.exceptions import JWSError
711

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

1017

1118
@pytest.fixture
@@ -291,15 +298,19 @@ def test_wrong_key(self, payload):
291298
with pytest.raises(JWSError):
292299
jws.verify(token, rsa_public_key, ALGORITHMS.HS256)
293300

294-
def test_private_verify(self, payload):
301+
@pytest.mark.skipif(RSAKey is CryptographyRSAKey, reason="Cryptography backend outright fails verification")
302+
def test_private_verify_raises_warning(self, payload):
295303
token = jws.sign(payload, rsa_private_key, algorithm='RS256')
296304

297305
# verify with public
298-
dec = jws.verify(token, rsa_public_key, algorithms='RS256')
306+
jws.verify(token, rsa_public_key, algorithms='RS256')
299307

300-
with pytest.raises(JWSError):
301-
# verify with private does not work
302-
dec = jws.verify(token, rsa_private_key, algorithms='RS256')
308+
with warnings.catch_warnings(record=True) as w:
309+
# verify with private raises warning
310+
jws.verify(token, rsa_private_key, algorithms='RS256')
311+
312+
assert ("Attempting to verify a message with a private key. "
313+
"This is not recommended.") == str(w[-1].message)
303314

304315

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

0 commit comments

Comments
 (0)