Skip to content

Commit da7be73

Browse files
committed
Handle compatibility branching in a compat module
1 parent e8746b0 commit da7be73

File tree

2 files changed

+48
-45
lines changed

2 files changed

+48
-45
lines changed

jwt/__init__.py

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,11 @@
99
import binascii
1010
import hashlib
1111
import hmac
12-
import sys
13-
1412
from datetime import datetime, timedelta
1513
from calendar import timegm
1614
from collections import Mapping
1715

18-
try:
19-
import json
20-
except ImportError:
21-
import simplejson as json
22-
23-
24-
if sys.version_info >= (3, 0, 0):
25-
unicode = str
26-
basestring = str
16+
from jwt.compat import json, unicode, basestring, constant_time_compare
2717

2818

2919
__version__ = '0.4.0'
@@ -234,32 +224,6 @@ def prepare_ES_key(key):
234224
pass
235225

236226

237-
try:
238-
constant_time_compare = hmac.compare_digest
239-
except AttributeError:
240-
# Fallback for Python < 2.7.7 and Python < 3.3
241-
def constant_time_compare(val1, val2):
242-
"""
243-
Returns True if the two strings are equal, False otherwise.
244-
245-
The time taken is independent of the number of characters that match.
246-
"""
247-
if len(val1) != len(val2):
248-
return False
249-
250-
result = 0
251-
252-
if sys.version_info >= (3, 0, 0):
253-
# Bytes are numbers
254-
for x, y in zip(val1, val2):
255-
result |= x ^ y
256-
else:
257-
for x, y in zip(val1, val2):
258-
result |= ord(x) ^ ord(y)
259-
260-
return result == 0
261-
262-
263227
def base64url_decode(input):
264228
rem = len(input) % 4
265229

@@ -389,14 +353,7 @@ def verify_signature(payload, signing_input, header, signature, key='',
389353
issuer=None):
390354

391355
if isinstance(leeway, timedelta):
392-
try:
393-
leeway.total_seconds
394-
except AttributeError:
395-
# On Python 2.6, timedelta instances do not have
396-
# a .total_seconds() method.
397-
leeway = leeway.days * 24 * 60 * 60 + leeway.seconds
398-
else:
399-
leeway = leeway.total_seconds()
356+
leeway = leeway.days * 24 * 60 * 60 + leeway.seconds
400357

401358
if not isinstance(audience, (basestring, type(None))):
402359
raise TypeError('audience must be a string or None')

jwt/compat.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
The `compat` module provides support for backwards compatibility with older
3+
versions of django/python, and compatibility wrappers around optional packages.
4+
"""
5+
# flake8: noqa
6+
import sys
7+
import hmac
8+
9+
try:
10+
import json
11+
except ImportError:
12+
import simplejson as json
13+
14+
15+
if sys.version_info >= (3, 0, 0):
16+
unicode = str
17+
basestring = str
18+
else:
19+
unicode = unicode
20+
basestring = basestring
21+
22+
23+
try:
24+
constant_time_compare = hmac.compare_digest
25+
except AttributeError:
26+
# Fallback for Python < 2.7.7 and Python < 3.3
27+
def constant_time_compare(val1, val2):
28+
"""
29+
Returns True if the two strings are equal, False otherwise.
30+
31+
The time taken is independent of the number of characters that match.
32+
"""
33+
if len(val1) != len(val2):
34+
return False
35+
36+
result = 0
37+
38+
if sys.version_info >= (3, 0, 0):
39+
# Bytes are numbers
40+
for x, y in zip(val1, val2):
41+
result |= x ^ y
42+
else:
43+
for x, y in zip(val1, val2):
44+
result |= ord(x) ^ ord(y)
45+
46+
return result == 0

0 commit comments

Comments
 (0)