Skip to content
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

Run pyupgrade to simplify code and use Python 3.6 syntax #536

Merged
merged 1 commit into from
Dec 16, 2020
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
2 changes: 1 addition & 1 deletion jwt/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def from_jwk(jwk):
"Coords should be 66 bytes for curve P-521"
)
else:
raise InvalidKeyError("Invalid curve: {}".format(curve))
raise InvalidKeyError(f"Invalid curve: {curve}")

public_numbers = ec.EllipticCurvePublicNumbers(
x=int_from_bytes(x, "big"),
Expand Down
4 changes: 1 addition & 3 deletions jwt/api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ def _load(self, jwt):
jwt = jwt.encode("utf-8")

if not isinstance(jwt, bytes):
raise DecodeError(
"Invalid token type. Token must be a {}".format(bytes)
)
raise DecodeError(f"Invalid token type. Token must be a {bytes}")

try:
signing_input, crypto_segment = jwt.rsplit(b".", 1)
Expand Down
2 changes: 1 addition & 1 deletion jwt/jwks_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_signing_key(self, kid):

if not signing_key:
raise PyJWKClientError(
'Unable to find a signing key that matches: "{}"'.format(kid)
f'Unable to find a signing key that matches: "{kid}"'
)

return signing_key
Expand Down
12 changes: 5 additions & 7 deletions tests/keys/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def decode_value(val):


def load_hmac_key():
with open(os.path.join(BASE_PATH, "jwk_hmac.json"), "r") as infile:
with open(os.path.join(BASE_PATH, "jwk_hmac.json")) as infile:
keyobj = json.load(infile)

return base64url_decode(force_bytes(keyobj["k"]))
Expand All @@ -31,15 +31,15 @@ def load_hmac_key():
if has_crypto:

def load_rsa_key():
with open(os.path.join(BASE_PATH, "jwk_rsa_key.json"), "r") as infile:
with open(os.path.join(BASE_PATH, "jwk_rsa_key.json")) as infile:
return RSAAlgorithm.from_jwk(infile.read())

def load_rsa_pub_key():
with open(os.path.join(BASE_PATH, "jwk_rsa_pub.json"), "r") as infile:
with open(os.path.join(BASE_PATH, "jwk_rsa_pub.json")) as infile:
return RSAAlgorithm.from_jwk(infile.read())

def load_ec_key():
with open(os.path.join(BASE_PATH, "jwk_ec_key.json"), "r") as infile:
with open(os.path.join(BASE_PATH, "jwk_ec_key.json")) as infile:
keyobj = json.load(infile)

return ec.EllipticCurvePrivateNumbers(
Expand All @@ -48,9 +48,7 @@ def load_ec_key():
)

def load_ec_pub_key_p_521():
with open(
os.path.join(BASE_PATH, "jwk_ec_pub_P-521.json"), "r"
) as infile:
with open(os.path.join(BASE_PATH, "jwk_ec_pub_P-521.json")) as infile:
keyobj = json.load(infile)

return ec.EllipticCurvePublicNumbers(
Expand Down
62 changes: 29 additions & 33 deletions tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,41 +80,41 @@ def test_hmac_should_throw_exception_if_key_is_pem_public_key(self):
algo = HMACAlgorithm(HMACAlgorithm.SHA256)

with pytest.raises(InvalidKeyError):
with open(key_path("testkey2_rsa.pub.pem"), "r") as keyfile:
with open(key_path("testkey2_rsa.pub.pem")) as keyfile:
algo.prepare_key(keyfile.read())

def test_hmac_should_throw_exception_if_key_is_x509_certificate(self):
algo = HMACAlgorithm(HMACAlgorithm.SHA256)

with pytest.raises(InvalidKeyError):
with open(key_path("testkey_rsa.cer"), "r") as keyfile:
with open(key_path("testkey_rsa.cer")) as keyfile:
algo.prepare_key(keyfile.read())

def test_hmac_should_throw_exception_if_key_is_ssh_public_key(self):
algo = HMACAlgorithm(HMACAlgorithm.SHA256)

with pytest.raises(InvalidKeyError):
with open(key_path("testkey_rsa.pub"), "r") as keyfile:
with open(key_path("testkey_rsa.pub")) as keyfile:
algo.prepare_key(keyfile.read())

def test_hmac_should_throw_exception_if_key_is_x509_cert(self):
algo = HMACAlgorithm(HMACAlgorithm.SHA256)

with pytest.raises(InvalidKeyError):
with open(key_path("testkey2_rsa.pub.pem"), "r") as keyfile:
with open(key_path("testkey2_rsa.pub.pem")) as keyfile:
algo.prepare_key(keyfile.read())

def test_hmac_should_throw_exception_if_key_is_pkcs1_pem_public(self):
algo = HMACAlgorithm(HMACAlgorithm.SHA256)

with pytest.raises(InvalidKeyError):
with open(key_path("testkey_pkcs1.pub.pem"), "r") as keyfile:
with open(key_path("testkey_pkcs1.pub.pem")) as keyfile:
algo.prepare_key(keyfile.read())

def test_hmac_jwk_should_parse_and_verify(self):
algo = HMACAlgorithm(HMACAlgorithm.SHA256)

with open(key_path("jwk_hmac.json"), "r") as keyfile:
with open(key_path("jwk_hmac.json")) as keyfile:
key = algo.from_jwk(keyfile.read())

signature = algo.sign(b"Hello World!", key)
Expand All @@ -129,7 +129,7 @@ def test_hmac_to_jwk_returns_correct_values(self):
def test_hmac_from_jwk_should_raise_exception_if_not_hmac_key(self):
algo = HMACAlgorithm(HMACAlgorithm.SHA256)

with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
with open(key_path("jwk_rsa_pub.json")) as keyfile:
with pytest.raises(InvalidKeyError):
algo.from_jwk(keyfile.read())

Expand All @@ -139,7 +139,7 @@ def test_hmac_from_jwk_should_raise_exception_if_not_hmac_key(self):
def test_rsa_should_parse_pem_public_key(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("testkey2_rsa.pub.pem"), "r") as pem_key:
with open(key_path("testkey2_rsa.pub.pem")) as pem_key:
algo.prepare_key(pem_key.read())

@pytest.mark.skipif(
Expand All @@ -157,7 +157,7 @@ def test_rsa_should_accept_pem_private_key_bytes(self):
def test_rsa_should_accept_unicode_key(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("testkey_rsa.priv"), "r") as rsa_key:
with open(key_path("testkey_rsa.priv")) as rsa_key:
algo.prepare_key(force_unicode(rsa_key.read()))

@pytest.mark.skipif(
Expand Down Expand Up @@ -190,7 +190,7 @@ def test_rsa_verify_should_return_false_if_signature_invalid(self):

sig += force_bytes("123") # Signature is now invalid

with open(key_path("testkey_rsa.pub"), "r") as keyfile:
with open(key_path("testkey_rsa.pub")) as keyfile:
pub_key = algo.prepare_key(keyfile.read())

result = algo.verify(message, pub_key, sig)
Expand All @@ -208,14 +208,10 @@ def test_ec_jwk_public_and_private_keys_should_parse_and_verify(self):
for (curve, hash) in tests.items():
algo = ECAlgorithm(hash)

with open(
key_path("jwk_ec_pub_{}.json".format(curve)), "r"
) as keyfile:
with open(key_path(f"jwk_ec_pub_{curve}.json")) as keyfile:
pub_key = algo.from_jwk(keyfile.read())

with open(
key_path("jwk_ec_key_{}.json".format(curve)), "r"
) as keyfile:
with open(key_path(f"jwk_ec_key_{curve}.json")) as keyfile:
priv_key = algo.from_jwk(keyfile.read())

signature = algo.sign(force_bytes("Hello World!"), priv_key)
Expand Down Expand Up @@ -290,10 +286,10 @@ def test_ec_jwk_fails_on_invalid_json(self):
def test_rsa_jwk_public_and_private_keys_should_parse_and_verify(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
with open(key_path("jwk_rsa_pub.json")) as keyfile:
pub_key = algo.from_jwk(keyfile.read())

with open(key_path("jwk_rsa_key.json"), "r") as keyfile:
with open(key_path("jwk_rsa_key.json")) as keyfile:
priv_key = algo.from_jwk(keyfile.read())

signature = algo.sign(force_bytes("Hello World!"), priv_key)
Expand All @@ -305,7 +301,7 @@ def test_rsa_jwk_public_and_private_keys_should_parse_and_verify(self):
def test_rsa_private_key_to_jwk_works_with_from_jwk(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("testkey_rsa.priv"), "r") as rsa_key:
with open(key_path("testkey_rsa.priv")) as rsa_key:
orig_key = algo.prepare_key(force_unicode(rsa_key.read()))

parsed_key = algo.from_jwk(algo.to_jwk(orig_key))
Expand All @@ -321,7 +317,7 @@ def test_rsa_private_key_to_jwk_works_with_from_jwk(self):
def test_rsa_public_key_to_jwk_works_with_from_jwk(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("testkey_rsa.pub"), "r") as rsa_key:
with open(key_path("testkey_rsa.pub")) as rsa_key:
orig_key = algo.prepare_key(force_unicode(rsa_key.read()))

parsed_key = algo.from_jwk(algo.to_jwk(orig_key))
Expand All @@ -333,7 +329,7 @@ def test_rsa_public_key_to_jwk_works_with_from_jwk(self):
def test_rsa_jwk_private_key_with_other_primes_is_invalid(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("jwk_rsa_key.json"), "r") as keyfile:
with open(key_path("jwk_rsa_key.json")) as keyfile:
with pytest.raises(InvalidKeyError):
keydata = json.loads(keyfile.read())
keydata["oth"] = []
Expand All @@ -346,7 +342,7 @@ def test_rsa_jwk_private_key_with_other_primes_is_invalid(self):
def test_rsa_jwk_private_key_with_missing_values_is_invalid(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("jwk_rsa_key.json"), "r") as keyfile:
with open(key_path("jwk_rsa_key.json")) as keyfile:
with pytest.raises(InvalidKeyError):
keydata = json.loads(keyfile.read())
del keydata["p"]
Expand All @@ -359,7 +355,7 @@ def test_rsa_jwk_private_key_with_missing_values_is_invalid(self):
def test_rsa_jwk_private_key_can_recover_prime_factors(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("jwk_rsa_key.json"), "r") as keyfile:
with open(key_path("jwk_rsa_key.json")) as keyfile:
keybytes = keyfile.read()
control_key = algo.from_jwk(keybytes).private_numbers()

Expand All @@ -383,7 +379,7 @@ def test_rsa_jwk_private_key_can_recover_prime_factors(self):
def test_rsa_jwk_private_key_with_missing_required_values_is_invalid(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("jwk_rsa_key.json"), "r") as keyfile:
with open(key_path("jwk_rsa_key.json")) as keyfile:
with pytest.raises(InvalidKeyError):
keydata = json.loads(keyfile.read())
del keydata["p"]
Expand All @@ -410,7 +406,7 @@ def test_rsa_jwk_raises_exception_if_not_a_valid_key(self):
def test_rsa_to_jwk_returns_correct_values_for_public_key(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("testkey_rsa.pub"), "r") as keyfile:
with open(key_path("testkey_rsa.pub")) as keyfile:
pub_key = algo.prepare_key(keyfile.read())

key = algo.to_jwk(pub_key)
Expand All @@ -436,7 +432,7 @@ def test_rsa_to_jwk_returns_correct_values_for_public_key(self):
def test_rsa_to_jwk_returns_correct_values_for_private_key(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("testkey_rsa.priv"), "r") as keyfile:
with open(key_path("testkey_rsa.priv")) as keyfile:
priv_key = algo.prepare_key(keyfile.read())

key = algo.to_jwk(priv_key)
Expand Down Expand Up @@ -504,7 +500,7 @@ def test_rsa_to_jwk_raises_exception_on_invalid_key(self):
def test_rsa_from_jwk_raises_exception_on_invalid_key(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("jwk_hmac.json"), "r") as keyfile:
with open(key_path("jwk_hmac.json")) as keyfile:
with pytest.raises(InvalidKeyError):
algo.from_jwk(keyfile.read())

Expand Down Expand Up @@ -532,7 +528,7 @@ def test_ec_should_accept_pem_private_key_bytes(self):
def test_ec_should_accept_ssh_public_key_bytes(self):
algo = ECAlgorithm(ECAlgorithm.SHA256)

with open(key_path("testkey_ec_ssh.pub"), "r") as ec_key:
with open(key_path("testkey_ec_ssh.pub")) as ec_key:
algo.prepare_key(ec_key.read())

@pytest.mark.skipif(
Expand All @@ -554,7 +550,7 @@ def test_ec_verify_should_return_false_if_signature_invalid(self):
)
)

with open(key_path("testkey_ec.pub"), "r") as keyfile:
with open(key_path("testkey_ec.pub")) as keyfile:
pub_key = algo.prepare_key(keyfile.read())

result = algo.verify(message, pub_key, sig)
Expand All @@ -570,7 +566,7 @@ def test_ec_verify_should_return_false_if_signature_wrong_length(self):

sig = base64.b64decode(force_bytes("AC+m4Jf/xI3guAC6w0w3"))

with open(key_path("testkey_ec.pub"), "r") as keyfile:
with open(key_path("testkey_ec.pub")) as keyfile:
pub_key = algo.prepare_key(keyfile.read())

result = algo.verify(message, pub_key, sig)
Expand All @@ -584,11 +580,11 @@ def test_rsa_pss_sign_then_verify_should_return_true(self):

message = force_bytes("Hello World!")

with open(key_path("testkey_rsa.priv"), "r") as keyfile:
with open(key_path("testkey_rsa.priv")) as keyfile:
priv_key = algo.prepare_key(keyfile.read())
sig = algo.sign(message, priv_key)

with open(key_path("testkey_rsa.pub"), "r") as keyfile:
with open(key_path("testkey_rsa.pub")) as keyfile:
pub_key = algo.prepare_key(keyfile.read())

result = algo.verify(message, pub_key, sig)
Expand All @@ -615,7 +611,7 @@ def test_rsa_pss_verify_should_return_false_if_signature_invalid(self):

jwt_sig += force_bytes("123") # Signature is now invalid

with open(key_path("testkey_rsa.pub"), "r") as keyfile:
with open(key_path("testkey_rsa.pub")) as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())

result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_api_jwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TestPyJWK:
def test_should_load_key_from_jwk_data_dict(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
with open(key_path("jwk_rsa_pub.json")) as keyfile:
pub_key = algo.from_jwk(keyfile.read())

key_data_str = algo.to_jwk(pub_key)
Expand All @@ -46,7 +46,7 @@ def test_should_load_key_from_jwk_data_dict(self):
def test_should_load_key_from_jwk_data_json_string(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
with open(key_path("jwk_rsa_pub.json")) as keyfile:
pub_key = algo.from_jwk(keyfile.read())

key_data_str = algo.to_jwk(pub_key)
Expand All @@ -72,7 +72,7 @@ class TestPyJWKSet:
def test_should_load_keys_from_jwk_data_dict(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
with open(key_path("jwk_rsa_pub.json")) as keyfile:
pub_key = algo.from_jwk(keyfile.read())

key_data_str = algo.to_jwk(pub_key)
Expand All @@ -97,7 +97,7 @@ def test_should_load_keys_from_jwk_data_dict(self):
def test_should_load_keys_from_jwk_data_json_string(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
with open(key_path("jwk_rsa_pub.json")) as keyfile:
pub_key = algo.from_jwk(keyfile.read())

key_data_str = algo.to_jwk(pub_key)
Expand Down
Loading