Skip to content

Instantly share code, notes, and snippets.

@steveneaston
Created September 15, 2015 17:21
Show Gist options
  • Save steveneaston/469191d0df7b17d9521e to your computer and use it in GitHub Desktop.
Save steveneaston/469191d0df7b17d9521e to your computer and use it in GitHub Desktop.
Verify a Shopify hook signature using Laravel Middleware
<?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));
}
}
@skoulix
Copy link

skoulix commented Oct 30, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment