Skip to content

Commit 691835e

Browse files
committed
update signing and add tests
1 parent 9788ca4 commit 691835e

File tree

4 files changed

+76
-23
lines changed

4 files changed

+76
-23
lines changed

http_client/src/vonage_http_client/auth.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,33 +89,30 @@ def sign_params(self, params: dict) -> dict:
8989
dict: The signed message parameters.
9090
"""
9191

92-
if self._signature_method:
93-
hasher = hmac.new(
94-
self._signature_secret.encode(),
95-
digestmod=self._signature_method,
96-
)
97-
else:
98-
hasher = hashlib.md5()
92+
hasher = hmac.new(
93+
self._signature_secret.encode(),
94+
digestmod=self._signature_method,
95+
)
9996

100-
if not params.get("timestamp"):
101-
params["timestamp"] = int(time())
97+
if not params.get('timestamp'):
98+
params['timestamp'] = int(time())
10299

103100
for key in sorted(params):
104101
value = params[key]
105102

106103
if isinstance(value, str):
107-
value = value.replace("&", "_").replace("=", "_")
104+
value = value.replace('&', '_').replace('=', '_')
108105

109-
hasher.update(f"&{key}={value}".encode("utf-8"))
106+
hasher.update(f'&{key}={value}'.encode('utf-8'))
110107

111108
if self._signature_method is None:
112109
hasher.update(self._signature_secret.encode())
113110
return hasher.hexdigest()
114111

115-
def check_signature(self, params) -> bool:
116-
params = dict(params)
112+
@validate_call
113+
def check_signature(self, params: dict) -> bool:
117114
signature = params.pop('sig', '').lower()
118-
return hmac.compare_digest(signature, self.signature(params))
115+
return hmac.compare_digest(signature, self._signature_secret(params))
119116

120117
def _validate_input_combinations(
121118
self, api_key, api_secret, application_id, private_key, signature_secret

http_client/src/vonage_http_client/http_client.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,8 @@ def make_request(
135135
elif auth_type == 'basic':
136136
self._headers['Authorization'] = self._auth.create_basic_auth_string()
137137
elif auth_type == 'signature':
138-
params = self._auth.sign_params(params)
139-
140-
print(params)
141-
print(self._auth.check_signature(params))
142-
138+
params['api_key'] = self._auth.api_key
139+
params['sig'] = self._auth.sign_params(params)
143140
with self._session.request(
144141
request_type,
145142
url,
@@ -165,7 +162,6 @@ def _parse_response(self, response: Response) -> Union[dict, None]:
165162
logger.debug(
166163
f'Response received from {response.url} with status code: {response.status_code}; headers: {response.headers}'
167164
)
168-
print(response.request.headers)
169165
content_type = response.headers['Content-Type'].split(';', 1)[0]
170166
if 200 <= response.status_code < 300:
171167
if response.status_code == 204:

http_client/tests/test_auth.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from os.path import dirname, join
22
from unittest.mock import patch
33
import hashlib
4-
4+
import hmac
55

66
from pydantic import ValidationError
77
from pytest import raises
@@ -160,3 +160,63 @@ def test_auth_init_with_invalid_combinations():
160160
assert auth._jwt_client is None
161161
assert auth._signature_secret == signature_secret
162162
assert auth._signature_method is None
163+
164+
165+
def test_sign_params():
166+
auth = Auth(signature_secret='signature_secret', signature_method='sha256')
167+
168+
params = {'param1': 'value1', 'param2': 'value2', 'timestamp': 1234567890}
169+
170+
signed_params = auth.sign_params(params)
171+
172+
assert signed_params == 'asdf'
173+
174+
175+
def test_sign_params_default_sig_method():
176+
auth = Auth()
177+
178+
params = {'param1': 'value1', 'param2': 'value2', 'timestamp': 1234567890}
179+
180+
signed_params = auth.sign_params(params)
181+
182+
assert signed_params == 'asdf'
183+
184+
185+
def test_sign_params_with_special_characters():
186+
auth = Auth(signature_secret='signature_secret', signature_method='sha1')
187+
188+
params = {'param1': 'value&1', 'param2': 'value=2', 'timestamp': 1234567890}
189+
190+
signed_params = auth.sign_params(params)
191+
192+
assert signed_params == 'asdf'
193+
194+
195+
# def test_check_signature_with_valid_signature():
196+
# auth = Auth(signature_secret='signature_secret')
197+
# params = {'param1': 'value1', 'param2': 'value2', 'sig': 'valid_signature'}
198+
# expected_signature = hmac.new(
199+
# b'signature_secret', b'param1value1param2value2', hashlib.sha256
200+
# ).hexdigest()
201+
202+
# assert auth.check_signature(params) == True
203+
204+
205+
# def test_check_signature_with_invalid_signature():
206+
# auth = Auth(signature_secret='signature_secret')
207+
# params = {'param1': 'value1', 'param2': 'value2', 'sig': 'invalid_signature'}
208+
# expected_signature = hmac.new(
209+
# b'signature_secret', b'param1value1param2value2', hashlib.sha256
210+
# ).hexdigest()
211+
212+
# assert auth.check_signature(params) == False
213+
214+
215+
# def test_check_signature_with_empty_signature():
216+
# auth = Auth(signature_secret='signature_secret')
217+
# params = {'param1': 'value1', 'param2': 'value2', 'sig': ''}
218+
# expected_signature = hmac.new(
219+
# b'signature_secret', b'param1value1param2value2', hashlib.sha256
220+
# ).hexdigest()
221+
222+
# assert auth.check_signature(params) == False

http_client/tests/test_http_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ def test_make_post_request_with_signature():
118118
auth_type='signature',
119119
)
120120
assert res['hello'] == 'world!'
121-
122-
assert loads(responses.calls[0].request.body) == params
121+
print(responses.calls[0].request.url)
122+
assert responses.calls[0].request.body == params
123123

124124

125125
@responses.activate

0 commit comments

Comments
 (0)