diff --git a/core/core.services.yml b/core/core.services.yml index b5f8446..a7addbd 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -829,6 +829,10 @@ services: tags: - { name: access_check, applies_to: _csrf_token, needs_incoming_request: TRUE } arguments: ['@csrf_token'] + access_check.auth_method: + class: Drupal\Core\Access\AuthenticationMethodCheck + tags: + - { name: access_check, needs_incoming_request: TRUE } maintenance_mode: class: Drupal\Core\Site\MaintenanceMode arguments: ['@state', '@current_user'] diff --git a/core/lib/Drupal/Core/Access/AuthenticationMethodCheck.php b/core/lib/Drupal/Core/Access/AuthenticationMethodCheck.php new file mode 100644 index 0000000..c27aa3e --- /dev/null +++ b/core/lib/Drupal/Core/Access/AuthenticationMethodCheck.php @@ -0,0 +1,71 @@ +defaultProviders = array('cookie' => TRUE); + } + + /** + * {@inheritdoc} + */ + public function applies(Route $route) { + return TRUE; + } + + /** + * Checks whether authentication was performed with an allowed method. + * + * @param Symfony\Component\Routing\Route $route + * The route to check. + * @param Symfony\Component\HttpFoundation\Request $request + * The incoming request. This parameter is not used by the method. Its + * purpose is to indicate that this access check only makes sense when + * processing the incoming request. + * @param Drupal\Core\Session\AccountProxyInterface $account + * The account proxy for the authenticated user. + */ + public function access(Route $route, Request $request, AccountProxyInterface $account) { + $provider_id = $account->getAuthenticationMethod(); + if (!isset($provider_id)) { + return AccessResult::allowed()->setCacheable(FALSE); + } + + $has_auth_option = $route->hasOption('_auth'); + if (!$has_auth_option && !empty($this->defaultProviders[$provider_id])) { + return AccessResult::allowed()->setCacheable(FALSE); + } + + if ($has_auth_option && in_array($provider_id, $route->getOption('_auth'))) { + return AccessResult::allowed()->setCacheable(FALSE); + } + + return AccessResult::forbidden()->setCacheable(FALSE); + } + +} diff --git a/core/lib/Drupal/Core/Access/NoCookieAuthCheck.php b/core/lib/Drupal/Core/Access/NoCookieAuthCheck.php deleted file mode 100644 index 23474b3..0000000 --- a/core/lib/Drupal/Core/Access/NoCookieAuthCheck.php +++ /dev/null @@ -1,83 +0,0 @@ -sessionConfiguration = $session_configuration; - } - - /** - * {@inheritdoc} - */ - public function applies(Route $route) { - return $this->appliesToRoute($route); - } - - /** - * Checks whether an incoming request has cookie auth credentials. - * - * This check is only applied to routes where cookie auth is *not* allowed. - */ - public function access(Request $request) { - if ($this->hasCredentials($request)) { - return AccessResult::forbidden()->setCacheable(FALSE); - } - else { - return AccessResult::allowed()->setCacheable(FALSE); - } - } - - /** - * Returns whether cookie auth is enabled on a route. - * - * @param \Symfony\Component\Routing\Route $route - * The route to be checked. - * - * @return bool - * TRUE if cookie auth is enabled, FALSE otherwise. - */ - protected function appliesToRoute(Route $route) { - return $route->hasOption('_auth') && !in_array('cookie', $route->getOption('_auth')); - } - - /** - * Returns whether cookie auth credentials are on the request. - * - * @param \Symfony\Component\HttpFoundation\Request $request - * The request to be checked. - * - * @return bool - * TRUE if cookie auth credentials are on the request, FALSE otherwise. - */ - protected function hasCredentials(Request $request) { - return $request->hasSession() && $this->sessionConfiguration->hasSession($request); - } - -} diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php index 118a05b..53af56a 100644 --- a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php +++ b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php @@ -75,7 +75,7 @@ public function applies(Request $request) { /** * {@inheritdoc} */ - public function authenticate(Request $request) { + public function authenticate(Request $request, &$triggered_provider = NULL) { $account = NULL; // Iterate the allowed providers. @@ -83,6 +83,7 @@ public function authenticate(Request $request) { if ($provider->applies($request)) { // Try to authenticate with this provider, skipping all others. $account = $provider->authenticate($request); + $triggered_provider = $provider_id; break; } } diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index ed62270..accceaa 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -688,6 +688,7 @@ protected function initializeContainer($rebuild = FALSE) { // Save the id of the currently logged in user. if ($this->container->initialized('current_user')) { $current_user_id = $this->container->get('current_user')->id(); + $current_user_auth = $this->container->get('current_user')->getAuthenticationMethod(); } // If there is a session manager, close and save the session. @@ -739,6 +740,7 @@ protected function initializeContainer($rebuild = FALSE) { if (!empty($current_user_id)) { $this->container->get('current_user')->setInitialAccountId($current_user_id); + $this->container->get('current_user')->setAuthenticationMethod($current_user_auth); } \Drupal::setContainer($this->container); diff --git a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php index 4d622a7..1146f30 100644 --- a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php @@ -58,8 +58,10 @@ public function __construct(AuthenticationProviderInterface $authentication_prov public function onKernelRequestAuthenticate(GetResponseEvent $event) { if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { $request = $event->getRequest(); - $account = $this->authenticationProvider->authenticate($request); + $triggered_provider = NULL; + $account = $this->authenticationProvider->authenticate($request, $triggered_provider); $this->accountProxy->setAccount($account); + $this->accountProxy->setAuthenticationMethod($triggered_provider); } } diff --git a/core/lib/Drupal/Core/Session/AccountProxy.php b/core/lib/Drupal/Core/Session/AccountProxy.php index b675b02..7c56b1b 100644 --- a/core/lib/Drupal/Core/Session/AccountProxy.php +++ b/core/lib/Drupal/Core/Session/AccountProxy.php @@ -28,6 +28,11 @@ class AccountProxy implements AccountProxyInterface { protected $account; /** + * The authentication method used. + */ + protected $authenticationMethod; + + /** * Initial account id. * * @var int @@ -182,4 +187,18 @@ public function setInitialAccountId($account_id) { $this->initialAccountId = $account_id; } + /** + * {@inheritdoc} + */ + public function setAuthenticationMethod($provider_id) { + $this->authenticationMethod = $provider_id; + } + + /** + * {@inheritdoc} + */ + public function getAuthenticationMethod() { + return $this->authenticationMethod; + } + } diff --git a/core/lib/Drupal/Core/Session/AccountProxyInterface.php b/core/lib/Drupal/Core/Session/AccountProxyInterface.php index 7ac9758..ee0f16a 100644 --- a/core/lib/Drupal/Core/Session/AccountProxyInterface.php +++ b/core/lib/Drupal/Core/Session/AccountProxyInterface.php @@ -48,4 +48,20 @@ public function getAccount(); */ public function setInitialAccountId($account_id); + /** + * Sets the used authentication method. + * + * @param string $provider_id + * The id of the used authentication method. + */ + public function setAuthenticationMethod($provider_id); + + /** + * Gets the used authentication method. + * + * @return string|NULL + * The id of the used authentication method or NULL if not authenticated. + */ + public function getAuthenticationMethod(); + } diff --git a/core/modules/basic_auth/basic_auth.services.yml b/core/modules/basic_auth/basic_auth.services.yml index 6021fbc..05d1252 100644 --- a/core/modules/basic_auth/basic_auth.services.yml +++ b/core/modules/basic_auth/basic_auth.services.yml @@ -4,7 +4,3 @@ services: arguments: ['@config.factory', '@user.auth', '@flood', '@entity.manager', '@current_user'] tags: - { name: authentication_provider, priority: 100 } - access_check.no_basic_auth: - class: Drupal\basic_auth\Access\NoBasicAuthCheck - tags: - - { name: access_check, needs_incoming_request: TRUE } diff --git a/core/modules/basic_auth/src/Access/NoBasicAuthCheck.php b/core/modules/basic_auth/src/Access/NoBasicAuthCheck.php deleted file mode 100644 index e33ca8f..0000000 --- a/core/modules/basic_auth/src/Access/NoBasicAuthCheck.php +++ /dev/null @@ -1,69 +0,0 @@ -appliesToRoute($route); - } - - /** - * Checks whether an incoming request has basic auth credentials. - * - * This check is only applied to routes where basic auth is *not* allowed. - */ - public function access(Request $request) { - if ($this->hasCredentials($request)) { - return AccessResult::forbidden()->setCacheable(FALSE); - } - else { - return AccessResult::allowed()->setCacheable(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); - } - -} diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml index dca89ea..2ef1a5c 100644 --- a/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -20,11 +20,6 @@ services: arguments: ['@session_configuration'] tags: - { name: authentication_provider, priority: 0 } - access_check.no_cookie_auth: - class: Drupal\Core\Access\NoCookieAuthCheck - arguments: ['@session_configuration'] - tags: - - { name: access_check, needs_incoming_request: TRUE } cache_context.user: class: Drupal\user\Cache\UserCacheContext arguments: ['@current_user']