Skip to content

Commit

Permalink
Merge pull request #169 from jschlyter/ruff_reformat
Browse files Browse the repository at this point in the history
Reformat with ruff
  • Loading branch information
jschlyter authored Oct 2, 2024
2 parents f18448c + 69fed0a commit e77cd91
Show file tree
Hide file tree
Showing 48 changed files with 244 additions and 401 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
run: poetry install
- name: Run pytest
run: |
poetry run pytest -vvv -ra --cov=cryptojwt --cov-report=xml --isort --black
poetry run pytest -vvv -ra --cov=cryptojwt --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .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
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
all:

test:
poetry run pytest --ruff --ruff-format --cov

reformat:
poetry run ruff check --select I --fix src tests
poetry run ruff format src tests
44 changes: 19 additions & 25 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
# PEP 518: https://www.python.org/dev/peps/pep-0518/

[tool.black]
line-length = 100

[tool.isort]
force_single_line = true
known_first_party = "cryptojwt"
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
line_length = 100

[tool.coverage.run]
branch = true

[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"raise NotImplementedError",
]

[tool.poetry]
name = "cryptojwt"
version = "1.9.2"
Expand All @@ -44,23 +24,31 @@ requests = "^2.25.1"

[tool.poetry.group.dev.dependencies]
alabaster = "^0.7.12"
black = "^24.4.2"
isort = "^5.13.2"
pytest = "^8.2.1"
pytest-black = "^0.3.12"
pytest-isort = "^4.0.0"
pytest-cov = "^4.0.0"
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.coverage.run]
branch = true

[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"raise NotImplementedError",
]

[tool.ruff]
line-length = 100

