Skip to content

Commit d379654

Browse files
committed
automatic fixes by ruff
1 parent 5fbc4d7 commit d379654

35 files changed

+119
-153
lines changed

src/cryptojwt/jwe/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
}
3939

4040

41-
class Encrypter(object):
41+
class Encrypter:
4242
"""Abstract base class for encryption algorithms."""
4343

4444
def __init__(self, with_digest=False):

src/cryptojwt/jwe/aes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(self, key_len=32, key=None, msg_padding="PKCS7"):
3030
self.padder = PKCS7(128).padder()
3131
self.unpadder = PKCS7(128).unpadder()
3232
else:
33-
raise Unsupported("Message padding: {}".format(msg_padding))
33+
raise Unsupported(f"Message padding: {msg_padding}")
3434

3535
self.iv = None
3636

src/cryptojwt/jwe/jwe.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
from ..jwk.ec import ECKey
55
from ..jwk.hmac import SYMKey
66
from ..jwk.jwk import key_from_jwk_dict
7-
from ..jwk.rsa import RSAKey
87
from ..jwx import JWx
98
from .exception import DecryptionFailed
109
from .exception import NoSuitableDecryptionKey
11-
from .exception import NoSuitableECDHKey
1210
from .exception import NoSuitableEncryptionKey
1311
from .exception import NotSupportedAlgorithm
1412
from .exception import WrongEncryptionAlgorithm
@@ -133,7 +131,7 @@ def encrypt(self, keys=None, cek="", iv="", **kwargs):
133131
except TypeError as err:
134132
raise err
135133
else:
136-
logger.debug("Encrypted message using key with kid={}".format(key.kid))
134+
logger.debug(f"Encrypted message using key with kid={key.kid}")
137135
return token
138136

139137
# logger.error("Could not find any suitable encryption key")

src/cryptojwt/jwe/utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from cryptography.hazmat.primitives.hashes import SHA384
88
from cryptography.hazmat.primitives.hashes import SHA512
99

10-
from ..utils import b64e
11-
1210
LENMET = {32: (16, SHA256), 48: (24, SHA384), 64: (32, SHA512)}
1311

1412

src/cryptojwt/jwk/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
USE = {"sign": "sig", "decrypt": "enc", "encrypt": "enc", "verify": "sig"}
1515

1616

17-
class JWK(object):
17+
class JWK:
1818
"""
1919
Basic JSON Web key class. Jason Web keys are described in
2020
RFC 7517 (https://tools.ietf.org/html/rfc7517).
@@ -56,7 +56,7 @@ def __init__(
5656
"ECDH-ES+A192KW",
5757
"ECDH-ES+A256KW",
5858
]:
59-
raise UnsupportedAlgorithm("Unknown algorithm: {}".format(alg))
59+
raise UnsupportedAlgorithm(f"Unknown algorithm: {alg}")
6060
elif use == "sig":
6161
# The list comes from https://tools.ietf.org/html/rfc7518#page-6
6262
# Should map against SIGNER_ALGS in cryptojwt.jws.jws
@@ -79,7 +79,7 @@ def __init__(
7979
"Ed448",
8080
"none",
8181
]:
82-
raise UnsupportedAlgorithm("Unknown algorithm: {}".format(alg))
82+
raise UnsupportedAlgorithm(f"Unknown algorithm: {alg}")
8383
else: # potentially used both for encryption and signing
8484
if alg not in [
8585
"HS256",
@@ -110,7 +110,7 @@ def __init__(
110110
"ECDH-ES+A192KW",
111111
"ECDH-ES+A256KW",
112112
]:
113-
raise UnsupportedAlgorithm("Unknown algorithm: {}".format(alg))
113+
raise UnsupportedAlgorithm(f"Unknown algorithm: {alg}")
114114
self.alg = alg
115115

116116
if isinstance(use, str):
@@ -271,7 +271,7 @@ def thumbprint(self, hash_function, members=None):
271271
else:
272272
if isinstance(_val, bytes):
273273
_val = as_unicode(_val)
274-
_se.append('"{}":{}'.format(elem, json.dumps(_val)))
274+
_se.append(f'"{elem}":{json.dumps(_val)}')
275275
_json = "{{{}}}".format(",".join(_se))
276276

277277
return b64e(DIGEST_HASH[hash_function](_json))
@@ -298,7 +298,7 @@ def update(self):
298298
pass
299299

300300
def key_len(self):
301-
raise NotImplemented
301+
raise NotImplementedError
302302

303303

304304
def pems_to_x5c(cert_chain):
@@ -360,7 +360,7 @@ def certificate_fingerprint(der, hash="sha256"):
360360

361361

362362
def pem_hash(pem_file):
363-
with open(pem_file, "r") as fp:
363+
with open(pem_file) as fp:
364364
pem = fp.read()
365365

366366
der = ssl.PEM_cert_to_DER_cert(pem)

src/cryptojwt/jwk/ec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,5 +339,5 @@ def import_ec_key(pem_data):
339339

340340

341341
def import_ec_key_from_cert_file(pem_file):
342-
with open(pem_file, "r") as cert_file:
342+
with open(pem_file) as cert_file:
343343
return import_ec_key(cert_file.read())

src/cryptojwt/jwk/hmac.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def appropriate_for(self, usage, alg="HS256"):
9090
else:
9191
return self.encryption_key(alg)
9292

93-
raise WrongUsage("This key can't be used for {}".format(usage))
93+
raise WrongUsage(f"This key can't be used for {usage}")
9494

9595
def encryption_key(self, alg, **kwargs):
9696
"""
@@ -121,7 +121,7 @@ def encryption_key(self, alg, **kwargs):
121121
else:
122122
raise JWKException("No support for symmetric keys > 512 bits")
123123

