Skip to content

Commit

Permalink
serialize callback body and use it in signature payload (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeUrban committed Oct 12, 2023
1 parent a60fe69 commit c6b39f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
10 changes: 6 additions & 4 deletions polaris/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ def test_make_on_change_callback_success(mock_serializer, mock_post):
mock_serializer.assert_called_once_with(mock_transaction)
mock_post.assert_called_once_with(
url=mock_transaction.on_change_callback,
json={"transaction": mock_serializer(mock_transaction).data},
data=json.dumps({}).encode(),
timeout=settings.CALLBACK_REQUEST_TIMEOUT,
headers={
"Signature": "test"
"Signature": "test",
"Content-Type": "application/json"
}
)

Expand All @@ -144,10 +145,11 @@ def test_make_on_change_callback_success_with_timeout(mock_serializer, mock_post
mock_serializer.assert_called_once_with(mock_transaction)
mock_post.assert_called_once_with(
url=mock_transaction.on_change_callback,
json={"transaction": mock_serializer(mock_transaction).data},
data=json.dumps({}).encode(),
timeout=5,
headers={
"Signature": "test"
"Signature": "test",
"Content-Type": "application/json"
}
)

Expand Down
18 changes: 12 additions & 6 deletions polaris/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,19 @@ def make_on_change_callback(
raise ValueError("invalid or missing on_change_callback")
if not timeout:
timeout = settings.CALLBACK_REQUEST_TIMEOUT
callback_body = {"transaction": TransactionSerializer(transaction).data}
callback_body = json.dumps({"transaction": TransactionSerializer(transaction).data})
try:
signature_header_value = compute_callback_signature(transaction.on_change_callback, callback_body)
except ValueError: #
logger.error(f"unable to parse host of transaction.on_change_callback for transaction {transaction.id}")
return None
headers = {"Signature": signature_header_value}
headers = {
"Signature": signature_header_value,
"Content-Type": "application/json"
}
return post(
url=transaction.on_change_callback,
json=callback_body,
data=callback_body.encode(),
timeout=timeout,
headers=headers
)
Expand Down Expand Up @@ -345,17 +348,20 @@ async def make_on_change_callback_async(
if not timeout:
timeout = settings.CALLBACK_REQUEST_TIMEOUT
timeout_obj = aiohttp.ClientTimeout(total=timeout)
callback_body = {"transaction": TransactionSerializer(transaction).data}
callback_body = json.dumps({"transaction": TransactionSerializer(transaction).data})
try:
signature_header_value = compute_callback_signature(transaction.on_change_callback, callback_body)
except ValueError: #
logger.error(f"unable to parse host of transaction.on_change_callback for transaction {transaction.id}")
return None
headers = {"Signature": signature_header_value}
headers = {
"Signature": signature_header_value,
"Content-Type": "application/json"
}
async with aiohttp.ClientSession(timeout=timeout_obj) as session:
return await session.post(
url=transaction.on_change_callback,
json=callback_body,
data=callback_body.encode(),
timeout=timeout,
headers=headers
)
Expand Down

0 comments on commit c6b39f1

Please sign in to comment.