diff --git a/core/authorize.php b/core/authorize.php index 6896e79..7510277 100644 --- a/core/authorize.php +++ b/core/authorize.php @@ -55,8 +55,10 @@ */ function authorize_access_allowed(Request $request) { $account = \Drupal::service('authentication')->authenticate($request); - \Drupal::service('current_user')->setAccount($account); - return Settings::get('allow_authorize_operations', TRUE) && $account->hasPermission('administer software updates'); + if ($account) { + \Drupal::service('current_user')->setAccount($account); + } + return Settings::get('allow_authorize_operations', TRUE) && \Drupal::service('current_user')->hasPermission('administer software updates'); } try { diff --git a/core/core.services.yml b/core/core.services.yml index c5c3c43..b5f8446 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1090,11 +1090,6 @@ services: arguments: ['@authentication', '@current_user'] tags: - { name: event_subscriber } - authentication_provider_check_subscriber: - class: Drupal\Core\EventSubscriber\AuthenticationProviderCheckSubscriber - arguments: ['@authentication'] - tags: - - { name: event_subscriber } account_switcher: class: Drupal\Core\Session\AccountSwitcher arguments: ['@current_user', '@session_handler.write_safe'] diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php index 69a4f44..d154abd 100644 --- a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php +++ b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php @@ -22,7 +22,7 @@ * * If no provider set an active user then the user is set to anonymous. */ -class AuthenticationManager implements AuthenticationProviderInterface, AuthenticationProviderAccessInterface { +class AuthenticationManager implements AuthenticationProviderInterface, AuthenticationProviderAccessInterface, AuthenticationProviderChallengeInterface { /** * Array of all registered authentication providers, keyed by ID. @@ -46,6 +46,27 @@ class AuthenticationManager implements AuthenticationProviderInterface, Authenti protected $sortedProviders; /** + * List of providers which implement the access interface. + * + * @var \Drupal\Core\Authentication\AuthenticationAccessProviderInterface[] + */ + protected $accessCheckers; + + /** + * List of providers which implement the challenge interface. + * + * @var \Drupal\Core\Authentication\AuthenticationChallengeProviderInterface[] + */ + protected $challengeProviders; + + /** + * List of providers which are allowed on routes with no _auth option. + * + * @var array + */ + protected $globalProviders; + + /** * Adds a provider to the array of registered providers. * * @param \Drupal\Core\Authentication\AuthenticationProviderInterface $provider @@ -63,56 +84,50 @@ public function addProvider(AuthenticationProviderInterface $provider, $id, $pri $this->providerOrders[$priority][$id] = $provider; // Force the builders to be re-sorted. $this->sortedProviders = NULL; + + if ($provider instanceof AuthenticationProviderAccessInterface) { + $this->accessCheckers[$id] = $provider; + } + if ($provider instanceof AuthenticationProviderChallengeInterface) { + $this->challengeProviders[$id] = $provider; + } + if ($id === 'cookie') { + $this->globalProviders[$id] = $provider; + } } /** * {@inheritdoc} */ public function applies(Request $request) { - return TRUE; + return (bool) $this->getProvider($request); } /** * {@inheritdoc} */ public function authenticate(Request $request) { - $account = NULL; - $provider_id = $this->getProvider($request); - if (isset($provider_id)) { - $account = $this->providers[$provider_id]->authenticate($request); - } - - // No provider returned a valid account, so set the user to anonymous. - if (!$account) { - $account = new AnonymousUserSession(); - } - - return $account; + return $this->providers[$provider_id]->authenticate($request); } /** * {@inheritdoc} */ public function access(Request $request) { - $result = TRUE; - $provider_id = $this->getProvider($request); - if (isset($provider_id)) { - $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'); + // Always grant access if no provider claims ownership of the request. + return $provider_id ? $this->hasAccess($request, $provider_id) : TRUE; + } - $allowed_by_option = $has_auth_option && in_array($provider_id, $route->getOption('_auth')); - $result = !$has_auth_option || $allowed_by_option; - } + /** + * {@inheritdoc} + */ + public function challengeException(Request $request, \Exception $previous) { + $provider_id = $this->getChallengeProvider($request); + if ($provider_id) { + return $this->challengeProviders[$provider_id]->challengeException($request, $previous); } - - return $result; } /** @@ -134,6 +149,53 @@ protected function getProvider(Request $request) { } /** + * Returns the id of the challenge 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 getChallengeProvider($request) { + if (!empty($this->challengeProviders)) { + foreach ($this->getSortedProviders() as $provider_id => $provider) { + if (isset($this->challengeProviders[$provider_id]) && $this->hasAccess($request, $provider_id) && !$provider->applies($request)) { + return $provider_id; + } + } + } + } + + /** + * Checks whether a provider is allowed on the given request. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The incoming request. + * @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 hasAccess(Request $request, $provider_id) { + if (isset($this->accessCheckers[$provider_id])) { + $result = $this->accessCheckers[$provider_id]->access($request); + } + 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; + } + + return $result; + } + + /** * Returns the sorted array of authentication providers. * * @return array @@ -152,15 +214,4 @@ protected function getSortedProviders() { return $this->sortedProviders; } - /** - * {@inheritdoc} - */ - public function handleException(GetResponseForExceptionEvent $event) { - foreach ($this->getSortedProviders() as $provider) { - if ($provider->handleException($event) === TRUE) { - break; - } - } - } - } diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationProviderAccessInterface.php b/core/lib/Drupal/Core/Authentication/AuthenticationProviderAccessInterface.php index d62dd1c..5fbba20 100644 --- a/core/lib/Drupal/Core/Authentication/AuthenticationProviderAccessInterface.php +++ b/core/lib/Drupal/Core/Authentication/AuthenticationProviderAccessInterface.php @@ -12,9 +12,9 @@ /** * Restrict authentication methods to a subset of the site. * - * Some authentication methods should not be available throughout the a whole - * site. E.g., there are good reasons to restrict insecure methods like HTTP - * basic auth or an URL token authentication method to API-only paths. + * Some authentication methods should not be available throughout a whole site. + * E.g., there are good reasons to restrict insecure methods like HTTP basic + * auth or an URL token authentication method to API-only routes/paths. */ interface AuthenticationProviderAccessInterface { diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationProviderChallengeInterface.php b/core/lib/Drupal/Core/Authentication/AuthenticationProviderChallengeInterface.php new file mode 100644 index 0000000..506a4ae --- /dev/null +++ b/core/lib/Drupal/Core/Authentication/AuthenticationProviderChallengeInterface.php @@ -0,0 +1,34 @@ +check = $check; - } - - /** - * Checks whether authentication was performed with a method allowed on the route. - * - * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event - * The request event. - */ - public function onKernelRequestCheckAuthenticationProvider(GetResponseEvent $event) { - if (!$this->check->access($event->getRequest())) { - throw new AccessDeniedHttpException(); - } - } - - /** - * {@inheritdoc} - */ - public static function getSubscribedEvents() { - // Runs after routing. - $events[KernelEvents::REQUEST][] = array('onKernelRequestCheckAuthenticationProvider', 31); - return $events; - } - -} diff --git a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php index 4d622a7..1630911 100644 --- a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php @@ -7,14 +7,16 @@ namespace Drupal\Core\EventSubscriber; +use Drupal\Core\Authentication\AuthenticationProviderAccessInterface; +use Drupal\Core\Authentication\AuthenticationProviderChallengeInterface; use Drupal\Core\Authentication\AuthenticationProviderInterface; use Drupal\Core\Session\AccountProxyInterface; -use Symfony\Component\HttpKernel\HttpKernelInterface; -use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; -use Symfony\Component\HttpKernel\Event\FilterResponseEvent; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\KernelEvents; /** * Authentication subscriber. @@ -31,6 +33,20 @@ class AuthenticationSubscriber implements EventSubscriberInterface { protected $authenticationProvider; /** + * Authentication access check. + * + * @var \Drupal\Core\Authentication\AuthenticationAccessProviderInterface|NULL + */ + protected $accessChecker; + + /** + * Authentication challenge provider. + * + * @var \Drupal\Core\Authentication\AuthenticationChallengeProviderInterface|NULL + */ + protected $challengeProvider; + + /** * Account proxy. * * @var \Drupal\Core\Session\AccountProxyInterface @@ -38,55 +54,92 @@ class AuthenticationSubscriber implements EventSubscriberInterface { protected $accountProxy; /** - * Keep authentication manager as private variable. + * Constructs an authentication subscriber. * - * @param \Drupal\Core\Authentication\AuthenticationProviderInterface $authentication_manager - * The authentication manager. + * @param \Drupal\Core\Authentication\AuthenticationProviderInterface $authentication_provider + * An authentication provider. * @param \Drupal\Core\Session\AccountProxyInterface $account_proxy * Account proxy. */ public function __construct(AuthenticationProviderInterface $authentication_provider, AccountProxyInterface $account_proxy) { $this->authenticationProvider = $authentication_provider; + $this->accessChecker = ($authentication_provider instanceof AuthenticationProviderAccessInterface) ? $authentication_provider : NULL; + $this->challengeProvider = ($authentication_provider instanceof AuthenticationProviderChallengeInterface) ? $authentication_provider : NULL; $this->accountProxy = $account_proxy; } /** * Authenticates user on request. * + * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event + * The request event. + * * @see \Drupal\Core\Authentication\AuthenticationProviderInterface::authenticate() */ public function onKernelRequestAuthenticate(GetResponseEvent $event) { - if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { + if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) { $request = $event->getRequest(); - $account = $this->authenticationProvider->authenticate($request); - $this->accountProxy->setAccount($account); + if ($this->authenticationProvider->applies($request)) { + $account = $this->authenticationProvider->authenticate($request); + if ($account) { + $this->accountProxy->setAccount($account); + } + } } } /** - * Pass exception handling to authentication manager. + * Denies access if authentication provider is not allowed on this route. + * + * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event + * The request event. + */ + public function onKernelRequestCheckProviderAccess(GetResponseEvent $event) { + if (isset($this->accessChecker) && $event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) { + $request = $event->getRequest(); + if ($this->authenticationProvider->applies($request) && !$this->accessChecker->access($request)) { + throw new AccessDeniedHttpException(); + } + } + } + + /** + * Respond with a challenge on access denied exceptions if appropriate. + * + * On a 403 (access denied), if there are no credentials on the request, some + * authentication methods (e.g. basic auth) require that a challenge is sent + * to the client. * * @param GetResponseForExceptionEvent $event + * The exception event. */ - public function onException(GetResponseForExceptionEvent $event) { - if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { - $this->authenticationProvider->handleException($event); + 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->accessChecker) || $this->accessChecker->access($request))) { + $challenge_exception = $this->challengeProvider->challengeException($request, $exception); + if ($challenge_exception) { + $event->setException($challenge_exception); + } + } } } /** * {@inheritdoc} - * - * The priority for request must be higher than the highest event subscriber - * accessing the current user. - * The priority for the response must be as low as possible allowing e.g the - * Cookie provider to send all relevant session data to the user. */ public static function getSubscribedEvents() { - // Priority must be higher than LanguageRequestSubscriber as LanguageManager - // access current user in case language module enabled. - $events[KernelEvents::REQUEST][] = array('onKernelRequestAuthenticate', 300); - $events[KernelEvents::EXCEPTION][] = ['onException', 75]; + // The priority for authentictaion 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. + $events[KernelEvents::REQUEST][] = ['onKernelRequestAuthenticate', 300]; + + // Access check must be performed after routing. + $events[KernelEvents::REQUEST][] = ['onKernelRequestCheckProviderAccess', 31]; + $events[KernelEvents::EXCEPTION][] = ['onExceptionSendChallange', 75]; return $events; } + } diff --git a/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php b/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php index ef6c29e..ec31954 100644 --- a/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php +++ b/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php @@ -9,7 +9,7 @@ use Drupal\Component\Utility\String; use Drupal\Core\Authentication\AuthenticationProviderInterface; -use Drupal\Core\Authentication\AuthenticationProviderAccessInterface; +use Drupal\Core\Authentication\AuthenticationProviderChallengeInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Flood\FloodInterface; @@ -17,15 +17,13 @@ use Drupal\Core\Session\AccountInterface; use Drupal\user\UserAuthInterface; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; -use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; use Symfony\Component\Routing\Route; /** * HTTP Basic authentication provider. */ -class BasicAuth implements AuthenticationProviderInterface, AuthenticationProviderAccessInterface { +class BasicAuth implements AuthenticationProviderInterface, AuthenticationProviderChallengeInterface { /** * The config factory. @@ -94,16 +92,6 @@ public function applies(Request $request) { /** * {@inheritdoc} */ - public function access(Request $request) { - $route = RouteMatch::createFromRequest($request)->getRouteObject(); - return isset($route) && - $route->hasOption('_auth') && - in_array('basic_auth', $route->getOption('_auth')); - } - - /** - * {@inheritdoc} - */ public function authenticate(Request $request) { $flood_config = $this->configFactory->get('user.flood'); $username = $request->headers->get('PHP_AUTH_USER'); @@ -153,21 +141,14 @@ public function authenticate(Request $request) { /** * {@inheritdoc} */ - public function handleException(GetResponseForExceptionEvent $event) { - $exception = $event->getException(); - $request = $event->getRequest(); - 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( - '@realm' => !empty($site_name) ? $site_name : $base_url, - )); - $event->setException(new UnauthorizedHttpException($challenge, 'No authentication credentials provided.', $exception)); - } - return TRUE; - } - return FALSE; + 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, + )); + return new UnauthorizedHttpException($challenge, 'No authentication credentials provided.', $previous); } }