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
22 namespace MediaWiki\Auth
;
27 * A primary authentication provider that uses the password field in the 'user' table.
31 class LocalPasswordPrimaryAuthenticationProvider
32 extends AbstractPasswordPrimaryAuthenticationProvider
35 /** @var bool If true, this instance is for legacy logins only. */
36 protected $loginOnly = false;
39 * @param array $params Settings
40 * - loginOnly: If true, the local passwords are for legacy logins only:
41 * the local password will be invalidated when authentication is changed
42 * and new users will not have a valid local password set.
44 public function __construct( $params = [] ) {
45 parent
::__construct( $params );
46 $this->loginOnly
= !empty( $params['loginOnly'] );
49 protected function getPasswordResetData( $username, $row ) {
51 $expiration = wfTimestampOrNull( TS_UNIX
, $row->user_password_expires
);
52 if ( $expiration === null ||
$expiration >= $now ) {
56 $grace = $this->config
->get( 'PasswordExpireGrace' );
57 if ( $expiration +
$grace < $now ) {
60 'msg' => \Status
::newFatal( 'resetpass-expired' )->getMessage(),
65 'msg' => \Status
::newFatal( 'resetpass-expired-soft' )->getMessage(),
72 public function beginPrimaryAuthentication( array $reqs ) {
73 $req = AuthenticationRequest
::getRequestByClass( $reqs, PasswordAuthenticationRequest
::class );
75 return AuthenticationResponse
::newAbstain();
78 if ( $req->username
=== null ||
$req->password
=== null ) {
79 return AuthenticationResponse
::newAbstain();
82 $username = User
::getCanonicalName( $req->username
, 'usable' );
83 if ( $username === false ) {
84 return AuthenticationResponse
::newAbstain();
88 'user_id', 'user_password', 'user_password_expires',
91 $dbw = wfGetDB( DB_MASTER
);
92 $row = $dbw->selectRow(
95 [ 'user_name' => $username ],
99 return AuthenticationResponse
::newAbstain();
102 // Check for *really* old password hashes that don't even have a type
103 // The old hash format was just an md5 hex hash, with no type information
104 if ( preg_match( '/^[0-9a-f]{32}$/', $row->user_password
) ) {
105 if ( $this->config
->get( 'PasswordSalt' ) ) {
106 $row->user_password
= ":A:{$row->user_id}:{$row->user_password}";
108 $row->user_password
= ":A:{$row->user_password}";
112 $status = $this->checkPasswordValidity( $username, $req->password
);
113 if ( !$status->isOk() ) {
114 // Fatal, can't log in
115 return AuthenticationResponse
::newFail( $status->getMessage() );
118 $pwhash = $this->getPassword( $row->user_password
);
119 if ( !$pwhash->equals( $req->password
) ) {
120 if ( $this->config
->get( 'LegacyEncoding' ) ) {
121 // Some wikis were converted from ISO 8859-1 to UTF-8, the passwords can't be converted
122 // Check for this with iconv
123 $cp1252Password = iconv( 'UTF-8', 'WINDOWS-1252//TRANSLIT', $req->password
);
124 if ( $cp1252Password === $req->password ||
!$pwhash->equals( $cp1252Password ) ) {
125 return $this->failResponse( $req );
128 return $this->failResponse( $req );
132 // @codeCoverageIgnoreStart
133 if ( $this->getPasswordFactory()->needsUpdate( $pwhash ) ) {
134 $pwhash = $this->getPasswordFactory()->newFromPlaintext( $req->password
);
137 [ 'user_password' => $pwhash->toString() ],
138 [ 'user_id' => $row->user_id
],
142 // @codeCoverageIgnoreEnd
144 $this->setPasswordResetFlag( $username, $status, $row );
146 return AuthenticationResponse
::newPass( $username );
149 public function testUserCanAuthenticate( $username ) {
150 $username = User
::getCanonicalName( $username, 'usable' );
151 if ( $username === false ) {
155 $dbw = wfGetDB( DB_MASTER
);
156 $row = $dbw->selectRow(
159 [ 'user_name' => $username ],
166 // Check for *really* old password hashes that don't even have a type
167 // The old hash format was just an md5 hex hash, with no type information
168 if ( preg_match( '/^[0-9a-f]{32}$/', $row->user_password
) ) {
172 return !$this->getPassword( $row->user_password
) instanceof \InvalidPassword
;
175 public function testUserExists( $username, $flags = User
::READ_NORMAL
) {
176 $username = User
::getCanonicalName( $username, 'usable' );
177 if ( $username === false ) {
181 list( $db, $options ) = \DBAccessObjectUtils
::getDBOptions( $flags );
182 return (bool)wfGetDB( $db )->selectField(
185 [ 'user_name' => $username ],
191 public function providerAllowsAuthenticationDataChange(
192 AuthenticationRequest
$req, $checkData = true
194 // We only want to blank the password if something else will accept the
195 // new authentication data, so return 'ignore' here.
196 if ( $this->loginOnly
) {
197 return \StatusValue
::newGood( 'ignored' );
200 if ( get_class( $req ) === PasswordAuthenticationRequest
::class ) {
202 return \StatusValue
::newGood();
205 $username = User
::getCanonicalName( $req->username
, 'usable' );
206 if ( $username !== false ) {
207 $row = wfGetDB( DB_MASTER
)->selectRow(
210 [ 'user_name' => $username ],
214 $sv = \StatusValue
::newGood();
215 if ( $req->password
!== null ) {
216 if ( $req->password
!== $req->retype
) {
217 $sv->fatal( 'badretype' );
219 $sv->merge( $this->checkPasswordValidity( $username, $req->password
) );
227 return \StatusValue
::newGood( 'ignored' );
230 public function providerChangeAuthenticationData( AuthenticationRequest
$req ) {
231 $username = $req->username
!== null ? User
::getCanonicalName( $req->username
, 'usable' ) : false;
232 if ( $username === false ) {
238 if ( $this->loginOnly
) {
239 $pwhash = $this->getPasswordFactory()->newFromCiphertext( null );
241 // @codeCoverageIgnoreStart
242 } elseif ( get_class( $req ) === PasswordAuthenticationRequest
::class ) {
243 // @codeCoverageIgnoreEnd
244 $pwhash = $this->getPasswordFactory()->newFromPlaintext( $req->password
);
245 $expiry = $this->getNewPasswordExpiry( $username );
249 $dbw = wfGetDB( DB_MASTER
);
253 'user_password' => $pwhash->toString(),
254 'user_password_expires' => $dbw->timestampOrNull( $expiry ),
256 [ 'user_name' => $username ],
262 public function accountCreationType() {
263 return $this->loginOnly ? self
::TYPE_NONE
: self
::TYPE_CREATE
;
266 public function testForAccountCreation( $user, $creator, array $reqs ) {
267 $req = AuthenticationRequest
::getRequestByClass( $reqs, PasswordAuthenticationRequest
::class );
269 $ret = \StatusValue
::newGood();
270 if ( !$this->loginOnly
&& $req && $req->username
!== null && $req->password
!== null ) {
271 if ( $req->password
!== $req->retype
) {
272 $ret->fatal( 'badretype' );
275 $this->checkPasswordValidity( $user->getName(), $req->password
)
282 public function beginPrimaryAccountCreation( $user, $creator, array $reqs ) {
283 if ( $this->accountCreationType() === self
::TYPE_NONE
) {
284 throw new \
BadMethodCallException( 'Shouldn\'t call this when accountCreationType() is NONE' );
287 $req = AuthenticationRequest
::getRequestByClass( $reqs, PasswordAuthenticationRequest
::class );
289 if ( $req->username
!== null && $req->password
!== null ) {
290 // Nothing we can do besides claim it, because the user isn't in
292 if ( $req->username
!== $user->getName() ) {
293 $req = clone( $req );
294 $req->username
= $user->getName();
296 $ret = AuthenticationResponse
::newPass( $req->username
);
297 $ret->createRequest
= $req;
301 return AuthenticationResponse
::newAbstain();
304 public function finishAccountCreation( $user, $creator, AuthenticationResponse
$res ) {
305 if ( $this->accountCreationType() === self
::TYPE_NONE
) {
306 throw new \
BadMethodCallException( 'Shouldn\'t call this when accountCreationType() is NONE' );
309 // Now that the user is in the DB, set the password on it.
310 $this->providerChangeAuthenticationData( $res->createRequest
);