Skip to content

Commit

Permalink
[py27] Remove useless compatibility files
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien Vallet committed Nov 4, 2019
1 parent d844ab0 commit 275ff70
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 71 deletions.
4 changes: 1 addition & 3 deletions jwt/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env python

from __future__ import absolute_import, print_function
#!/usr/bin/env python3

import argparse
import json
Expand Down
62 changes: 11 additions & 51 deletions jwt/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,26 @@
import struct
import sys

PY3 = sys.version_info[0] == 3


if PY3:
text_type = str
binary_type = bytes
else:
text_type = unicode
binary_type = str

string_types = (text_type, binary_type)
text_type = str
binary_type = bytes
string_types = (str, bytes)

try:
# Importing ABCs from collections will be removed in PY3.8
from collections.abc import Iterable, Mapping
except ImportError:
from collections import Iterable, Mapping

try:
constant_time_compare = hmac.compare_digest
except AttributeError:
# Fallback for Python < 2.7
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

for x, y in zip(val1, val2):
result |= ord(x) ^ ord(y)

return result == 0


# Use int.to_bytes if it exists (Python 3)
if getattr(int, "to_bytes", None):

def bytes_from_int(val):
remaining = val
byte_length = 0

while remaining != 0:
remaining = remaining >> 8
byte_length += 1

return val.to_bytes(byte_length, "big", signed=False)
constant_time_compare = hmac.compare_digest


else:
def bytes_from_int(val):
remaining = val
byte_length = 0

def bytes_from_int(val):
buf = []
while val:
val, remainder = divmod(val, 256)
buf.append(remainder)
while remaining != 0:
remaining = remaining >> 8
byte_length += 1

buf.reverse()
return struct.pack("%sB" % len(buf), *buf)
return val.to_bytes(byte_length, "big", signed=False)
12 changes: 0 additions & 12 deletions tests/compat.py

This file was deleted.

8 changes: 3 additions & 5 deletions tests/test_api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
)
from jwt.utils import base64url_decode, force_bytes, force_unicode

from .compat import string_types, text_type

try:
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import (
Expand Down Expand Up @@ -107,7 +105,7 @@ def test_decode_fails_when_alg_is_not_on_method_algorithms_param(

def test_decode_works_with_unicode_token(self, jws):
secret = "secret"
unicode_jws = text_type(
unicode_jws = (
"eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9"
".eyJoZWxsbyI6ICJ3b3JsZCJ9"
".tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8"
Expand Down Expand Up @@ -732,13 +730,13 @@ def test_encode_headers_parameter_adds_headers(self, jws, payload):
headers = {"testheader": True}
token = jws.encode(payload, "secret", headers=headers)

if not isinstance(token, string_types):
if not isinstance(token, str):
token = token.decode()

header = token[0 : token.index(".")].encode()
header = base64url_decode(header)

if not isinstance(header, text_type):
if not isinstance(header, str):
header = header.decode()

header_obj = json.loads(header)
Expand Down

0 comments on commit 275ff70

Please sign in to comment.