| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Auth; |
| 23 |
|
| 24 |
use MediaWiki\MainConfigNames; |
| 25 |
use MediaWiki\Password\InvalidPassword; |
| 26 |
use MediaWiki\Password\Password; |
| 27 |
use MediaWiki\Password\PasswordFactory; |
| 28 |
use MediaWiki\SpecialPage\SpecialPage; |
| 29 |
use MediaWiki\Title\Title; |
| 30 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 31 |
use MediaWiki\User\User; |
| 32 |
use MediaWiki\User\UserRigorOptions; |
| 33 |
use Wikimedia\IPUtils; |
| 34 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 35 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
abstract class AbstractTemporaryPasswordPrimaryAuthenticationProvider |
| 49 |
extends AbstractPasswordPrimaryAuthenticationProvider |
| 50 |
{ |
| 51 |
|
| 52 |
protected $emailEnabled = null; |
| 53 |
|
| 54 |
|
| 55 |
protected $newPasswordExpiry = null; |
| 56 |
|
| 57 |
|
| 58 |
protected $passwordReminderResendTime = null; |
| 59 |
|
| 60 |
protected IConnectionProvider $dbProvider; |
| 61 |
protected UserOptionsLookup $userOptionsLookup; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function __construct( |
| 73 |
IConnectionProvider $dbProvider, |
| 74 |
UserOptionsLookup $userOptionsLookup, |
| 75 |
$params = [] |
| 76 |
) { |
| 77 |
parent::__construct( $params ); |
| 78 |
|
| 79 |
if ( isset( $params['emailEnabled'] ) ) { |
| 80 |
$this->emailEnabled = (bool)$params['emailEnabled']; |
| 81 |
} |
| 82 |
if ( isset( $params['newPasswordExpiry'] ) ) { |
| 83 |
$this->newPasswordExpiry = (int)$params['newPasswordExpiry']; |
| 84 |
} |
| 85 |
if ( isset( $params['passwordReminderResendTime'] ) ) { |
| 86 |
$this->passwordReminderResendTime = $params['passwordReminderResendTime']; |
| 87 |
} |
| 88 |
$this->dbProvider = $dbProvider; |
| 89 |
$this->userOptionsLookup = $userOptionsLookup; |
| 90 |
} |
| 91 |
|
| 92 |
protected function postInitSetup() { |
| 93 |
$this->emailEnabled ??= $this->config->get( MainConfigNames::EnableEmail ); |
| 94 |
$this->newPasswordExpiry ??= $this->config->get( MainConfigNames::NewPasswordExpiry ); |
| 95 |
$this->passwordReminderResendTime ??= |
| 96 |
$this->config->get( MainConfigNames::PasswordReminderResendTime ); |
| 97 |
} |
| 98 |
|
| 99 |
protected function getPasswordResetData( $username, $data ) { |
| 100 |
|
| 101 |
return (object)[ |
| 102 |
'msg' => wfMessage( 'resetpass-temp-emailed' ), |
| 103 |
'hard' => true, |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
public function getAuthenticationRequests( $action, array $options ) { |
| 108 |
switch ( $action ) { |
| 109 |
case AuthManager::ACTION_LOGIN: |
| 110 |
return [ new PasswordAuthenticationRequest() ]; |
| 111 |
|
| 112 |
case AuthManager::ACTION_CHANGE: |
| 113 |
return [ TemporaryPasswordAuthenticationRequest::newRandom() ]; |
| 114 |
|
| 115 |
case AuthManager::ACTION_CREATE: |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
if ( |
| 121 |
isset( $options['username'] ) && |
| 122 |
!$this->userNameUtils->isTemp( $options['username'] ) && |
| 123 |
$this->emailEnabled |
| 124 |
) { |
| 125 |
return [ TemporaryPasswordAuthenticationRequest::newRandom() ]; |
| 126 |
} else { |
| 127 |
return []; |
| 128 |
} |
| 129 |
|
| 130 |
case AuthManager::ACTION_REMOVE: |
| 131 |
return [ new TemporaryPasswordAuthenticationRequest ]; |
| 132 |
|
| 133 |
default: |
| 134 |
return []; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public function beginPrimaryAuthentication( array $reqs ) { |
| 139 |
$req = AuthenticationRequest::getRequestByClass( $reqs, PasswordAuthenticationRequest::class ); |
| 140 |
if ( !$req || $req->username === null || $req->password === null ) { |
| 141 |
return AuthenticationResponse::newAbstain(); |
| 142 |
} |
| 143 |
|
| 144 |
$username = $this->userNameUtils->getCanonical( |
| 145 |
$req->username, UserRigorOptions::RIGOR_USABLE ); |
| 146 |
if ( $username === false ) { |
| 147 |
return AuthenticationResponse::newAbstain(); |
| 148 |
} |
| 149 |
|
| 150 |
[ $tempPassHash, $tempPassTime ] = $this->getTemporaryPassword( $username, IDBAccessObject::READ_LATEST ); |
| 151 |
if ( !$tempPassHash ) { |
| 152 |
return AuthenticationResponse::newAbstain(); |
| 153 |
} |
| 154 |
|
| 155 |
$status = $this->checkPasswordValidity( $username, $req->password ); |
| 156 |
if ( !$status->isOK() ) { |
| 157 |
return $this->getFatalPasswordErrorResponse( $username, $status ); |
| 158 |
} |
| 159 |
|
| 160 |
if ( !$tempPassHash->verify( $req->password ) || |
| 161 |
!$this->isTimestampValid( $tempPassTime ) |
| 162 |
) { |
| 163 |
return $this->failResponse( $req ); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
$this->logger->info( "{user} successfully logged in using temp password", |
| 170 |
[ |
| 171 |
'provider' => static::class, |
| 172 |
'user' => $username, |
| 173 |
'requestIP' => $this->manager->getRequest()->getIP() |
| 174 |
] |
| 175 |
); |
| 176 |
|
| 177 |
$this->setPasswordResetFlag( $username, $status ); |
| 178 |
|
| 179 |
return AuthenticationResponse::newPass( $username ); |
| 180 |
} |
| 181 |
|
| 182 |
public function testUserCanAuthenticate( $username ) { |
| 183 |
$username = $this->userNameUtils->getCanonical( $username, UserRigorOptions::RIGOR_USABLE ); |
| 184 |
if ( $username === false ) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
[ $tempPassHash, $tempPassTime ] = $this->getTemporaryPassword( $username ); |
| 189 |
return $tempPassHash && |
| 190 |
!( $tempPassHash instanceof InvalidPassword ) && |
| 191 |
$this->isTimestampValid( $tempPassTime ); |
| 192 |
} |
| 193 |
|
| 194 |
public function providerAllowsAuthenticationDataChange( |
| 195 |
AuthenticationRequest $req, $checkData = true |
| 196 |
) { |
| 197 |
if ( get_class( $req ) !== TemporaryPasswordAuthenticationRequest::class ) { |
| 198 |
|
| 199 |
return \StatusValue::newGood( 'ignored' ); |
| 200 |
} |
| 201 |
|
| 202 |
if ( !$checkData ) { |
| 203 |
return \StatusValue::newGood(); |
| 204 |
} |
| 205 |
|
| 206 |
$username = $this->userNameUtils->getCanonical( |
| 207 |
$req->username, UserRigorOptions::RIGOR_USABLE ); |
| 208 |
if ( $username === false ) { |
| 209 |
return \StatusValue::newGood( 'ignored' ); |
| 210 |
} |
| 211 |
|
| 212 |
[ $tempPassHash, $tempPassTime ] = $this->getTemporaryPassword( $username, IDBAccessObject::READ_LATEST ); |
| 213 |
if ( !$tempPassHash ) { |
| 214 |
return \StatusValue::newGood( 'ignored' ); |
| 215 |
} |
| 216 |
|
| 217 |
$sv = \StatusValue::newGood(); |
| 218 |
if ( $req->password !== null ) { |
| 219 |
$sv->merge( $this->checkPasswordValidity( $username, $req->password ) ); |
| 220 |
|
| 221 |
if ( $req->mailpassword ) { |
| 222 |
if ( !$this->emailEnabled ) { |
| 223 |
return \StatusValue::newFatal( 'passwordreset-emaildisabled' ); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if ( |
| 232 |
$this->passwordReminderResendTime |
| 233 |
&& $tempPassTime |
| 234 |
&& time() < (int)wfTimestamp( TS_UNIX, $tempPassTime ) |
| 235 |
+ $this->passwordReminderResendTime * 3600 |
| 236 |
) { |
| 237 |
|
| 238 |
|
| 239 |
return \StatusValue::newFatal( 'throttled-mailpassword', |
| 240 |
round( $this->passwordReminderResendTime, 3 ) ); |
| 241 |
} |
| 242 |
|
| 243 |
if ( !$req->caller ) { |
| 244 |
return \StatusValue::newFatal( 'passwordreset-nocaller' ); |
| 245 |
} |
| 246 |
if ( !IPUtils::isValid( $req->caller ) ) { |
| 247 |
$caller = User::newFromName( $req->caller ); |
| 248 |
if ( !$caller ) { |
| 249 |
return \StatusValue::newFatal( 'passwordreset-nosuchcaller', $req->caller ); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
return $sv; |
| 255 |
} |
| 256 |
|
| 257 |
public function providerChangeAuthenticationData( AuthenticationRequest $req ) { |
| 258 |
$username = $req->username !== null ? |
| 259 |
$this->userNameUtils->getCanonical( $req->username, UserRigorOptions::RIGOR_USABLE ) : false; |
| 260 |
if ( $username === false ) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
$sendMail = false; |
| 265 |
if ( $req->action !== AuthManager::ACTION_REMOVE && |
| 266 |
get_class( $req ) === TemporaryPasswordAuthenticationRequest::class |
| 267 |
) { |
| 268 |
$tempPassHash = $this->getPasswordFactory()->newFromPlaintext( $req->password ); |
| 269 |
$tempPassTime = wfTimestampNow(); |
| 270 |
$sendMail = $req->mailpassword; |
| 271 |
|
| 272 |
$req->mailpassword = false; |
| 273 |
} else { |
| 274 |
|
| 275 |
$tempPassHash = PasswordFactory::newInvalidPassword(); |
| 276 |
$tempPassTime = null; |
| 277 |
} |
| 278 |
|
| 279 |
$this->setTemporaryPassword( $username, $tempPassHash, $tempPassTime ); |
| 280 |
|
| 281 |
if ( $sendMail ) { |
| 282 |
$this->maybeSendPasswordResetEmail( $req ); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
public function accountCreationType() { |
| 287 |
return self::TYPE_CREATE; |
| 288 |
} |
| 289 |
|
| 290 |
public function testForAccountCreation( $user, $creator, array $reqs ) { |
| 291 |
|
| 292 |
$req = AuthenticationRequest::getRequestByClass( |
| 293 |
$reqs, TemporaryPasswordAuthenticationRequest::class |
| 294 |
); |
| 295 |
|
| 296 |
$ret = \StatusValue::newGood(); |
| 297 |
if ( $req ) { |
| 298 |
if ( $req->mailpassword ) { |
| 299 |
if ( !$this->emailEnabled ) { |
| 300 |
$ret->merge( \StatusValue::newFatal( 'emaildisabled' ) ); |
| 301 |
} elseif ( !$user->getEmail() ) { |
| 302 |
$ret->merge( \StatusValue::newFatal( 'noemailcreate' ) ); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
$ret->merge( |
| 307 |
$this->checkPasswordValidity( $user->getName(), $req->password ) |
| 308 |
); |
| 309 |
} |
| 310 |
return $ret; |
| 311 |
} |
| 312 |
|
| 313 |
public function beginPrimaryAccountCreation( $user, $creator, array $reqs ) { |
| 314 |
|
| 315 |
$req = AuthenticationRequest::getRequestByClass( |
| 316 |
$reqs, TemporaryPasswordAuthenticationRequest::class |
| 317 |
); |
| 318 |
if ( $req && $req->username !== null && $req->password !== null ) { |
| 319 |
|
| 320 |
if ( $req->username !== $user->getName() ) { |
| 321 |
$req = clone $req; |
| 322 |
$req->username = $user->getName(); |
| 323 |
} |
| 324 |
|
| 325 |
if ( $req->mailpassword ) { |
| 326 |
|
| 327 |
$this->manager->setAuthenticationSessionData( 'no-email', true ); |
| 328 |
} |
| 329 |
|
| 330 |
$ret = AuthenticationResponse::newPass( $req->username ); |
| 331 |
$ret->createRequest = $req; |
| 332 |
return $ret; |
| 333 |
} |
| 334 |
return AuthenticationResponse::newAbstain(); |
| 335 |
} |
| 336 |
|
| 337 |
public function finishAccountCreation( $user, $creator, AuthenticationResponse $res ) { |
| 338 |
|
| 339 |
$req = $res->createRequest; |
| 340 |
$mailpassword = $req->mailpassword; |
| 341 |
|
| 342 |
$req->mailpassword = false; |
| 343 |
|
| 344 |
|
| 345 |
$this->providerChangeAuthenticationData( $req ); |
| 346 |
|
| 347 |
if ( $mailpassword ) { |
| 348 |
$this->maybeSendNewAccountEmail( $user, $creator, $req->password ); |
| 349 |
} |
| 350 |
|
| 351 |
return $mailpassword ? 'byemail' : null; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
protected function isTimestampValid( $timestamp ) { |
| 361 |
$time = wfTimestampOrNull( TS_MW, $timestamp ); |
| 362 |
if ( $time !== null ) { |
| 363 |
$expiry = (int)wfTimestamp( TS_UNIX, $time ) + $this->newPasswordExpiry; |
| 364 |
if ( time() >= $expiry ) { |
| 365 |
return false; |
| 366 |
} |
| 367 |
} |
| 368 |
return true; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
protected function maybeSendNewAccountEmail( User $user, User $creatingUser, $password ): void { |
| 383 |
|
| 384 |
$this->dbProvider->getPrimaryDatabase()->onTransactionCommitOrIdle( |
| 385 |
function () use ( $user, $creatingUser, $password ) { |
| 386 |
$this->sendNewAccountEmail( $user, $creatingUser, $password ); |
| 387 |
}, |
| 388 |
__METHOD__ |
| 389 |
); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
protected function sendNewAccountEmail( User $user, User $creatingUser, $password ): void { |
| 401 |
$ip = $creatingUser->getRequest()->getIP(); |
| 402 |
|
| 403 |
if ( !$ip ) { |
| 404 |
return; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
$this->getHookRunner()->onUser__mailPasswordInternal( $creatingUser, $ip, $user ); |
| 409 |
|
| 410 |
$mainPageUrl = Title::newMainPage()->getCanonicalURL(); |
| 411 |
$userLanguage = $this->userOptionsLookup->getOption( $user, 'language' ); |
| 412 |
$subjectMessage = wfMessage( 'createaccount-title' )->inLanguage( $userLanguage ); |
| 413 |
$bodyMessage = wfMessage( 'createaccount-text', $ip, $user->getName(), $password, |
| 414 |
'<' . $mainPageUrl . '>', round( $this->newPasswordExpiry / 86400 ) ) |
| 415 |
->inLanguage( $userLanguage ); |
| 416 |
|
| 417 |
$status = $user->sendMail( $subjectMessage->text(), $bodyMessage->text() ); |
| 418 |
|
| 419 |
|
| 420 |
if ( !$status->isGood() ) { |
| 421 |
$this->logger->warning( 'Could not send account creation email: ' . |
| 422 |
$status->getWikiText( false, false, 'en' ) ); |
| 423 |
} |
| 424 |
|
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
protected function maybeSendPasswordResetEmail( TemporaryPasswordAuthenticationRequest $req ): void { |
| 436 |
|
| 437 |
$this->dbProvider->getPrimaryDatabase()->onTransactionCommitOrIdle( |
| 438 |
function () use ( $req ) { |
| 439 |
$this->sendPasswordResetEmail( $req ); |
| 440 |
}, |
| 441 |
__METHOD__ |
| 442 |
); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
protected function sendPasswordResetEmail( TemporaryPasswordAuthenticationRequest $req ): void { |
| 452 |
$user = User::newFromName( $req->username ); |
| 453 |
if ( !$user ) { |
| 454 |
return; |
| 455 |
} |
| 456 |
$userLanguage = $this->userOptionsLookup->getOption( $user, 'language' ); |
| 457 |
$callerIsAnon = IPUtils::isValid( $req->caller ); |
| 458 |
$callerName = $callerIsAnon ? $req->caller : User::newFromName( $req->caller )->getName(); |
| 459 |
$passwordMessage = wfMessage( 'passwordreset-emailelement', $user->getName(), |
| 460 |
$req->password )->inLanguage( $userLanguage ); |
| 461 |
$emailMessage = wfMessage( $callerIsAnon ? 'passwordreset-emailtext-ip' |
| 462 |
: 'passwordreset-emailtext-user' )->inLanguage( $userLanguage ); |
| 463 |
$body = $emailMessage->params( $callerName, $passwordMessage->text(), 1, |
| 464 |
'<' . Title::newMainPage()->getCanonicalURL() . '>', |
| 465 |
round( $this->newPasswordExpiry / 86400 ) )->text(); |
| 466 |
|
| 467 |
|
| 468 |
if ( |
| 469 |
!$this->userOptionsLookup->getBoolOption( $user, 'requireemail' ) |
| 470 |
) { |
| 471 |
$url = SpecialPage::getTitleFor( 'Preferences', false, 'mw-prefsection-personal-email' ) |
| 472 |
->getCanonicalURL(); |
| 473 |
$body .= "\n\n" . wfMessage( 'passwordreset-emailtext-require-email' ) |
| 474 |
->inLanguage( $userLanguage ) |
| 475 |
->params( "<$url>" ) |
| 476 |
->text(); |
| 477 |
} |
| 478 |
|
| 479 |
$emailTitle = wfMessage( 'passwordreset-emailtitle' )->inLanguage( $userLanguage ); |
| 480 |
$user->sendMail( $emailTitle->text(), $body ); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
abstract protected function getTemporaryPassword( string $username, $flags = IDBAccessObject::READ_NORMAL ): array; |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
abstract protected function setTemporaryPassword( string $username, Password $tempPassHash, $tempPassTime ): void; |
| 510 |
} |