header('x-access-token', ''); $key = config('services.client_api.key'); if (!$key || !$this->isValid($token, $key)) { return response()->json([ 'error' => true, 'message' => 'Invalid access token', 'code' => 403, ], 403); } return $next($request); } private function isValid(string $token, string $key): bool { $raw = base64_decode($token, true); if ($raw === false || strlen($raw) <= 16) { return false; } $iv = substr($raw, 0, 16); $ciphertext = substr($raw, 16); $plaintext = openssl_decrypt($ciphertext, 'aes-256-cbc', hex2bin($key), OPENSSL_RAW_DATA, $iv); if ($plaintext === false) { return false; } return hash_equals(config('services.client_api.secret'), $plaintext); } }