3 * User password reset helper for MediaWiki.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\Auth\AuthManager
;
24 use MediaWiki\Auth\TemporaryPasswordAuthenticationRequest
;
25 use Psr\Log\LoggerAwareInterface
;
26 use Psr\Log\LoggerInterface
;
27 use MediaWiki\Logger\LoggerFactory
;
30 * Helper class for the password reset functionality shared by the web UI and the API.
32 * Requires the TemporaryPasswordPrimaryAuthenticationProvider and the
33 * EmailNotificationSecondaryAuthenticationProvider (or something providing equivalent
34 * functionality) to be enabled.
36 class PasswordReset
implements LoggerAwareInterface
{
40 /** @var AuthManager */
41 protected $authManager;
43 /** @var LoggerInterface */
47 * In-process cache for isAllowed lookups, by username.
48 * Contains a StatusValue object
51 private $permissionCache;
53 public function __construct( Config
$config, AuthManager
$authManager ) {
54 $this->config
= $config;
55 $this->authManager
= $authManager;
56 $this->permissionCache
= new HashBagOStuff( [ 'maxKeys' => 1 ] );
57 $this->logger
= LoggerFactory
::getInstance( 'authentication' );
61 * Set the logger instance to use.
63 * @param LoggerInterface $logger
66 public function setLogger( LoggerInterface
$logger ) {
67 $this->logger
= $logger;
71 * Check if a given user has permission to use this functionality.
73 * @param bool $displayPassword If set, also check whether the user is allowed to reset the
74 * password of another user and see the temporary password.
75 * @since 1.29 Second argument for displayPassword removed.
78 public function isAllowed( User
$user ) {
79 $status = $this->permissionCache
->get( $user->getName() );
81 $resetRoutes = $this->config
->get( 'PasswordResetRoutes' );
82 $status = StatusValue
::newGood();
84 if ( !is_array( $resetRoutes ) ||
85 !in_array( true, array_values( $resetRoutes ), true )
87 // Maybe password resets are disabled, or there are no allowable routes
88 $status = StatusValue
::newFatal( 'passwordreset-disabled' );
90 ( $providerStatus = $this->authManager
->allowsAuthenticationDataChange(
91 new TemporaryPasswordAuthenticationRequest(), false ) )
92 && !$providerStatus->isGood()
94 // Maybe the external auth plugin won't allow local password changes
95 $status = StatusValue
::newFatal( 'resetpass_forbidden-reason',
96 $providerStatus->getMessage() );
97 } elseif ( !$this->config
->get( 'EnableEmail' ) ) {
98 // Maybe email features have been disabled
99 $status = StatusValue
::newFatal( 'passwordreset-emaildisabled' );
100 } elseif ( !$user->isAllowed( 'editmyprivateinfo' ) ) {
101 // Maybe not all users have permission to change private data
102 $status = StatusValue
::newFatal( 'badaccess' );
103 } elseif ( $this->isBlocked( $user ) ) {
104 // Maybe the user is blocked (check this here rather than relying on the parent
105 // method as we have a more specific error message to use here and we want to
106 // ignore some types of blocks)
107 $status = StatusValue
::newFatal( 'blocked-mailpassword' );
110 $this->permissionCache
->set( $user->getName(), $status );
117 * Do a password reset. Authorization is the caller's responsibility.
119 * Process the form. At this point we know that the user passes all the criteria in
120 * userCanExecute(), and if the data array contains 'Username', etc, then Username
121 * resets are allowed.
123 * @since 1.29 Fourth argument for displayPassword removed.
124 * @param User $performingUser The user that does the password reset
125 * @param string $username The user whose password is reset
126 * @param string $email Alternative way to specify the user
127 * @return StatusValue Will contain the passwords as a username => password array if the
128 * $displayPassword flag was set
129 * @throws LogicException When the user is not allowed to perform the action
130 * @throws MWException On unexpected DB errors
132 public function execute(
133 User
$performingUser, $username = null, $email = null
135 if ( !$this->isAllowed( $performingUser )->isGood() ) {
136 throw new LogicException( 'User ' . $performingUser->getName()
137 . ' is not allowed to reset passwords' );
140 $resetRoutes = $this->config
->get( 'PasswordResetRoutes' )
141 +
[ 'username' => false, 'email' => false ];
142 if ( $resetRoutes['username'] && $username ) {
143 $method = 'username';
144 $users = [ User
::newFromName( $username ) ];
146 } elseif ( $resetRoutes['email'] && $email ) {
147 if ( !Sanitizer
::validateEmail( $email ) ) {
148 return StatusValue
::newFatal( 'passwordreset-invalidemail' );
151 $users = $this->getUsersByEmail( $email );
154 // The user didn't supply any data
155 return StatusValue
::newFatal( 'passwordreset-nodata' );
158 // Check for hooks (captcha etc), and allow them to modify the users list
161 'Username' => $username,
164 if ( !Hooks
::run( 'SpecialPasswordResetOnSubmit', [ &$users, $data, &$error ] ) ) {
165 return StatusValue
::newFatal( Message
::newFromSpecifier( $error ) );
169 if ( $method === 'email' ) {
170 // Don't reveal whether or not an email address is in use
171 return StatusValue
::newGood( [] );
173 return StatusValue
::newFatal( 'noname' );
177 $firstUser = $users[0];
179 if ( !$firstUser instanceof User ||
!$firstUser->getId() ) {
180 // Don't parse username as wikitext (T67501)
181 return StatusValue
::newFatal( wfMessage( 'nosuchuser', wfEscapeWikiText( $username ) ) );
184 // Check against the rate limiter
185 if ( $performingUser->pingLimiter( 'mailpassword' ) ) {
186 return StatusValue
::newFatal( 'actionthrottledtext' );
189 // All the users will have the same email address
190 if ( !$firstUser->getEmail() ) {
191 // This won't be reachable from the email route, so safe to expose the username
192 return StatusValue
::newFatal( wfMessage( 'noemail',
193 wfEscapeWikiText( $firstUser->getName() ) ) );
196 // We need to have a valid IP address for the hook, but per T20347, we should
197 // send the user's name if they're logged in.
198 $ip = $performingUser->getRequest()->getIP();
200 return StatusValue
::newFatal( 'badipaddress' );
203 Hooks
::run( 'User::mailPasswordInternal', [ &$performingUser, &$ip, &$firstUser ] );
205 $result = StatusValue
::newGood();
207 foreach ( $users as $user ) {
208 $req = TemporaryPasswordAuthenticationRequest
::newRandom();
209 $req->username
= $user->getName();
210 $req->mailpassword
= true;
211 $req->caller
= $performingUser->getName();
212 $status = $this->authManager
->allowsAuthenticationDataChange( $req, true );
213 if ( $status->isGood() && $status->getValue() !== 'ignored' ) {
215 } elseif ( $result->isGood() ) {
216 // only record the first error, to avoid exposing the number of users having the
217 // same email address
218 if ( $status->getValue() === 'ignored' ) {
219 $status = StatusValue
::newFatal( 'passwordreset-ignored' );
221 $result->merge( $status );
226 'requestingIp' => $ip,
227 'requestingUser' => $performingUser->getName(),
228 'targetUsername' => $username,
229 'targetEmail' => $email,
230 'actualUser' => $firstUser->getName(),
233 if ( !$result->isGood() ) {
235 "{requestingUser} attempted password reset of {actualUser} but failed",
236 $logContext +
[ 'errors' => $result->getErrors() ]
242 foreach ( $reqs as $req ) {
243 $this->authManager
->changeAuthenticationData( $req );
247 "{requestingUser} did password reset of {actualUser}",
251 return StatusValue
::newGood( $passwords );
255 * Check whether the user is blocked.
256 * Ignores certain types of system blocks that are only meant to force users to log in.
261 protected function isBlocked( User
$user ) {
262 $block = $user->getBlock() ?
: $user->getGlobalBlock();
266 $type = $block->getSystemBlockType();
267 if ( in_array( $type, [ null, 'global-block' ], true ) ) {
268 // Normal block. Maybe it was meant for someone else and the user just needs to log in;
269 // or maybe it was issued specifically to prevent some IP from messing with password
270 // reset? Go out on a limb and use the registration allowed flag to decide.
271 return $block->prevents( 'createaccount' );
272 } elseif ( $type === 'proxy' ) {
273 // we disallow actions through proxy even if the user is logged in
274 // so it makes sense to disallow password resets as well
276 } elseif ( in_array( $type, [ 'dnsbl', 'wgSoftBlockRanges' ], true ) ) {
277 // these are just meant to force login so let's not prevent that
280 // some extension - we'll have to guess
286 * @param string $email
288 * @throws MWException On unexpected database errors
290 protected function getUsersByEmail( $email ) {
291 $res = wfGetDB( DB_REPLICA
)->select(
293 User
::selectFields(),
294 [ 'user_email' => $email ],
299 // Some sort of database error, probably unreachable
300 throw new MWException( 'Unknown database error in ' . __METHOD__
);
304 foreach ( $res as $row ) {
305 $users[] = User
::newFromRow( $row );