public function dispatch(RequestInterface $request, ResponseInterface $response)
{
if ($request instanceof CliRequest) {
$this->initiateDispatchLoop($request, $response);
return;
}
// NOTE: The dispatcher is used for both Action- and CLI-Requests. For the latter case dispatching might happen during compile-time, that's why we can't inject the following dependencies
/** @var Context $securityContext */
$securityContext = $this->objectManager->get(Context::class);
if ($securityContext->areAuthorizationChecksDisabled()) {
$this->initiateDispatchLoop($request, $response);
return;
}
/** @var FirewallInterface $firewall */
$firewall = $this->objectManager->get(FirewallInterface::class);
/** @var SecurityLoggerInterface $securityLogger */
$securityLogger = $this->objectManager->get(SecurityLoggerInterface::class);
try {
/** @var ActionRequest $request */
$firewall->blockIllegalRequests($request);
$this->initiateDispatchLoop($request, $response);
} catch (AuthenticationRequiredException $exception) {
$entryPointFound = false;
/** @var $token TokenInterface */
foreach ($securityContext->getAuthenticationTokens() as $token) {
$entryPoint = $token->getAuthenticationEntryPoint();
if ($entryPoint === null) {
continue;
}
$entryPointFound = true;
if ($entryPoint instanceof WebRedirect) {
$securityLogger->log('Redirecting to authentication entry point', LOG_INFO, $entryPoint->getOptions());
} else {
$securityLogger->log(sprintf('Starting authentication with entry point of type "%s"', get_class($entryPoint)), LOG_INFO);
}
$securityContext->setInterceptedRequest($request->getMainRequest());
/** @var HttpResponse $response */
$entryPoint->startAuthentication($request->getHttpRequest(), $response);
}
if ($entryPointFound === false) {
$securityLogger->log('No authentication entry point found for active tokens, therefore cannot authenticate or redirect to authentication automatically.', LOG_NOTICE);
throw $exception;
}
} catch (AccessDeniedException $exception) {
$securityLogger->log('Access denied', LOG_WARNING);
throw $exception;
}
}