|
5 | 5 | http://self-issued.info/docs/draft-jones-json-web-token-01.html |
6 | 6 | """ |
7 | 7 | from __future__ import unicode_literals |
| 8 | + |
8 | 9 | import base64 |
9 | 10 | import binascii |
10 | 11 | import hashlib |
|
13 | 14 | from calendar import timegm |
14 | 15 | from collections import Mapping |
15 | 16 |
|
16 | | -from .compat import (json, unicode, basestring, constant_time_compare, |
| 17 | +from .compat import (json, string_types, text_type, constant_time_compare, |
17 | 18 | timedelta_total_seconds) |
18 | 19 |
|
19 | 20 |
|
@@ -77,10 +78,10 @@ class InvalidIssuerError(InvalidTokenError): |
77 | 78 |
|
78 | 79 |
|
79 | 80 | def prepare_HS_key(key): |
80 | | - if not isinstance(key, basestring) and not isinstance(key, bytes): |
| 81 | + if not isinstance(key, string_types) and not isinstance(key, bytes): |
81 | 82 | raise TypeError('Expecting a string- or bytes-formatted key.') |
82 | 83 |
|
83 | | - if isinstance(key, unicode): |
| 84 | + if isinstance(key, text_type): |
84 | 85 | key = key.encode('utf-8') |
85 | 86 |
|
86 | 87 | return key |
@@ -142,8 +143,8 @@ def prepare_RS_key(key): |
142 | 143 | isinstance(key, interfaces.RSAPublicKey): |
143 | 144 | return key |
144 | 145 |
|
145 | | - if isinstance(key, basestring): |
146 | | - if isinstance(key, unicode): |
| 146 | + if isinstance(key, string_types): |
| 147 | + if isinstance(key, text_type): |
147 | 148 | key = key.encode('utf-8') |
148 | 149 |
|
149 | 150 | try: |
@@ -198,8 +199,8 @@ def prepare_ES_key(key): |
198 | 199 | isinstance(key, interfaces.EllipticCurvePublicKey): |
199 | 200 | return key |
200 | 201 |
|
201 | | - if isinstance(key, basestring): |
202 | | - if isinstance(key, unicode): |
| 202 | + if isinstance(key, string_types): |
| 203 | + if isinstance(key, text_type): |
203 | 204 | key = key.encode('utf-8') |
204 | 205 |
|
205 | 206 | # Attempt to load key. We don't know if it's |
@@ -239,7 +240,7 @@ def base64url_encode(input): |
239 | 240 |
|
240 | 241 |
|
241 | 242 | def header(jwt): |
242 | | - if isinstance(jwt, unicode): |
| 243 | + if isinstance(jwt, text_type): |
243 | 244 | jwt = jwt.encode('utf-8') |
244 | 245 | header_segment = jwt.split(b'.', 1)[0] |
245 | 246 | try: |
@@ -311,7 +312,7 @@ def decode(jwt, key='', verify=True, **kwargs): |
311 | 312 |
|
312 | 313 |
|
313 | 314 | def load(jwt): |
314 | | - if isinstance(jwt, unicode): |
| 315 | + if isinstance(jwt, text_type): |
315 | 316 | jwt = jwt.encode('utf-8') |
316 | 317 | try: |
317 | 318 | signing_input, crypto_segment = jwt.rsplit(b'.', 1) |
@@ -356,7 +357,7 @@ def verify_signature(payload, signing_input, header, signature, key='', |
356 | 357 | if isinstance(leeway, timedelta): |
357 | 358 | leeway = timedelta_total_seconds(leeway) |
358 | 359 |
|
359 | | - if not isinstance(audience, (basestring, type(None))): |
| 360 | + if not isinstance(audience, (string_types, type(None))): |
360 | 361 | raise TypeError('audience must be a string or None') |
361 | 362 |
|
362 | 363 | try: |
@@ -388,11 +389,11 @@ def verify_signature(payload, signing_input, header, signature, key='', |
388 | 389 |
|
389 | 390 | if 'aud' in payload: |
390 | 391 | audience_claims = payload['aud'] |
391 | | - if isinstance(audience_claims, basestring): |
| 392 | + if isinstance(audience_claims, string_types): |
392 | 393 | audience_claims = [audience_claims] |
393 | 394 | if not isinstance(audience_claims, list): |
394 | 395 | raise InvalidAudienceError('Invalid claim format in token') |
395 | | - if any(not isinstance(c, basestring) for c in audience_claims): |
| 396 | + if any(not isinstance(c, string_types) for c in audience_claims): |
396 | 397 | raise InvalidAudienceError('Invalid claim format in token') |
397 | 398 | if audience not in audience_claims: |
398 | 399 | raise InvalidAudienceError('Invalid audience') |
|
0 commit comments