diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php index 925f334..69a4f44 100644 --- a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php +++ b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php @@ -46,14 +46,6 @@ class AuthenticationManager implements AuthenticationProviderInterface, Authenti protected $sortedProviders; /** - * Associative array of providers available on every request unless - * explicitely excluded. - * - * @var array - */ - protected $defaultProviders = ['cookie' => TRUE]; - - /** * Adds a provider to the array of registered providers. * * @param \Drupal\Core\Authentication\AuthenticationProviderInterface $provider @@ -105,21 +97,33 @@ public function authenticate(Request $request) { public function access(Request $request) { $result = TRUE; - $route = RouteMatch::createFromRequest($request)->getRouteObject(); $provider_id = $this->getProvider($request); if (isset($provider_id)) { - $has_auth_option = isset($route) && $route->hasOption('_auth'); + $provider = $this->providers[$provider_id]; + if ($provider instanceof AuthenticationProviderAccessInterface) { + $result = $provider->access($request); + } + else { + $route = RouteMatch::createFromRequest($request)->getRouteObject(); + $has_auth_option = isset($route) && $route->hasOption('_auth'); - $allowed_by_default = !$has_auth_option && isset($this->defaultProviders[$provider_id]); - $allowed_by_option = $has_auth_option && in_array($provider_id, $route->getOption('_auth')); - $result = $allowed_by_default || $allowed_by_option; + $allowed_by_option = $has_auth_option && in_array($provider_id, $route->getOption('_auth')); + $result = !$has_auth_option || $allowed_by_option; + } } return $result; } /** - * Returns id of the authentication provider for a request. + * Returns the id of the authentication provider for a request. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The incoming request. + * + * @return string|NULL + * The id of the first authentication provider which applies to the request. + * If no application detects appropriate credentials, then NULL is returned. */ protected function getProvider(Request $request) { foreach ($this->getSortedProviders() as $provider_id => $provider) { diff --git a/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php b/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php index 330e626..ef6c29e 100644 --- a/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php +++ b/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\String; use Drupal\Core\Authentication\AuthenticationProviderInterface; +use Drupal\Core\Authentication\AuthenticationProviderAccessInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Flood\FloodInterface; @@ -24,7 +25,7 @@ /** * HTTP Basic authentication provider. */ -class BasicAuth implements AuthenticationProviderInterface { +class BasicAuth implements AuthenticationProviderInterface, AuthenticationProviderAccessInterface { /** * The config factory. @@ -85,7 +86,19 @@ public function __construct(ConfigFactoryInterface $config_factory, UserAuthInte * {@inheritdoc} */ public function applies(Request $request) { - return $this->hasCredentials($request); + $username = $request->headers->get('PHP_AUTH_USER'); + $password = $request->headers->get('PHP_AUTH_PW'); + return isset($username) && isset($password); + } + + /** + * {@inheritdoc} + */ + public function access(Request $request) { + $route = RouteMatch::createFromRequest($request)->getRouteObject(); + return isset($route) && + $route->hasOption('_auth') && + in_array('basic_auth', $route->getOption('_auth')); } /** @@ -143,9 +156,8 @@ public function authenticate(Request $request) { public function handleException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $request = $event->getRequest(); - if ($this->currentUser->isAnonymous() && $exception instanceof AccessDeniedHttpException && !$this->hasCredentials($request)) { - $route = RouteMatch::createFromRequest($request)->getRouteObject(); - if (isset($route) && $this->appliesToRoute($route)) { + if ($this->currentUser->isAnonymous() && $exception instanceof AccessDeniedHttpException && !$this->applies($request)) { + if ($this->access($request)) { $site_name = $this->configFactory->get('system.site')->get('name'); global $base_url; $challenge = String::format('Basic realm="@realm"', array( @@ -158,32 +170,4 @@ public function handleException(GetResponseForExceptionEvent $event) { return FALSE; } - /** - * Returns whether basic auth is enabled on a route. - * - * @param \Symfony\Component\Routing\Route $route - * The route to be checked. - * - * @return bool - * TRUE if basic auth is enabled, FALSE otherwise. - */ - protected function appliesToRoute(Route $route) { - return $route->hasOption('_auth') && in_array('basic_auth', $route->getOption('_auth')); - } - - /** - * Returns whether basic auth credentials are on the request. - * - * @param \Symfony\Component\HttpFoundation\Request $request - * The request to be checked. - * - * @return bool - * TRUE if basic auth credentials are on the request, FALSE otherwise. - */ - protected function hasCredentials(Request $request) { - $username = $request->headers->get('PHP_AUTH_USER'); - $password = $request->headers->get('PHP_AUTH_PW'); - return isset($username) && isset($password); - } - }