diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php index aea0900..6b2ac51 100644 --- a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php +++ b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php @@ -57,7 +57,7 @@ class AuthenticationManager implements AuthenticationProviderInterface, Authenti * * @var \Drupal\Core\Authentication\AuthenticationChallengeProviderInterface[] */ - protected $challengeProviders; + protected $challengers; /** * List of providers which are allowed on routes with no _auth option. @@ -89,7 +89,7 @@ public function addProvider(AuthenticationProviderInterface $provider, $id, $pri $this->filters[$id] = $provider; } if ($provider instanceof AuthenticationProviderChallengeInterface) { - $this->challengeProviders[$id] = $provider; + $this->challengers[$id] = $provider; } if ($id === 'cookie') { $this->globalProviders[$id] = $provider; @@ -114,19 +114,31 @@ public function authenticate(Request $request) { /** * {@inheritdoc} */ - public function appliesToRoutedRequest(Request $request) { - $provider_id = $this->getProvider($request); - // Always grant access if no provider claims ownership of the request. - return $provider_id ? $this->applyFilter($request, $provider_id) : TRUE; + public function appliesToRoutedRequest(Request $request, $authenticated) { + $result = FALSE; + + if ($authenticated) { + $result = $this->applyFilter($request, $authenticated, $this->getProvider($request)); + } + else { + foreach ($this->getSortedProviders() as $provider_id => $provider) { + if ($this->applyFilter($request, $authenticated, $provider_id)) { + $result = TRUE; + break; + } + } + } + + return $result; } /** * {@inheritdoc} */ public function challengeException(Request $request, \Exception $previous) { - $provider_id = $this->getChallengeProvider($request); + $provider_id = $this->getChallenger($request); if ($provider_id) { - return $this->challengeProviders[$provider_id]->challengeException($request, $previous); + return $this->challengers[$provider_id]->challengeException($request, $previous); } } @@ -158,10 +170,10 @@ protected function getProvider(Request $request) { * The id of the first authentication provider which applies to the request. * If no application detects appropriate credentials, then NULL is returned. */ - protected function getChallengeProvider($request) { - if (!empty($this->challengeProviders)) { - foreach ($this->getSortedProviders() as $provider_id => $provider) { - if (isset($this->challengeProviders[$provider_id]) && $this->applyFilter($request, $provider_id) && !$provider->applies($request)) { + protected function getChallenger($request) { + if (!empty($this->challengers)) { + foreach ($this->getSortedProviders($request, FALSE) as $provider_id => $provider) { + if (isset($this->challengers[$provider_id]) && !$provider->applies($request) && $this->applyFilter($request, FALSE, $provider_id)) { return $provider_id; } } @@ -171,31 +183,60 @@ protected function getChallengeProvider($request) { /** * Checks whether a provider is allowed on the given request. * + * If no filter is registered for the given provider id, the default filter + * is applied. + * * @param \Symfony\Component\HttpFoundation\Request $request * The incoming request. + * @param bool $authenticated + * Whether or not the request is authenticated. * @param string $provider_id * The id of the authentication provider to check access for. * * @return bool * TRUE if provider is allowed, FALSE otherwise. */ - protected function applyFilter(Request $request, $provider_id) { + protected function applyFilter(Request $request, $authenticated, $provider_id) { if (isset($this->filters[$provider_id])) { - $result = $this->filters[$provider_id]->appliesToRoutedRequest($request); + $result = $this->filters[$provider_id]->appliesToRoutedRequest($request, $authenticated); } else { - $route = RouteMatch::createFromRequest($request)->getRouteObject(); - $has_auth_option = isset($route) && $route->hasOption('_auth'); - - $allowed_by_default = !$has_auth_option && isset($this->globalProviders[$provider_id]); - $allowed_by_option = $has_auth_option && in_array($provider_id, $route->getOption('_auth')); - $result = $allowed_by_default || $allowed_by_option; + $result = $this->defaultFilter($request, $authenticated, $provider_id); } return $result; } /** + * Default implementation of the provider filter. + * + * Checks whether a provider is allowed as per the _auth option on a route. If + * the option is not set or if the request did not match any route, only + * providers from the global provider set are allowed. + * + * If no filter is registered for the given provider id, the default filter + * is applied. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The incoming request. + * @param bool $authenticated + * Whether or not the request is authenticated. + * @param string $provider_id + * The id of the authentication provider to check access for. + * + * @return bool + * TRUE if provider is allowed, FALSE otherwise. + */ + protected function defaultFilter(Request $request, $authenticated, $provider_id) { + $route = RouteMatch::createFromRequest($request)->getRouteObject(); + $has_auth_option = isset($route) && $route->hasOption('_auth'); + + $allowed_by_default = !$has_auth_option && isset($this->globalProviders[$provider_id]); + $allowed_by_option = $has_auth_option && in_array($provider_id, $route->getOption('_auth')); + return $allowed_by_default || $allowed_by_option; + } + + /** * Returns the sorted array of authentication providers. * * @return array diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationProviderFilterInterface.php b/core/lib/Drupal/Core/Authentication/AuthenticationProviderFilterInterface.php index f1a71e4..604a02b 100644 --- a/core/lib/Drupal/Core/Authentication/AuthenticationProviderFilterInterface.php +++ b/core/lib/Drupal/Core/Authentication/AuthenticationProviderFilterInterface.php @@ -27,11 +27,13 @@ * * @param \Symfony\Component\HttpFoundation\Request $request * The request. + * @param bool $authenticated + * Whether or not the request is authenticated. * * @return bool * TRUE if an authentication method is allowed on the request, otherwise * FALSE. */ - public function appliesToRoutedRequest(Request $request); + public function appliesToRoutedRequest(Request $request, $authenticated); } diff --git a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php index 94d6473..3619e2b 100644 --- a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php @@ -97,7 +97,7 @@ public function onKernelRequestAuthenticate(GetResponseEvent $event) { public function onKernelRequestFilterProvider(GetResponseEvent $event) { if (isset($this->filter) && $event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) { $request = $event->getRequest(); - if ($this->authenticationProvider->applies($request) && !$this->filter->appliesToRoutedRequest($request)) { + if ($this->authenticationProvider->applies($request) && !$this->filter->appliesToRoutedRequest($request, TRUE)) { throw new AccessDeniedHttpException(); } } @@ -117,7 +117,7 @@ public function onExceptionSendChallange(GetResponseForExceptionEvent $event) { if (isset($this->challengeProvider) && $event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) { $request = $event->getRequest(); $exception = $event->getException(); - if ($exception instanceof AccessDeniedHttpException && !$this->authenticationProvider->applies($request) && (!isset($this->filter) || $this->filter->appliesToRoutedRequest($request))) { + if ($exception instanceof AccessDeniedHttpException && !$this->authenticationProvider->applies($request) && (!isset($this->filter) || $this->filter->appliesToRoutedRequest($request, FALSE))) { $challenge_exception = $this->challengeProvider->challengeException($request, $exception); if ($challenge_exception) { $event->setException($challenge_exception); @@ -130,7 +130,7 @@ public function onExceptionSendChallange(GetResponseForExceptionEvent $event) { * {@inheritdoc} */ public static function getSubscribedEvents() { - // The priority for authentictaion must be higher than the highest event + // The priority for authentication must be higher than the highest event // subscriber accessing the current user. Especially it must be higher than // LanguageRequestSubscriber as LanguageManager accesses the current user if // the language module is enabled. diff --git a/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php b/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php index 30f1873..660d44c 100644 --- a/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php +++ b/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php @@ -133,11 +133,9 @@ public function authenticate(Request $request) { * {@inheritdoc} */ public function challengeException(Request $request, \Exception $previous) { - global $base_url; - $site_name = $this->configFactory->get('system.site')->get('name'); $challenge = String::format('Basic realm="@realm"', array( - '@realm' => !empty($site_name) ? $site_name : $base_url, + '@realm' => !empty($site_name) ? $site_name : 'Access restricted', )); return new UnauthorizedHttpException($challenge, 'No authentication credentials provided.', $previous); }