Skip to content

Commit

Permalink
B904 Exception fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gagandeepp committed Oct 5, 2024
1 parent c4eb823 commit 041b475
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
26 changes: 13 additions & 13 deletions jwt/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def from_jwk(jwk: str | JWKDict) -> bytes:
else:
raise ValueError
except ValueError:
raise InvalidKeyError("Key is not valid JSON")
raise InvalidKeyError("Key is not valid JSON") from None

if obj.get("kty") != "oct":
raise InvalidKeyError("Not an HMAC key")
Expand Down Expand Up @@ -346,7 +346,7 @@ def prepare_key(self, key: AllowedRSAKeys | str | bytes) -> AllowedRSAKeys:
try:
return cast(RSAPublicKey, load_pem_public_key(key_bytes))
except (ValueError, UnsupportedAlgorithm):
raise InvalidKeyError("Could not parse the provided public key.")
raise InvalidKeyError("Could not parse the provided public key.") from None

@overload
@staticmethod
Expand Down Expand Up @@ -409,10 +409,10 @@ def from_jwk(jwk: str | JWKDict) -> AllowedRSAKeys:
else:
raise ValueError
except ValueError:
raise InvalidKeyError("Key is not valid JSON")
raise InvalidKeyError("Key is not valid JSON") from None

if obj.get("kty") != "RSA":
raise InvalidKeyError("Not an RSA key")
raise InvalidKeyError("Not an RSA key") from None

if "d" in obj and "e" in obj and "n" in obj:
# Private key
Expand All @@ -428,7 +428,7 @@ def from_jwk(jwk: str | JWKDict) -> AllowedRSAKeys:
if any_props_found and not all(props_found):
raise InvalidKeyError(
"RSA key must include all parameters if any are present besides d"
)
) from None

public_numbers = RSAPublicNumbers(
from_base64url_uint(obj["e"]),
Expand Down Expand Up @@ -520,7 +520,7 @@ def prepare_key(self, key: AllowedECKeys | str | bytes) -> AllowedECKeys:
):
raise InvalidKeyError(
"Expecting a EllipticCurvePrivateKey/EllipticCurvePublicKey. Wrong key provided for ECDSA algorithms"
)
) from None

return crypto_key

Expand Down Expand Up @@ -605,13 +605,13 @@ def from_jwk(jwk: str | JWKDict) -> AllowedECKeys:
else:
raise ValueError
except ValueError:
raise InvalidKeyError("Key is not valid JSON")
raise InvalidKeyError("Key is not valid JSON") from None

if obj.get("kty") != "EC":
raise InvalidKeyError("Not an Elliptic curve key")
raise InvalidKeyError("Not an Elliptic curve key") from None

if "x" not in obj or "y" not in obj:
raise InvalidKeyError("Not an Elliptic curve key")
raise InvalidKeyError("Not an Elliptic curve key") from None

x = base64url_decode(obj.get("x"))
y = base64url_decode(obj.get("y"))
Expand All @@ -623,17 +623,17 @@ def from_jwk(jwk: str | JWKDict) -> AllowedECKeys:
if len(x) == len(y) == 32:
curve_obj = SECP256R1()
else:
raise InvalidKeyError("Coords should be 32 bytes for curve P-256")
raise InvalidKeyError("Coords should be 32 bytes for curve P-256") from None
elif curve == "P-384":
if len(x) == len(y) == 48:
curve_obj = SECP384R1()
else:
raise InvalidKeyError("Coords should be 48 bytes for curve P-384")
raise InvalidKeyError("Coords should be 48 bytes for curve P-384") from None
elif curve == "P-521":
if len(x) == len(y) == 66:
curve_obj = SECP521R1()
else:
raise InvalidKeyError("Coords should be 66 bytes for curve P-521")
raise InvalidKeyError("Coords should be 66 bytes for curve P-521") from None
elif curve == "secp256k1":
if len(x) == len(y) == 32:
curve_obj = SECP256K1()
Expand Down Expand Up @@ -834,7 +834,7 @@ def from_jwk(jwk: str | JWKDict) -> AllowedOKPKeys:
else:
raise ValueError
except ValueError:
raise InvalidKeyError("Key is not valid JSON")
raise InvalidKeyError("Key is not valid JSON") from None

if obj.get("kty") != "OKP":
raise InvalidKeyError("Not an Octet Key Pair")
Expand Down
2 changes: 1 addition & 1 deletion jwt/api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def _verify_signature(
try:
alg = header["alg"]
except KeyError:
raise InvalidAlgorithmError("Algorithm not specified")
raise InvalidAlgorithmError("Algorithm not specified") from None

if not alg or (algorithms is not None and alg not in algorithms):
raise InvalidAlgorithmError("The specified alg value is not allowed")
Expand Down
8 changes: 4 additions & 4 deletions jwt/api_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def _decode_payload(self, decoded: dict[str, Any]) -> Any:
try:
payload = json.loads(decoded["payload"])
except ValueError as e:
raise DecodeError(f"Invalid payload string: {e}")
raise DecodeError(f"Invalid payload string: {e}") from e
if not isinstance(payload, dict):
raise DecodeError("Invalid payload string: must be a json object")
return payload
Expand Down Expand Up @@ -269,7 +269,7 @@ def _validate_iat(
try:
iat = int(payload["iat"])
except ValueError:
raise InvalidIssuedAtError("Issued At claim (iat) must be an integer.")
raise InvalidIssuedAtError("Issued At claim (iat) must be an integer.") from None
if iat > (now + leeway):
raise ImmatureSignatureError("The token is not yet valid (iat)")

Expand All @@ -282,7 +282,7 @@ def _validate_nbf(
try:
nbf = int(payload["nbf"])
except ValueError:
raise DecodeError("Not Before claim (nbf) must be an integer.")
raise DecodeError("Not Before claim (nbf) must be an integer.") from None

if nbf > (now + leeway):
raise ImmatureSignatureError("The token is not yet valid (nbf)")
Expand All @@ -296,7 +296,7 @@ def _validate_exp(
try:
exp = int(payload["exp"])
except ValueError:
raise DecodeError("Expiration Time claim (exp) must be an integer.")
raise DecodeError("Expiration Time claim (exp) must be an integer.") from None

if exp <= (now - leeway):
raise ExpiredSignatureError("Signature has expired")
Expand Down
2 changes: 1 addition & 1 deletion jwt/jwks_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def fetch_data(self) -> Any:
except (URLError, TimeoutError) as e:
raise PyJWKClientConnectionError(
f'Fail to fetch data from the url, err: "{e}"'
)
) from e
else:
return jwk_set
finally:
Expand Down

0 comments on commit 041b475

Please sign in to comment.