Skip to content

Commit d296a36

Browse files
committed
Removed django dependency
1 parent 699f2bb commit d296a36

5 files changed

Lines changed: 37 additions & 21 deletions

File tree

hyperwallet/tests/test_encryption.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ def test_should_throw_exception_when_jws_signature_has_expired(self):
157157

158158
self.assertEqual(exc.exception.message, 'JWS signature has expired, checked by [exp] JWS header')
159159

160-
161160
def __getJwkKeySet(self, location):
162161

163162
try:
@@ -184,5 +183,6 @@ def __findJwkKeyByAlgorithm(self, jwkKeySet, algorithm):
184183

185184
raise HyperwalletException('JWK set doesn\'t contain key with algorithm = ' + algorithm)
186185

186+
187187
if __name__ == '__main__':
188-
unittest.main()
188+
unittest.main()

hyperwallet/utils/apiclient.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def _makeRequest(self,
9494
response = self.session.request(
9595
method=method,
9696
url=urljoin(self.baseUrl, url),
97-
data=(data if data is None else self.encryption.encrypt(data)) if self.encrypted else data,
97+
data=self.__getRequestData(data),
9898
headers=headers,
9999
params=params
100100
)
@@ -192,3 +192,15 @@ def doPut(self, partialUrl, data):
192192
url=partialUrl,
193193
data=json.dumps(data).encode('utf-8')
194194
)
195+
196+
def __getRequestData(data):
197+
'''
198+
If encryption is enabled try to encrypt request data, otherwise no action required.
199+
200+
:param data:
201+
Not encrypted request data. **REQUIRED**
202+
:returns:
203+
Request data, encrypted if necessary.
204+
'''
205+
206+
return (data if data is None else self.encryption.encrypt(data)) if self.encrypted else data

hyperwallet/utils/encryption.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
from jose import jws
1313

1414
from hyperwallet.exceptions import HyperwalletException
15-
from django.core.validators import URLValidator
16-
from django.core.exceptions import ValidationError
15+
try:
16+
from urlparse import urlparse
17+
except:
18+
from urllib.parse import urlparse
1719

1820

1921
class Encryption(object):
@@ -35,8 +37,11 @@ class Encryption(object):
3537
'''
3638

3739
def __init__(self,
38-
clientPrivateKeySetLocation, hyperwalletKeySetLocation,
39-
encryptionAlgorithm='RSA-OAEP-256', signAlgorithm='RS256', encryptionMethod='A256CBC-HS512',
40+
clientPrivateKeySetLocation,
41+
hyperwalletKeySetLocation,
42+
encryptionAlgorithm='RSA-OAEP-256',
43+
signAlgorithm='RS256',
44+
encryptionMethod='A256CBC-HS512',
4045
jwsExpirationMinutes=5):
4146
'''
4247
Encryption service for hyperwallet client
@@ -116,17 +121,17 @@ def __getJwkKeySet(self, location):
116121
:returns:
117122
JWK key set found at given location.
118123
'''
119-
120-
try:
121-
URLValidator()(location)
122-
except ValidationError:
123-
if os.path.isfile(location):
124-
with open(location) as f:
125-
return f.read()
126-
else:
127-
raise HyperwalletException('Wrong JWK key set location path = ' + location)
128-
129-
return requests.get(location).text
124+
try:
125+
url = urlparse(location)
126+
if url.scheme and url.netloc and url.path:
127+
return requests.get(location).text
128+
raise HyperwalletException('Failed to parse url from string = ' + location)
129+
except:
130+
if os.path.isfile(location):
131+
with open(location) as f:
132+
return f.read()
133+
else:
134+
raise HyperwalletException('Wrong JWK key set location path = ' + location)
130135

131136
def __findJwkKeyByAlgorithm(self, jwkKeySet, algorithm):
132137
'''

requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
requests
22
requests-toolbelt
33
jwcrypto
4-
python-jose
5-
django
4+
python-jose

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def extract_metaitem(meta):
3434
maintainer = extract_metaitem('author'),
3535
maintainer_email = extract_metaitem('email'),
3636
packages = find_packages(exclude = ('tests', 'doc')),
37-
install_requires = ['requests', 'requests-toolbelt', 'jwcrypto', 'python-jose', 'django<2'],
37+
install_requires = ['requests', 'requests-toolbelt', 'jwcrypto', 'python-jose'],
3838
test_suite = 'nose.collector',
3939
tests_require = [ 'mock', 'nose'],
4040
keywords='hyperwallet api',

0 commit comments

Comments
 (0)