Skip to content

Commit 8e2adae

Browse files
committed
Fixed a couple of anomalies after the last rebase.
1 parent e49582f commit 8e2adae

File tree

3 files changed

+8
-41
lines changed

3 files changed

+8
-41
lines changed

jwt/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66
"""
77

88
import binascii
9-
import sys
109

1110
from calendar import timegm
1211
from collections import Mapping
1312
from datetime import datetime, timedelta
1413

1514
from jwt.utils import base64url_decode, base64url_encode
1615

17-
from .compat import (json, string_types, text_type, constant_time_compare,
18-
timedelta_total_seconds)
16+
from .compat import (json, string_types, text_type, timedelta_total_seconds)
1917

2018

2119
__version__ = '0.4.1'
@@ -53,6 +51,7 @@ def register_algorithm(alg_id, alg_obj):
5351
from jwt.algorithms import Algorithm, _register_default_algorithms # NOQA
5452
_register_default_algorithms()
5553

54+
5655
class InvalidTokenError(Exception):
5756
pass
5857

jwt/algorithms.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import hashlib
22
import hmac
3-
import sys
43

54
from jwt import register_algorithm
6-
from jwt.utils import constant_time_compare
7-
8-
if sys.version_info >= (3, 0, 0):
9-
unicode = str
10-
basestring = str
5+
from jwt.compat import constant_time_compare, string_types, text_type
116

127
try:
138
from cryptography.hazmat.primitives import interfaces, hashes
@@ -66,10 +61,10 @@ def __init__(self, hash_alg):
6661
self.hash_alg = hash_alg
6762

6863
def prepare_key(self, key):
69-
if not isinstance(key, basestring) and not isinstance(key, bytes):
64+
if not isinstance(key, string_types) and not isinstance(key, text_type):
7065
raise TypeError('Expecting a string- or bytes-formatted key.')
7166

72-
if isinstance(key, unicode):
67+
if isinstance(key, text_type):
7368
key = key.encode('utf-8')
7469

7570
return key
@@ -92,7 +87,7 @@ def prepare_key(self, key):
9287
return key
9388

9489
if isinstance(key, basestring):
95-
if isinstance(key, unicode):
90+
if isinstance(key, text_type):
9691
key = key.encode('utf-8')
9792

9893
try:
@@ -140,8 +135,8 @@ def prepare_key(self, key):
140135
isinstance(key, interfaces.EllipticCurvePublicKey):
141136
return key
142137

143-
if isinstance(key, basestring):
144-
if isinstance(key, unicode):
138+
if isinstance(key, string_types):
139+
if isinstance(key, text_type):
145140
key = key.encode('utf-8')
146141

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

jwt/utils.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import base64
2-
import hmac
3-
import sys
42

53

64
def base64url_decode(input):
@@ -14,28 +12,3 @@ def base64url_decode(input):
1412

1513
def base64url_encode(input):
1614
return base64.urlsafe_b64encode(input).replace(b'=', b'')
17-
18-
try:
19-
constant_time_compare = hmac.compare_digest
20-
except AttributeError:
21-
# Fallback for Python < 2.7.7 and Python < 3.3
22-
def constant_time_compare(val1, val2):
23-
"""
24-
Returns True if the two strings are equal, False otherwise.
25-
26-
The time taken is independent of the number of characters that match.
27-
"""
28-
if len(val1) != len(val2):
29-
return False
30-
31-
result = 0
32-
33-
if sys.version_info >= (3, 0, 0):
34-
# Bytes are numbers
35-
for x, y in zip(val1, val2):
36-
result |= x ^ y
37-
else:
38-
for x, y in zip(val1, val2):
39-
result |= ord(x) ^ ord(y)
40-
41-
return result == 0

0 commit comments

Comments
 (0)