Created
August 13, 2021 10:09
-
-
Save mahdyar/711beee9fec9cab6bb2f6e48d061d077 to your computer and use it in GitHub Desktop.
Verify GitHub webhook request sha256 in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// It's better to be exported as an environment variable or in a .env file. | |
define("SECRET", "<SECRET>"); | |
$body = file_get_contents("php://input"); | |
// $decodedBody = json_decode(urldecode($body)); | |
if (verifySignature($body) !== false) { | |
// verified | |
} else { | |
// unverified | |
http_response_code(403); | |
echo "unauthorized"; | |
} | |
function verifySignature($body){ | |
$headers = getallheaders(); | |
return hash_equals('sha256='.hash_hmac('sha256', $body, SECRET), $headers['x-hub-signature-256']); | |
} |
For me, it only worked if the header was in the correct case:
X-Hub-Signature-256
. So, if anyone is having issues, try this.
Thank you @gaborszita!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!
For me, it only worked if the header was in the correct case:
X-Hub-Signature-256
. So, if anyone is having issues, try this.