From 041b475f036cef8a7cfc42b6d43670be45eb1712 Mon Sep 17 00:00:00 2001 From: gagandeepp Date: Sat, 5 Oct 2024 14:46:03 +0530 Subject: [PATCH] B904 Exception fixes --- jwt/algorithms.py | 26 +++++++++++++------------- jwt/api_jws.py | 2 +- jwt/api_jwt.py | 8 ++++---- jwt/jwks_client.py | 2 +- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/jwt/algorithms.py b/jwt/algorithms.py index 9be50b20..51de9845 100644 --- a/jwt/algorithms.py +++ b/jwt/algorithms.py @@ -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") @@ -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 @@ -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 @@ -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"]), @@ -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 @@ -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")) @@ -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() @@ -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") diff --git a/jwt/api_jws.py b/jwt/api_jws.py index 20515433..f90c30f9 100644 --- a/jwt/api_jws.py +++ b/jwt/api_jws.py @@ -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") diff --git a/jwt/api_jwt.py b/jwt/api_jwt.py index c55f94b9..889e3529 100644 --- a/jwt/api_jwt.py +++ b/jwt/api_jwt.py @@ -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 @@ -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)") @@ -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)") @@ -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") diff --git a/jwt/jwks_client.py b/jwt/jwks_client.py index 9abe871e..9a8992ca 100644 --- a/jwt/jwks_client.py +++ b/jwt/jwks_client.py @@ -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: