Skip to content

Commit

Permalink
Chore: inline Variables that immediately Returned (#690)
Browse files Browse the repository at this point in the history
* Fix Inline variables & Refactor Code Expression

* Fix Linting Issue
  • Loading branch information
yezz123 committed Oct 3, 2021
1 parent e7a6c02 commit a988e1a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
56 changes: 26 additions & 30 deletions jwt/algorithms.py
Expand Up @@ -247,22 +247,21 @@ def __init__(self, hash_alg):
self.hash_alg = hash_alg

def prepare_key(self, key):
if isinstance(key, RSAPrivateKey) or isinstance(key, RSAPublicKey):
if isinstance(key, (RSAPrivateKey, RSAPublicKey)):
return key

if isinstance(key, (bytes, str)):
key = force_bytes(key)

try:
if key.startswith(b"ssh-rsa"):
key = load_ssh_public_key(key)
else:
key = load_pem_private_key(key, password=None)
except ValueError:
key = load_pem_public_key(key)
else:
if not isinstance(key, (bytes, str)):
raise TypeError("Expecting a PEM-formatted key.")

key = force_bytes(key)

try:
if key.startswith(b"ssh-rsa"):
key = load_ssh_public_key(key)
else:
key = load_pem_private_key(key, password=None)
except ValueError:
key = load_pem_public_key(key)
return key

@staticmethod
Expand Down Expand Up @@ -399,28 +398,25 @@ def __init__(self, hash_alg):
self.hash_alg = hash_alg

def prepare_key(self, key):
if isinstance(key, EllipticCurvePrivateKey) or isinstance(
key, EllipticCurvePublicKey
):
if isinstance(key, (EllipticCurvePrivateKey, EllipticCurvePublicKey)):
return key

if isinstance(key, (bytes, str)):
key = force_bytes(key)

# Attempt to load key. We don't know if it's
# a Signing Key or a Verifying Key, so we try
# the Verifying Key first.
try:
if key.startswith(b"ecdsa-sha2-"):
key = load_ssh_public_key(key)
else:
key = load_pem_public_key(key)
except ValueError:
key = load_pem_private_key(key, password=None)

else:
if not isinstance(key, (bytes, str)):
raise TypeError("Expecting a PEM-formatted key.")

key = force_bytes(key)

# Attempt to load key. We don't know if it's
# a Signing Key or a Verifying Key, so we try
# the Verifying Key first.
try:
if key.startswith(b"ecdsa-sha2-"):
key = load_ssh_public_key(key)
else:
key = load_pem_public_key(key)
except ValueError:
key = load_pem_private_key(key, password=None)

return key

def sign(self, msg, key):
Expand Down
3 changes: 0 additions & 3 deletions jwt/api_jws.py
Expand Up @@ -90,9 +90,6 @@ def encode(
if headers and "alg" in headers and headers["alg"]:
algorithm = headers["alg"]

if algorithm not in self._valid_algs:
pass

# Header
header = {"typ": self.header_typ, "alg": algorithm}

Expand Down
2 changes: 1 addition & 1 deletion jwt/api_jwt.py
Expand Up @@ -204,7 +204,7 @@ def _validate_aud(self, payload, audience):
if isinstance(audience, str):
audience = [audience]

if not any(aud in audience_claims for aud in audience):
if all(aud not in audience_claims for aud in audience):
raise InvalidAudienceError("Invalid audience")

def _validate_iss(self, payload, issuer):
Expand Down
12 changes: 6 additions & 6 deletions jwt/jwks_client.py
Expand Up @@ -26,13 +26,13 @@ def get_jwk_set(self) -> PyJWKSet:

def get_signing_keys(self) -> List[PyJWK]:
jwk_set = self.get_jwk_set()
signing_keys = []
signing_keys = [
jwk_set_key
for jwk_set_key in jwk_set.keys
if jwk_set_key.public_key_use in ["sig", None] and jwk_set_key.key_id
]

for jwk_set_key in jwk_set.keys:
if jwk_set_key.public_key_use in ["sig", None] and jwk_set_key.key_id:
signing_keys.append(jwk_set_key)

if len(signing_keys) == 0:
if not signing_keys:
raise PyJWKClientError("The JWKS endpoint did not contain any signing keys")

return signing_keys
Expand Down
5 changes: 2 additions & 3 deletions jwt/utils.py
Expand Up @@ -59,8 +59,7 @@ def from_base64url_uint(val: Union[str, bytes]) -> int:

def number_to_bytes(num: int, num_bytes: int) -> bytes:
padded_hex = "%0*x" % (2 * num_bytes, num)
big_endian = binascii.a2b_hex(padded_hex.encode("ascii"))
return big_endian
return binascii.a2b_hex(padded_hex.encode("ascii"))


def bytes_to_number(string: bytes) -> int:
Expand All @@ -72,7 +71,7 @@ def bytes_from_int(val: int) -> bytes:
byte_length = 0

while remaining != 0:
remaining = remaining >> 8
remaining >>= 8
byte_length += 1

return val.to_bytes(byte_length, "big", signed=False)
Expand Down

0 comments on commit a988e1a

Please sign in to comment.