[tool.ruff.lint]
select = [
# pycodestyle
Expand All @@ -78,3 +66,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/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
from cryptojwt.key_jar import KeyJar

from .exception import BadSyntax
from .utils import as_unicode
from .utils import b64d
from .utils import b64encode_item
from .utils import split_token
from .utils import as_unicode, b64d, b64encode_item, split_token

__version__ = version("cryptojwt")

Expand Down
8 changes: 2 additions & 6 deletions src/cryptojwt/jwe/aes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
from struct import pack

from cryptography.hazmat.primitives import hmac
from cryptography.hazmat.primitives.ciphers import Cipher
from cryptography.hazmat.primitives.ciphers import algorithms
from cryptography.hazmat.primitives.ciphers import modes
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.padding import PKCS7

from ..exception import MissingKey
from ..exception import Unsupported
from ..exception import VerificationError
from ..exception import MissingKey, Unsupported, VerificationError
from . import Encrypter
from .exception import UnsupportedBitLength
from .utils import get_keys_seclen_dgst
Expand Down
3 changes: 1 addition & 2 deletions src/cryptojwt/jwe/fernet.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import base64
import os
from typing import Optional
from typing import Union
from typing import Optional, Union

from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
Expand Down
17 changes: 8 additions & 9 deletions src/cryptojwt/jwe/jwe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from ..jwk.hmac import SYMKey
from ..jwk.jwk import key_from_jwk_dict
from ..jwx import JWx
from .exception import DecryptionFailed
from .exception import NoSuitableDecryptionKey
from .exception import NoSuitableEncryptionKey
from .exception import NotSupportedAlgorithm
from .exception import WrongEncryptionAlgorithm
from .exception import (
DecryptionFailed,
NoSuitableDecryptionKey,
NoSuitableEncryptionKey,
NotSupportedAlgorithm,
WrongEncryptionAlgorithm,
)
from .jwe_ec import JWE_EC
from .jwe_hmac import JWE_SYM
from .jwe_rsa import JWE_RSA
Expand Down Expand Up @@ -171,10 +173,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
16 changes: 5 additions & 11 deletions src/cryptojwt/jwe/jwe_ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@
import struct

from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.keywrap import aes_key_unwrap
from cryptography.hazmat.primitives.keywrap import aes_key_wrap

from ..jwk.ec import NIST2SEC
from ..jwk.ec import ECKey
from ..utils import as_bytes
from ..utils import as_unicode
from ..utils import b64d
from ..utils import b64e
from cryptography.hazmat.primitives.keywrap import aes_key_unwrap, aes_key_wrap

from ..jwk.ec import NIST2SEC, ECKey
from ..utils import as_bytes, as_unicode, b64d, b64e
from . import KEY_LEN
from .jwekey import JWEKey
from .jwenc import JWEnc
from .utils import concat_sha256
from .utils import get_random_bytes
from .utils import concat_sha256, get_random_bytes


def ecdh_derive_key(key, epk, apu, apv, alg, dk_len):
Expand Down
9 changes: 3 additions & 6 deletions src/cryptojwt/jwe/jwe_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
import logging
import zlib

from cryptography.hazmat.primitives.keywrap import aes_key_unwrap
from cryptography.hazmat.primitives.keywrap import aes_key_wrap
from cryptography.hazmat.primitives.keywrap import aes_key_unwrap, aes_key_wrap

from ..exception import MissingKey
from ..exception import WrongNumberOfParts
from ..exception import MissingKey, WrongNumberOfParts
from ..jwk.hmac import SYMKey
from ..utils import as_bytes
from ..utils import intarr2str
from ..utils import as_bytes, intarr2str
from .jwekey import JWEKey
from .jwenc import JWEnc

Expand Down
3 changes: 1 addition & 2 deletions src/cryptojwt/jwe/jwe_rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

from ..utils import as_bytes
from . import SUPPORTED
from .exception import NotSupportedAlgorithm
from .exception import ParameterError
from .exception import NotSupportedAlgorithm, ParameterError
from .jwekey import JWEKey
from .jwenc import JWEnc
from .rsa import RSAEncrypter
Expand Down
10 changes: 3 additions & 7 deletions src/cryptojwt/jwe/jwekey.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from ..jwx import JWx
from . import KEY_LEN_BYTES
from .aes import AES_CBCEncrypter
from .aes import AES_GCMEncrypter
from .exception import DecryptionFailed
from .exception import NotSupportedAlgorithm
from .utils import alg2keytype
from .utils import get_random_bytes
from .utils import split_ctx_and_tag
from .aes import AES_CBCEncrypter, AES_GCMEncrypter
from .exception import DecryptionFailed, NotSupportedAlgorithm
from .utils import alg2keytype, get_random_bytes, split_ctx_and_tag


class JWEKey(JWx):
Expand Down
4 changes: 1 addition & 3 deletions src/cryptojwt/jwe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
from math import ceil

from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.hashes import SHA256
from cryptography.hazmat.primitives.hashes import SHA384
from cryptography.hazmat.primitives.hashes import SHA512
from cryptography.hazmat.primitives.hashes import SHA256, SHA384, SHA512

LENMET = {32: (16, SHA256), 48: (24, SHA384), 64: (32, SHA512)}

Expand Down
11 changes: 2 additions & 9 deletions src/cryptojwt/jwk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
from typing import List

from ..exception import UnsupportedAlgorithm
from ..utils import as_bytes
from ..utils import as_unicode
from ..utils import b64e
from ..utils import base64url_to_long
from ..utils import as_bytes, as_unicode, b64e, base64url_to_long
from .utils import DIGEST_HASH

USE = {"sign": "sig", "decrypt": "enc", "encrypt": "enc", "verify": "sig"}
Expand Down Expand Up @@ -236,11 +233,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
5 changes: 2 additions & 3 deletions src/cryptojwt/jwk/asym.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from . import JWK
from . import USE
from . import JWK, USE


class AsymmetricKey(JWK):
Expand All @@ -19,7 +18,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
16 changes: 7 additions & 9 deletions src/cryptojwt/jwk/ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

from cryptojwt.exception import KeyNotFound

from ..exception import DeSerializationNotPossible
from ..exception import JWKESTException
from ..exception import UnsupportedECurve
from ..utils import as_unicode
from ..utils import deser
from ..utils import long_to_base64
from ..exception import DeSerializationNotPossible, JWKESTException, UnsupportedECurve
from ..utils import as_unicode, deser, long_to_base64
from .asym import AsymmetricKey
from .x509 import import_private_key_from_pem_file
from .x509 import import_public_key_from_pem_data
from .x509 import import_public_key_from_pem_file
from .x509 import (
import_private_key_from_pem_file,
import_public_key_from_pem_data,
import_public_key_from_pem_file,
)

# This is used to translate between the curve representation in
# Cryptography and the one used by NIST (and in RFC 7518)
Expand Down
16 changes: 4 additions & 12 deletions src/cryptojwt/jwk/hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@

from cryptojwt.exception import KeyNotFound

from ..exception import JWKException
from ..exception import UnsupportedAlgorithm
from ..exception import WrongUsage
from ..utils import as_bytes
from ..utils import as_unicode
from ..utils import b64d
from ..utils import b64e
from . import JWK
from . import USE
from .utils import sha256_digest
from .utils import sha384_digest
from .utils import sha512_digest
from ..exception import JWKException, UnsupportedAlgorithm, WrongUsage
from ..utils import as_bytes, as_unicode, b64d, b64e
from . import JWK, USE
from .utils import sha256_digest, sha384_digest, sha512_digest

logger = logging.getLogger(__name__)

Expand Down
19 changes: 5 additions & 14 deletions src/cryptojwt/jwk/jwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@
import json
import os

from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.asymmetric import ed448
from cryptography.hazmat.primitives.asymmetric import ed25519
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.asymmetric.rsa import rsa_crt_dmp1
from cryptography.hazmat.primitives.asymmetric.rsa import rsa_crt_dmq1
from cryptography.hazmat.primitives.asymmetric.rsa import rsa_crt_iqmp

from ..exception import MissingValue
from ..exception import UnknownKeyType
from ..exception import UnsupportedAlgorithm
from ..exception import WrongKeyType
from cryptography.hazmat.primitives.asymmetric import ec, ed448, ed25519, rsa
from cryptography.hazmat.primitives.asymmetric.rsa import rsa_crt_dmp1, rsa_crt_dmq1, rsa_crt_iqmp

from ..exception import MissingValue, UnknownKeyType, UnsupportedAlgorithm, WrongKeyType
from ..utils import base64url_to_long
from .ec import NIST2SEC
from .ec import ECKey
from .ec import NIST2SEC, ECKey
from .hmac import SYMKey
from .okp import OKPKey
from .rsa import RSAKey
Expand Down
20 changes: 8 additions & 12 deletions src/cryptojwt/jwk/okp.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
from typing import Union

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ed448
from cryptography.hazmat.primitives.asymmetric import ed25519
from cryptography.hazmat.primitives.asymmetric import x448
from cryptography.hazmat.primitives.asymmetric import x25519
from cryptography.hazmat.primitives.asymmetric import ed448, ed25519, x448, x25519

from cryptojwt.exception import KeyNotFound

from ..exception import DeSerializationNotPossible
from ..exception import JWKESTException
from ..exception import UnsupportedOKPCurve
from ..utils import b64d
from ..utils import b64e
from ..exception import DeSerializationNotPossible, JWKESTException, UnsupportedOKPCurve
from ..utils import b64d, b64e
from .asym import AsymmetricKey
from .x509 import import_private_key_from_pem_file
from .x509 import import_public_key_from_pem_data
from .x509 import import_public_key_from_pem_file
from .x509 import (
import_private_key_from_pem_file,
import_public_key_from_pem_data,
import_public_key_from_pem_file,
)

OKPPublicKey = Union[
ed25519.Ed25519PublicKey, ed448.Ed448PublicKey, x25519.X25519PublicKey, x448.X448PublicKey
Expand Down
Loading

0 comments on commit e77cd91

Please sign in to comment.