| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\SpecialPage; |
| 4 |
|
| 5 |
use ErrorPageError; |
| 6 |
use InvalidArgumentException; |
| 7 |
use LogicException; |
| 8 |
use MediaWiki\Auth\AuthenticationRequest; |
| 9 |
use MediaWiki\Auth\AuthenticationResponse; |
| 10 |
use MediaWiki\Auth\AuthManager; |
| 11 |
use MediaWiki\Context\DerivativeContext; |
| 12 |
use MediaWiki\HTMLForm\Field\HTMLInfoField; |
| 13 |
use MediaWiki\HTMLForm\HTMLForm; |
| 14 |
use MediaWiki\Language\RawMessage; |
| 15 |
use MediaWiki\Logger\LoggerFactory; |
| 16 |
use MediaWiki\Message\Message; |
| 17 |
use MediaWiki\Request\DerivativeRequest; |
| 18 |
use MediaWiki\Request\WebRequest; |
| 19 |
use MediaWiki\Session\Token; |
| 20 |
use MediaWiki\Status\Status; |
| 21 |
use MWCryptRand; |
| 22 |
use StatusValue; |
| 23 |
use UnexpectedValueException; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
abstract class AuthManagerSpecialPage extends SpecialPage { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
protected static $allowedActions = [ |
| 40 |
AuthManager::ACTION_LOGIN, AuthManager::ACTION_LOGIN_CONTINUE, |
| 41 |
AuthManager::ACTION_CREATE, AuthManager::ACTION_CREATE_CONTINUE, |
| 42 |
AuthManager::ACTION_LINK, AuthManager::ACTION_LINK_CONTINUE, |
| 43 |
AuthManager::ACTION_CHANGE, AuthManager::ACTION_REMOVE, AuthManager::ACTION_UNLINK, |
| 44 |
]; |
| 45 |
|
| 46 |
|
| 47 |
protected static $messages = []; |
| 48 |
|
| 49 |
|
| 50 |
protected $authAction; |
| 51 |
|
| 52 |
|
| 53 |
protected $authRequests; |
| 54 |
|
| 55 |
|
| 56 |
protected $subPage; |
| 57 |
|
| 58 |
|
| 59 |
protected $isReturn; |
| 60 |
|
| 61 |
|
| 62 |
protected $savedRequest; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function onAuthChangeFormFields( |
| 77 |
array $requests, array $fieldInfo, array &$formDescriptor, $action |
| 78 |
) { |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
protected function getLoginSecurityLevel() { |
| 86 |
return $this->getName(); |
| 87 |
} |
| 88 |
|
| 89 |
public function getRequest() { |
| 90 |
return $this->savedRequest ?: $this->getContext()->getRequest(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
protected function setRequest( array $data, $wasPosted = null ) { |
| 104 |
$request = $this->getContext()->getRequest(); |
| 105 |
$this->savedRequest = new DerivativeRequest( |
| 106 |
$request, |
| 107 |
$data + $request->getQueryValues(), |
| 108 |
$wasPosted ?? $request->wasPosted() |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
protected function beforeExecute( $subPage ) { |
| 119 |
$this->getOutput()->disallowUserJs(); |
| 120 |
|
| 121 |
return $this->handleReturnBeforeExecute( $subPage ) |
| 122 |
&& $this->handleReauthBeforeExecute( $subPage ); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
protected function handleReturnBeforeExecute( $subPage ) { |
| 142 |
$authManager = $this->getAuthManager(); |
| 143 |
$key = 'AuthManagerSpecialPage:return:' . $this->getName(); |
| 144 |
|
| 145 |
if ( $subPage === 'return' ) { |
| 146 |
$this->loadAuth( $subPage ); |
| 147 |
$preservedParams = $this->getPreservedParams(); |
| 148 |
|
| 149 |
|
| 150 |
$authData = array_diff_key( $this->getRequest()->getValues(), |
| 151 |
$preservedParams, [ 'title' => 1 ] ); |
| 152 |
$authManager->setAuthenticationSessionData( $key, $authData ); |
| 153 |
|
| 154 |
$url = $this->getPageTitle()->getFullURL( $preservedParams, false, PROTO_HTTPS ); |
| 155 |
$this->getOutput()->redirect( $url ); |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
$authData = $authManager->getAuthenticationSessionData( $key ); |
| 160 |
if ( $authData ) { |
| 161 |
$authManager->removeAuthenticationSessionData( $key ); |
| 162 |
$this->isReturn = true; |
| 163 |
$this->setRequest( $authData, true ); |
| 164 |
} |
| 165 |
|
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
protected function handleReauthBeforeExecute( $subPage ) { |
| 180 |
$authManager = $this->getAuthManager(); |
| 181 |
$request = $this->getRequest(); |
| 182 |
$key = 'AuthManagerSpecialPage:reauth:' . $this->getName(); |
| 183 |
|
| 184 |
$securityLevel = $this->getLoginSecurityLevel(); |
| 185 |
if ( $securityLevel ) { |
| 186 |
$securityStatus = $authManager->securitySensitiveOperationStatus( $securityLevel ); |
| 187 |
if ( $securityStatus === AuthManager::SEC_REAUTH ) { |
| 188 |
$queryParams = array_diff_key( $request->getQueryValues(), [ 'title' => true ] ); |
| 189 |
|
| 190 |
if ( $request->wasPosted() ) { |
| 191 |
|
| 192 |
$uniqueId = MWCryptRand::generateHex( 6 ); |
| 193 |
$key .= ':' . $uniqueId; |
| 194 |
|
| 195 |
$queryParams = [ 'authUniqueId' => $uniqueId ] + $queryParams; |
| 196 |
$authData = array_diff_key( $request->getValues(), |
| 197 |
$this->getPreservedParams(), [ 'title' => 1 ] ); |
| 198 |
$authManager->setAuthenticationSessionData( $key, $authData ); |
| 199 |
} |
| 200 |
|
| 201 |
$title = SpecialPage::getTitleFor( 'Userlogin' ); |
| 202 |
$url = $title->getFullURL( [ |
| 203 |
'returnto' => $this->getFullTitle()->getPrefixedDBkey(), |
| 204 |
'returntoquery' => wfArrayToCgi( $queryParams ), |
| 205 |
'force' => $securityLevel, |
| 206 |
], false, PROTO_HTTPS ); |
| 207 |
|
| 208 |
$this->getOutput()->redirect( $url ); |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
if ( $securityStatus !== AuthManager::SEC_OK ) { |
| 213 |
throw new ErrorPageError( 'cannotauth-not-allowed-title', 'cannotauth-not-allowed' ); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
$uniqueId = $request->getVal( 'authUniqueId' ); |
| 218 |
if ( $uniqueId ) { |
| 219 |
$key .= ':' . $uniqueId; |
| 220 |
$authData = $authManager->getAuthenticationSessionData( $key ); |
| 221 |
if ( $authData ) { |
| 222 |
$authManager->removeAuthenticationSessionData( $key ); |
| 223 |
$this->setRequest( $authData, true ); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
return true; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
abstract protected function getDefaultAction( $subPage ); |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
protected function messageKey( $defaultKey ) { |
| 246 |
return array_key_exists( $defaultKey, static::$messages ) |
| 247 |
? static::$messages[$defaultKey] : $defaultKey; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
protected function getRequestBlacklist() { |
| 256 |
return []; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
protected function loadAuth( $subPage, $authAction = null, $reset = false ) { |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
if ( |
| 274 |
!$reset && $this->subPage === $subPage && $this->authAction |
| 275 |
&& ( !$authAction || $authAction === $this->authAction ) |
| 276 |
) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
$request = $this->getRequest(); |
| 281 |
$this->subPage = $subPage; |
| 282 |
$this->authAction = $authAction ?: $request->getText( 'authAction' ); |
| 283 |
if ( !in_array( $this->authAction, static::$allowedActions, true ) ) { |
| 284 |
$this->authAction = $this->getDefaultAction( $subPage ); |
| 285 |
if ( $request->wasPosted() ) { |
| 286 |
$continueAction = $this->getContinueAction( $this->authAction ); |
| 287 |
if ( in_array( $continueAction, static::$allowedActions, true ) ) { |
| 288 |
$this->authAction = $continueAction; |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
$allReqs = $this->getAuthManager()->getAuthenticationRequests( |
| 294 |
$this->authAction, $this->getUser() ); |
| 295 |
$this->authRequests = array_filter( $allReqs, function ( $req ) { |
| 296 |
return !in_array( get_class( $req ), $this->getRequestBlacklist(), true ); |
| 297 |
} ); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
protected function isContinued() { |
| 305 |
return in_array( $this->authAction, [ |
| 306 |
AuthManager::ACTION_LOGIN_CONTINUE, |
| 307 |
AuthManager::ACTION_CREATE_CONTINUE, |
| 308 |
AuthManager::ACTION_LINK_CONTINUE, |
| 309 |
], true ); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
protected function getContinueAction( $action ) { |
| 318 |
switch ( $action ) { |
| 319 |
case AuthManager::ACTION_LOGIN: |
| 320 |
$action = AuthManager::ACTION_LOGIN_CONTINUE; |
| 321 |
break; |
| 322 |
case AuthManager::ACTION_CREATE: |
| 323 |
$action = AuthManager::ACTION_CREATE_CONTINUE; |
| 324 |
break; |
| 325 |
case AuthManager::ACTION_LINK: |
| 326 |
$action = AuthManager::ACTION_LINK_CONTINUE; |
| 327 |
break; |
| 328 |
} |
| 329 |
return $action; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
protected function isActionAllowed( $action ) { |
| 340 |
$authManager = $this->getAuthManager(); |
| 341 |
if ( !in_array( $action, static::$allowedActions, true ) ) { |
| 342 |
throw new InvalidArgumentException( 'invalid action: ' . $action ); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
$requests = ( $action === $this->authAction ) ? $this->authRequests |
| 347 |
: $authManager->getAuthenticationRequests( $action ); |
| 348 |
if ( !$requests ) { |
| 349 |
|
| 350 |
return false; |
| 351 |
} |
| 352 |
|
| 353 |
switch ( $action ) { |
| 354 |
case AuthManager::ACTION_LOGIN: |
| 355 |
case AuthManager::ACTION_LOGIN_CONTINUE: |
| 356 |
return $authManager->canAuthenticateNow(); |
| 357 |
case AuthManager::ACTION_CREATE: |
| 358 |
case AuthManager::ACTION_CREATE_CONTINUE: |
| 359 |
return $authManager->canCreateAccounts(); |
| 360 |
case AuthManager::ACTION_LINK: |
| 361 |
case AuthManager::ACTION_LINK_CONTINUE: |
| 362 |
return $authManager->canLinkAccounts(); |
| 363 |
case AuthManager::ACTION_CHANGE: |
| 364 |
case AuthManager::ACTION_REMOVE: |
| 365 |
case AuthManager::ACTION_UNLINK: |
| 366 |
return true; |
| 367 |
default: |
| 368 |
|
| 369 |
throw new InvalidArgumentException( 'invalid action: ' . $action ); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
protected function performAuthenticationStep( $action, array $requests ) { |
| 380 |
if ( !in_array( $action, static::$allowedActions, true ) ) { |
| 381 |
throw new InvalidArgumentException( 'invalid action: ' . $action ); |
| 382 |
} |
| 383 |
|
| 384 |
$authManager = $this->getAuthManager(); |
| 385 |
$returnToUrl = $this->getPageTitle( 'return' ) |
| 386 |
->getFullURL( $this->getPreservedParams( [ 'withToken' => true ] ), false, PROTO_HTTPS ); |
| 387 |
|
| 388 |
switch ( $action ) { |
| 389 |
case AuthManager::ACTION_LOGIN: |
| 390 |
return $authManager->beginAuthentication( $requests, $returnToUrl ); |
| 391 |
case AuthManager::ACTION_LOGIN_CONTINUE: |
| 392 |
return $authManager->continueAuthentication( $requests ); |
| 393 |
case AuthManager::ACTION_CREATE: |
| 394 |
return $authManager->beginAccountCreation( $this->getAuthority(), $requests, |
| 395 |
$returnToUrl ); |
| 396 |
case AuthManager::ACTION_CREATE_CONTINUE: |
| 397 |
return $authManager->continueAccountCreation( $requests ); |
| 398 |
case AuthManager::ACTION_LINK: |
| 399 |
return $authManager->beginAccountLink( $this->getUser(), $requests, $returnToUrl ); |
| 400 |
case AuthManager::ACTION_LINK_CONTINUE: |
| 401 |
return $authManager->continueAccountLink( $requests ); |
| 402 |
case AuthManager::ACTION_CHANGE: |
| 403 |
case AuthManager::ACTION_REMOVE: |
| 404 |
case AuthManager::ACTION_UNLINK: |
| 405 |
if ( count( $requests ) > 1 ) { |
| 406 |
throw new InvalidArgumentException( 'only one auth request can be changed at a time' ); |
| 407 |
} |
| 408 |
|
| 409 |
if ( !$requests ) { |
| 410 |
throw new InvalidArgumentException( 'no auth request' ); |
| 411 |
} |
| 412 |
$req = reset( $requests ); |
| 413 |
$status = $authManager->allowsAuthenticationDataChange( $req ); |
| 414 |
$this->getHookRunner()->onChangeAuthenticationDataAudit( $req, $status ); |
| 415 |
if ( !$status->isGood() ) { |
| 416 |
return AuthenticationResponse::newFail( $status->getMessage() ); |
| 417 |
} |
| 418 |
$authManager->changeAuthenticationData( $req ); |
| 419 |
return AuthenticationResponse::newPass(); |
| 420 |
default: |
| 421 |
|
| 422 |
throw new InvalidArgumentException( 'invalid action: ' . $action ); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
protected function trySubmit() { |
| 437 |
$status = false; |
| 438 |
|
| 439 |
$form = $this->getAuthForm( $this->authRequests, $this->authAction ); |
| 440 |
$form->setSubmitCallback( [ $this, 'handleFormSubmit' ] ); |
| 441 |
|
| 442 |
if ( $this->getRequest()->wasPosted() ) { |
| 443 |
|
| 444 |
$requestTokenValue = $this->getRequest()->getVal( $this->getTokenName() ); |
| 445 |
$sessionToken = $this->getToken(); |
| 446 |
if ( $sessionToken->wasNew() ) { |
| 447 |
return Status::newFatal( $this->messageKey( 'authform-newtoken' ) ); |
| 448 |
} elseif ( !$requestTokenValue ) { |
| 449 |
return Status::newFatal( $this->messageKey( 'authform-notoken' ) ); |
| 450 |
} elseif ( !$sessionToken->match( $requestTokenValue ) ) { |
| 451 |
return Status::newFatal( $this->messageKey( 'authform-wrongtoken' ) ); |
| 452 |
} |
| 453 |
|
| 454 |
$form->prepareForm(); |
| 455 |
$status = $form->trySubmit(); |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
if ( $status === true ) { |
| 460 |
|
| 461 |
throw new UnexpectedValueException( 'HTMLForm::trySubmit() returned true' ); |
| 462 |
} elseif ( $status === false ) { |
| 463 |
|
| 464 |
} elseif ( $status instanceof Status ) { |
| 465 |
|
| 466 |
} elseif ( $status instanceof StatusValue ) { |
| 467 |
|
| 468 |
|
| 469 |
$status = Status::wrap( $status ); |
| 470 |
} elseif ( is_string( $status ) ) { |
| 471 |
$status = Status::newFatal( new RawMessage( '$1', [ $status ] ) ); |
| 472 |
} elseif ( is_array( $status ) ) { |
| 473 |
if ( is_string( reset( $status ) ) ) { |
| 474 |
|
| 475 |
$status = Status::newFatal( ...$status ); |
| 476 |
} elseif ( is_array( reset( $status ) ) ) { |
| 477 |
$ret = Status::newGood(); |
| 478 |
foreach ( $status as $message ) { |
| 479 |
|
| 480 |
$ret->fatal( ...$message ); |
| 481 |
} |
| 482 |
$status = $ret; |
| 483 |
} else { |
| 484 |
throw new UnexpectedValueException( 'invalid HTMLForm::trySubmit() return value: ' |
| 485 |
. 'first element of array is ' . get_debug_type( reset( $status ) ) ); |
| 486 |
} |
| 487 |
} else { |
| 488 |
|
| 489 |
|
| 490 |
throw new UnexpectedValueException( 'invalid HTMLForm::trySubmit() return type: ' |
| 491 |
. get_debug_type( $status ) ); |
| 492 |
} |
| 493 |
|
| 494 |
if ( ( !$status || !$status->isOK() ) && $this->isReturn ) { |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
LoggerFactory::getInstance( 'authentication' ) |
| 500 |
->warning( 'Validation error on return', [ 'data' => $form->mFieldData, |
| 501 |
'status' => $status->getWikiText( false, false, 'en' ) ] ); |
| 502 |
$status = $this->handleFormSubmit( $form->mFieldData ); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
$changeActions = [ |
| 507 |
AuthManager::ACTION_CHANGE, AuthManager::ACTION_REMOVE, AuthManager::ACTION_UNLINK |
| 508 |
]; |
| 509 |
if ( in_array( $this->authAction, $changeActions, true ) && $status && !$status->isOK() ) { |
| 510 |
$this->getHookRunner()->onChangeAuthenticationDataAudit( reset( $this->authRequests ), $status ); |
| 511 |
} |
| 512 |
|
| 513 |
return $status; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
public function handleFormSubmit( $data ) { |
| 523 |
$requests = AuthenticationRequest::loadRequestsFromSubmission( $this->authRequests, $data ); |
| 524 |
$response = $this->performAuthenticationStep( $this->authAction, $requests ); |
| 525 |
|
| 526 |
|
| 527 |
return Status::newGood( $response ); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
protected function getPreservedParams( $options = [] ) { |
| 551 |
if ( is_bool( $options ) ) { |
| 552 |
wfDeprecated( __METHOD__ . ' boolean $options', '1.43' ); |
| 553 |
$options = [ 'withToken' => $options ]; |
| 554 |
} |
| 555 |
$options += [ |
| 556 |
'reset' => false, |
| 557 |
'withToken' => false, |
| 558 |
]; |
| 559 |
|
| 560 |
'@phan-var array{reset: bool, withToken: bool} $options'; |
| 561 |
$params = []; |
| 562 |
$request = $this->getRequest(); |
| 563 |
|
| 564 |
$params += [ |
| 565 |
'uselang' => $request->getVal( 'uselang' ), |
| 566 |
'variant' => $request->getVal( 'variant' ), |
| 567 |
'returnto' => $request->getVal( 'returnto' ), |
| 568 |
'returntoquery' => $request->getVal( 'returntoquery' ), |
| 569 |
'returntoanchor' => $request->getVal( 'returntoanchor' ), |
| 570 |
]; |
| 571 |
|
| 572 |
if ( !$options['reset'] && $this->authAction !== $this->getDefaultAction( $this->subPage ) ) { |
| 573 |
$params['authAction'] = $this->getContinueAction( $this->authAction ); |
| 574 |
} |
| 575 |
|
| 576 |
if ( $options['withToken'] ) { |
| 577 |
$params[$this->getTokenName()] = $this->getToken()->toString(); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
$this->getHookRunner()->onAuthPreserveQueryParams( |
| 583 |
$params, [ 'reset' => $options['reset'] ] |
| 584 |
); |
| 585 |
|
| 586 |
return array_filter( $params, fn ( $val ) => $val !== null ); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
protected function getAuthFormDescriptor( $requests, $action ) { |
| 597 |
$fieldInfo = AuthenticationRequest::mergeFieldInfo( $requests ); |
| 598 |
$formDescriptor = $this->fieldInfoToFormDescriptor( $requests, $fieldInfo, $action ); |
| 599 |
|
| 600 |
$this->addTabIndex( $formDescriptor ); |
| 601 |
|
| 602 |
return $formDescriptor; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
protected function getAuthForm( array $requests, $action ) { |
| 612 |
$formDescriptor = $this->getAuthFormDescriptor( $requests, $action ); |
| 613 |
$context = $this->getContext(); |
| 614 |
if ( $context->getRequest() !== $this->getRequest() ) { |
| 615 |
|
| 616 |
$context = new DerivativeContext( $this->getContext() ); |
| 617 |
$context->setRequest( $this->getRequest() ); |
| 618 |
} |
| 619 |
$form = HTMLForm::factory( 'ooui', $formDescriptor, $context ); |
| 620 |
$form->setAction( $this->getFullTitle()->getFullURL( $this->getPreservedParams() ) ); |
| 621 |
$form->addHiddenField( $this->getTokenName(), $this->getToken()->toString() ); |
| 622 |
$form->addHiddenField( 'authAction', $this->authAction ); |
| 623 |
$form->suppressDefaultSubmit( !$this->needsSubmitButton( $requests ) ); |
| 624 |
|
| 625 |
return $form; |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
protected function displayForm( $status ) { |
| 633 |
if ( $status instanceof StatusValue ) { |
| 634 |
$status = Status::wrap( $status ); |
| 635 |
} |
| 636 |
$form = $this->getAuthForm( $this->authRequests, $this->authAction ); |
| 637 |
$form->prepareForm()->displayForm( $status ); |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
protected function needsSubmitButton( array $requests ) { |
| 652 |
$customSubmitButtonPresent = false; |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
foreach ( $requests as $req ) { |
| 659 |
if ( $req->required === AuthenticationRequest::PRIMARY_REQUIRED ) { |
| 660 |
if ( $this->hasOwnSubmitButton( $req ) ) { |
| 661 |
$customSubmitButtonPresent = true; |
| 662 |
} else { |
| 663 |
return true; |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
return !$customSubmitButtonPresent; |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
protected function hasOwnSubmitButton( AuthenticationRequest $req ) { |
| 676 |
foreach ( $req->getFieldInfo() as $info ) { |
| 677 |
if ( $info['type'] === 'button' ) { |
| 678 |
return true; |
| 679 |
} |
| 680 |
} |
| 681 |
return false; |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
protected function addTabIndex( &$formDescriptor ) { |
| 690 |
$i = 1; |
| 691 |
foreach ( $formDescriptor as &$definition ) { |
| 692 |
$class = false; |
| 693 |
if ( array_key_exists( 'class', $definition ) ) { |
| 694 |
$class = $definition['class']; |
| 695 |
} elseif ( array_key_exists( 'type', $definition ) ) { |
| 696 |
$class = HTMLForm::$typeMappings[$definition['type']]; |
| 697 |
} |
| 698 |
if ( $class !== HTMLInfoField::class ) { |
| 699 |
$definition['tabindex'] = $i; |
| 700 |
$i++; |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
protected function getToken() { |
| 711 |
return $this->getRequest()->getSession()->getToken( 'AuthManagerSpecialPage:' |
| 712 |
. $this->getName() ); |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
protected function getTokenName() { |
| 721 |
return 'wpAuthToken'; |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
protected function fieldInfoToFormDescriptor( array $requests, array $fieldInfo, $action ) { |
| 734 |
$formDescriptor = []; |
| 735 |
foreach ( $fieldInfo as $fieldName => $singleFieldInfo ) { |
| 736 |
$formDescriptor[$fieldName] = self::mapSingleFieldInfo( $singleFieldInfo, $fieldName ); |
| 737 |
} |
| 738 |
|
| 739 |
$requestSnapshot = serialize( $requests ); |
| 740 |
$this->onAuthChangeFormFields( $requests, $fieldInfo, $formDescriptor, $action ); |
| 741 |
$this->getHookRunner()->onAuthChangeFormFields( $requests, $fieldInfo, |
| 742 |
$formDescriptor, $action ); |
| 743 |
if ( $requestSnapshot !== serialize( $requests ) ) { |
| 744 |
LoggerFactory::getInstance( 'authentication' )->warning( |
| 745 |
'AuthChangeFormFields hook changed auth requests' ); |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
self::sortFormDescriptorFields( $formDescriptor ); |
| 751 |
|
| 752 |
return $formDescriptor; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
protected static function mapSingleFieldInfo( $singleFieldInfo, $fieldName ) { |
| 763 |
$type = self::mapFieldInfoTypeToFormDescriptorType( $singleFieldInfo['type'] ); |
| 764 |
$descriptor = [ |
| 765 |
'type' => $type, |
| 766 |
|
| 767 |
'name' => $fieldName, |
| 768 |
]; |
| 769 |
|
| 770 |
if ( $type === 'submit' && isset( $singleFieldInfo['label'] ) ) { |
| 771 |
$descriptor['default'] = $singleFieldInfo['label']->plain(); |
| 772 |
} elseif ( $type !== 'submit' ) { |
| 773 |
$descriptor += array_filter( [ |
| 774 |
|
| 775 |
'label-message' => self::getField( $singleFieldInfo, 'label' ), |
| 776 |
] ); |
| 777 |
|
| 778 |
if ( isset( $singleFieldInfo['options'] ) ) { |
| 779 |
$descriptor['options'] = array_flip( array_map( static function ( $message ) { |
| 780 |
|
| 781 |
return $message->parse(); |
| 782 |
}, $singleFieldInfo['options'] ) ); |
| 783 |
} |
| 784 |
|
| 785 |
if ( isset( $singleFieldInfo['value'] ) ) { |
| 786 |
$descriptor['default'] = $singleFieldInfo['value']; |
| 787 |
} |
| 788 |
|
| 789 |
if ( empty( $singleFieldInfo['optional'] ) ) { |
| 790 |
$descriptor['required'] = true; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
return $descriptor; |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
protected static function sortFormDescriptorFields( array &$formDescriptor ) { |
| 804 |
$i = 0; |
| 805 |
foreach ( $formDescriptor as &$field ) { |
| 806 |
$field['__index'] = $i++; |
| 807 |
} |
| 808 |
unset( $field ); |
| 809 |
uasort( $formDescriptor, static function ( $first, $second ) { |
| 810 |
return self::getField( $first, 'weight', 0 ) <=> self::getField( $second, 'weight', 0 ) |
| 811 |
?: $first['__index'] <=> $second['__index']; |
| 812 |
} ); |
| 813 |
foreach ( $formDescriptor as &$field ) { |
| 814 |
unset( $field['__index'] ); |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
protected static function getField( array $array, $fieldName, $default = null ) { |
| 826 |
if ( array_key_exists( $fieldName, $array ) ) { |
| 827 |
return $array[$fieldName]; |
| 828 |
} else { |
| 829 |
return $default; |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
protected static function mapFieldInfoTypeToFormDescriptorType( $type ) { |
| 841 |
$map = [ |
| 842 |
'string' => 'text', |
| 843 |
'password' => 'password', |
| 844 |
'select' => 'select', |
| 845 |
'checkbox' => 'check', |
| 846 |
'multiselect' => 'multiselect', |
| 847 |
'button' => 'submit', |
| 848 |
'hidden' => 'hidden', |
| 849 |
'null' => 'info', |
| 850 |
]; |
| 851 |
if ( !array_key_exists( $type, $map ) ) { |
| 852 |
throw new InvalidArgumentException( 'invalid field type: ' . $type ); |
| 853 |
} |
| 854 |
return $map[$type]; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
protected static function mergeDefaultFormDescriptor( |
| 870 |
array $fieldInfo, array $formDescriptor, array $defaultFormDescriptor |
| 871 |
) { |
| 872 |
|
| 873 |
foreach ( $defaultFormDescriptor as $fieldName => $defaultField ) { |
| 874 |
|
| 875 |
|
| 876 |
if ( |
| 877 |
!isset( $fieldInfo[$fieldName] ) |
| 878 |
&& ( |
| 879 |
!isset( $defaultField['baseField'] ) |
| 880 |
|| !isset( $fieldInfo[$defaultField['baseField']] ) |
| 881 |
) |
| 882 |
&& ( |
| 883 |
!isset( $defaultField['type'] ) |
| 884 |
|| !in_array( $defaultField['type'], [ 'submit', 'info' ], true ) |
| 885 |
) |
| 886 |
) { |
| 887 |
$defaultFormDescriptor[$fieldName] = null; |
| 888 |
continue; |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
$requestField = $formDescriptor[$fieldName] ?? []; |
| 893 |
if ( |
| 894 |
isset( $defaultField['label'] ) |
| 895 |
|| isset( $defaultField['label-message'] ) |
| 896 |
|| isset( $defaultField['label-raw'] ) |
| 897 |
) { |
| 898 |
unset( $requestField['label'], $requestField['label-message'], $defaultField['label-raw'] ); |
| 899 |
} |
| 900 |
|
| 901 |
$defaultFormDescriptor[$fieldName] += $requestField; |
| 902 |
} |
| 903 |
|
| 904 |
return array_filter( $defaultFormDescriptor + $formDescriptor ); |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
class_alias( AuthManagerSpecialPage::class, 'AuthManagerSpecialPage' ); |