PasswordReset.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /**
  3. * User password reset helper for MediaWiki.
  4. *
  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.
  9. *
  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.
  14. *
  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
  19. *
  20. * @file
  21. */
  22. use MediaWiki\Auth\AuthManager;
  23. use MediaWiki\Auth\TemporaryPasswordAuthenticationRequest;
  24. use MediaWiki\Config\ServiceOptions;
  25. use MediaWiki\Logger\LoggerFactory;
  26. use MediaWiki\MediaWikiServices;
  27. use MediaWiki\Permissions\PermissionManager;
  28. use Psr\Log\LoggerAwareInterface;
  29. use Psr\Log\LoggerAwareTrait;
  30. use Psr\Log\LoggerInterface;
  31. use Wikimedia\Rdbms\ILoadBalancer;
  32. /**
  33. * Helper class for the password reset functionality shared by the web UI and the API.
  34. *
  35. * Requires the TemporaryPasswordPrimaryAuthenticationProvider and the
  36. * EmailNotificationSecondaryAuthenticationProvider (or something providing equivalent
  37. * functionality) to be enabled.
  38. */
  39. class PasswordReset implements LoggerAwareInterface {
  40. use LoggerAwareTrait;
  41. /** @var ServiceOptions|Config */
  42. protected $config;
  43. /** @var AuthManager */
  44. protected $authManager;
  45. /** @var PermissionManager */
  46. protected $permissionManager;
  47. /** @var ILoadBalancer */
  48. protected $loadBalancer;
  49. /**
  50. * In-process cache for isAllowed lookups, by username.
  51. * Contains a StatusValue object
  52. * @var MapCacheLRU
  53. */
  54. private $permissionCache;
  55. public const CONSTRUCTOR_OPTIONS = [
  56. 'AllowRequiringEmailForResets',
  57. 'EnableEmail',
  58. 'PasswordResetRoutes',
  59. ];
  60. /**
  61. * This class is managed by MediaWikiServices, don't instantiate directly.
  62. *
  63. * @param ServiceOptions|Config $config
  64. * @param AuthManager $authManager
  65. * @param PermissionManager $permissionManager
  66. * @param ILoadBalancer|null $loadBalancer
  67. * @param LoggerInterface|null $logger
  68. */
  69. public function __construct(
  70. $config,
  71. AuthManager $authManager,
  72. PermissionManager $permissionManager,
  73. ILoadBalancer $loadBalancer = null,
  74. LoggerInterface $logger = null
  75. ) {
  76. $this->config = $config;
  77. $this->authManager = $authManager;
  78. $this->permissionManager = $permissionManager;
  79. if ( !$loadBalancer ) {
  80. wfDeprecated( 'Not passing LoadBalancer to ' . __METHOD__, '1.34' );
  81. $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
  82. }
  83. $this->loadBalancer = $loadBalancer;
  84. if ( !$logger ) {
  85. wfDeprecated( 'Not passing LoggerInterface to ' . __METHOD__, '1.34' );
  86. $logger = LoggerFactory::getInstance( 'authentication' );
  87. }
  88. $this->logger = $logger;
  89. $this->permissionCache = new MapCacheLRU( 1 );
  90. }
  91. /**
  92. * Check if a given user has permission to use this functionality.
  93. * @param User $user
  94. * @since 1.29 Second argument for displayPassword removed.
  95. * @return StatusValue
  96. */
  97. public function isAllowed( User $user ) {
  98. $status = $this->permissionCache->get( $user->getName() );
  99. if ( !$status ) {
  100. $resetRoutes = $this->config->get( 'PasswordResetRoutes' );
  101. $status = StatusValue::newGood();
  102. if ( !is_array( $resetRoutes ) || !in_array( true, $resetRoutes, true ) ) {
  103. // Maybe password resets are disabled, or there are no allowable routes
  104. $status = StatusValue::newFatal( 'passwordreset-disabled' );
  105. } elseif (
  106. ( $providerStatus = $this->authManager->allowsAuthenticationDataChange(
  107. new TemporaryPasswordAuthenticationRequest(), false ) )
  108. && !$providerStatus->isGood()
  109. ) {
  110. // Maybe the external auth plugin won't allow local password changes
  111. $status = StatusValue::newFatal( 'resetpass_forbidden-reason',
  112. $providerStatus->getMessage() );
  113. } elseif ( !$this->config->get( 'EnableEmail' ) ) {
  114. // Maybe email features have been disabled
  115. $status = StatusValue::newFatal( 'passwordreset-emaildisabled' );
  116. } elseif ( !$this->permissionManager->userHasRight( $user, 'editmyprivateinfo' ) ) {
  117. // Maybe not all users have permission to change private data
  118. $status = StatusValue::newFatal( 'badaccess' );
  119. } elseif ( $this->isBlocked( $user ) ) {
  120. // Maybe the user is blocked (check this here rather than relying on the parent
  121. // method as we have a more specific error message to use here and we want to
  122. // ignore some types of blocks)
  123. $status = StatusValue::newFatal( 'blocked-mailpassword' );
  124. }
  125. $this->permissionCache->set( $user->getName(), $status );
  126. }
  127. return $status;
  128. }
  129. /**
  130. * Do a password reset. Authorization is the caller's responsibility.
  131. *
  132. * Process the form. At this point we know that the user passes all the criteria in
  133. * userCanExecute(), and if the data array contains 'Username', etc, then Username
  134. * resets are allowed.
  135. *
  136. * @since 1.29 Fourth argument for displayPassword removed.
  137. * @param User $performingUser The user that does the password reset
  138. * @param string|null $username The user whose password is reset
  139. * @param string|null $email Alternative way to specify the user
  140. * @return StatusValue Will contain the passwords as a username => password array if the
  141. * $displayPassword flag was set
  142. * @throws LogicException When the user is not allowed to perform the action
  143. * @throws MWException On unexpected DB errors
  144. */
  145. public function execute(
  146. User $performingUser, $username = null, $email = null
  147. ) {
  148. if ( !$this->isAllowed( $performingUser )->isGood() ) {
  149. throw new LogicException( 'User ' . $performingUser->getName()
  150. . ' is not allowed to reset passwords' );
  151. }
  152. $username = $username ?? '';
  153. $email = $email ?? '';
  154. $resetRoutes = $this->config->get( 'PasswordResetRoutes' )
  155. + [ 'username' => false, 'email' => false ];
  156. if ( $resetRoutes['username'] && $username ) {
  157. $method = 'username';
  158. $users = [ $this->lookupUser( $username ) ];
  159. } elseif ( $resetRoutes['email'] && $email ) {
  160. if ( !Sanitizer::validateEmail( $email ) ) {
  161. return StatusValue::newFatal( 'passwordreset-invalidemail' );
  162. }
  163. $method = 'email';
  164. $users = $this->getUsersByEmail( $email );
  165. $username = null;
  166. } else {
  167. // The user didn't supply any data
  168. return StatusValue::newFatal( 'passwordreset-nodata' );
  169. }
  170. // Check for hooks (captcha etc), and allow them to modify the users list
  171. $error = [];
  172. $data = [
  173. 'Username' => $username,
  174. // Email gets set to null for backward compatibility
  175. 'Email' => $method === 'email' ? $email : null,
  176. ];
  177. if ( !Hooks::run( 'SpecialPasswordResetOnSubmit', [ &$users, $data, &$error ] ) ) {
  178. return StatusValue::newFatal( Message::newFromSpecifier( $error ) );
  179. }
  180. $firstUser = $users[0] ?? null;
  181. $requireEmail = $this->config->get( 'AllowRequiringEmailForResets' )
  182. && $method === 'username'
  183. && $firstUser
  184. && $firstUser->getBoolOption( 'requireemail' );
  185. if ( $requireEmail ) {
  186. if ( $email === '' ) {
  187. return StatusValue::newFatal( 'passwordreset-username-email-required' );
  188. }
  189. if ( !Sanitizer::validateEmail( $email ) ) {
  190. return StatusValue::newFatal( 'passwordreset-invalidemail' );
  191. }
  192. }
  193. // Check against the rate limiter
  194. if ( $performingUser->pingLimiter( 'mailpassword' ) ) {
  195. return StatusValue::newFatal( 'actionthrottledtext' );
  196. }
  197. if ( !$users ) {
  198. if ( $method === 'email' ) {
  199. // Don't reveal whether or not an email address is in use
  200. return StatusValue::newGood( [] );
  201. } else {
  202. return StatusValue::newFatal( 'noname' );
  203. }
  204. }
  205. if ( !$firstUser instanceof User || !$firstUser->getId() ) {
  206. // Don't parse username as wikitext (T67501)
  207. return StatusValue::newFatal( wfMessage( 'nosuchuser', wfEscapeWikiText( $username ) ) );
  208. }
  209. // All the users will have the same email address
  210. if ( !$firstUser->getEmail() ) {
  211. // This won't be reachable from the email route, so safe to expose the username
  212. return StatusValue::newFatal( wfMessage( 'noemail',
  213. wfEscapeWikiText( $firstUser->getName() ) ) );
  214. }
  215. if ( $requireEmail && $firstUser->getEmail() !== $email ) {
  216. // Pretend everything's fine to avoid disclosure
  217. return StatusValue::newGood();
  218. }
  219. // We need to have a valid IP address for the hook, but per T20347, we should
  220. // send the user's name if they're logged in.
  221. $ip = $performingUser->getRequest()->getIP();
  222. if ( !$ip ) {
  223. return StatusValue::newFatal( 'badipaddress' );
  224. }
  225. Hooks::run( 'User::mailPasswordInternal', [ &$performingUser, &$ip, &$firstUser ] );
  226. $result = StatusValue::newGood();
  227. $reqs = [];
  228. foreach ( $users as $user ) {
  229. $req = TemporaryPasswordAuthenticationRequest::newRandom();
  230. $req->username = $user->getName();
  231. $req->mailpassword = true;
  232. $req->caller = $performingUser->getName();
  233. $status = $this->authManager->allowsAuthenticationDataChange( $req, true );
  234. if ( $status->isGood() && $status->getValue() !== 'ignored' ) {
  235. $reqs[] = $req;
  236. } elseif ( $result->isGood() ) {
  237. // only record the first error, to avoid exposing the number of users having the
  238. // same email address
  239. if ( $status->getValue() === 'ignored' ) {
  240. $status = StatusValue::newFatal( 'passwordreset-ignored' );
  241. }
  242. $result->merge( $status );
  243. }
  244. }
  245. $logContext = [
  246. 'requestingIp' => $ip,
  247. 'requestingUser' => $performingUser->getName(),
  248. 'targetUsername' => $username,
  249. 'targetEmail' => $email,
  250. 'actualUser' => $firstUser->getName(),
  251. ];
  252. if ( !$result->isGood() ) {
  253. $this->logger->info(
  254. "{requestingUser} attempted password reset of {actualUser} but failed",
  255. $logContext + [ 'errors' => $result->getErrors() ]
  256. );
  257. return $result;
  258. }
  259. $passwords = [];
  260. foreach ( $reqs as $req ) {
  261. // This is adding a new temporary password, not intentionally changing anything
  262. // (even though it might technically invalidate an old temporary password).
  263. $this->authManager->changeAuthenticationData( $req, /* $isAddition */ true );
  264. }
  265. $this->logger->info(
  266. "{requestingUser} did password reset of {actualUser}",
  267. $logContext
  268. );
  269. return StatusValue::newGood( $passwords );
  270. }
  271. /**
  272. * Check whether the user is blocked.
  273. * Ignores certain types of system blocks that are only meant to force users to log in.
  274. * @param User $user
  275. * @return bool
  276. * @since 1.30
  277. */
  278. protected function isBlocked( User $user ) {
  279. $block = $user->getBlock() ?: $user->getGlobalBlock();
  280. if ( !$block ) {
  281. return false;
  282. }
  283. return $block->appliesToPasswordReset();
  284. }
  285. /**
  286. * @param string $email
  287. * @return User[]
  288. * @throws MWException On unexpected database errors
  289. */
  290. protected function getUsersByEmail( $email ) {
  291. $userQuery = User::getQueryInfo();
  292. $res = $this->loadBalancer->getConnectionRef( DB_REPLICA )->select(
  293. $userQuery['tables'],
  294. $userQuery['fields'],
  295. [ 'user_email' => $email ],
  296. __METHOD__,
  297. [],
  298. $userQuery['joins']
  299. );
  300. if ( !$res ) {
  301. // Some sort of database error, probably unreachable
  302. throw new MWException( 'Unknown database error in ' . __METHOD__ );
  303. }
  304. $users = [];
  305. foreach ( $res as $row ) {
  306. $users[] = User::newFromRow( $row );
  307. }
  308. return $users;
  309. }
  310. /**
  311. * User object creation helper for testability
  312. * @codeCoverageIgnore
  313. *
  314. * @param string $username
  315. * @return User|false
  316. */
  317. protected function lookupUser( $username ) {
  318. return User::newFromName( $username );
  319. }
  320. }