I am trying to validate GitHub webhook secret using API Gateway.
This is my lambda:
import json
import hmac
import hashlib
import re
GITHUB_SECRET = 'HELLO WORLD' # from Github UI
def lambda_handler(event, context):
print("Lambda execution starting up...")
incoming_signature = re.sub(r'^sha1=', '', event['headers']['X-Hub-Signature'])
enhanced_body_msg = json.dumps(event['body'], default=str)
calculated_signature = calculate_signature(GITHUB_SECRET, enhanced_body_msg.encode('utf-8'))
print("Incoming sig:", incoming_signature)
print("calculated_signature:", calculated_signature)
if incoming_signature != calculated_signature:
print('Unauthorized attempt')
return {
'statusCode': 403,
'body': json.dumps('Forbidden')
}
print('Request successfully authorized')
# do stuff in Lambda
return {
'statusCode': 200,
'body': json.dumps(f'Work in progress')
}
def calculate_signature(github_signature, githhub_payload):
signature_bytes = bytes(github_signature, 'utf-8')
digest = hmac.new(key=signature_bytes, msg=githhub_payload, digestmod=hashlib.sha1)
signature = digest.hexdigest()
return signature
Used this as a reference(Github Webhooks secret with AWS API Gateway) but still, it's always failing to match. Please if someone can point out a mistake. Also tried for X-Hub-Signature-256
same issue.