summaryrefslogtreecommitdiff
path: root/jwt/algorithms.py
diff options
context:
space:
mode:
authorYasser Tahiri <yasserth19@gmail.com>2021-10-03 06:32:59 +0100
committerGitHub <noreply@github.com>2021-10-03 11:32:59 +0600
commita988e1a11e5abb5869dd641f3f4f6a5bb4e70fdf (patch)
tree2e5908e8285ca95f61371e507663f5f8b3e1194b /jwt/algorithms.py
parente7a6c022f3f2e5ba329cbadd242c788014926a7e (diff)
downloadpyjwt-a988e1a11e5abb5869dd641f3f4f6a5bb4e70fdf.tar.gz
Chore: inline Variables that immediately Returned (#690)
* Fix Inline variables & Refactor Code Expression * Fix Linting Issue
Diffstat (limited to 'jwt/algorithms.py')
-rw-r--r--jwt/algorithms.py56
1 files changed, 26 insertions, 30 deletions
diff --git a/jwt/algorithms.py b/jwt/algorithms.py
index 2f73835..1f8865a 100644
--- a/jwt/algorithms.py
+++ b/jwt/algorithms.py
@@ -247,22 +247,21 @@ if has_crypto:
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
@@ -399,28 +398,25 @@ if has_crypto:
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):