| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\User; |
| 24 |
|
| 25 |
use Iterator; |
| 26 |
use LogicException; |
| 27 |
use MapCacheLRU; |
| 28 |
use MediaWiki\Auth\AuthManager; |
| 29 |
use MediaWiki\Auth\TemporaryPasswordAuthenticationRequest; |
| 30 |
use MediaWiki\Config\ServiceOptions; |
| 31 |
use MediaWiki\Deferred\DeferredUpdates; |
| 32 |
use MediaWiki\Deferred\SendPasswordResetEmailUpdate; |
| 33 |
use MediaWiki\HookContainer\HookContainer; |
| 34 |
use MediaWiki\HookContainer\HookRunner; |
| 35 |
use MediaWiki\MainConfigNames; |
| 36 |
use MediaWiki\Message\Message; |
| 37 |
use MediaWiki\Parser\Sanitizer; |
| 38 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 39 |
use Psr\Log\LoggerAwareInterface; |
| 40 |
use Psr\Log\LoggerAwareTrait; |
| 41 |
use Psr\Log\LoggerInterface; |
| 42 |
use StatusValue; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
class PasswordReset implements LoggerAwareInterface { |
| 52 |
use LoggerAwareTrait; |
| 53 |
|
| 54 |
private ServiceOptions $config; |
| 55 |
private AuthManager $authManager; |
| 56 |
private HookRunner $hookRunner; |
| 57 |
private UserIdentityLookup $userIdentityLookup; |
| 58 |
private UserFactory $userFactory; |
| 59 |
private UserNameUtils $userNameUtils; |
| 60 |
private UserOptionsLookup $userOptionsLookup; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private MapCacheLRU $permissionCache; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public const CONSTRUCTOR_OPTIONS = [ |
| 72 |
MainConfigNames::EnableEmail, |
| 73 |
MainConfigNames::PasswordResetRoutes, |
| 74 |
]; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function __construct( |
| 89 |
ServiceOptions $config, |
| 90 |
LoggerInterface $logger, |
| 91 |
AuthManager $authManager, |
| 92 |
HookContainer $hookContainer, |
| 93 |
UserIdentityLookup $userIdentityLookup, |
| 94 |
UserFactory $userFactory, |
| 95 |
UserNameUtils $userNameUtils, |
| 96 |
UserOptionsLookup $userOptionsLookup |
| 97 |
) { |
| 98 |
$config->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 99 |
|
| 100 |
$this->config = $config; |
| 101 |
$this->logger = $logger; |
| 102 |
|
| 103 |
$this->authManager = $authManager; |
| 104 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 105 |
$this->userIdentityLookup = $userIdentityLookup; |
| 106 |
$this->userFactory = $userFactory; |
| 107 |
$this->userNameUtils = $userNameUtils; |
| 108 |
$this->userOptionsLookup = $userOptionsLookup; |
| 109 |
|
| 110 |
$this->permissionCache = new MapCacheLRU( 1 ); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function isAllowed( User $user ) { |
| 120 |
return $this->permissionCache->getWithSetCallback( |
| 121 |
$user->getName(), |
| 122 |
function () use ( $user ) { |
| 123 |
return $this->computeIsAllowed( $user ); |
| 124 |
} |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function isEnabled(): StatusValue { |
| 133 |
$resetRoutes = $this->config->get( MainConfigNames::PasswordResetRoutes ); |
| 134 |
if ( !is_array( $resetRoutes ) || !in_array( true, $resetRoutes, true ) ) { |
| 135 |
|
| 136 |
return StatusValue::newFatal( 'passwordreset-disabled' ); |
| 137 |
} |
| 138 |
|
| 139 |
$providerStatus = $this->authManager->allowsAuthenticationDataChange( |
| 140 |
new TemporaryPasswordAuthenticationRequest(), false ); |
| 141 |
if ( !$providerStatus->isGood() ) { |
| 142 |
|
| 143 |
return StatusValue::newFatal( 'resetpass_forbidden-reason', |
| 144 |
$providerStatus->getMessage() ); |
| 145 |
} |
| 146 |
if ( !$this->config->get( MainConfigNames::EnableEmail ) ) { |
| 147 |
|
| 148 |
return StatusValue::newFatal( 'passwordreset-emaildisabled' ); |
| 149 |
} |
| 150 |
return StatusValue::newGood(); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
private function computeIsAllowed( User $user ): StatusValue { |
| 158 |
$enabledStatus = $this->isEnabled(); |
| 159 |
if ( !$enabledStatus->isGood() ) { |
| 160 |
return $enabledStatus; |
| 161 |
} |
| 162 |
if ( !$user->isAllowed( 'editmyprivateinfo' ) ) { |
| 163 |
|
| 164 |
return StatusValue::newFatal( 'badaccess' ); |
| 165 |
} |
| 166 |
if ( $this->isBlocked( $user ) ) { |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
return StatusValue::newFatal( 'blocked-mailpassword' ); |
| 171 |
} |
| 172 |
return StatusValue::newGood(); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function execute( |
| 191 |
User $performingUser, |
| 192 |
$username = null, |
| 193 |
$email = null |
| 194 |
) { |
| 195 |
if ( !$this->isAllowed( $performingUser )->isGood() ) { |
| 196 |
throw new LogicException( |
| 197 |
'User ' . $performingUser->getName() . ' is not allowed to reset passwords' |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
if ( $performingUser->pingLimiter( 'mailpassword' ) ) { |
| 204 |
return StatusValue::newGood(); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
$ip = $performingUser->getRequest()->getIP(); |
| 210 |
if ( !$ip ) { |
| 211 |
return StatusValue::newFatal( 'badipaddress' ); |
| 212 |
} |
| 213 |
|
| 214 |
$resetRoutes = $this->config->get( MainConfigNames::PasswordResetRoutes ) |
| 215 |
+ [ 'username' => false, 'email' => false ]; |
| 216 |
if ( !$resetRoutes['username'] || $username === '' ) { |
| 217 |
$username = null; |
| 218 |
} |
| 219 |
if ( !$resetRoutes['email'] || $email === '' ) { |
| 220 |
$email = null; |
| 221 |
} |
| 222 |
|
| 223 |
if ( $username !== null && !$this->userNameUtils->getCanonical( $username ) ) { |
| 224 |
return StatusValue::newFatal( 'noname' ); |
| 225 |
} |
| 226 |
if ( $email !== null && !Sanitizer::validateEmail( $email ) ) { |
| 227 |
return StatusValue::newFatal( 'passwordreset-invalidemail' ); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
$users = []; |
| 233 |
|
| 234 |
if ( $username !== null ) { |
| 235 |
$user = $this->userFactory->newFromName( $username ); |
| 236 |
|
| 237 |
if ( $user && $user->isRegistered() && $user->getEmail() && ( |
| 238 |
!$this->userOptionsLookup->getBoolOption( $user, 'requireemail' ) || |
| 239 |
$user->getEmail() === $email |
| 240 |
) ) { |
| 241 |
|
| 242 |
|
| 243 |
$users[] = $user; |
| 244 |
} |
| 245 |
|
| 246 |
} elseif ( $email !== null ) { |
| 247 |
foreach ( $this->getUsersByEmail( $email ) as $userIdent ) { |
| 248 |
|
| 249 |
if ( $this->userOptionsLookup->getBoolOption( $userIdent, 'requireemail' ) ) { |
| 250 |
continue; |
| 251 |
} |
| 252 |
$users[] = $this->userFactory->newFromUserIdentity( $userIdent ); |
| 253 |
} |
| 254 |
|
| 255 |
} else { |
| 256 |
|
| 257 |
return StatusValue::newFatal( 'passwordreset-nodata' ); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
$data = [ |
| 262 |
'Username' => $username, |
| 263 |
'Email' => $email, |
| 264 |
]; |
| 265 |
|
| 266 |
$error = []; |
| 267 |
if ( !$this->hookRunner->onSpecialPasswordResetOnSubmit( $users, $data, $error ) ) { |
| 268 |
return StatusValue::newFatal( Message::newFromSpecifier( $error ) ); |
| 269 |
} |
| 270 |
|
| 271 |
if ( !$users ) { |
| 272 |
|
| 273 |
return StatusValue::newGood(); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
$firstUser = reset( $users ); |
| 279 |
|
| 280 |
$this->hookRunner->onUser__mailPasswordInternal( $performingUser, $ip, $firstUser ); |
| 281 |
|
| 282 |
$result = StatusValue::newGood(); |
| 283 |
$reqs = []; |
| 284 |
foreach ( $users as $user ) { |
| 285 |
$req = TemporaryPasswordAuthenticationRequest::newRandom(); |
| 286 |
$req->username = $user->getName(); |
| 287 |
$req->mailpassword = true; |
| 288 |
$req->caller = $performingUser->getName(); |
| 289 |
|
| 290 |
$status = $this->authManager->allowsAuthenticationDataChange( $req, true ); |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
if ( $status->isGood() && $status->getValue() === 'throttled-mailpassword' ) { |
| 295 |
return StatusValue::newGood(); |
| 296 |
} |
| 297 |
|
| 298 |
if ( $status->isGood() && $status->getValue() !== 'ignored' ) { |
| 299 |
$reqs[] = $req; |
| 300 |
} elseif ( $result->isGood() ) { |
| 301 |
|
| 302 |
|
| 303 |
if ( $status->getValue() === 'ignored' ) { |
| 304 |
$status = StatusValue::newFatal( 'passwordreset-ignored' ); |
| 305 |
} |
| 306 |
$result->merge( $status ); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
$logContext = [ |
| 311 |
'requestingIp' => $ip, |
| 312 |
'requestingUser' => $performingUser->getName(), |
| 313 |
'targetUsername' => $username, |
| 314 |
'targetEmail' => $email, |
| 315 |
]; |
| 316 |
|
| 317 |
if ( !$result->isGood() ) { |
| 318 |
$this->logger->info( |
| 319 |
"{requestingUser} attempted password reset of {targetUsername} but failed", |
| 320 |
$logContext + [ 'errors' => $result->getErrors() ] |
| 321 |
); |
| 322 |
return $result; |
| 323 |
} |
| 324 |
|
| 325 |
DeferredUpdates::addUpdate( |
| 326 |
new SendPasswordResetEmailUpdate( $this->authManager, $reqs, $logContext ), |
| 327 |
DeferredUpdates::POSTSEND |
| 328 |
); |
| 329 |
|
| 330 |
return StatusValue::newGood(); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
private function isBlocked( User $user ) { |
| 341 |
$block = $user->getBlock(); |
| 342 |
return $block && $block->appliesToPasswordReset(); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
protected function getUsersByEmail( $email ) { |
| 353 |
return $this->userIdentityLookup->newSelectQueryBuilder() |
| 354 |
->join( 'user', null, [ "actor_user=user_id" ] ) |
| 355 |
->where( [ 'user_email' => $email ] ) |
| 356 |
->caller( __METHOD__ ) |
| 357 |
->fetchUserIdentities(); |
| 358 |
} |
| 359 |
|
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
class_alias( PasswordReset::class, 'PasswordReset' ); |