Skip to content

Commit

Permalink
Merge pull request #213 from jpadilla/fix-type-error-on-bytes-key
Browse files Browse the repository at this point in the history
Fix a bug where a PEM private key as bytes raises a TypeError
  • Loading branch information
mark-adams committed Aug 7, 2016
2 parents 736873c + 2fe85a3 commit 899fdaf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
14 changes: 7 additions & 7 deletions jwt/algorithms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import hashlib
import hmac

from .compat import constant_time_compare, string_types, text_type
from .compat import binary_type, constant_time_compare, is_string_type
from .exceptions import InvalidKeyError
from .utils import der_to_raw_signature, raw_to_der_signature

Expand Down Expand Up @@ -112,10 +112,10 @@ def __init__(self, hash_alg):
self.hash_alg = hash_alg

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

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

invalid_strings = [
Expand Down Expand Up @@ -156,8 +156,8 @@ def prepare_key(self, key):
isinstance(key, RSAPublicKey):
return key

if isinstance(key, string_types):
if isinstance(key, text_type):
if is_string_type(key):
if not isinstance(key, binary_type):
key = key.encode('utf-8')

try:
Expand Down Expand Up @@ -213,8 +213,8 @@ def prepare_key(self, key):
isinstance(key, EllipticCurvePublicKey):
return key

if isinstance(key, string_types):
if isinstance(key, text_type):
if is_string_type(key):
if not isinstance(key, binary_type):
key = key.encode('utf-8')

# Attempt to load key. We don't know if it's
Expand Down
8 changes: 6 additions & 2 deletions jwt/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@


if PY3:
string_types = str,
text_type = str
binary_type = bytes
else:
string_types = basestring,
text_type = unicode
binary_type = str

string_types = (text_type, binary_type)


def is_string_type(val):
return any([isinstance(val, typ) for typ in string_types])


def timedelta_total_seconds(delta):
try:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ def test_rsa_should_parse_pem_public_key(self):
with open(key_path('testkey2_rsa.pub.pem'), 'r') as pem_key:
algo.prepare_key(pem_key.read())

@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
def test_rsa_should_accept_pem_private_key_bytes(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path('testkey_rsa'), 'rb') as pem_key:
algo.prepare_key(pem_key.read())

@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
def test_rsa_should_accept_unicode_key(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
Expand Down Expand Up @@ -141,6 +148,13 @@ def test_ec_should_accept_unicode_key(self):
with open(key_path('testkey_ec'), 'r') as ec_key:
algo.prepare_key(ensure_unicode(ec_key.read()))

@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
def test_ec_should_accept_pem_private_key_bytes(self):
algo = ECAlgorithm(ECAlgorithm.SHA256)

with open(key_path('testkey_ec'), 'rb') as ec_key:
algo.prepare_key(ec_key.read())

@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
def test_ec_verify_should_return_false_if_signature_invalid(self):
algo = ECAlgorithm(ECAlgorithm.SHA256)
Expand Down

0 comments on commit 899fdaf

Please sign in to comment.