Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / auth / AbstractPasswordPrimaryAuthenticationProvider.php
blobf5bfc2a20b6227ae56eaf545629d81b3ef0d6218
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @ingroup Auth
22 namespace MediaWiki\Auth;
24 use Password;
25 use PasswordFactory;
26 use Status;
28 /**
29 * Basic framework for a primary authentication provider that uses passwords
30 * @ingroup Auth
31 * @since 1.27
33 abstract class AbstractPasswordPrimaryAuthenticationProvider
34 extends AbstractPrimaryAuthenticationProvider
36 /** @var bool Whether this provider should ABSTAIN (false) or FAIL (true) on password failure */
37 protected $authoritative;
39 private $passwordFactory = null;
41 /**
42 * @param array $params Settings
43 * - authoritative: Whether this provider should ABSTAIN (false) or FAIL
44 * (true) on password failure
46 public function __construct( array $params = [] ) {
47 $this->authoritative = !isset( $params['authoritative'] ) || (bool)$params['authoritative'];
50 /**
51 * Get the PasswordFactory
52 * @return PasswordFactory
54 protected function getPasswordFactory() {
55 if ( $this->passwordFactory === null ) {
56 $this->passwordFactory = new PasswordFactory();
57 $this->passwordFactory->init( $this->config );
59 return $this->passwordFactory;
62 /**
63 * Get a Password object from the hash
64 * @param string $hash
65 * @return Password
67 protected function getPassword( $hash ) {
68 $passwordFactory = $this->getPasswordFactory();
69 try {
70 return $passwordFactory->newFromCiphertext( $hash );
71 } catch ( \PasswordError $e ) {
72 $class = static::class;
73 $this->logger->debug( "Invalid password hash in {$class}::getPassword()" );
74 return $passwordFactory->newFromCiphertext( null );
78 /**
79 * Return the appropriate response for failure
80 * @param PasswordAuthenticationRequest $req
81 * @return AuthenticationResponse
83 protected function failResponse( PasswordAuthenticationRequest $req ) {
84 if ( $this->authoritative ) {
85 return AuthenticationResponse::newFail(
86 wfMessage( $req->password === '' ? 'wrongpasswordempty' : 'wrongpassword' )
88 } else {
89 return AuthenticationResponse::newAbstain();
93 /**
94 * Check that the password is valid
96 * This should be called *before* validating the password. If the result is
97 * not ok, login should fail immediately.
99 * @param string $username
100 * @param string $password
101 * @return Status
103 protected function checkPasswordValidity( $username, $password ) {
104 return \User::newFromName( $username )->checkPasswordValidity( $password );
108 * Check if the password should be reset
110 * This should be called after a successful login. It sets 'reset-pass'
111 * authentication data if necessary, see
112 * ResetPassSecondaryAuthenticationProvider.
114 * @param string $username
115 * @param Status $status From $this->checkPasswordValidity()
116 * @param mixed $data Passed through to $this->getPasswordResetData()
118 protected function setPasswordResetFlag( $username, Status $status, $data = null ) {
119 $reset = $this->getPasswordResetData( $username, $data );
121 if ( !$reset && $this->config->get( 'InvalidPasswordReset' ) && !$status->isGood() ) {
122 $reset = (object)[
123 'msg' => $status->getMessage( 'resetpass-validity-soft' ),
124 'hard' => false,
128 if ( $reset ) {
129 $this->manager->setAuthenticationSessionData( 'reset-pass', $reset );
134 * Get password reset data, if any
136 * @param string $username
137 * @param mixed $data
138 * @return object|null { 'hard' => bool, 'msg' => Message }
140 protected function getPasswordResetData( $username, $data ) {
141 return null;
145 * Get expiration date for a new password, if any
147 * @param string $username
148 * @return string|null
150 protected function getNewPasswordExpiry( $username ) {
151 $days = $this->config->get( 'PasswordExpirationDays' );
152 $expires = $days ? wfTimestamp( TS_MW, time() + $days * 86400 ) : null;
154 // Give extensions a chance to force an expiration
155 \Hooks::run( 'ResetPasswordExpiration', [ \User::newFromName( $username ), &$expires ] );
157 return $expires;
160 public function getAuthenticationRequests( $action, array $options ) {
161 switch ( $action ) {
162 case AuthManager::ACTION_LOGIN:
163 case AuthManager::ACTION_REMOVE:
164 case AuthManager::ACTION_CREATE:
165 case AuthManager::ACTION_CHANGE:
166 return [ new PasswordAuthenticationRequest() ];
167 default:
168 return [];