diff --git a/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php b/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php index cebce3e..50c88db 100644 --- a/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php +++ b/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php @@ -12,12 +12,13 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Flood\FloodInterface; +use Drupal\Core\Routing\RouteMatch; use Drupal\user\UserAuthInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; -use Symfony\Cmf\Component\Routing\RouteObjectInterface; +use Symfony\Component\Routing\Route; /** @@ -76,14 +77,10 @@ public function __construct(ConfigFactoryInterface $config_factory, UserAuthInte * {@inheritdoc} */ public function applies(Request $request) { - // Only allow basic auth when it was specified explicitely on the route. - if ($request->attributes->has(RouteObjectInterface::ROUTE_OBJECT)) { - $route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT); - if ($route->hasOption('_auth') && in_array('basic_auth', $route->getOption('_auth'))) { - $username = $request->headers->get('PHP_AUTH_USER'); - $password = $request->headers->get('PHP_AUTH_PW'); - return isset($username) && isset($password); - } + if ($this->hasCredentials($request)) { + // Only allow basic auth when it was specified explicitely on the route. + $route = RouteMatch::createFromRequest($request)->getRouteObject(); + return isset($route) && $this->appliesToRoute($route); } } @@ -146,8 +143,10 @@ public function cleanup(Request $request) {} */ public function handleException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); - if ($GLOBALS['user']->isAnonymous() && $exception instanceof AccessDeniedHttpException) { - if (!$this->applies($event->getRequest())) { + $request = $event->getRequest(); + if ($GLOBALS['user']->isAnonymous() && $exception instanceof AccessDeniedHttpException && !$this->hasCredentials($request)) { + $route = RouteMatch::createFromRequest($request)->getRouteObject(); + if (isset($route) && $this->appliesToRoute($route)) { $site_name = $this->configFactory->get('system.site')->get('name'); global $base_url; $challenge = String::format('Basic realm="@realm"', array( @@ -160,4 +159,32 @@ 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); + } + }