Skip to content

more lint fixes #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repos:
- id: check-yaml
- id: check-json
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.9
rev: v0.6.8
hooks:
- id: ruff
- id: ruff-format
#- id: ruff-format
11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ responses = "^0.13.0"
sphinx = "^3.5.2"
sphinx-autobuild = "^2021.3.14"
coverage = "^7"
ruff = "^0.4.6"
ruff = "^0.6.3"
pytest-ruff = "^0.3.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.ruff]
line-length = 100

[tool.ruff.lint]
select = [
# pycodestyle
Expand All @@ -78,3 +81,9 @@ select = [
]
ignore = ["E501", "I001", "SIM102"]
exclude = ["examples/*"]

[tool.ruff.lint.isort]
force-sort-within-sections = false
combine-as-imports = true
split-on-trailing-comma = false
known-first-party = ["cryptojwt"]
5 changes: 1 addition & 4 deletions src/cryptojwt/jwe/jwe.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ def decrypt(self, token=None, keys=None, alg=None, cek=None):
elif _alg.startswith("ECDH-ES"):
decrypter = JWE_EC(**self._dict)

if isinstance(keys[0], AsymmetricKey):
_key = keys[0].private_key()
else:
_key = keys[0].key
_key = keys[0].private_key() if isinstance(keys[0], AsymmetricKey) else keys[0].key

cek = decrypter.dec_setup(_jwe, key=_key)
else:
Expand Down
6 changes: 1 addition & 5 deletions src/cryptojwt/jwk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,7 @@ def __eq__(self, other):
if set(self.__dict__.keys()) != set(other.__dict__.keys()):
return False

for key in self.public_members:
if getattr(other, key) != getattr(self, key):
return False

return True
return all(getattr(other, key) == getattr(self, key) for key in self.public_members)

def keys(self):
return list(self.to_dict().keys())
Expand Down
2 changes: 1 addition & 1 deletion src/cryptojwt/jwk/asym.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(
k="",
pub_key=None,
priv_key=None,
**kwargs
**kwargs,
):
JWK.__init__(self, kty, alg, use, kid, x5c, x5t, x5u, **kwargs)
self.k = k
Expand Down
7 changes: 2 additions & 5 deletions src/cryptojwt/jwk/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def __init__(
dq="",
di="",
qi="",
**kwargs
**kwargs,
):
AsymmetricKey.__init__(self, kty, alg, use, kid, x5c, x5t, x5u, **kwargs)
self.n = n
Expand Down Expand Up @@ -322,10 +322,7 @@ def deserialize(self):
_cert_chain.append(der_cert(base64.b64decode(der_data)))

if self.x5t: # verify the cert thumbprint
if isinstance(self.x5t, bytes):
_x5t = self.x5t
else:
_x5t = self.x5t.encode("ascii")
_x5t = self.x5t if isinstance(self.x5t, bytes) else self.x5t.encode("ascii")
if _x5t != x5t_calculation(self.x5c[0]):
raise DeSerializationNotPossible(
"The thumbprint 'x5t' does not match the certificate."
Expand Down
14 changes: 5 additions & 9 deletions src/cryptojwt/jws/jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ def __len__(self):
return len(self.part)

def valid(self):
if len(self) != 3:
return False

return True
return len(self) == 3


class JWS(JWx):
Expand Down Expand Up @@ -382,11 +379,10 @@ def _is_json_serialized_jws(self, json_jws):
"""
json_ser_keys = {"payload", "signatures"}
flattened_json_ser_keys = {"payload", "signature"}
if not json_ser_keys.issubset(json_jws.keys()) and not flattened_json_ser_keys.issubset(
json_jws.keys()
):
return False
return True
return not (
not json_ser_keys.issubset(json_jws.keys())
and not flattened_json_ser_keys.issubset(json_jws.keys())
)

def _is_compact_jws(self, jws):
"""
Expand Down
5 changes: 1 addition & 4 deletions src/cryptojwt/key_issuer.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,7 @@

if not key_type:
if alg:
if use == "sig":
key_type = jws_alg2keytype(alg)
else:
key_type = jwe_alg2keytype(alg)
key_type = jws_alg2keytype(alg) if use == "sig" else jwe_alg2keytype(alg)

Check warning on line 289 in src/cryptojwt/key_issuer.py

View check run for this annotation

Codecov / codecov/patch

src/cryptojwt/key_issuer.py#L289

Added line #L289 was not covered by tests

lst = []
for bundle in self._bundles:
Expand Down
5 changes: 1 addition & 4 deletions src/cryptojwt/key_jar.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,7 @@ def keys_by_alg_and_usage(self, issuer_id, alg, usage):
:param usage: What the key should be used for
:return: A possibly empty list of keys
"""
if usage in ["sig", "ver"]:
ktype = jws_alg2keytype(alg)
else:
ktype = jwe_alg2keytype(alg)
ktype = jws_alg2keytype(alg) if usage in ["sig", "ver"] else jwe_alg2keytype(alg)

return self.get(usage, ktype, issuer_id)

Expand Down
1 change: 1 addition & 0 deletions src/cryptojwt/tools/keyconv.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

"""Convert JWK from/to PEM and other formats"""

import argparse
import json
from binascii import hexlify
Expand Down
1 change: 1 addition & 0 deletions src/cryptojwt/tools/keygen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

"""JSON Web Key (JWK) Generator"""

import argparse
import json
import sys
Expand Down
10 changes: 2 additions & 8 deletions src/cryptojwt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,7 @@ def is_compact_jws(token):
except Exception:
return False

if "alg" not in _header:
return False

return True
return "alg" in _header


def is_jwe(token):
Expand All @@ -324,10 +321,7 @@ def is_jwe(token):
except Exception:
return False

if "alg" not in _header or "enc" not in _header:
return False

return True
return not ("alg" not in _header or "enc" not in _header)


def is_json_jws(token):
Expand Down