summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Adams <mark@markadams.me>2015-01-18 10:36:09 -0600
committerMark Adams <mark@markadams.me>2015-01-18 10:36:34 -0600
commit8e2adaefbf9e92e128ea034d6c5ab52dc1053047 (patch)
treeee8fe3b17bb5dabba448b52420f0c4caa1c7bb0e
parente49582f6d1bad64d3bf5260049aab4eb08a94e70 (diff)
downloadpyjwt-8e2adaefbf9e92e128ea034d6c5ab52dc1053047.tar.gz
Fixed a couple of anomalies after the last rebase.
-rw-r--r--jwt/__init__.py5
-rw-r--r--jwt/algorithms.py17
-rw-r--r--jwt/utils.py27
3 files changed, 8 insertions, 41 deletions
diff --git a/jwt/__init__.py b/jwt/__init__.py
index d48528f..a1c11e2 100644
--- a/jwt/__init__.py
+++ b/jwt/__init__.py
@@ -6,7 +6,6 @@ http://self-issued.info/docs/draft-jones-json-web-token-01.html
"""
import binascii
-import sys
from calendar import timegm
from collections import Mapping
@@ -14,8 +13,7 @@ from datetime import datetime, timedelta
from jwt.utils import base64url_decode, base64url_encode
-from .compat import (json, string_types, text_type, constant_time_compare,
- timedelta_total_seconds)
+from .compat import (json, string_types, text_type, timedelta_total_seconds)
__version__ = '0.4.1'
@@ -53,6 +51,7 @@ def register_algorithm(alg_id, alg_obj):
from jwt.algorithms import Algorithm, _register_default_algorithms # NOQA
_register_default_algorithms()
+
class InvalidTokenError(Exception):
pass
diff --git a/jwt/algorithms.py b/jwt/algorithms.py
index 1fd0ca0..9390636 100644
--- a/jwt/algorithms.py
+++ b/jwt/algorithms.py
@@ -1,13 +1,8 @@
import hashlib
import hmac
-import sys
from jwt import register_algorithm
-from jwt.utils import constant_time_compare
-
-if sys.version_info >= (3, 0, 0):
- unicode = str
- basestring = str
+from jwt.compat import constant_time_compare, string_types, text_type
try:
from cryptography.hazmat.primitives import interfaces, hashes
@@ -66,10 +61,10 @@ class HMACAlgorithm(Algorithm):
self.hash_alg = hash_alg
def prepare_key(self, key):
- if not isinstance(key, basestring) and not isinstance(key, bytes):
+ if not isinstance(key, string_types) and not isinstance(key, text_type):
raise TypeError('Expecting a string- or bytes-formatted key.')
- if isinstance(key, unicode):
+ if isinstance(key, text_type):
key = key.encode('utf-8')
return key
@@ -92,7 +87,7 @@ if has_crypto:
return key
if isinstance(key, basestring):
- if isinstance(key, unicode):
+ if isinstance(key, text_type):
key = key.encode('utf-8')
try:
@@ -140,8 +135,8 @@ if has_crypto:
isinstance(key, interfaces.EllipticCurvePublicKey):
return key
- if isinstance(key, basestring):
- if isinstance(key, unicode):
+ if isinstance(key, string_types):
+ if isinstance(key, text_type):
key = key.encode('utf-8')
# Attempt to load key. We don't know if it's
diff --git a/jwt/utils.py b/jwt/utils.py
index 6e3cfb9..e6c1ef3 100644
--- a/jwt/utils.py
+++ b/jwt/utils.py
@@ -1,6 +1,4 @@
import base64
-import hmac
-import sys
def base64url_decode(input):
@@ -14,28 +12,3 @@ def base64url_decode(input):
def base64url_encode(input):
return base64.urlsafe_b64encode(input).replace(b'=', b'')
-
-try:
- constant_time_compare = hmac.compare_digest
-except AttributeError:
- # Fallback for Python < 2.7.7 and Python < 3.3
- def constant_time_compare(val1, val2):
- """
- Returns True if the two strings are equal, False otherwise.
-
- The time taken is independent of the number of characters that match.
- """
- if len(val1) != len(val2):
- return False
-
- result = 0
-
- if sys.version_info >= (3, 0, 0):
- # Bytes are numbers
- for x, y in zip(val1, val2):
- result |= x ^ y
- else:
- for x, y in zip(val1, val2):
- result |= ord(x) ^ ord(y)
-
- return result == 0