Skip to content

Commit e44cbc8

Browse files
authored
Remove more usages of six (#253)
1 parent 5fb8673 commit e44cbc8

File tree

8 files changed

+36
-38
lines changed

8 files changed

+36
-38
lines changed

jose/backends/base.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import six
2-
3-
from ..utils import base64url_encode
1+
from ..utils import base64url_encode, ensure_binary
42

53

64
class Key:
@@ -79,7 +77,7 @@ def unwrap_key(self, wrapped_key):
7977

8078
class DIRKey(Key):
8179
def __init__(self, key_data, algorithm):
82-
self._key = six.ensure_binary(key_data)
80+
self._key = ensure_binary(key_data)
8381
self._alg = algorithm
8482

8583
def to_dict(self):

jose/backends/cryptography_backend.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import math
22
import warnings
33

4-
import six
54
from cryptography.exceptions import InvalidSignature, InvalidTag
65
from cryptography.hazmat.backends import default_backend
76
from cryptography.hazmat.bindings.openssl.binding import Binding
@@ -17,7 +16,7 @@
1716

1817
from ..constants import ALGORITHMS
1918
from ..exceptions import JWEError, JWKError
20-
from ..utils import base64_to_long, base64url_decode, base64url_encode, long_to_base64
19+
from ..utils import base64_to_long, base64url_decode, base64url_encode, ensure_binary, long_to_base64
2120
from .base import Key
2221

2322
_binding = None
@@ -486,7 +485,7 @@ def to_dict(self):
486485
return data
487486

488487
def encrypt(self, plain_text, aad=None):
489-
plain_text = six.ensure_binary(plain_text)
488+
plain_text = ensure_binary(plain_text)
490489
try:
491490
iv = get_random_bytes(algorithms.AES.block_size//8)
492491
mode = self._mode(iv)
@@ -509,9 +508,9 @@ def encrypt(self, plain_text, aad=None):
509508
raise JWEError(e)
510509

511510
def decrypt(self, cipher_text, iv=None, aad=None, tag=None):
512-
cipher_text = six.ensure_binary(cipher_text)
511+
cipher_text = ensure_binary(cipher_text)
513512
try:
514-
iv = six.ensure_binary(iv)
513+
iv = ensure_binary(iv)
515514
mode = self._mode(iv)
516515
if mode.name == "GCM":
517516
if tag is None:
@@ -537,12 +536,12 @@ def decrypt(self, cipher_text, iv=None, aad=None, tag=None):
537536
raise JWEError(e)
538537

539538
def wrap_key(self, key_data):
540-
key_data = six.ensure_binary(key_data)
539+
key_data = ensure_binary(key_data)
541540
cipher_text = aes_key_wrap(self._key, key_data, default_backend())
542541
return cipher_text # IV, cipher text, auth tag
543542

544543
def unwrap_key(self, wrapped_key):
545-
wrapped_key = six.ensure_binary(wrapped_key)
544+
wrapped_key = ensure_binary(wrapped_key)
546545
try:
547546
plain_text = aes_key_unwrap(self._key, wrapped_key, default_backend())
548547
except InvalidUnwrap as cause:
@@ -611,15 +610,15 @@ def to_dict(self):
611610
}
612611

613612
def sign(self, msg):
614-
msg = six.ensure_binary(msg)
613+
msg = ensure_binary(msg)
615614
h = hmac.HMAC(self.prepared_key, self._hash_alg, backend=default_backend())
616615
h.update(msg)
617616
signature = h.finalize()
618617
return signature
619618

620619
def verify(self, msg, sig):
621-
msg = six.ensure_binary(msg)
622-
sig = six.ensure_binary(sig)
620+
msg = ensure_binary(msg)
621+
sig = ensure_binary(sig)
623622
h = hmac.HMAC(self.prepared_key, self._hash_alg, backend=default_backend())
624623
h.update(msg)
625624
try:

jose/jwe.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import zlib
44
from struct import pack
55

6-
import six
7-
86
try:
97
from collections.abc import Mapping # Python 3
108
except ImportError:
@@ -14,7 +12,7 @@
1412
from .backends import get_random_bytes
1513
from .constants import ALGORITHMS, ZIPS
1614
from .exceptions import JWEError, JWEParseError
17-
from .utils import base64url_decode, base64url_encode
15+
from .utils import base64url_decode, base64url_encode, ensure_binary
1816

1917

2018
def encrypt(plaintext, key, encryption=ALGORITHMS.A256GCM,
@@ -49,7 +47,7 @@ def encrypt(plaintext, key, encryption=ALGORITHMS.A256GCM,
4947
'eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..McILMB3dYsNJSuhcDzQshA.OfX9H_mcUpHDeRM4IA.CcnTWqaqxNsjT4eCaUABSg'
5048
5149
"""
52-
plaintext = six.ensure_binary(plaintext) # Make sure it's bytes
50+
plaintext = ensure_binary(plaintext) # Make sure it's bytes
5351
if algorithm not in ALGORITHMS.SUPPORTED:
5452
raise JWEError('Algorithm %s not supported.' % algorithm)
5553
if encryption not in ALGORITHMS.SUPPORTED:
@@ -277,7 +275,7 @@ def _jwe_compact_deserialize(jwe_bytes):
277275
# Vector, the JWE Ciphertext, the JWE Authentication Tag, and the
278276
# JWE AAD, following the restriction that no line breaks,
279277
# whitespace, or other additional characters have been used.
280-
jwe_bytes = six.ensure_binary(jwe_bytes)
278+
jwe_bytes = ensure_binary(jwe_bytes)
281279
try:
282280
header_segment, encrypted_key_segment, iv_segment, \
283281
cipher_text_segment, auth_tag_segment = jwe_bytes.split(b'.', 4)
@@ -305,9 +303,9 @@ def _jwe_compact_deserialize(jwe_bytes):
305303
# values that together comprise the JOSE Header.
306304

307305
try:
308-
header = json.loads(six.ensure_str(header_data))
306+
header = json.loads(header_data)
309307
except ValueError as e:
310-
raise JWEParseError('Invalid header string: %s' % e)
308+
raise JWEParseError(f'Invalid header string: {e}')
311309

312310
if not isinstance(header, Mapping):
313311
raise JWEParseError('Invalid header string: must be a json object')
@@ -598,7 +596,7 @@ def _jwe_compact_serialize(encoded_header, encrypted_cek, iv, cipher_text, auth_
598596
Returns:
599597
(str): JWE compact serialized string
600598
"""
601-
cipher_text = six.ensure_binary(cipher_text) # Maker sure it's bytes
599+
cipher_text = ensure_binary(cipher_text)
602600
encoded_encrypted_cek = base64url_encode(encrypted_cek)
603601
encoded_iv = base64url_encode(iv)
604602
encoded_cipher_text = base64url_encode(cipher_text)

jose/utils.py

+10
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,13 @@ def timedelta_total_seconds(delta):
9595
delta (timedelta): A timedelta to convert to seconds.
9696
"""
9797
return delta.days * 24 * 60 * 60 + delta.seconds
98+
99+
100+
def ensure_binary(s):
101+
"""Coerce **s** to bytes."""
102+
103+
if isinstance(s, bytes):
104+
return s
105+
if isinstance(s, str):
106+
return s.encode('utf-8', 'strict')
107+
raise TypeError(f"not expecting type '{type(s)}'")

requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
pycryptodome
2-
six
32
rsa
43
ecdsa != 0.15
54
pyasn1

setup.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ def get_packages(package):
2626
'pycrypto': ['pycrypto >=2.6.0, <2.7.0'] + pyasn1,
2727
'pycryptodome': ['pycryptodome >=3.3.1, <4.0.0'] + pyasn1,
2828
}
29-
legacy_backend_requires = ['ecdsa != 0.15', 'rsa'] + pyasn1
30-
install_requires = ['six <2.0']
31-
3229
# TODO: work this into the extras selection instead.
33-
install_requires += legacy_backend_requires
30+
install_requires = ['ecdsa != 0.15', 'rsa'] + pyasn1
3431

3532

3633
setup(
@@ -70,7 +67,6 @@ def get_packages(package):
7067
'setuptools>=39.2.0',
7168
],
7269
tests_require=[
73-
'six',
7470
'ecdsa != 0.15',
7571
'pytest',
7672
'pytest-cov',

tests/algorithms/test_AES.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from binascii import hexlify, unhexlify
22

33
import pytest
4-
import six
54

65
from jose.constants import ALGORITHMS
76

tests/test_jwe.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22

33
import pytest
4-
import six
54

65
import jose.backends
76
from jose import jwe
@@ -429,7 +428,7 @@ def test_alg_enc_headers(self):
429428
enc = ALGORITHMS.A256CBC_HS512
430429
alg = ALGORITHMS.RSA_OAEP_256
431430
encrypted = jwe.encrypt("Text", PUBLIC_KEY_PEM, enc, alg)
432-
header = json.loads(six.ensure_str(base64url_decode(encrypted.split(b".")[0])))
431+
header = json.loads(base64url_decode(encrypted.split(b".")[0]))
433432
assert header["enc"] == enc
434433
assert header["alg"] == alg
435434

@@ -439,15 +438,15 @@ def test_cty_header_present_when_provided(self):
439438
alg = ALGORITHMS.RSA_OAEP_256
440439
encrypted = jwe.encrypt("Text", PUBLIC_KEY_PEM, enc, alg,
441440
cty="expected")
442-
header = json.loads(six.ensure_str(base64url_decode(encrypted.split(b".")[0])))
441+
header = json.loads(base64url_decode(encrypted.split(b".")[0]))
443442
assert header["cty"] == "expected"
444443

445444
@pytest.mark.skipif(AESKey is None, reason="No AES backend")
446445
def test_cty_header_not_present_when_not_provided(self):
447446
enc = ALGORITHMS.A256CBC_HS512
448447
alg = ALGORITHMS.RSA_OAEP_256
449448
encrypted = jwe.encrypt("Text", PUBLIC_KEY_PEM, enc, alg)
450-
header = json.loads(six.ensure_str(base64url_decode(encrypted.split(b".")[0])))
449+
header = json.loads(base64url_decode(encrypted.split(b".")[0]))
451450
assert "cty" not in header
452451

453452
@pytest.mark.skipif(AESKey is None, reason="No AES backend")
@@ -456,15 +455,15 @@ def test_zip_header_present_when_provided(self):
456455
alg = ALGORITHMS.RSA_OAEP_256
457456
encrypted = jwe.encrypt(b"Text", PUBLIC_KEY_PEM, enc, alg,
458457
zip=ZIPS.DEF)
459-
header = json.loads(six.ensure_str(base64url_decode(encrypted.split(b".")[0])))
458+
header = json.loads(base64url_decode(encrypted.split(b".")[0]))
460459
assert header["zip"] == ZIPS.DEF
461460

462461
@pytest.mark.skipif(AESKey is None, reason="No AES backend")
463462
def test_zip_header_not_present_when_not_provided(self):
464463
enc = ALGORITHMS.A256CBC_HS512
465464
alg = ALGORITHMS.RSA_OAEP_256
466465
encrypted = jwe.encrypt(b"Text", PUBLIC_KEY_PEM, enc, alg)
467-
header = json.loads(six.ensure_str(base64url_decode(encrypted.split(b".")[0])))
466+
header = json.loads(base64url_decode(encrypted.split(b".")[0]))
468467
assert "zip" not in header
469468

470469
@pytest.mark.skipif(AESKey is None, reason="No AES backend")
@@ -473,7 +472,7 @@ def test_zip_header_not_present_when_none(self):
473472
alg = ALGORITHMS.RSA_OAEP_256
474473
encrypted = jwe.encrypt("Text", PUBLIC_KEY_PEM, enc, alg,
475474
zip=ZIPS.NONE)
476-
header = json.loads(six.ensure_str(base64url_decode(encrypted.split(b".")[0])))
475+
header = json.loads(base64url_decode(encrypted.split(b".")[0]))
477476
assert "zip" not in header
478477

479478
@pytest.mark.skipif(AESKey is None, reason="No AES backend")
@@ -482,13 +481,13 @@ def test_kid_header_present_when_provided(self):
482481
alg = ALGORITHMS.RSA_OAEP_256
483482
encrypted = jwe.encrypt("Text", PUBLIC_KEY_PEM, enc, alg,
484483
kid="expected")
485-
header = json.loads(six.ensure_str(base64url_decode(encrypted.split(b".")[0])))
484+
header = json.loads(base64url_decode(encrypted.split(b".")[0]))
486485
assert header["kid"] == "expected"
487486

488487
@pytest.mark.skipif(AESKey is None, reason="No AES backend")
489488
def test_kid_header_not_present_when_not_provided(self):
490489
enc = ALGORITHMS.A256CBC_HS512
491490
alg = ALGORITHMS.RSA_OAEP_256
492491
encrypted = jwe.encrypt("Text", PUBLIC_KEY_PEM, enc, alg)
493-
header = json.loads(six.ensure_str(base64url_decode(encrypted.split(b".")[0])))
492+
header = json.loads(base64url_decode(encrypted.split(b".")[0]))
494493
assert "kid" not in header

0 commit comments

Comments
 (0)