| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\User; |
| 22 |
|
| 23 |
use MediaWiki\Auth\AuthenticationResponse; |
| 24 |
use MediaWiki\Auth\Throttler; |
| 25 |
use MediaWiki\Config\Config; |
| 26 |
use MediaWiki\HookContainer\HookRunner; |
| 27 |
use MediaWiki\Json\FormatJson; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\MediaWikiServices; |
| 30 |
use MediaWiki\Password\InvalidPassword; |
| 31 |
use MediaWiki\Password\Password; |
| 32 |
use MediaWiki\Password\PasswordError; |
| 33 |
use MediaWiki\Password\PasswordFactory; |
| 34 |
use MediaWiki\Request\WebRequest; |
| 35 |
use MediaWiki\Session\BotPasswordSessionProvider; |
| 36 |
use MediaWiki\Session\SessionManager; |
| 37 |
use MediaWiki\Status\Status; |
| 38 |
use MWRestrictions; |
| 39 |
use stdClass; |
| 40 |
use UnexpectedValueException; |
| 41 |
use Wikimedia\Rdbms\IDatabase; |
| 42 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 43 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
class BotPassword { |
| 50 |
|
| 51 |
public const APPID_MAXLENGTH = 32; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public const PASSWORD_MINLENGTH = 32; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public const RESTRICTIONS_MAXLENGTH = 65535; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public const GRANTS_MAXLENGTH = 65535; |
| 69 |
|
| 70 |
|
| 71 |
private $isSaved; |
| 72 |
|
| 73 |
|
| 74 |
private $centralId; |
| 75 |
|
| 76 |
|
| 77 |
private $appId; |
| 78 |
|
| 79 |
|
| 80 |
private $token; |
| 81 |
|
| 82 |
|
| 83 |
private $restrictions; |
| 84 |
|
| 85 |
|
| 86 |
private $grants; |
| 87 |
|
| 88 |
|
| 89 |
private $flags; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function __construct( $row, $isSaved, $flags = IDBAccessObject::READ_NORMAL ) { |
| 99 |
$this->isSaved = $isSaved; |
| 100 |
$this->flags = $flags; |
| 101 |
|
| 102 |
$this->centralId = (int)$row->bp_user; |
| 103 |
$this->appId = $row->bp_app_id; |
| 104 |
$this->token = $row->bp_token; |
| 105 |
$this->restrictions = MWRestrictions::newFromJson( $row->bp_restrictions ); |
| 106 |
$this->grants = FormatJson::decode( $row->bp_grants ); |
| 107 |
} |
| 108 |
|
| 109 |
public static function getReplicaDatabase(): IReadableDatabase { |
| 110 |
return MediaWikiServices::getInstance() |
| 111 |
->getBotPasswordStore() |
| 112 |
->getReplicaDatabase(); |
| 113 |
} |
| 114 |
|
| 115 |
public static function getPrimaryDatabase(): IDatabase { |
| 116 |
return MediaWikiServices::getInstance() |
| 117 |
->getBotPasswordStore() |
| 118 |
->getPrimaryDatabase(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public static function newFromUser( UserIdentity $userIdentity, $appId, $flags = IDBAccessObject::READ_NORMAL ) { |
| 129 |
return MediaWikiServices::getInstance() |
| 130 |
->getBotPasswordStore() |
| 131 |
->getByUser( $userIdentity, (string)$appId, (int)$flags ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public static function newFromCentralId( $centralId, $appId, $flags = IDBAccessObject::READ_NORMAL ) { |
| 142 |
return MediaWikiServices::getInstance() |
| 143 |
->getBotPasswordStore() |
| 144 |
->getByCentralId( (int)$centralId, (string)$appId, (int)$flags ); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public static function newUnsaved( array $data, $flags = IDBAccessObject::READ_NORMAL ) { |
| 160 |
return MediaWikiServices::getInstance() |
| 161 |
->getBotPasswordStore() |
| 162 |
->newUnsavedBotPassword( $data, (int)$flags ); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function isSaved() { |
| 170 |
return $this->isSaved; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public function getUserCentralId() { |
| 178 |
return $this->centralId; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function getAppId() { |
| 185 |
return $this->appId; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function getToken() { |
| 192 |
return $this->token; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public function getRestrictions() { |
| 199 |
return $this->restrictions; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function getGrants() { |
| 206 |
return $this->grants; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public static function getSeparator() { |
| 214 |
$userrightsInterwikiDelimiter = MediaWikiServices::getInstance() |
| 215 |
->getMainConfig()->get( MainConfigNames::UserrightsInterwikiDelimiter ); |
| 216 |
return $userrightsInterwikiDelimiter; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
private function getPassword() { |
| 223 |
if ( ( $this->flags & IDBAccessObject::READ_LATEST ) == IDBAccessObject::READ_LATEST ) { |
| 224 |
$db = self::getPrimaryDatabase(); |
| 225 |
} else { |
| 226 |
$db = self::getReplicaDatabase(); |
| 227 |
} |
| 228 |
|
| 229 |
$password = $db->newSelectQueryBuilder() |
| 230 |
->select( 'bp_password' ) |
| 231 |
->from( 'bot_passwords' ) |
| 232 |
->where( [ 'bp_user' => $this->centralId, 'bp_app_id' => $this->appId ] ) |
| 233 |
->recency( $this->flags ) |
| 234 |
->caller( __METHOD__ )->fetchField(); |
| 235 |
if ( $password === false ) { |
| 236 |
return PasswordFactory::newInvalidPassword(); |
| 237 |
} |
| 238 |
|
| 239 |
$passwordFactory = MediaWikiServices::getInstance()->getPasswordFactory(); |
| 240 |
try { |
| 241 |
return $passwordFactory->newFromCiphertext( $password ); |
| 242 |
} catch ( PasswordError $ex ) { |
| 243 |
return PasswordFactory::newInvalidPassword(); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public function isInvalid() { |
| 253 |
return $this->getPassword() instanceof InvalidPassword; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public function save( $operation, ?Password $password = null ) { |
| 264 |
|
| 265 |
if ( $operation !== 'insert' && $operation !== 'update' ) { |
| 266 |
throw new UnexpectedValueException( |
| 267 |
"Expected 'insert' or 'update'; got '{$operation}'." |
| 268 |
); |
| 269 |
} |
| 270 |
|
| 271 |
$store = MediaWikiServices::getInstance()->getBotPasswordStore(); |
| 272 |
if ( $operation === 'insert' ) { |
| 273 |
$statusValue = $store->insertBotPassword( $this, $password ); |
| 274 |
} else { |
| 275 |
|
| 276 |
$statusValue = $store->updateBotPassword( $this, $password ); |
| 277 |
} |
| 278 |
|
| 279 |
if ( $statusValue->isGood() ) { |
| 280 |
$this->token = $statusValue->getValue(); |
| 281 |
$this->isSaved = true; |
| 282 |
return Status::newGood(); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
return Status::wrap( $statusValue ); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public function delete() { |
| 295 |
$ok = MediaWikiServices::getInstance() |
| 296 |
->getBotPasswordStore() |
| 297 |
->deleteBotPassword( $this ); |
| 298 |
if ( $ok ) { |
| 299 |
$this->token = '**unsaved**'; |
| 300 |
$this->isSaved = false; |
| 301 |
} |
| 302 |
return $ok; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public static function invalidateAllPasswordsForUser( $username ) { |
| 311 |
return MediaWikiServices::getInstance() |
| 312 |
->getBotPasswordStore() |
| 313 |
->invalidateUserPasswords( (string)$username ); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public static function removeAllPasswordsForUser( $username ) { |
| 322 |
return MediaWikiServices::getInstance() |
| 323 |
->getBotPasswordStore() |
| 324 |
->removeUserPasswords( (string)$username ); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public static function generatePassword( $config ) { |
| 333 |
return PasswordFactory::generateRandomPasswordString( self::PASSWORD_MINLENGTH ); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public static function canonicalizeLoginData( $username, $password ) { |
| 346 |
$sep = self::getSeparator(); |
| 347 |
|
| 348 |
if ( strlen( $password ) >= self::PASSWORD_MINLENGTH && str_contains( $username, $sep ) ) { |
| 349 |
|
| 350 |
if ( preg_match( '/^[0-9a-w]{' . self::PASSWORD_MINLENGTH . ',}$/', $password ) ) { |
| 351 |
return [ $username, $password ]; |
| 352 |
} |
| 353 |
} elseif ( strlen( $password ) > self::PASSWORD_MINLENGTH && str_contains( $password, $sep ) ) { |
| 354 |
$segments = explode( $sep, $password ); |
| 355 |
$password = array_pop( $segments ); |
| 356 |
$appId = implode( $sep, $segments ); |
| 357 |
if ( preg_match( '/^[0-9a-w]{' . self::PASSWORD_MINLENGTH . ',}$/', $password ) ) { |
| 358 |
return [ $username . $sep . $appId, $password ]; |
| 359 |
} |
| 360 |
} |
| 361 |
return false; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public static function login( $username, $password, WebRequest $request ) { |
| 372 |
$enableBotPasswords = MediaWikiServices::getInstance()->getMainConfig() |
| 373 |
->get( MainConfigNames::EnableBotPasswords ); |
| 374 |
$passwordAttemptThrottle = MediaWikiServices::getInstance()->getMainConfig() |
| 375 |
->get( MainConfigNames::PasswordAttemptThrottle ); |
| 376 |
if ( !$enableBotPasswords ) { |
| 377 |
return Status::newFatal( 'botpasswords-disabled' ); |
| 378 |
} |
| 379 |
|
| 380 |
$provider = SessionManager::singleton()->getProvider( BotPasswordSessionProvider::class ); |
| 381 |
if ( !$provider ) { |
| 382 |
return Status::newFatal( 'botpasswords-no-provider' ); |
| 383 |
} |
| 384 |
|
| 385 |
$performer = $request->getSession()->getUser(); |
| 386 |
|
| 387 |
$sep = self::getSeparator(); |
| 388 |
if ( !str_contains( $username, $sep ) ) { |
| 389 |
return self::loginHook( |
| 390 |
$username, null, $performer, Status::newFatal( 'botpasswords-invalid-name', $sep ) |
| 391 |
); |
| 392 |
} |
| 393 |
[ $name, $appId ] = explode( $sep, $username, 2 ); |
| 394 |
|
| 395 |
|
| 396 |
$user = User::newFromName( $name ); |
| 397 |
if ( !$user || $user->isAnon() ) { |
| 398 |
return self::loginHook( $user ?: $name, null, $performer, Status::newFatal( 'nosuchuser', $name ) ); |
| 399 |
} |
| 400 |
|
| 401 |
if ( $user->isLocked() ) { |
| 402 |
return Status::newFatal( 'botpasswords-locked' ); |
| 403 |
} |
| 404 |
|
| 405 |
$throttle = null; |
| 406 |
if ( $passwordAttemptThrottle ) { |
| 407 |
$throttle = new Throttler( $passwordAttemptThrottle, [ |
| 408 |
'type' => 'botpassword', |
| 409 |
'cache' => MediaWikiServices::getInstance()->getObjectCacheFactory() |
| 410 |
->getLocalClusterInstance(), |
| 411 |
] ); |
| 412 |
$result = $throttle->increase( $user->getName(), $request->getIP(), __METHOD__ ); |
| 413 |
if ( $result ) { |
| 414 |
$msg = wfMessage( 'login-throttled' )->durationParams( $result['wait'] ); |
| 415 |
return self::loginHook( $user, null, $performer, Status::newFatal( $msg ) ); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
$bp = self::newFromUser( $user, $appId ); |
| 421 |
if ( !$bp ) { |
| 422 |
return self::loginHook( $user, $bp, $performer, |
| 423 |
Status::newFatal( 'botpasswords-not-exist', $name, $appId ) ); |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
$status = $bp->getRestrictions()->check( $request ); |
| 428 |
if ( !$status->isOK() ) { |
| 429 |
return self::loginHook( $user, $bp, $performer, |
| 430 |
Status::newFatal( 'botpasswords-restriction-failed' ) ); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
$passwordObj = $bp->getPassword(); |
| 435 |
if ( $passwordObj instanceof InvalidPassword ) { |
| 436 |
return self::loginHook( $user, $bp, $performer, |
| 437 |
Status::newFatal( 'botpasswords-needs-reset', $name, $appId ) ); |
| 438 |
} |
| 439 |
if ( !$passwordObj->verify( $password ) ) { |
| 440 |
return self::loginHook( $user, $bp, $performer, Status::newFatal( 'wrongpassword' ) ); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
if ( $throttle ) { |
| 445 |
$throttle->clear( $user->getName(), $request->getIP() ); |
| 446 |
} |
| 447 |
return self::loginHook( $user, $bp, $performer, |
| 448 |
|
| 449 |
Status::newGood( $provider->newSessionForRequest( $user, $bp, $request ) ) ); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
private static function loginHook( $user, $bp, User $performer, Status $status ) { |
| 465 |
$extraData = [ |
| 466 |
'performer' => $performer |
| 467 |
]; |
| 468 |
if ( $user instanceof User ) { |
| 469 |
$name = $user->getName(); |
| 470 |
if ( $bp ) { |
| 471 |
$extraData['appId'] = $name . self::getSeparator() . $bp->getAppId(); |
| 472 |
} |
| 473 |
} else { |
| 474 |
$name = $user; |
| 475 |
$user = null; |
| 476 |
} |
| 477 |
|
| 478 |
if ( $status->isGood() ) { |
| 479 |
$response = AuthenticationResponse::newPass( $name ); |
| 480 |
} else { |
| 481 |
$response = AuthenticationResponse::newFail( $status->getMessage() ); |
| 482 |
} |
| 483 |
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ) |
| 484 |
->onAuthManagerLoginAuthenticateAudit( $response, $user, $name, $extraData ); |
| 485 |
|
| 486 |
return $status; |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
class_alias( BotPassword::class, 'BotPassword' ); |