request = $request; $this->router = $router; $this->storeManager = $storeManager; $this->authorization = $authorization; $this->backpressureContextFactory = $backpressureContextFactory ?? ObjectManager::getInstance()->get(BackpressureContextFactory::class); $this->backpressureEnforcer = $backpressureEnforcer ?? ObjectManager::getInstance()->get(BackpressureEnforcerInterface::class); } /** * Validates the request * * @throws AuthorizationException * @throws WebapiException * @return void */ public function validate() { $route = $this->router->match($this->request); $this->checkPermissions($route); $this->onlyHttps($route); $this->checkBackpressure($route); } /** * Perform authentication and authorization * * @param Route $route * @return void * @throws AuthorizationException */ private function checkPermissions(Route $route) { if ($this->authorization->isAllowed($route->getAclResources())) { return; } throw new AuthorizationException( __( "The consumer isn't authorized to access %resources.", ['resources' => implode(', ', $route->getAclResources())] ) ); } /** * Checks if operation allowed only in HTTPS * * @param Route $route * @throws WebapiException */ private function onlyHttps(Route $route) { if ($route->isSecure() && !$this->request->isSecure()) { throw new WebapiException(__('Operation allowed only in HTTPS')); } } /** * Checks backpressure * * @param Route $route * @throws WebapiException */ private function checkBackpressure(Route $route) { $context = $this->backpressureContextFactory->create( $route->getServiceClass(), $route->getServiceMethod(), $route->getRoutePath() ); if ($context) { try { $this->backpressureEnforcer->enforce($context); } catch (BackpressureExceededException $exception) { throw new WebapiException( __('Too Many Requests'), 0, WebapiException::HTTP_TOO_MANY_REQUESTS ); } } } }