MediaWiki master
AuthManagerSpecialPage.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
6use LogicException;
27use StatusValue;
28use UnexpectedValueException;
29
40abstract class AuthManagerSpecialPage extends SpecialPage {
44 protected static $allowedActions = [
45 AuthManager::ACTION_LOGIN, AuthManager::ACTION_LOGIN_CONTINUE,
46 AuthManager::ACTION_CREATE, AuthManager::ACTION_CREATE_CONTINUE,
47 AuthManager::ACTION_LINK, AuthManager::ACTION_LINK_CONTINUE,
48 AuthManager::ACTION_CHANGE, AuthManager::ACTION_REMOVE, AuthManager::ACTION_UNLINK,
49 ];
50
52 protected static $messages = [];
53
55 protected $authAction;
56
58 protected $authRequests;
59
61 protected $subPage;
62
64 protected $isReturn;
65
67 protected $savedRequest;
68
70 protected $isFakePostRequest = false;
71
84 public function onAuthChangeFormFields(
85 array $requests, array $fieldInfo, array &$formDescriptor, $action
86 ) {
87 }
88
93 protected function getLoginSecurityLevel() {
94 return $this->getName();
95 }
96
98 public function getRequest() {
99 return $this->savedRequest ?: $this->getContext()->getRequest();
100 }
101
112 protected function setRequest( array $data, $wasPosted = null ) {
113 $request = $this->getContext()->getRequest();
114 $this->isFakePostRequest = $wasPosted === true && $request->wasPosted() === false;
115 $this->savedRequest = new DerivativeRequest(
116 $request,
117 $data + $request->getQueryValues(),
118 $wasPosted ?? $request->wasPosted()
119 );
120 }
121
123 protected function beforeExecute( $subPage ) {
124 $this->getOutput()->disallowUserJs();
125
126 return $this->handleReturnBeforeExecute( $subPage )
128 }
129
146 protected function handleReturnBeforeExecute( $subPage ) {
147 $authManager = $this->getAuthManager();
148 $key = 'AuthManagerSpecialPage:return:' . $this->getName();
149
150 if ( $subPage === 'return' ) {
151 $this->loadAuth( $subPage );
152 $preservedParams = $this->getPreservedParams();
153
154 // FIXME save POST values only from request
155 $authData = array_diff_key( $this->getRequest()->getValues(),
156 $preservedParams, [ 'title' => 1 ] );
157 $uniqueId = MWCryptRand::generateHex( 6 );
158 $preservedParams['authUniqueId'] = $uniqueId;
159 $key .= ':' . $uniqueId;
160 $authManager->setAuthenticationSessionData( $key, $authData );
161
162 $url = $this->getPageTitle()->getFullURL( $preservedParams, false, PROTO_HTTPS );
163 $this->getOutput()->redirect( $url );
164 return false;
165 } elseif ( $this->getRequest()->getCheck( 'authUniqueId' ) ) {
166 $uniqueId = $this->getRequest()->getVal( 'authUniqueId' );
167 $key .= ':' . $uniqueId;
168 $authData = $authManager->getAuthenticationSessionData( $key );
169 if ( $authData ) {
170 $authManager->removeAuthenticationSessionData( $key );
171 $this->isReturn = true;
172 $this->setRequest( $authData, true );
173 $this->setPostTransactionProfilerExpectations( __METHOD__ );
174 }
175 }
176
177 return true;
178 }
179
190 protected function handleReauthBeforeExecute( $subPage ) {
191 $authManager = $this->getAuthManager();
192 $request = $this->getRequest();
193 $key = 'AuthManagerSpecialPage:reauth:' . $this->getName();
194
195 $securityLevel = $this->getLoginSecurityLevel();
196 if ( $securityLevel ) {
197 $securityStatus = $authManager->securitySensitiveOperationStatus( $securityLevel );
198 if ( $securityStatus === AuthManager::SEC_REAUTH ) {
199 $queryParams = array_diff_key( $request->getQueryValues(), [ 'title' => true ] );
200
201 if ( $request->wasPosted() ) {
202 // unique ID in case the same special page is open in multiple browser tabs
203 $uniqueId = MWCryptRand::generateHex( 6 );
204 $key .= ':' . $uniqueId;
205
206 $queryParams = [ 'authUniqueId' => $uniqueId ] + $queryParams;
207 $authData = array_diff_key( $request->getValues(),
208 $this->getPreservedParams(), [ 'title' => 1 ] );
209 $authManager->setAuthenticationSessionData( $key, $authData );
210 }
211
212 // Copied from RedirectSpecialPage::getRedirectQuery()
213 // Would using $this->getPreservedParams() be appropriate here?
214 $keepParams = [ 'uselang', 'useskin', 'useformat', 'variant', 'debug', 'safemode' ];
215
216 $title = SpecialPage::getTitleFor( 'Userlogin' );
217 $url = $title->getFullURL( [
218 'returnto' => $this->getFullTitle()->getPrefixedDBkey(),
219 'returntoquery' => wfArrayToCgi( $queryParams ),
220 'force' => $securityLevel,
221 ] + array_intersect_key( $queryParams, array_fill_keys( $keepParams, true ) ), false, PROTO_HTTPS );
222
223 $this->getOutput()->redirect( $url );
224 return false;
225 }
226
227 if ( $securityStatus !== AuthManager::SEC_OK ) {
228 throw new ErrorPageError( 'cannotauth-not-allowed-title', 'cannotauth-not-allowed' );
229 }
230
231 $this->getAuthManager()->securitySensitiveOperationDone( $securityLevel );
232 }
233
234 $uniqueId = $request->getVal( 'authUniqueId' );
235 if ( $uniqueId ) {
236 $key .= ':' . $uniqueId;
237 $authData = $authManager->getAuthenticationSessionData( $key );
238 if ( $authData ) {
239 $authManager->removeAuthenticationSessionData( $key );
240 $this->setRequest( $authData, true );
241 $this->setPostTransactionProfilerExpectations( __METHOD__ );
242 }
243 }
244
245 return true;
246 }
247
248 private function setPostTransactionProfilerExpectations( string $fname ) {
249 $trxLimits = $this->getConfig()->get( MainConfigNames::TrxProfilerLimits );
250 $trxProfiler = Profiler::instance()->getTransactionProfiler();
251 $trxProfiler->redefineExpectations( $trxLimits['POST'], $fname );
252 DeferredUpdates::addCallableUpdate( static function () use ( $trxProfiler, $trxLimits, $fname ) {
253 $trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname );
254 } );
255 }
256
264 abstract protected function getDefaultAction( $subPage );
265
272 protected function messageKey( $defaultKey ) {
273 return array_key_exists( $defaultKey, static::$messages )
274 ? static::$messages[$defaultKey] : $defaultKey;
275 }
276
282 protected function getRequestBlacklist() {
283 return [];
284 }
285
298 protected function getAuthenticationRequests( $action, ?UserIdentity $user = null ) {
299 return $this->getAuthManager()->getAuthenticationRequests( $action,
300 $user );
301 }
302
313 protected function loadAuth( $subPage, $authAction = null, $reset = false ) {
314 // Do not load if already loaded, to cut down on the number of getAuthenticationRequests
315 // calls. This is important for requests which have hidden information, so any
316 // getAuthenticationRequests call would mean putting data into some cache.
317 if (
318 !$reset && $this->subPage === $subPage && $this->authAction
319 && ( !$authAction || $authAction === $this->authAction )
320 ) {
321 return;
322 }
323
324 $request = $this->getRequest();
325 $this->subPage = $subPage;
326 $this->authAction = $authAction ?: $request->getText( 'authAction' );
327 if ( !in_array( $this->authAction, static::$allowedActions, true ) ) {
328 $this->authAction = $this->getDefaultAction( $subPage );
329 if ( $request->wasPosted() ) {
330 $continueAction = $this->getContinueAction( $this->authAction );
331 if ( in_array( $continueAction, static::$allowedActions, true ) ) {
332 $this->authAction = $continueAction;
333 }
334 }
335 }
336
337 $allReqs = $this->getAuthenticationRequests( $this->authAction, $this->getUser() );
338 $this->authRequests = array_filter( $allReqs, function ( $req ) {
339 return !in_array( get_class( $req ), $this->getRequestBlacklist(), true );
340 } );
341 }
342
347 protected function isContinued() {
348 return in_array( $this->authAction, [
349 AuthManager::ACTION_LOGIN_CONTINUE,
350 AuthManager::ACTION_CREATE_CONTINUE,
351 AuthManager::ACTION_LINK_CONTINUE,
352 ], true );
353 }
354
360 protected function getContinueAction( $action ) {
361 switch ( $action ) {
362 case AuthManager::ACTION_LOGIN:
363 $action = AuthManager::ACTION_LOGIN_CONTINUE;
364 break;
365 case AuthManager::ACTION_CREATE:
366 $action = AuthManager::ACTION_CREATE_CONTINUE;
367 break;
368 case AuthManager::ACTION_LINK:
369 $action = AuthManager::ACTION_LINK_CONTINUE;
370 break;
371 }
372 return $action;
373 }
374
382 protected function isActionAllowed( $action ) {
383 $authManager = $this->getAuthManager();
384 if ( !in_array( $action, static::$allowedActions, true ) ) {
385 throw new InvalidArgumentException( 'invalid action: ' . $action );
386 }
387
388 // calling getAuthenticationRequests can be expensive, avoid if possible
389 $requests = ( $action === $this->authAction ) ? $this->authRequests
390 : $this->getAuthenticationRequests( $action );
391 if ( !$requests ) {
392 // no provider supports this action in the current state
393 return false;
394 }
395
396 switch ( $action ) {
397 case AuthManager::ACTION_LOGIN:
398 case AuthManager::ACTION_LOGIN_CONTINUE:
399 return $authManager->canAuthenticateNow();
400 case AuthManager::ACTION_CREATE:
401 case AuthManager::ACTION_CREATE_CONTINUE:
402 return $authManager->canCreateAccounts();
403 case AuthManager::ACTION_LINK:
404 case AuthManager::ACTION_LINK_CONTINUE:
405 return $authManager->canLinkAccounts();
406 case AuthManager::ACTION_CHANGE:
407 case AuthManager::ACTION_REMOVE:
408 case AuthManager::ACTION_UNLINK:
409 return true;
410 default:
411 // should never reach here but makes static code analyzers happy
412 throw new InvalidArgumentException( 'invalid action: ' . $action );
413 }
414 }
415
422 protected function performAuthenticationStep( $action, array $requests ) {
423 if ( !in_array( $action, static::$allowedActions, true ) ) {
424 throw new InvalidArgumentException( 'invalid action: ' . $action );
425 }
426
427 $authManager = $this->getAuthManager();
428 $returnToUrl = $this->getPageTitle( 'return' )
429 ->getFullURL( $this->getPreservedParams( [ 'withToken' => true ] ), false, PROTO_HTTPS );
430
431 switch ( $action ) {
432 case AuthManager::ACTION_LOGIN:
433 return $authManager->beginAuthentication( $requests, $returnToUrl );
434 case AuthManager::ACTION_LOGIN_CONTINUE:
435 return $authManager->continueAuthentication( $requests );
436 case AuthManager::ACTION_CREATE:
437 return $authManager->beginAccountCreation( $this->getAuthority(), $requests,
438 $returnToUrl );
439 case AuthManager::ACTION_CREATE_CONTINUE:
440 return $authManager->continueAccountCreation( $requests );
441 case AuthManager::ACTION_LINK:
442 return $authManager->beginAccountLink( $this->getUser(), $requests, $returnToUrl );
443 case AuthManager::ACTION_LINK_CONTINUE:
444 return $authManager->continueAccountLink( $requests );
445 case AuthManager::ACTION_CHANGE:
446 case AuthManager::ACTION_REMOVE:
447 case AuthManager::ACTION_UNLINK:
448 if ( count( $requests ) > 1 ) {
449 throw new InvalidArgumentException( 'only one auth request can be changed at a time' );
450 }
451
452 if ( !$requests ) {
453 throw new InvalidArgumentException( 'no auth request' );
454 }
455 $req = reset( $requests );
456 $status = $authManager->allowsAuthenticationDataChange( $req );
457 $this->getHookRunner()->onChangeAuthenticationDataAudit( $req, $status );
458 if ( !$status->isGood() ) {
459 return AuthenticationResponse::newFail( $status->getMessage() );
460 }
461 $authManager->changeAuthenticationData( $req );
462 return AuthenticationResponse::newPass();
463 default:
464 // should never reach here but makes static code analyzers happy
465 throw new InvalidArgumentException( 'invalid action: ' . $action );
466 }
467 }
468
479 protected function trySubmit() {
480 $status = false;
481
482 $form = $this->getAuthForm( $this->authRequests, $this->authAction );
483 $form->setSubmitCallback( $this->handleFormSubmit( ... ) );
484
485 if ( $this->getRequest()->wasPosted() ) {
486 // handle tokens manually; $form->tryAuthorizedSubmit only works for logged-in users
487 $requestTokenValue = $this->getRequest()->getVal( $this->getTokenName() );
488 $sessionToken = $this->getToken();
489 if ( $sessionToken->wasNew() && ( !$this->isFakePostRequest || $this->isReturn ) ) {
490 // TODO: This should use `$this->getRequest()->getSession()->getProvider()->whyNoSession()`.
491 return Status::newFatal( $this->messageKey( 'authform-newtoken' ) );
492 } elseif ( !$requestTokenValue ) {
493 return Status::newFatal( $this->messageKey( 'authform-notoken' ) );
494 } elseif ( !$sessionToken->match( $requestTokenValue ) ) {
495 return Status::newFatal( $this->messageKey( 'authform-wrongtoken' ) );
496 }
497
498 $form->prepareForm();
499 $status = $form->trySubmit();
500
501 // HTMLForm submit return values are a mess; let's ensure it is false or a Status
502 // FIXME this probably should be in HTMLForm
503 if ( $status === true ) {
504 // not supposed to happen since our submit handler should always return a Status
505 throw new UnexpectedValueException( 'HTMLForm::trySubmit() returned true' );
506 } elseif ( $status === false ) {
507 // form was not submitted; nothing to do
508 } elseif ( $status instanceof Status ) {
509 // already handled by the form; nothing to do
510 } elseif ( $status instanceof StatusValue ) {
511 // in theory not an allowed return type but nothing stops the submit handler from
512 // accidentally returning it so best check and fix
513 $status = Status::wrap( $status );
514 } elseif ( is_string( $status ) ) {
515 $status = Status::newFatal( new RawMessage( '$1', [ $status ] ) );
516 } elseif ( is_array( $status ) ) {
517 if ( is_string( reset( $status ) ) ) {
518 // @phan-suppress-next-line PhanParamTooFewUnpack
519 $status = Status::newFatal( ...$status );
520 } elseif ( is_array( reset( $status ) ) ) {
521 $ret = Status::newGood();
522 foreach ( $status as $message ) {
523 // @phan-suppress-next-line PhanParamTooFewUnpack
524 $ret->fatal( ...$message );
525 }
526 $status = $ret;
527 } else {
528 throw new UnexpectedValueException( 'invalid HTMLForm::trySubmit() return value: '
529 . 'first element of array is ' . get_debug_type( reset( $status ) ) );
530 }
531 } else {
532 // not supposed to happen, but HTMLForm does not verify the return type
533 // from the submit callback; better safe then sorry!
534 throw new UnexpectedValueException( 'invalid HTMLForm::trySubmit() return type: '
535 . get_debug_type( $status ) );
536 }
537
538 if ( ( !$status || !$status->isOK() ) && $this->isReturn ) {
539 // This is awkward. There was a form validation error, which means the data was not
540 // passed to AuthManager. Normally we would display the form with an error message,
541 // but for the data we received via the redirect flow that would not be helpful at all.
542 // Let's just submit the data to AuthManager directly instead.
543 LoggerFactory::getInstance( 'authentication' )
544 ->warning( 'Validation error on return', [ 'data' => $form->mFieldData,
545 'status' => $status->getWikiText( false, false, 'en' ) ] );
546 $status = $this->handleFormSubmit( $form->mFieldData );
547 }
548 }
549
550 $changeActions = [
551 AuthManager::ACTION_CHANGE, AuthManager::ACTION_REMOVE, AuthManager::ACTION_UNLINK
552 ];
553 if ( in_array( $this->authAction, $changeActions, true ) && $status && !$status->isOK() ) {
554 $this->getHookRunner()->onChangeAuthenticationDataAudit( reset( $this->authRequests ), $status );
555 }
556
557 return $status;
558 }
559
566 public function handleFormSubmit( $data ) {
567 $requests = AuthenticationRequest::loadRequestsFromSubmission( $this->authRequests, $data );
568 $response = $this->performAuthenticationStep( $this->authAction, $requests );
569
570 // we can't handle FAIL or similar as failure here since it might require changing the form
571 return Status::newGood( $response );
572 }
573
595 protected function getPreservedParams( $options = [] ) {
596 if ( is_bool( $options ) ) {
597 wfDeprecated( __METHOD__ . ' boolean $options', '1.43' );
598 $options = [ 'withToken' => $options ];
599 }
600 $options += [
601 'reset' => false,
602 'withToken' => false,
603 ];
604
605 $params = $options['params'] ?? [];
606 if ( !$options['reset'] && $this->authAction !== $this->getDefaultAction( $this->subPage ) ) {
607 $params['authAction'] = $this->getContinueAction( $this->authAction );
608 }
609 if ( $options['withToken'] ) {
610 $params[$this->getTokenName()] = $this->getToken()->toString();
611 }
612
613 $loginHelper = new LoginHelper( $this->getContext() );
614 return $loginHelper->getPreservedParams( [ 'reset' => $options['reset'], 'params' => $params ] );
615 }
616
624 protected function getAuthFormDescriptor( $requests, $action ) {
625 $fieldInfo = AuthenticationRequest::mergeFieldInfo( $requests );
626 $formDescriptor = $this->fieldInfoToFormDescriptor( $requests, $fieldInfo, $action );
627
628 $this->addTabIndex( $formDescriptor );
629
630 return $formDescriptor;
631 }
632
639 protected function getAuthForm( array $requests, $action ) {
640 $formDescriptor = $this->getAuthFormDescriptor( $requests, $action );
641 $context = $this->getContext();
642 if ( $context->getRequest() !== $this->getRequest() ) {
643 // We have overridden the request, need to make sure the form uses that too.
644 $context = new DerivativeContext( $this->getContext() );
645 $context->setRequest( $this->getRequest() );
646 }
647 $form = HTMLForm::factory( 'ooui', $formDescriptor, $context );
648 $form->setAction( $this->getFullTitle()->getFullURL( $this->getPreservedParams() ) );
649 $form->addHiddenField( $this->getTokenName(), $this->getToken()->toString() );
650 $form->addHiddenField( 'authAction', $this->authAction );
651 $form->suppressDefaultSubmit( !$this->needsSubmitButton( $requests ) );
652
653 return $form;
654 }
655
660 protected function displayForm( $status ) {
661 if ( $status instanceof StatusValue ) {
662 $status = Status::wrap( $status );
663 }
664 $form = $this->getAuthForm( $this->authRequests, $this->authAction );
665 $form->prepareForm()->displayForm( $status );
666 }
667
679 protected function needsSubmitButton( array $requests ) {
680 $customSubmitButtonPresent = false;
681
682 // Secondary and preauth providers always need their data; they will not care what button
683 // is used, so they can be ignored. So can OPTIONAL buttons createdby primary providers;
684 // that's the point in being optional. Se we need to check whether all primary providers
685 // have their own buttons and whether there is at least one button present.
686 foreach ( $requests as $req ) {
687 if ( $req->required === AuthenticationRequest::PRIMARY_REQUIRED ) {
688 if ( $this->hasOwnSubmitButton( $req ) ) {
689 $customSubmitButtonPresent = true;
690 } else {
691 return true;
692 }
693 }
694 }
695 return !$customSubmitButtonPresent;
696 }
697
703 protected function hasOwnSubmitButton( AuthenticationRequest $req ) {
704 foreach ( $req->getFieldInfo() as $info ) {
705 if ( $info['type'] === 'button' ) {
706 return true;
707 }
708 }
709 return false;
710 }
711
717 protected function addTabIndex( &$formDescriptor ) {
718 $i = 1;
719 foreach ( $formDescriptor as &$definition ) {
720 $class = false;
721 if ( array_key_exists( 'class', $definition ) ) {
722 $class = $definition['class'];
723 } elseif ( array_key_exists( 'type', $definition ) ) {
724 $class = HTMLForm::$typeMappings[$definition['type']];
725 }
726 if ( $class !== HTMLInfoField::class ) {
727 $definition['tabindex'] = $i;
728 $i++;
729 }
730 }
731 }
732
738 protected function getToken() {
739 return $this->getRequest()->getSession()->getToken( 'AuthManagerSpecialPage:'
740 . $this->getName() );
741 }
742
748 protected function getTokenName() {
749 return 'wpAuthToken';
750 }
751
761 protected function fieldInfoToFormDescriptor( array $requests, array $fieldInfo, $action ) {
762 $formDescriptor = [];
763 foreach ( $fieldInfo as $fieldName => $singleFieldInfo ) {
764 $formDescriptor[$fieldName] = self::mapSingleFieldInfo( $singleFieldInfo, $fieldName );
765 }
766
767 $requestSnapshot = serialize( $requests );
768 $this->onAuthChangeFormFields( $requests, $fieldInfo, $formDescriptor, $action );
769 $this->getHookRunner()->onAuthChangeFormFields( $requests, $fieldInfo,
770 $formDescriptor, $action );
771 if ( $requestSnapshot !== serialize( $requests ) ) {
772 LoggerFactory::getInstance( 'authentication' )->warning(
773 'AuthChangeFormFields hook changed auth requests' );
774 }
775
776 // Process the special 'weight' property, which is a way for AuthChangeFormFields hook
777 // subscribers (who only see one field at a time) to influence ordering.
778 self::sortFormDescriptorFields( $formDescriptor );
779
780 return $formDescriptor;
781 }
782
790 protected static function mapSingleFieldInfo( $singleFieldInfo, $fieldName ) {
791 $type = self::mapFieldInfoTypeToFormDescriptorType( $singleFieldInfo['type'] );
792 $descriptor = [
793 'type' => $type,
794 // Do not prefix input name with 'wp'. This is important for the redirect flow.
795 'name' => $fieldName,
796 ];
797
798 if ( $type === 'submit' && isset( $singleFieldInfo['label'] ) ) {
799 $descriptor['default'] = $singleFieldInfo['label']->plain();
800 } elseif ( $type !== 'submit' ) {
801 $descriptor += array_filter( [
802 // help-message is omitted as it is usually not really useful for a web interface
803 'label-message' => self::getField( $singleFieldInfo, 'label' ),
804 ] );
805
806 if ( isset( $singleFieldInfo['options'] ) ) {
807 $descriptor['options'] = array_flip( array_map( static function ( $message ) {
809 return $message->parse();
810 }, $singleFieldInfo['options'] ) );
811 }
812
813 if ( isset( $singleFieldInfo['value'] ) ) {
814 $descriptor['default'] = $singleFieldInfo['value'];
815 }
816
817 if ( empty( $singleFieldInfo['optional'] ) ) {
818 $descriptor['required'] = true;
819 }
820 }
821
822 return $descriptor;
823 }
824
830 protected static function sortFormDescriptorFields( array &$formDescriptor ) {
831 $i = 0;
832 foreach ( $formDescriptor as &$field ) {
833 $field['__index'] = $i++;
834 }
835 unset( $field );
836 uasort( $formDescriptor, static function ( $first, $second ) {
837 return self::getField( $first, 'weight', 0 ) <=> self::getField( $second, 'weight', 0 )
838 ?: $first['__index'] <=> $second['__index'];
839 } );
840 foreach ( $formDescriptor as &$field ) {
841 unset( $field['__index'] );
842 }
843 }
844
852 protected static function getField( array $array, $fieldName, $default = null ) {
853 if ( array_key_exists( $fieldName, $array ) ) {
854 return $array[$fieldName];
855 } else {
856 return $default;
857 }
858 }
859
867 protected static function mapFieldInfoTypeToFormDescriptorType( $type ) {
868 $map = [
869 'string' => 'text',
870 'password' => 'password',
871 'select' => 'select',
872 'checkbox' => 'check',
873 'multiselect' => 'multiselect',
874 'button' => 'submit',
875 'hidden' => 'hidden',
876 'null' => 'info',
877 ];
878 if ( !array_key_exists( $type, $map ) ) {
879 throw new InvalidArgumentException( 'invalid field type: ' . $type );
880 }
881 return $map[$type];
882 }
883
896 protected static function mergeDefaultFormDescriptor(
897 array $fieldInfo, array $formDescriptor, array $defaultFormDescriptor
898 ) {
899 // keep the ordering from $defaultFormDescriptor where there is no explicit weight
900 foreach ( $defaultFormDescriptor as $fieldName => $defaultField ) {
901 // remove everything that is not in the fieldinfo, is not marked as a supplemental field
902 // to something in the fieldinfo, and is not an info field or a submit button
903 if (
904 !isset( $fieldInfo[$fieldName] )
905 && (
906 !isset( $defaultField['baseField'] )
907 || !isset( $fieldInfo[$defaultField['baseField']] )
908 )
909 && (
910 !isset( $defaultField['type'] )
911 || !in_array( $defaultField['type'], [ 'submit', 'info' ], true )
912 )
913 ) {
914 $defaultFormDescriptor[$fieldName] = null;
915 continue;
916 }
917
918 // default message labels should always take priority
919 $requestField = $formDescriptor[$fieldName] ?? [];
920 if (
921 isset( $defaultField['label'] )
922 || isset( $defaultField['label-message'] )
923 || isset( $defaultField['label-raw'] )
924 ) {
925 unset( $requestField['label'], $requestField['label-message'], $defaultField['label-raw'] );
926 }
927
928 $defaultFormDescriptor[$fieldName] += $requestField;
929 }
930
931 return array_filter( $defaultFormDescriptor + $formDescriptor );
932 }
933}
934
935// @codeCoverageIgnoreStart
937class_alias( AuthManagerSpecialPage::class, 'AuthManagerSpecialPage' );
938// @codeCoverageIgnoreEnd
const PROTO_HTTPS
Definition Defines.php:218
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
AuthManager is the authentication system in MediaWiki and serves entry point for authentication.
This is a value object for authentication requests.
getFieldInfo()
Fetch input field info.
This is a value object to hold authentication response data.
An IContextSource implementation which will inherit context from another source but allow individual ...
Defer callable updates to run later in the PHP process.
An error page which can definitely be safely rendered using the OutputPage.
An information field (text blob), not a proper input.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:214
Variant of the Message class.
Create PSR-3 logger objects.
A class containing constants representing the names of configuration variables.
const TrxProfilerLimits
Name constant for the TrxProfilerLimits setting, for use with Config\get().
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
Profiler base class that defines the interface and some shared functionality.
Definition Profiler.php:26
Similar to MediaWiki\Request\FauxRequest, but only fakes URL parameters and method (POST or GET) and ...
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
Value object representing a CSRF token.
Definition Token.php:19
A special page subclass for authentication-related special pages.
bool $isReturn
True if the current request is a result of returning from a redirect flow.
handleReturnBeforeExecute( $subPage)
Handle redirection from the /return subpage.
static mapFieldInfoTypeToFormDescriptorType( $type)
Maps AuthenticationRequest\getFieldInfo() types to HTMLForm types.
getAuthFormDescriptor( $requests, $action)
Generates a HTMLForm descriptor array from a set of authentication requests.
setRequest(array $data, $wasPosted=null)
Override the POST data, GET data from the real request is preserved.
static getField(array $array, $fieldName, $default=null)
Get an array value, or a default if it does not exist.
getContinueAction( $action)
Gets the _CONTINUE version of an action.
static sortFormDescriptorFields(array &$formDescriptor)
Sort the fields of a form descriptor by their 'weight' property.
onAuthChangeFormFields(array $requests, array $fieldInfo, array &$formDescriptor, $action)
Change the form descriptor that determines how a field will look in the authentication form.
static mapSingleFieldInfo( $singleFieldInfo, $fieldName)
Maps an authentication field configuration for a single field (as returned by AuthenticationRequest\g...
isContinued()
Returns true if this is not the first step of the authentication.
needsSubmitButton(array $requests)
Returns true if the form built from the given AuthenticationRequests needs a submit button.
handleReauthBeforeExecute( $subPage)
Handle redirection when the user needs to (re)authenticate.
string $authAction
one of the AuthManager\ACTION_* constants.
messageKey( $defaultKey)
Return custom message key.
static string[] $allowedActions
The list of actions this special page deals with.
static mergeDefaultFormDescriptor(array $fieldInfo, array $formDescriptor, array $defaultFormDescriptor)
Apply defaults to a form descriptor, without creating non-existent fields.
isActionAllowed( $action)
Checks whether AuthManager is ready to perform the action.
beforeExecute( $subPage)
Gets called before execute.Return false to prevent calling execute() (since 1.27+)....
getRequestBlacklist()
Allows blacklisting certain request types.
trySubmit()
Attempts to do an authentication step with the submitted data.
loadAuth( $subPage, $authAction=null, $reset=false)
Load or initialize $authAction, $authRequests and $subPage.
getTokenName()
Returns the name of the CSRF token (under which it should be found in the POST or GET data).
addTabIndex(&$formDescriptor)
Adds a sequential tabindex starting from 1 to all form elements.
getPreservedParams( $options=[])
Returns URL query parameters which should be preserved between authentication requests.
fieldInfoToFormDescriptor(array $requests, array $fieldInfo, $action)
Turns a field info array into a form descriptor.
handleFormSubmit( $data)
Submit handler callback for HTMLForm.
bool $isFakePostRequest
Set when we're pretending that we got a POST request during redirect flows.
getDefaultAction( $subPage)
Get the default action for this special page if none is given via URL/POST data.
getAuthenticationRequests( $action, ?UserIdentity $user=null)
Get the list of AuthenticationRequests from the AuthManager.
hasOwnSubmitButton(AuthenticationRequest $req)
Checks whether the given AuthenticationRequest has its own submit button.
getRequest()
Get the WebRequest being used for this instance.WebRequest 1.18
WebRequest null $savedRequest
If set, will be used instead of the real request.
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
getUser()
Shortcut to get the User executing this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
getConfig()
Shortcut to get main config object.
getContext()
Gets the context this SpecialPage is executed in.
getOutput()
Get the OutputPage being used for this instance.
getAuthority()
Shortcut to get the Authority executing this instance.
getName()
Get the canonical, unlocalized name of this special page without namespace.
getFullTitle()
Return the full title, including $par.
Helper functions for the login form that need to be shared with other special pages (such as CentralA...
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
A cryptographic random generator class used for generating secret keys.
Interface for objects representing user identity.