|
4 | 4 |
|
5 | 5 | from collections import Mapping |
6 | 6 |
|
7 | | -from .algorithms import Algorithm, get_default_algorithms # NOQA |
| 7 | +from .algorithms import ( |
| 8 | + Algorithm, get_default_algorithms, has_crypto, requires_cryptography # NOQA |
| 9 | +) |
8 | 10 | from .compat import binary_type, string_types, text_type |
9 | 11 | from .exceptions import DecodeError, InvalidAlgorithmError, InvalidTokenError |
10 | 12 | from .utils import base64url_decode, base64url_encode, force_bytes, merge_dict |
@@ -101,7 +103,13 @@ def encode(self, payload, key, algorithm='HS256', headers=None, |
101 | 103 | signature = alg_obj.sign(signing_input, key) |
102 | 104 |
|
103 | 105 | except KeyError: |
104 | | - raise NotImplementedError('Algorithm not supported') |
| 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 | + ) |
| 111 | + else: |
| 112 | + raise NotImplementedError('Algorithm not supported') |
105 | 113 |
|
106 | 114 | segments.append(base64url_encode(signature)) |
107 | 115 |
|
@@ -198,6 +206,7 @@ def _validate_kid(self, kid): |
198 | 206 | if not isinstance(kid, string_types): |
199 | 207 | raise InvalidTokenError('Key ID header parameter must be a string') |
200 | 208 |
|
| 209 | + |
201 | 210 | _jws_global_obj = PyJWS() |
202 | 211 | encode = _jws_global_obj.encode |
203 | 212 | decode = _jws_global_obj.decode |
|
0 commit comments