forked from HubSpot/hubspot-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.py
More file actions
29 lines (25 loc) · 835 Bytes
/
webhooks.py
File metadata and controls
29 lines (25 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import hashlib
from hubspot.exceptions import InvalidSignatureError
def validate_signature(
signature: str,
client_secret: str,
http_uri: str,
request_body: str,
http_method="POST",
signature_version="v2",
):
if signature_version == "v1":
source_string = client_secret + request_body
elif signature_version == "v2":
source_string = client_secret + http_method + http_uri + request_body
else:
raise ValueError(
"Not supported signature version: {}".format(signature_version)
)
hash_result = hashlib.sha256(source_string.encode("utf-8")).hexdigest()
if hash_result != signature:
raise InvalidSignatureError(
signature=signature,
signature_version=signature_version,
hash_result=hash_result,
)