Skip to content

Commit c9e1035

Browse files
authored
Update code to to use python 3.6+ syntax. (#241)
1 parent 23c4e75 commit c9e1035

File tree

12 files changed

+41
-65
lines changed

12 files changed

+41
-65
lines changed

jose/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
__version__ = "3.2.0"
32
__author__ = 'Michael Davis'
43
__license__ = 'MIT'

jose/backends/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ..utils import base64url_encode
44

55

6-
class Key(object):
6+
class Key:
77
"""
88
A simple interface for implementing JWK keys.
99
"""

jose/backends/cryptography_backend.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import division
2-
31
import math
42
import warnings
53

@@ -79,10 +77,10 @@ def __init__(self, key, algorithm, cryptography_backend=default_backend):
7977
self.prepared_key = self._process_jwk(key)
8078
return
8179

82-
if isinstance(key, six.string_types):
80+
if isinstance(key, str):
8381
key = key.encode('utf-8')
8482

85-
if isinstance(key, six.binary_type):
83+
if isinstance(key, bytes):
8684
# Attempt to load key. We don't know if it's
8785
# a Public Key or a Private Key, so we try
8886
# the Public Key first.
@@ -258,10 +256,10 @@ def __init__(self, key, algorithm, cryptography_backend=default_backend):
258256
self.prepared_key = self._process_jwk(key)
259257
return
260258

261-
if isinstance(key, six.string_types):
259+
if isinstance(key, str):
262260
key = key.encode('utf-8')
263261

264-
if isinstance(key, six.binary_type):
262+
if isinstance(key, bytes):
265263
try:
266264
if key.startswith(b'-----BEGIN CERTIFICATE-----'):
267265
self._process_cert(key)
@@ -468,15 +466,15 @@ def __init__(self, key, algorithm):
468466
self._mode = self.MODES.get(self._algorithm)
469467

470468
if algorithm in self.KEY_128 and len(key) != 16:
471-
raise JWKError("Key must be 128 bit for alg {}".format(algorithm))
469+
raise JWKError(f"Key must be 128 bit for alg {algorithm}")
472470
elif algorithm in self.KEY_192 and len(key) != 24:
473-
raise JWKError("Key must be 192 bit for alg {}".format(algorithm))
471+
raise JWKError(f"Key must be 192 bit for alg {algorithm}")
474472
elif algorithm in self.KEY_256 and len(key) != 32:
475-
raise JWKError("Key must be 256 bit for alg {}".format(algorithm))
473+
raise JWKError(f"Key must be 256 bit for alg {algorithm}")
476474
elif algorithm in self.KEY_384 and len(key) != 48:
477-
raise JWKError("Key must be 384 bit for alg {}".format(algorithm))
475+
raise JWKError(f"Key must be 384 bit for alg {algorithm}")
478476
elif algorithm in self.KEY_512 and len(key) != 64:
479-
raise JWKError("Key must be 512 bit for alg {}".format(algorithm))
477+
raise JWKError(f"Key must be 512 bit for alg {algorithm}")
480478

481479
self._key = key
482480

@@ -575,10 +573,10 @@ def __init__(self, key, algorithm):
575573
self.prepared_key = self._process_jwk(key)
576574
return
577575

578-
if not isinstance(key, six.string_types) and not isinstance(key, bytes):
576+
if not isinstance(key, str) and not isinstance(key, bytes):
579577
raise JWKError('Expecting a string- or bytes-formatted key.')
580578

581-
if isinstance(key, six.text_type):
579+
if isinstance(key, str):
582580
key = key.encode('utf-8')
583581

584582
invalid_strings = [

jose/backends/ecdsa_backend.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import hashlib
2-
import six
32

43
from jose.backends.base import Key
54
import ecdsa
@@ -49,10 +48,10 @@ def __init__(self, key, algorithm):
4948
self.prepared_key = self._process_jwk(key)
5049
return
5150

52-
if isinstance(key, six.string_types):
51+
if isinstance(key, str):
5352
key = key.encode('utf-8')
5453

55-
if isinstance(key, six.binary_type):
54+
if isinstance(key, bytes):
5655
# Attempt to load key. We don't know if it's
5756
# a Signing Key or a Verifying Key, so we try
5857
# the Verifying Key first.
@@ -85,7 +84,7 @@ def _process_jwk(self, jwk_dict):
8584
y = base64_to_long(jwk_dict.get('y'))
8685

8786
if not ecdsa.ecdsa.point_is_valid(self.curve.generator, x, y):
88-
raise JWKError("Point: %s, %s is not a valid point" % (x, y))
87+
raise JWKError(f"Point: {x}, {y} is not a valid point")
8988

9089
point = ecdsa.ellipticcurve.Point(self.curve.curve, x, y, self.curve.order)
9190
return ecdsa.keys.VerifyingKey.from_public_point(point, self.curve)

jose/backends/native.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import hmac
33
import os
44

5-
import six
65

76
from jose.backends.base import Key
87
from jose.constants import ALGORITHMS
@@ -35,10 +34,10 @@ def __init__(self, key, algorithm):
3534
self.prepared_key = self._process_jwk(key)
3635
return
3736

38-
if not isinstance(key, six.string_types) and not isinstance(key, bytes):
37+
if not isinstance(key, str) and not isinstance(key, bytes):
3938
raise JWKError('Expecting a string- or bytes-formatted key.')
4039

41-
if isinstance(key, six.text_type):
40+
if isinstance(key, str):
4241
key = key.encode('utf-8')
4342

4443
invalid_strings = [

jose/backends/rsa_backend.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import binascii
22

3-
import six
43
import warnings
54

65
from pyasn1.error import PyAsn1Error
@@ -147,10 +146,10 @@ def __init__(self, key, algorithm):
147146
self._prepared_key = key
148147
return
149148

150-
if isinstance(key, six.string_types):
149+
if isinstance(key, str):
151150
key = key.encode('utf-8')
152151

153-
if isinstance(key, six.binary_type):
152+
if isinstance(key, bytes):
154153
try:
155154
self._prepared_key = pyrsa.PublicKey.load_pkcs1(key)
156155
except ValueError:
@@ -234,7 +233,7 @@ def to_pem(self, pem_format='PKCS8'):
234233
elif pem_format == 'PKCS1':
235234
pem = pyrsa_pem.save_pem(der, pem_marker='RSA PRIVATE KEY')
236235
else:
237-
raise ValueError("Invalid pem format specified: %r" % (pem_format,))
236+
raise ValueError(f"Invalid pem format specified: {pem_format!r}")
238237
else:
239238
if pem_format == 'PKCS8':
240239
pkcs1_der = self._prepared_key.save_pkcs1(format="DER")
@@ -244,7 +243,7 @@ def to_pem(self, pem_format='PKCS8'):
244243
der = self._prepared_key.save_pkcs1(format='DER')
245244
pem = pyrsa_pem.save_pem(der, pem_marker='RSA PUBLIC KEY')
246245
else:
247-
raise ValueError("Invalid pem format specified: %r" % (pem_format,))
246+
raise ValueError(f"Invalid pem format specified: {pem_format!r}")
248247
return pem
249248

250249
def to_dict(self):

jose/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import hashlib
22

33

4-
class Algorithms(object):
4+
class Algorithms:
55
# DS Algorithms
66
NONE = 'none'
77
HS256 = 'HS256'
@@ -92,7 +92,7 @@ class Algorithms(object):
9292
ALGORITHMS = Algorithms()
9393

9494

95-
class Zips(object):
95+
class Zips:
9696
DEF = "DEF"
9797
NONE = None
9898
SUPPORTED = {DEF, NONE}

jose/exceptions.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
class JOSEError(Exception):
42
pass
53

jose/jwe.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def decrypt(jwe_str, key):
146146
# recipient or not.
147147
cek_valid = True
148148
except NotImplementedError:
149-
raise JWEError("alg {} is not implemented".format(alg))
149+
raise JWEError(f"alg {alg} is not implemented")
150150
except Exception:
151151
# Record whether the CEK could be successfully determined for this
152152
# recipient or not.
@@ -186,7 +186,7 @@ def decrypt(jwe_str, key):
186186
try:
187187
plain_text = _decrypt_and_auth(cek_bytes, enc, cipher_text, iv, aad, auth_tag)
188188
except NotImplementedError:
189-
raise JWEError("enc {} is not implemented".format(enc))
189+
raise JWEError(f"enc {enc} is not implemented")
190190
except Exception as e:
191191
raise JWEError(e)
192192

@@ -243,7 +243,7 @@ def _decrypt_and_auth(cek_bytes, enc, cipher_text, iv, aad, auth_tag):
243243
encryption_key = jwk.construct(cek_bytes, enc)
244244
auth_tag_check = auth_tag # GCM check auth on decrypt
245245
else:
246-
raise NotImplementedError("enc {} is not implemented!".format(enc))
246+
raise NotImplementedError(f"enc {enc} is not implemented!")
247247

248248
plaintext = encryption_key.decrypt(cipher_text, iv, aad, auth_tag)
249249
if auth_tag != auth_tag_check:
@@ -389,7 +389,7 @@ def _encrypt_and_auth(key, alg, enc, zip, plaintext, aad):
389389
try:
390390
cek_bytes, kw_cek = _get_cek(enc, alg, key)
391391
except NotImplementedError:
392-
raise JWEError("alg {} is not implemented".format(alg))
392+
raise JWEError(f"alg {alg} is not implemented")
393393

394394
if enc in ALGORITHMS.HMAC_AUTH_TAG:
395395
encryption_key, mac_key, key_len = _get_encryption_key_mac_key_and_key_length_from_cek(cek_bytes, enc)
@@ -399,7 +399,7 @@ def _encrypt_and_auth(key, alg, enc, zip, plaintext, aad):
399399
encryption_key = jwk.construct(cek_bytes, enc)
400400
iv, ciphertext, auth_tag = encryption_key.encrypt(plaintext, aad)
401401
else:
402-
raise NotImplementedError("enc {} is not implemented!".format(enc))
402+
raise NotImplementedError(f"enc {enc} is not implemented!")
403403

404404
return kw_cek, iv, ciphertext, auth_tag
405405

@@ -558,7 +558,7 @@ def _get_random_cek_bytes_for_enc(enc):
558558
elif enc == ALGORITHMS.A256CBC_HS512:
559559
num_bits = 512
560560
else:
561-
raise NotImplementedError("{} not supported".format(enc))
561+
raise NotImplementedError(f"{enc} not supported")
562562
cek_bytes = get_random_bytes(num_bits // 8)
563563
return cek_bytes
564564

jose/jws.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
21
import binascii
32
import json
4-
import six
5-
6-
try:
7-
from collections.abc import Mapping, Iterable # Python 3
8-
except ImportError:
9-
from collections import Mapping, Iterable # Python 2, will be deprecated in Python 3.8
3+
from collections.abc import Mapping, Iterable
104

115
from jose import jwk
126
from jose.backends.base import Key
@@ -181,7 +175,7 @@ def _sign_header_and_claims(encoded_header, encoded_claims, algorithm, key):
181175

182176

183177
def _load(jwt):
184-
if isinstance(jwt, six.text_type):
178+
if isinstance(jwt, str):
185179
jwt = jwt.encode('utf-8')
186180
try:
187181
signing_input, crypto_segment = jwt.rsplit(b'.', 1)
@@ -251,7 +245,7 @@ def _get_keys(key):
251245

252246
# Iterable but not text or mapping => list- or tuple-like
253247
elif (isinstance(key, Iterable) and
254-
not (isinstance(key, six.string_types) or isinstance(key, six.binary_type))):
248+
not (isinstance(key, str) or isinstance(key, bytes))):
255249
return key
256250

257251
# Scalar value, wrap in tuple.

0 commit comments

Comments
 (0)