Created
December 2, 2020 08:17
-
-
Save ibrahim-dogan/63b4b5c85d1222c0c25bb92ee688ee82 to your computer and use it in GitHub Desktop.
Facebook webhook request validation middleware
This file contains 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 namespace Common\Http\Middleware; | |
use Closure; | |
use Illuminate\Http\Request; | |
class FacebookWebhookMiddleware | |
{ | |
/** | |
* Verify the facebook callback | |
* | |
* @param $request | |
* @param Closure $next | |
* | |
* @return \Illuminate\Http\Response|\Laravel\Lumen\Http\ResponseFactory | |
*/ | |
public function handle(Request $request, Closure $next) | |
{ | |
$signature = $request->headers->get('X-Hub-Signature'); | |
/** | |
* get the raw content | |
* calculate against raw content to get escaped hex for utf characters if any | |
* https://developers.facebook.com/docs/graph-api/webhooks#receiveupdates | |
*/ | |
$payload = $request->getContent(); | |
if ($signature === null || empty($signature)) { | |
return response('Signature is missing.', 400); | |
} | |
//calculate sha1 hash & prefix with sha1= | |
$hash = 'sha1=' . hash_hmac('sha1', $payload, config('services.facebook.client_secret')); | |
if ($signature !== $hash) { | |
return response('Invalid Signature', 400); | |
} | |
return $next($request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment