How to validate a Github Webhook
To begin with, you need to set up your server and create an endpoint for the webhook. To access the
header, I will utilize the Gin Web Framework. To perform the verification, we require a function that will
extract the request body and the value from the header using the key X-Hub-Signature-256
. Essentially,
Github uses the body and your key to generate a hash to prevent any brute-forcing of your key. Consequently,
the X-Hub-Signature-256 will always differ. If you want to learn more, you should refer to the Github documentations
Don’t forget to handle the errors when you are programming yourself.
Initially, we must generate a HMAC hash object and provide our secret key, which we also provided to Github in the webhook settings. This enables us to construct our payload and authenticate it. The sum function is subsequently applied to finalize the HMAC computation, producing the MAC as a byte slice. We can then assemble our string and compare it to the given hash.