Skip to content

Commit 0550fa3

Browse files
committed
Changes per code review
1 parent 8520c8f commit 0550fa3

File tree

3 files changed

+21
-44
lines changed

3 files changed

+21
-44
lines changed

jwt/algorithms.py

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,8 @@
3131
except ImportError:
3232
has_crypto = False
3333

34-
35-
def _get_crypto_algorithms():
36-
crypto_algorithms = {
37-
'RS256': None,
38-
'RS384': None,
39-
'RS512': None,
40-
'ES256': None,
41-
'ES384': None,
42-
'ES521': None,
43-
'ES512': None,
44-
'PS256': None,
45-
'PS384': None,
46-
'PS512': None
47-
}
48-
49-
if has_crypto:
50-
crypto_algorithms['RS256'] = RSAAlgorithm(RSAAlgorithm.SHA256)
51-
crypto_algorithms['RS384'] = RSAAlgorithm(RSAAlgorithm.SHA384)
52-
crypto_algorithms['RS512'] = RSAAlgorithm(RSAAlgorithm.SHA512)
53-
crypto_algorithms['ES256'] = ECAlgorithm(ECAlgorithm.SHA256)
54-
crypto_algorithms['ES384'] = ECAlgorithm(ECAlgorithm.SHA384)
55-
crypto_algorithms['ES521'] = ECAlgorithm(ECAlgorithm.SHA512)
56-
crypto_algorithms['ES512'] = ECAlgorithm(ECAlgorithm.SHA512)
57-
crypto_algorithms['PS256'] = RSAPSSAlgorithm(RSAPSSAlgorithm.SHA256)
58-
crypto_algorithms['PS384'] = RSAPSSAlgorithm(RSAPSSAlgorithm.SHA384)
59-
crypto_algorithms['PS512'] = RSAPSSAlgorithm(RSAPSSAlgorithm.SHA512)
60-
61-
return crypto_algorithms
34+
requires_cryptography = {'RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES521',
35+
'ES512', 'PS256', 'PS384', 'PS512'}
6236

6337

6438
def get_default_algorithms():
@@ -73,21 +47,22 @@ def get_default_algorithms():
7347
}
7448

7549
if has_crypto:
76-
crypto_algorithms = _get_crypto_algorithms()
77-
default_algorithms.update(crypto_algorithms)
50+
default_algorithms.update({
51+
'RS256': RSAAlgorithm(RSAAlgorithm.SHA256),
52+
'RS384': RSAAlgorithm(RSAAlgorithm.SHA384),
53+
'RS512': RSAAlgorithm(RSAAlgorithm.SHA512),
54+
'ES256': ECAlgorithm(ECAlgorithm.SHA256),
55+
'ES384': ECAlgorithm(ECAlgorithm.SHA384),
56+
'ES521': ECAlgorithm(ECAlgorithm.SHA512),
57+
'ES512': ECAlgorithm(ECAlgorithm.SHA512), # Backward compat for #219 fix
58+
'PS256': RSAPSSAlgorithm(RSAPSSAlgorithm.SHA256),
59+
'PS384': RSAPSSAlgorithm(RSAPSSAlgorithm.SHA384),
60+
'PS512': RSAPSSAlgorithm(RSAPSSAlgorithm.SHA512)
61+
})
7862

7963
return default_algorithms
8064

8165

82-
def get_crypto_algorithms():
83-
"""
84-
Returns a set of algorithm names that require the cryptography package to
85-
be installed in order to use.
86-
"""
87-
crypto_algorithms = _get_crypto_algorithms().keys()
88-
return set(crypto_algorithms)
89-
90-
9166
class Algorithm(object):
9267
"""
9368
The interface for an algorithm used to sign and verify tokens.

jwt/api_jws.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import Mapping
66

77
from .algorithms import (
8-
Algorithm, get_crypto_algorithms, get_default_algorithms, has_crypto # NOQA
8+
Algorithm, get_default_algorithms, has_crypto, requires_cryptography # NOQA
99
)
1010
from .compat import binary_type, string_types, text_type
1111
from .exceptions import DecodeError, InvalidAlgorithmError, InvalidTokenError
@@ -103,9 +103,11 @@ def encode(self, payload, key, algorithm='HS256', headers=None,
103103
signature = alg_obj.sign(signing_input, key)
104104

105105
except KeyError:
106-
if not has_crypto and algorithm in get_crypto_algorithms():
107-
raise NotImplementedError('"cryptography" package must be '
108-
'installed to use this algorithm')
106+
if not has_crypto and algorithm in requires_cryptography:
107+
raise NotImplementedError(
108+
"Algorithm '%s' could not be found. Do you have cryptography "
109+
"installed?" % algorithm
110+
)
109111
else:
110112
raise NotImplementedError('Algorithm not supported')
111113

tests/test_api_jws.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def test_invalid_crypto_alg(self, jws, payload):
303303
with pytest.raises(NotImplementedError):
304304
jws.encode(payload, 'secret', algorithm='HS1024')
305305

306-
@pytest.mark.skipif(has_crypto, reason='Tests better errors if crypography not installed')
306+
@pytest.mark.skipif(has_crypto, reason='Scenario requires cryptography to not be installed')
307307
def test_missing_crypto_library_better_error_messages(self, jws, payload):
308308
with pytest.raises(NotImplementedError) as excinfo:
309309
jws.encode(payload, 'secret', algorithm='RS256')

0 commit comments

Comments
 (0)