def test_ed25519_prepare_key_should_be_idempotent(self): algo = Ed25519Algorithm() with open(key_path("testkey_ed25519.pub")) as keyfile: jwt_pub_key_first = algo.prepare_key(keyfile.read()) jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first) assert jwt_pub_key_first == jwt_pub_key_second
def test_ed25519_verify_should_return_true_if_signature_valid(self): algo = Ed25519Algorithm() jwt_message = self.hello_world jwt_sig = base64.b64decode(force_bytes(self.hello_world_sig)) with open(key_path("testkey_ed25519.pub")) as keyfile: jwt_pub_key = algo.prepare_key(keyfile.read()) result = algo.verify(jwt_message, jwt_pub_key, jwt_sig) assert result
def test_ed25519_should_reject_non_string_key(self): algo = Ed25519Algorithm() with pytest.raises(TypeError): algo.prepare_key(None) with open(key_path("testkey_ed25519")) as keyfile: jwt_key = algo.prepare_key(keyfile.read()) with open(key_path("testkey_ed25519.pub")) as keyfile: jwt_pub_key = algo.prepare_key(keyfile.read())
def test_ed25519_sign_should_generate_correct_signature_value(self): algo = Ed25519Algorithm() jwt_message = self.hello_world expected_sig = base64.b64decode(force_bytes(self.hello_world_sig)) with open(key_path("testkey_ed25519")) as keyfile: jwt_key = algo.prepare_key(keyfile.read()) with open(key_path("testkey_ed25519.pub")) as keyfile: jwt_pub_key = algo.prepare_key(keyfile.read()) algo.sign(jwt_message, jwt_key) result = algo.verify(jwt_message, jwt_pub_key, expected_sig) assert result
def get_default_algorithms(): """ Returns the algorithms that are implemented by the library. """ default_algorithms = { "none": NoneAlgorithm(), "HS256": HMACAlgorithm(HMACAlgorithm.SHA256), "HS384": HMACAlgorithm(HMACAlgorithm.SHA384), "HS512": HMACAlgorithm(HMACAlgorithm.SHA512), } if has_crypto: default_algorithms.update({ "RS256": RSAAlgorithm(RSAAlgorithm.SHA256), "RS384": RSAAlgorithm(RSAAlgorithm.SHA384), "RS512": RSAAlgorithm(RSAAlgorithm.SHA512), "ES256": ECAlgorithm(ECAlgorithm.SHA256), "ES384": ECAlgorithm(ECAlgorithm.SHA384), "ES521": ECAlgorithm(ECAlgorithm.SHA512), "ES512": ECAlgorithm(ECAlgorithm.SHA512), # Backward compat for #219 fix "PS256": RSAPSSAlgorithm(RSAPSSAlgorithm.SHA256), "PS384": RSAPSSAlgorithm(RSAPSSAlgorithm.SHA384), "PS512": RSAPSSAlgorithm(RSAPSSAlgorithm.SHA512), }) # Older versions of the `cryptography` libraries may not have Ed25519 available. # Needs a minimum of version 2.6 try: from jwt.contrib.algorithms.py_ed25519 import Ed25519Algorithm default_algorithms.update({ "EdDSA": Ed25519Algorithm(), }) except ImportError: pass return default_algorithms
def test_ed25519_should_accept_unicode_key(self): algo = Ed25519Algorithm() with open(key_path("testkey_ed25519")) as ec_key: algo.prepare_key(force_unicode(ec_key.read()))