Created
November 8, 2019 16:00
-
-
Save danny50610/9e4e884342bff7a1e593d11398c5cfe0 to your computer and use it in GitHub Desktop.
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 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