Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
30 / 30 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| TemporaryPasswordPrimaryAuthenticationProvider | |
100.00% |
30 / 30 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| testUserExists | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| getTemporaryPassword | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| setTemporaryPassword | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Auth; |
| 8 | |
| 9 | use MediaWiki\Password\Password; |
| 10 | use MediaWiki\User\UserRigorOptions; |
| 11 | use Wikimedia\Rdbms\DBAccessObjectUtils; |
| 12 | use Wikimedia\Rdbms\IDBAccessObject; |
| 13 | |
| 14 | /** |
| 15 | * A primary authentication provider that uses the temporary password field in |
| 16 | * the 'user' table. |
| 17 | * |
| 18 | * A successful login will force a password reset. |
| 19 | * |
| 20 | * @note For proper operation, this should generally come before any other |
| 21 | * password-based authentication providers. |
| 22 | * |
| 23 | * @ingroup Auth |
| 24 | * @since 1.27 |
| 25 | */ |
| 26 | class TemporaryPasswordPrimaryAuthenticationProvider |
| 27 | extends AbstractTemporaryPasswordPrimaryAuthenticationProvider |
| 28 | { |
| 29 | |
| 30 | /** @inheritDoc */ |
| 31 | public function testUserExists( $username, $flags = IDBAccessObject::READ_NORMAL ) { |
| 32 | $username = $this->userNameUtils->getCanonical( $username, UserRigorOptions::RIGOR_USABLE ); |
| 33 | if ( $username === false ) { |
| 34 | return false; |
| 35 | } |
| 36 | $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags ); |
| 37 | return (bool)$db->newSelectQueryBuilder() |
| 38 | ->select( [ 'user_id' ] ) |
| 39 | ->from( 'user' ) |
| 40 | ->where( [ 'user_name' => $username ] ) |
| 41 | ->caller( __METHOD__ )->fetchField(); |
| 42 | } |
| 43 | |
| 44 | /** @inheritDoc */ |
| 45 | protected function getTemporaryPassword( string $username, $flags = IDBAccessObject::READ_NORMAL ): array { |
| 46 | $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags ); |
| 47 | $row = $db->newSelectQueryBuilder() |
| 48 | ->select( [ 'user_newpassword', 'user_newpass_time' ] ) |
| 49 | ->from( 'user' ) |
| 50 | ->where( [ 'user_name' => $username ] ) |
| 51 | ->caller( __METHOD__ )->fetchRow(); |
| 52 | |
| 53 | if ( !$row ) { |
| 54 | return [ null, null ]; |
| 55 | } |
| 56 | return [ |
| 57 | $this->getPassword( $row->user_newpassword ), |
| 58 | $row->user_newpass_time, |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | /** @inheritDoc */ |
| 63 | protected function setTemporaryPassword( string $username, Password $tempPassHash, $tempPassTime ): void { |
| 64 | $db = $this->dbProvider->getPrimaryDatabase(); |
| 65 | $db->newUpdateQueryBuilder() |
| 66 | ->update( 'user' ) |
| 67 | ->set( [ |
| 68 | 'user_newpassword' => $tempPassHash->toString(), |
| 69 | 'user_newpass_time' => $db->timestampOrNull( $tempPassTime ), |
| 70 | ] ) |
| 71 | ->where( [ 'user_name' => $username ] ) |
| 72 | ->caller( __METHOD__ )->execute(); |
| 73 | } |
| 74 | |
| 75 | } |