translator = $translator; $this->routesWhitelist = $routesWhitelist; $this->logger = $logger ?: new NullLogger(); $this->requestToAuthPlugin = $requestToAuthPlugin; } /** * Process an incoming server request and return a response, optionally delegating * to the next middleware component to create the response. * * @param Request $request * @param RequestHandlerInterface $handler * * @return Response * @throws \InvalidArgumentException */ public function process(Request $request, RequestHandlerInterface $handler): Response { /** @var RouteResult|null $routeResult */ $routeResult = $request->getAttribute(RouteResult::class); if ($routeResult === null || $routeResult->isFailure() || $request->getMethod() === self::METHOD_OPTIONS || in_array($routeResult->getMatchedRouteName(), $this->routesWhitelist, true) ) { return $handler->handle($request); } try { $plugin = $this->requestToAuthPlugin->fromRequest($request); } catch (ContainerExceptionInterface | NoAuthenticationException $e) { $this->logger->warning('Invalid or no authentication provided.' . PHP_EOL . $e); return $this->createErrorResponse(sprintf($this->translator->translate( 'Expected one of the following authentication headers, but none were provided, ["%s"]' ), implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS))); } try { $plugin->verify($request); $response = $handler->handle($request); return $plugin->update($request, $response); } catch (VerifyAuthenticationException $e) { $this->logger->warning('Authentication verification failed.' . PHP_EOL . $e); return $this->createErrorResponse($e->getPublicMessage(), $e->getErrorCode()); } } private function createErrorResponse( string $message, string $errorCode = RestUtils::INVALID_AUTHORIZATION_ERROR ): JsonResponse { return new JsonResponse([ 'error' => $errorCode, 'message' => $message, ], self::STATUS_UNAUTHORIZED); } }