Skip to content

Instantly share code, notes, and snippets.

@AlienHoboken
Created March 3, 2017 20:07
Show Gist options
  • Save AlienHoboken/83cb307e0cfb25e8b6420275802820d1 to your computer and use it in GitHub Desktop.
Save AlienHoboken/83cb307e0cfb25e8b6420275802820d1 to your computer and use it in GitHub Desktop.
Support for GitHub webhook to update local repository utilizing secret key matching
<?php
//polyfill for missing hash_equals
if(!function_exists('hash_equals'))
{
function hash_equals($str1, $str2)
{
if(strlen($str1) != strlen($str2))
{
return false;
}
else
{
$res = $str1 ^ $str2;
$ret = 0;
for($i = strlen($res) - 1; $i >= 0; $i--)
{
$ret |= ord($res[$i]);
}
return !$ret;
}
}
}
$signatureHeader = $_SERVER['HTTP_X_HUB_SIGNATURE'];
$requestBody = file_get_contents('php://input');
$signature = "sha1=" . hash_hmac('sha1', $requestBody, 'your secret key here');
if(hash_equals($signatureHeader, $signature))
exec('cd /path/to/your/repo git reset --hard HEAD && git pull');
else
echo "Sorry pal, can't do that!";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment