diff options
author | Mark Adams <mark@markadams.me> | 2015-10-23 18:02:49 -0500 |
---|---|---|
committer | Mark Adams <mark@markadams.me> | 2015-10-23 18:02:49 -0500 |
commit | d505613e11ebcf7288d5d6768a0279d420625b37 (patch) | |
tree | c9cbba520ff6f1ae7201182ac8b6e5aa3f73af91 | |
parent | d3388831042051862f7fd8ba993bf814d341511d (diff) | |
parent | 3a50a425822fa3663643a98c6f6692f5acd8194a (diff) | |
download | pyjwt-d505613e11ebcf7288d5d6768a0279d420625b37.tar.gz |
Merge pull request #184 from maoaiz/master
Fixed #183 AttributeError: 'NoneType' object has no attribute 'rsplit'
-rw-r--r-- | jwt/api_jws.py | 6 | ||||
-rw-r--r-- | jwt/compat.py | 2 | ||||
-rw-r--r-- | tests/test_api_jws.py | 20 |
3 files changed, 27 insertions, 1 deletions
diff --git a/jwt/api_jws.py b/jwt/api_jws.py index 3e79d41..177f5ff 100644 --- a/jwt/api_jws.py +++ b/jwt/api_jws.py @@ -5,7 +5,7 @@ import warnings from collections import Mapping from .algorithms import Algorithm, get_default_algorithms # NOQA -from .compat import string_types, text_type +from .compat import binary_type, string_types, text_type from .exceptions import DecodeError, InvalidAlgorithmError, InvalidTokenError from .utils import base64url_decode, base64url_encode, merge_dict @@ -135,6 +135,10 @@ class PyJWS(object): if isinstance(jwt, text_type): jwt = jwt.encode('utf-8') + if not issubclass(type(jwt), binary_type): + raise DecodeError("Invalid token type. Token must be a {0}".format( + binary_type)) + try: signing_input, crypto_segment = jwt.rsplit(b'.', 1) header_segment, payload_segment = signing_input.split(b'.', 1) diff --git a/jwt/compat.py b/jwt/compat.py index 11d423b..8f6bfed 100644 --- a/jwt/compat.py +++ b/jwt/compat.py @@ -13,9 +13,11 @@ PY3 = sys.version_info[0] == 3 if PY3: string_types = str, text_type = str + binary_type = bytes else: string_types = basestring, text_type = unicode + binary_type = str def timedelta_total_seconds(delta): diff --git a/tests/test_api_jws.py b/tests/test_api_jws.py index 2079bef..c56ec4b 100644 --- a/tests/test_api_jws.py +++ b/tests/test_api_jws.py @@ -122,6 +122,26 @@ class TestJWS: exception = context.value assert str(exception) == 'Not enough segments' + def test_decode_invalid_token_type_is_none(self, jws): + example_jws = None + example_secret = 'secret' + + with pytest.raises(DecodeError) as context: + jws.decode(example_jws, example_secret) + + exception = context.value + assert 'Invalid token type' in str(exception) + + def test_decode_invalid_token_type_is_int(self, jws): + example_jws = 123 + example_secret = 'secret' + + with pytest.raises(DecodeError) as context: + jws.decode(example_jws, example_secret) + + exception = context.value + assert 'Invalid token type' in str(exception) + def test_decode_with_non_mapping_header_throws_exception(self, jws): secret = 'secret' example_jws = ('MQ' # == 1 |