diff --git a/core/modules/basic_auth/basic_auth.services.yml b/core/modules/basic_auth/basic_auth.services.yml index 13a6cb1..4c7a449 100644 --- a/core/modules/basic_auth/basic_auth.services.yml +++ b/core/modules/basic_auth/basic_auth.services.yml @@ -1,6 +1,6 @@ services: authentication.basic_auth: class: Drupal\basic_auth\Authentication\Provider\BasicAuth - arguments: ['@config.factory'] + arguments: ['@config.factory', '@user.auth'] tags: - { name: authentication_provider, priority: 100 } diff --git a/core/modules/basic_auth/lib/Drupal/basic_auth/Authentication/Provider/BasicAuth.php b/core/modules/basic_auth/lib/Drupal/basic_auth/Authentication/Provider/BasicAuth.php index 79fe59f..f8f221a 100644 --- a/core/modules/basic_auth/lib/Drupal/basic_auth/Authentication/Provider/BasicAuth.php +++ b/core/modules/basic_auth/lib/Drupal/basic_auth/Authentication/Provider/BasicAuth.php @@ -11,6 +11,7 @@ use Drupal\Core\Authentication\AuthenticationProviderInterface; use Drupal\Core\Config\Config; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\user\UserAuthInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; @@ -29,13 +30,21 @@ class BasicAuth implements AuthenticationProviderInterface { protected $configFactory; /** + * The user auth service. + * + * @var \Drupal\user\UserAuthInterface + */ + protected $userAuth; + + /** * Constructs a HTTP basic authentication provider object. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. */ - public function __construct(ConfigFactoryInterface $config_factory) { + public function __construct(ConfigFactoryInterface $config_factory, UserAuthInterface $user_auth) { $this->configFactory = $config_factory; + $this->userAuth = $user_auth; } /** @@ -53,7 +62,7 @@ public function applies(Request $request) { public function authenticate(Request $request) { $username = $request->headers->get('PHP_AUTH_USER'); $password = $request->headers->get('PHP_AUTH_PW'); - $uid = user_authenticate($username, $password); + $uid = $this->userAuth->authenticate($username, $password); if ($uid) { return user_load($uid); } diff --git a/core/modules/user/lib/Drupal/user/UserAuth.php b/core/modules/user/lib/Drupal/user/UserAuth.php index 01ecf46..3ce2683 100644 --- a/core/modules/user/lib/Drupal/user/UserAuth.php +++ b/core/modules/user/lib/Drupal/user/UserAuth.php @@ -28,17 +28,19 @@ class UserAuth implements UserAuthInterface { * * @var \Drupal\Core\Password\PasswordInterface */ - protected $password; + protected $passwordChecker; /** * Constructs a UserAuth object. * - * @param EntityStorageControllerInterface $storage - * @param PasswordInterface $password + * @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage + * The user storage. + * @param \Drupal\Core\Password\PasswordInterface $password_checker + * The password service. */ - public function __construct(EntityManagerInterface $entity_manager, PasswordInterface $password) { + public function __construct(EntityManagerInterface $entity_manager, PasswordInterface $password_checker) { $this->storage = $entity_manager->getStorageController('user'); - $this->password = $password; + $this->passwordChecker = $password_checker; } /** @@ -51,12 +53,12 @@ public function authenticate($username, $password) { $account_search = $this->storage->loadByProperties(array('name' => $username)); if ($account = reset($account_search)) { - if ($this->password->check($password, $account)) { + if ($this->passwordChecker->check($password, $account)) { // Successful authentication. $uid = $account->id(); // Update user to new password scheme if needed. - if ($this->password->userNeedsNewHash($account)) { + if ($this->passwordChecker->userNeedsNewHash($account)) { $account->setPassword($password); $account->save(); } diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 9b2194b..9d7b032 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -900,26 +900,12 @@ function user_page_title(UserInterface $account = NULL) { * A plain-text password, such as trimmed text from form values. * @return * The user's uid on success, or FALSE on failure to authenticate. + * + * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0. + * Use \Drupal\user\UserAuth::authenticate() instead. */ function user_authenticate($name, $password) { - $uid = FALSE; - if (!empty($name) && !empty($password)) { - $account = user_load_by_name($name); - if ($account) { - $password_hasher = \Drupal::service('password'); - if ($password_hasher->check($password, $account)) { - // Successful authentication. - $uid = $account->id(); - - // Update user to new password scheme if needed. - if ($password_hasher->userNeedsNewHash($account)) { - $account->setPassword($password); - $account->save(); - } - } - } - } - return $uid; + return \Drupal::service('user_auth')->authenticate($name, $password); } /**