124-
logger.debug("Symmetric encryption key: {}".format(as_unicode(b64e(_enc_key))))
124+
logger.debug(f"Symmetric encryption key: {as_unicode(b64e(_enc_key))}")
125125

126126
return _enc_key
127127

src/cryptojwt/jwk/jwk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def ensure_params(kty, provided, required):
7474
"""Ensure all required parameters are present in dictionary"""
7575
if not required <= provided:
7676
missing = required - provided
77-
raise MissingValue("Missing properties for kty={}, {}".format(kty, str(list(missing))))
77+
raise MissingValue(f"Missing properties for kty={kty}, {str(list(missing))}")
7878

7979

8080
def key_from_jwk_dict(jwk_dict, private=None):

src/cryptojwt/jwk/okp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ def deserialize(self):
146146
try:
147147
self.priv_key = OKP_CRV2PRIVATE[self.crv].from_private_bytes(deser(self.d))
148148
except KeyError:
149-
raise UnsupportedOKPCurve("Unsupported OKP curve: {}".format(self.crv))
149+
raise UnsupportedOKPCurve(f"Unsupported OKP curve: {self.crv}")
150150
self.pub_key = self.priv_key.public_key()
151151
except ValueError as err:
152152
raise DeSerializationNotPossible(str(err))
153153
else:
154154
try:
155155
self.pub_key = OKP_CRV2PUBLIC[self.crv].from_public_bytes(_x)
156156
except KeyError:
157-
raise UnsupportedOKPCurve("Unsupported OKP curve: {}".format(self.crv))
157+
raise UnsupportedOKPCurve(f"Unsupported OKP curve: {self.crv}")
158158

159159
def _serialize_public(self, key):
160160
self.x = b64e(
@@ -376,5 +376,5 @@ def import_okp_key(pem_data):
376376

377377

378378
def import_okp_key_from_cert_file(pem_file):
379-
with open(pem_file, "r") as cert_file:
379+
with open(pem_file) as cert_file:
380380
return import_okp_key(cert_file.read())

src/cryptojwt/jwk/rsa.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def import_rsa_key(pem_data):
105105

106106

107107
def import_rsa_key_from_cert_file(pem_file):
108-
with open(pem_file, "r") as cert_file:
108+
with open(pem_file) as cert_file:
109109
return import_rsa_key(cert_file.read())
110110

111111

@@ -284,9 +284,7 @@ def __init__(
284284
self.pub_key = self.priv_key.public_key()
285285
elif self.pub_key:
286286
self._serialize(self.pub_key)
287-
elif has_public_key_parts:
288-
self.deserialize()
289-
elif has_x509_cert_chain:
287+
elif has_public_key_parts or has_x509_cert_chain:
290288
self.deserialize()
291289
elif not self.n and not self.e:
292290
pass

0 commit comments

Comments
 (0)