Skip to content

Commit 2fe85a3

Browse files
committed
Fix a bug where a PEM private key as bytes raises a TypeError
1 parent daf79c1 commit 2fe85a3

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

jwt/algorithms.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import hashlib
22
import hmac
33

4-
from .compat import constant_time_compare, string_types, text_type
4+
from .compat import binary_type, constant_time_compare, is_string_type
55
from .exceptions import InvalidKeyError
66
from .utils import der_to_raw_signature, raw_to_der_signature
77

@@ -112,10 +112,10 @@ def __init__(self, hash_alg):
112112
self.hash_alg = hash_alg
113113

114114
def prepare_key(self, key):
115-
if not isinstance(key, string_types) and not isinstance(key, bytes):
115+
if not is_string_type(key):
116116
raise TypeError('Expecting a string- or bytes-formatted key.')
117117

118-
if isinstance(key, text_type):
118+
if not isinstance(key, binary_type):
119119
key = key.encode('utf-8')
120120

121121
invalid_strings = [
@@ -156,8 +156,8 @@ def prepare_key(self, key):
156156
isinstance(key, RSAPublicKey):
157157
return key
158158

159-
if isinstance(key, string_types):
160-
if isinstance(key, text_type):
159+
if is_string_type(key):
160+
if not isinstance(key, binary_type):
161161
key = key.encode('utf-8')
162162

163163
try:
@@ -213,8 +213,8 @@ def prepare_key(self, key):
213213
isinstance(key, EllipticCurvePublicKey):
214214
return key
215215

216-
if isinstance(key, string_types):
217-
if isinstance(key, text_type):
216+
if is_string_type(key):
217+
if not isinstance(key, binary_type):
218218
key = key.encode('utf-8')
219219

220220
# Attempt to load key. We don't know if it's

jwt/compat.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111

1212

1313
if PY3:
14-
string_types = str,
1514
text_type = str
1615
binary_type = bytes
1716
else:
18-
string_types = basestring,
1917
text_type = unicode
2018
binary_type = str
2119

20+
string_types = (text_type, binary_type)
21+
22+
23+
def is_string_type(val):
24+
return any([isinstance(val, typ) for typ in string_types])
25+
2226

2327
def timedelta_total_seconds(delta):
2428
try:

setup.cfg

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[flake8]
22
max-line-length = 119
3-
exclude = docs/
3+
exclude =
4+
docs/,
5+
.tox/
46

57
[wheel]
68
universal = 1

tests/test_algorithms.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ def test_rsa_should_parse_pem_public_key(self):
9191
with open(key_path('testkey2_rsa.pub.pem'), 'r') as pem_key:
9292
algo.prepare_key(pem_key.read())
9393

94+
@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
95+
def test_rsa_should_accept_pem_private_key_bytes(self):
96+
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
97+
98+
with open(key_path('testkey_rsa'), 'rb') as pem_key:
99+
algo.prepare_key(pem_key.read())
100+
94101
@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
95102
def test_rsa_should_accept_unicode_key(self):
96103
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
@@ -141,6 +148,13 @@ def test_ec_should_accept_unicode_key(self):
141148
with open(key_path('testkey_ec'), 'r') as ec_key:
142149
algo.prepare_key(ensure_unicode(ec_key.read()))
143150

151+
@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
152+
def test_ec_should_accept_pem_private_key_bytes(self):
153+
algo = ECAlgorithm(ECAlgorithm.SHA256)
154+
155+
with open(key_path('testkey_ec'), 'rb') as ec_key:
156+
algo.prepare_key(ec_key.read())
157+
144158
@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
145159
def test_ec_verify_should_return_false_if_signature_invalid(self):
146160
algo = ECAlgorithm(ECAlgorithm.SHA256)

0 commit comments

Comments
 (0)