diff --git a/src/controller/ControllerWrapper.php b/src/controller/ControllerWrapper.php index 701ea8fd3ecc6352ecb6c972f158d99db5072dee..54a75411fa66b683039e119bd09dcb07164b610a 100644 --- a/src/controller/ControllerWrapper.php +++ b/src/controller/ControllerWrapper.php @@ -1,13 +1,17 @@ <?php require_once __DIR__ . "/BaseController.php"; +require_once __DIR__ . "/../middleware/ApiKeyHandler.php"; class ControllerWrapper { private BaseController $controller; + private string $apiKey; public function __construct(BaseController $controller) { + $headers = getallheaders(); + $this->apiKey = $headers['X-API-KEY'] ?? ''; $this->controller = $controller; } @@ -24,25 +28,32 @@ class ControllerWrapper } public function respond() - { - $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); - + { + $apiKeyHandler = ApiKeyHandler::getInstance(); + $res = $apiKeyHandler->checkApiKey($this->apiKey); $strErrorDesc = ''; $strErrorHeader = ''; - $responseData = []; - - try { - $this->controller->respond($requestMethod, $responseData, $strErrorDesc, $strErrorHeader); - } catch (Error $e) { - $strErrorDesc = $e->getMessage() . ' . Something went wrong!'; - $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; - } catch (ServiceException $e) { - $strErrorDesc = (string) "Service Exception Encountered"; - $strErrorHeader = 'HTTP/1.1 400 Bad Request'; - } catch (Exception $e) { - $strErrorDesc = $e->getMessage(); + if (!$res) { + $strErrorDesc = 'Invalid API Key'; $strErrorHeader = 'HTTP/1.1 400 Bad Request'; + } else { + $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); + + $responseData = []; + + try { + $this->controller->respond($requestMethod, $responseData, $strErrorDesc, $strErrorHeader); + } catch (Error $e) { + $strErrorDesc = $e->getMessage() . ' . Something went wrong!'; + $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; + } catch (ServiceException $e) { + $strErrorDesc = (string) "Service Exception Encountered"; + $strErrorHeader = 'HTTP/1.1 400 Bad Request'; + } catch (Exception $e) { + $strErrorDesc = $e->getMessage(); + $strErrorHeader = 'HTTP/1.1 400 Bad Request'; + } } // send output