Web Hook Verification
When integrating AllCore webhooks, verifying the authenticity of incoming requests is essential. This ensures they genuinely originate from AllCore rather than a malicious source. AllCore attaches an HMAC-SHA1 signature in the X-Payload-Digest header for each webhook, which you can use to confirm the request’s legitimacy. Below is a Python function illustrating how to validate this signature.
import hashlib
import hmac
import binascii
SECRET = 'secret_value' #value set as webhook secret
def verifySignature(callback_raw_response, callback_headers):
callback_signature = callback_headers['X-Payload-Digest']
dig = hmac.new(bytes(SECRET , 'utf-8'), bytes(callback_raw_response , 'utf-8'), hashlib.sha1).digest()
signature = binascii.hexlify(dig).decode()
return callback_signature == signature
callback_raw_response = '{"field":"value"}' # use raw unformatted response body
callback_headers = {"X-Payload-Digest": "7e36252a10fd65cbaacd7ff288df2fd3f9e75a46"} # header from response
print(verifySignature(callback_raw_response, callback_headers))Secret Value: The
SECRETvariable must be set to the same secret you used when creating the webhook.Verify Signature Function:
Parameters: The
verifySignaturefunction accepts the raw webhook body (callback_raw_response) and its headers (callback_headers).Steps: a. Extract the signature from the
X-Payload-Digestheader. b. Generate a newHMAC-SHA1signature using theSECRETand the raw response body. c. Compare the generated signature with the header signature. If they match, the function returnsTrue, confirming the webhook is authentic.
Sample Data: The
callback_raw_responseandcallback_headersexamples provide sample data to test this process.
Last updated