Skip to content

Instantly share code, notes, and snippets.

@danny50610
Created November 8, 2019 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danny50610/9e4e884342bff7a1e593d11398c5cfe0 to your computer and use it in GitHub Desktop.
Save danny50610/9e4e884342bff7a1e593d11398c5cfe0 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Middleware;
use Closure;
class VerifyBaHaData
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$signature = $request->header('x-baha-data-signature');
if (is_null($signature)) {
return response()->json(['msg' => 'Need signature']);
}
// 未來需要考慮多 bot 的情況
$app_secret = config('services.haha-gamer.bot.magic_conch.app_secret');
if (empty($app_secret)) {
return response()->json(['msg' => 'App secret is empty']);
}
if (!hash_equals($signature,
'sha1=' . hash_hmac('sha1', $request->getContent(), $app_secret))) {
return response()->json(['msg' => 'Wrong signature']);
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment