|
9 | 9 | from jwcrypto.common import json_encode |
10 | 10 | from hyperwallet.exceptions import HyperwalletException |
11 | 11 | from hyperwallet.utils.encryption import Encryption |
12 | | -from django.core.validators import URLValidator |
13 | | -from django.core.exceptions import ValidationError |
| 12 | +from six.moves.urllib.parse import urlparse |
14 | 13 |
|
15 | 14 |
|
16 | 15 | class EncryptionTest(unittest.TestCase): |
@@ -158,19 +157,37 @@ def test_should_throw_exception_when_jws_signature_has_expired(self): |
158 | 157 | self.assertEqual(exc.exception.message, 'JWS signature has expired, checked by [exp] JWS header') |
159 | 158 |
|
160 | 159 | def __getJwkKeySet(self, location): |
161 | | - |
| 160 | + ''' |
| 161 | + Retrieves JWK key data from given location. |
| 162 | +
|
| 163 | + :param location: |
| 164 | + Location(can be a URL or path to file) of JWK key data. **REQUIRED** |
| 165 | + :returns: |
| 166 | + JWK key set found at given location. |
| 167 | + ''' |
162 | 168 | try: |
163 | | - URLValidator()(location) |
164 | | - except ValidationError: |
| 169 | + url = urlparse(location) |
| 170 | + if url.scheme and url.netloc and url.path: |
| 171 | + return requests.get(location).text |
| 172 | + raise HyperwalletException('Failed to parse url from string = ' + location) |
| 173 | + except Exception as e: |
165 | 174 | if os.path.isfile(location): |
166 | 175 | with open(location) as f: |
167 | 176 | return f.read() |
168 | 177 | else: |
169 | 178 | raise HyperwalletException('Wrong JWK key set location path = ' + location) |
170 | 179 |
|
171 | | - return requests.get(location).text |
172 | | - |
173 | 180 | def __findJwkKeyByAlgorithm(self, jwkKeySet, algorithm): |
| 181 | + ''' |
| 182 | + Finds JWK key by given algorithm. |
| 183 | +
|
| 184 | + :param jwkKeySet: |
| 185 | + JSON representation of JWK key set. **REQUIRED** |
| 186 | + :param algorithm: |
| 187 | + Algorithm of the JWK key to be found in key set. **REQUIRED** |
| 188 | + :returns: |
| 189 | + JWK key with given algorithm. |
| 190 | + ''' |
174 | 191 |
|
175 | 192 | try: |
176 | 193 | keySet = json.loads(jwkKeySet) |
|
0 commit comments