|
9 | 9 | import binascii |
10 | 10 | import hashlib |
11 | 11 | import hmac |
12 | | -import sys |
13 | | - |
14 | 12 | from datetime import datetime, timedelta |
15 | 13 | from calendar import timegm |
16 | 14 | from collections import Mapping |
17 | 15 |
|
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 |
27 | 17 |
|
28 | 18 |
|
29 | 19 | __version__ = '0.4.0' |
@@ -234,32 +224,6 @@ def prepare_ES_key(key): |
234 | 224 | pass |
235 | 225 |
|
236 | 226 |
|
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 | | - |
263 | 227 | def base64url_decode(input): |
264 | 228 | rem = len(input) % 4 |
265 | 229 |
|
@@ -389,14 +353,7 @@ def verify_signature(payload, signing_input, header, signature, key='', |
389 | 353 | issuer=None): |
390 | 354 |
|
391 | 355 | 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 |
400 | 357 |
|
401 | 358 | if not isinstance(audience, (basestring, type(None))): |
402 | 359 | raise TypeError('audience must be a string or None') |
|
0 commit comments