resolveApiKey($request); if ($apiKey === '') { throw new ApiException('Please provide api-key', ReturnCode::UNAUTHORIZED); } if (!hash_equals($expected, $apiKey)) { throw new ApiException('Invalid api-key', ReturnCode::FORBIDDEN); } return $handler($request); } private function resolveApiKey(Request $request): string { $headerValue = $request->header('api-key'); if ($headerValue !== null && trim((string) $headerValue) !== '') { return trim((string) $headerValue); } foreach (['api_key', 'api-key'] as $key) { $val = $request->get($key); if ($val !== null && trim((string) $val) !== '') { return trim((string) $val); } } foreach (['api_key', 'api-key'] as $key) { $val = $request->post($key); if ($val !== null && trim((string) $val) !== '') { return trim((string) $val); } } return ''; } }