Skip to content

Instantly share code, notes, and snippets.

@jstacoder
Last active May 16, 2019 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstacoder/180925be6dc7bba6db9a3b2c3e0f0924 to your computer and use it in GitHub Desktop.
Save jstacoder/180925be6dc7bba6db9a3b2c3e0f0924 to your computer and use it in GitHub Desktop.
VALIDATE QUICKBOOKS SIGNATURE HEADER FOR WEBHOOK PYTHON
import base64
import hmac
import hashlib
def validate_signature_header(verifier_token, request_body, signature):
# per quickbooks documentation
# 1st step:
# hash the notification payload (request_body) with HMAC_SHA256_ALGORITHM
# using <verifier token> as the key
hmac_hex_digest = hmac.new(
verifier_token,
request_body,
hashlib.sha256
)
# 2nd step:
# convert the intuit-signature header from base-64 to base-16
decoded_hex_signature = base64.b64decode(
signature
).encode('hex')
# 3rd step
# compare values from step 1 and 2
return hmac_hex_digest == decoded_hex_signature
@aakashsingh24
Copy link

aakashsingh24 commented Dec 14, 2018

Hello I am getting a error,
File "", line 14, in validate_signature_header
AttributeError: 'bytes' object has no attribute 'encode'

Mine verifier_token = b'123##########'
request_body = request.body,
signature = request.META.get('HTTP_INTUIT_SIGNATURE').encode('latin1')

@sharozmirza
Copy link

I think the original code was written for python2. It did not work for me as well since I was using python3. Here is the code that worked for me:

import base64
import hmac
import hashlib

def validate_signature_header(verifier_token, request_body, signature):

hmac_hex_digest = hmac.new(
    verifier_token,    # token from quickbooks in bytes
    request_body,    # request_body = request.data
    hashlib.sha256
).hexdigest()

decoded_hex_signature = base64.b64decode(
    signature   # request.headers.get('intuit-signature')
).hex()

return hmac_hex_digest == decoded_hex_signature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment