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 MediaWiki\Permissions\PermissionManager
;
26 use Psr\Log\LoggerAwareInterface
;
27 use Psr\Log\LoggerInterface
;
28 use MediaWiki\Logger\LoggerFactory
;
31 * Helper class for the password reset functionality shared by the web UI and the API.
33 * Requires the TemporaryPasswordPrimaryAuthenticationProvider and the
34 * EmailNotificationSecondaryAuthenticationProvider (or something providing equivalent
35 * functionality) to be enabled.
37 class PasswordReset
implements LoggerAwareInterface
{
41 /** @var AuthManager */
42 protected $authManager;
44 /** @var PermissionManager */
45 private $permissionManager;
47 /** @var LoggerInterface */
51 * In-process cache for isAllowed lookups, by username.
52 * Contains a StatusValue object
55 private $permissionCache;
57 public function __construct(
59 AuthManager
$authManager,
60 PermissionManager
$permissionManager
62 $this->config
= $config;
63 $this->authManager
= $authManager;
64 $this->permissionManager
= $permissionManager;
65 $this->permissionCache
= new MapCacheLRU( 1 );
66 $this->logger
= LoggerFactory
::getInstance( 'authentication' );
70 * Set the logger instance to use.
72 * @param LoggerInterface $logger
75 public function setLogger( LoggerInterface
$logger ) {
76 $this->logger
= $logger;
80 * Check if a given user has permission to use this functionality.
82 * @since 1.29 Second argument for displayPassword removed.
85 public function isAllowed( User
$user ) {
86 $status = $this->permissionCache
->get( $user->getName() );
88 $resetRoutes = $this->config
->get( 'PasswordResetRoutes' );
89 $status = StatusValue
::newGood();
91 if ( !is_array( $resetRoutes ) ||
!in_array( true, $resetRoutes, true ) ) {
92 // Maybe password resets are disabled, or there are no allowable routes
93 $status = StatusValue
::newFatal( 'passwordreset-disabled' );
95 ( $providerStatus = $this->authManager
->allowsAuthenticationDataChange(
96 new TemporaryPasswordAuthenticationRequest(), false ) )
97 && !$providerStatus->isGood()
99 // Maybe the external auth plugin won't allow local password changes
100 $status = StatusValue
::newFatal( 'resetpass_forbidden-reason',
101 $providerStatus->getMessage() );
102 } elseif ( !$this->config
->get( 'EnableEmail' ) ) {
103 // Maybe email features have been disabled
104 $status = StatusValue
::newFatal( 'passwordreset-emaildisabled' );
105 } elseif ( !$this->permissionManager
->userHasRight( $user, 'editmyprivateinfo' ) ) {
106 // Maybe not all users have permission to change private data
107 $status = StatusValue
::newFatal( 'badaccess' );
108 } elseif ( $this->isBlocked( $user ) ) {
109 // Maybe the user is blocked (check this here rather than relying on the parent
110 // method as we have a more specific error message to use here and we want to
111 // ignore some types of blocks)
112 $status = StatusValue
::newFatal( 'blocked-mailpassword' );
115 $this->permissionCache
->set( $user->getName(), $status );
122 * Do a password reset. Authorization is the caller's responsibility.
124 * Process the form. At this point we know that the user passes all the criteria in
125 * userCanExecute(), and if the data array contains 'Username', etc, then Username
126 * resets are allowed.
128 * @since 1.29 Fourth argument for displayPassword removed.
129 * @param User $performingUser The user that does the password reset
130 * @param string|null $username The user whose password is reset
131 * @param string|null $email Alternative way to specify the user
132 * @return StatusValue Will contain the passwords as a username => password array if the
133 * $displayPassword flag was set
134 * @throws LogicException When the user is not allowed to perform the action
135 * @throws MWException On unexpected DB errors
137 public function execute(
138 User
$performingUser, $username = null, $email = null
140 if ( !$this->isAllowed( $performingUser )->isGood() ) {
141 throw new LogicException( 'User ' . $performingUser->getName()
142 . ' is not allowed to reset passwords' );
145 $resetRoutes = $this->config
->get( 'PasswordResetRoutes' )
146 +
[ 'username' => false, 'email' => false ];
147 if ( $resetRoutes['username'] && $username ) {
148 $method = 'username';
149 $users = [ User
::newFromName( $username ) ];
151 } elseif ( $resetRoutes['email'] && $email ) {
152 if ( !Sanitizer
::validateEmail( $email ) ) {
153 return StatusValue
::newFatal( 'passwordreset-invalidemail' );
156 $users = $this->getUsersByEmail( $email );
159 // The user didn't supply any data
160 return StatusValue
::newFatal( 'passwordreset-nodata' );
163 // Check for hooks (captcha etc), and allow them to modify the users list
166 'Username' => $username,
169 if ( !Hooks
::run( 'SpecialPasswordResetOnSubmit', [ &$users, $data, &$error ] ) ) {
170 return StatusValue
::newFatal( Message
::newFromSpecifier( $error ) );
174 if ( $method === 'email' ) {
175 // Don't reveal whether or not an email address is in use
176 return StatusValue
::newGood( [] );
178 return StatusValue
::newFatal( 'noname' );
182 $firstUser = $users[0];
184 if ( !$firstUser instanceof User ||
!$firstUser->getId() ) {
185 // Don't parse username as wikitext (T67501)
186 return StatusValue
::newFatal( wfMessage( 'nosuchuser', wfEscapeWikiText( $username ) ) );
189 // Check against the rate limiter
190 if ( $performingUser->pingLimiter( 'mailpassword' ) ) {
191 return StatusValue
::newFatal( 'actionthrottledtext' );
194 // All the users will have the same email address
195 if ( !$firstUser->getEmail() ) {
196 // This won't be reachable from the email route, so safe to expose the username
197 return StatusValue
::newFatal( wfMessage( 'noemail',
198 wfEscapeWikiText( $firstUser->getName() ) ) );
201 // We need to have a valid IP address for the hook, but per T20347, we should
202 // send the user's name if they're logged in.
203 $ip = $performingUser->getRequest()->getIP();
205 return StatusValue
::newFatal( 'badipaddress' );
208 Hooks
::run( 'User::mailPasswordInternal', [ &$performingUser, &$ip, &$firstUser ] );
210 $result = StatusValue
::newGood();
212 foreach ( $users as $user ) {
213 $req = TemporaryPasswordAuthenticationRequest
::newRandom();
214 $req->username
= $user->getName();
215 $req->mailpassword
= true;
216 $req->caller
= $performingUser->getName();
217 $status = $this->authManager
->allowsAuthenticationDataChange( $req, true );
218 if ( $status->isGood() && $status->getValue() !== 'ignored' ) {
220 } elseif ( $result->isGood() ) {
221 // only record the first error, to avoid exposing the number of users having the
222 // same email address
223 if ( $status->getValue() === 'ignored' ) {
224 $status = StatusValue
::newFatal( 'passwordreset-ignored' );
226 $result->merge( $status );
231 'requestingIp' => $ip,
232 'requestingUser' => $performingUser->getName(),
233 'targetUsername' => $username,
234 'targetEmail' => $email,
235 'actualUser' => $firstUser->getName(),
238 if ( !$result->isGood() ) {
240 "{requestingUser} attempted password reset of {actualUser} but failed",
241 $logContext +
[ 'errors' => $result->getErrors() ]
247 foreach ( $reqs as $req ) {
248 // This is adding a new temporary password, not intentionally changing anything
249 // (even though it might technically invalidate an old temporary password).
250 $this->authManager
->changeAuthenticationData( $req, /* $isAddition */ true );
254 "{requestingUser} did password reset of {actualUser}",
258 return StatusValue
::newGood( $passwords );
262 * Check whether the user is blocked.
263 * Ignores certain types of system blocks that are only meant to force users to log in.
268 protected function isBlocked( User
$user ) {
269 $block = $user->getBlock() ?
: $user->getGlobalBlock();
273 return $block->appliesToPasswordReset();
277 * @param string $email
279 * @throws MWException On unexpected database errors
281 protected function getUsersByEmail( $email ) {
282 $userQuery = User
::getQueryInfo();
283 $res = wfGetDB( DB_REPLICA
)->select(
284 $userQuery['tables'],
285 $userQuery['fields'],
286 [ 'user_email' => $email ],
293 // Some sort of database error, probably unreachable
294 throw new MWException( 'Unknown database error in ' . __METHOD__
);
298 foreach ( $res as $row ) {
299 $users[] = User
::newFromRow( $row );