Created
September 15, 2015 17:21
-
-
Save steveneaston/469191d0df7b17d9521e to your computer and use it in GitHub Desktop.
Verify a Shopify hook signature using Laravel Middleware
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 | |
namespace App\Http\Middleware; | |
use Closure; | |
class VerifyShopifyWebhook | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
if (! $this->verifyShopifySignature($request)) { | |
abort(401); | |
} | |
return $next($request); | |
} | |
private function verifyShopifySignature($request) | |
{ | |
return ($request->header('x-shopify-hmac-sha256') == $this->calculateHmac()); | |
} | |
private function calculateHmac() | |
{ | |
$data = file_get_contents('php://input'); | |
return base64_encode(hash_hmac('sha256', $data, env('SHOPIFY_APP_SECRET'), true)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
How this works exactly? I can successfully verify the signature but I'm getting a 405 => Method not allowed. I'm using the middleware as a routeMiddleware.
Any help would be appreciated.
Thank you.