-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/add signature validator (#13)
* feat: [no-ticket] add support for request verification --------- Co-authored-by: Andrii Gerasymchuk <[email protected]>
- Loading branch information
1 parent
4443295
commit 802b78d
Showing
10 changed files
with
89 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import hashlib | ||
import hmac | ||
|
||
|
||
def validate_signature(signing_key: str, timestamp: int, request_body: bytes, signature: str) -> bool: | ||
"""Validate that request was sent from Customer.io | ||
Doc: https://customer.io/docs/journeys/webhooks/#securely-verify-requests | ||
:param signing_key: value for SIGNING KEY from Customer.io | ||
:param timestamp: unix timestamp, value from header X-CIO-Timestamp | ||
:param request_body: body of the request | ||
:param signature: value from header X-CIO-Signature | ||
:returns: True if the request passes validation, False if not | ||
""" | ||
payload = b"v0:" + str(timestamp).encode() + b":" + request_body | ||
computed_signature = hmac.new(key=signing_key.encode(), msg=payload, digestmod=hashlib.sha256).hexdigest() | ||
return hmac.compare_digest(computed_signature, signature) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import pytest | ||
from async_customerio import validate_signature | ||
|
||
BODY = ( | ||
b'{"data":{"action_id":42,"campaign_id":23,"content":"Welcome to the club, we are with you.",' | ||
b'"customer_id":"user-123","delivery_id":"RAECAAFwnUSneIa0ZXkmq8EdkAM==","headers":{"Custom-Header":["custom-value"]},' | ||
b'"identifiers":{"id":"user-123"},"recipient":"[email protected]","subject":"Thanks for signing up"},' | ||
b'"event_id":"01E2EMRMM6TZ12TF9WGZN0WJQT","metric":"sent","object_type":"email","timestamp":1692633432}' | ||
) | ||
X_CIO_SIGNATURE = "c097b83a7d57a0810625180a61213eab7e0389a54b33dd11c3a6f17790c8427a" | ||
X_CIO_TIMESTAMP = 1692633432 | ||
|
||
@pytest.mark.parametrize("signature, body, x_cio_timestamp, expected", [ | ||
(X_CIO_SIGNATURE, BODY, X_CIO_TIMESTAMP, True), | ||
(X_CIO_SIGNATURE, BODY, int(f'{X_CIO_TIMESTAMP + 1}'), False), | ||
("WRONG" + X_CIO_SIGNATURE[5:], BODY, X_CIO_TIMESTAMP, False), | ||
(X_CIO_SIGNATURE, b'{"malicious_key": "malicious_value"}', X_CIO_TIMESTAMP, False), | ||
]) | ||
def test_validate_signature(signature, body, x_cio_timestamp, expected): | ||
signing_key = '755781b5e03a973f3405a85474d5a032a60fd56fabaad66039b12eadd83955fa' | ||
assert validate_signature( | ||
signing_key=signing_key, | ||
timestamp=x_cio_timestamp, | ||
request_body=body, | ||
signature=signature | ||
) is expected | ||
|