MediaWiki  master
ApiClientLogin.php
Go to the documentation of this file.
1 <?php
26 
32 class ApiClientLogin extends ApiBase {
33 
35  private $authManager;
36 
42  public function __construct(
43  ApiMain $main,
44  $action,
46  ) {
47  parent::__construct( $main, $action, 'login' );
48  $this->authManager = $authManager;
49  }
50 
51  public function getFinalDescription() {
52  // A bit of a hack to append 'api-help-authmanager-general-usage'
53  $msgs = parent::getFinalDescription();
54  $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
55  $this->getModulePrefix(),
56  $this->getModuleName(),
57  $this->getModulePath(),
58  AuthManager::ACTION_LOGIN,
59  $this->needsToken(),
60  ] );
61  return $msgs;
62  }
63 
64  public function execute() {
65  $params = $this->extractRequestParams();
66 
67  $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
68 
69  if ( $params['returnurl'] !== null ) {
70  $bits = wfParseUrl( $params['returnurl'] );
71  if ( !$bits || $bits['scheme'] === '' ) {
72  $encParamName = $this->encodeParamName( 'returnurl' );
73  $this->dieWithError(
74  [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
75  "badurl_{$encParamName}"
76  );
77  }
78  }
79 
80  $helper = new ApiAuthManagerHelper( $this, $this->authManager );
81 
82  // Make sure it's possible to log in
83  if ( !$this->authManager->canAuthenticateNow() ) {
84  $this->getResult()->addValue( null, 'clientlogin', $helper->formatAuthenticationResponse(
85  AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LOGIN ) )
86  ) );
87  $helper->logAuthenticationResult( 'login', 'userlogin-cannot-' . AuthManager::ACTION_LOGIN );
88  return;
89  }
90 
91  // Perform the login step
92  if ( $params['continue'] ) {
93  $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN_CONTINUE );
94  $res = $this->authManager->continueAuthentication( $reqs );
95  } else {
96  $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN );
97  if ( $params['preservestate'] ) {
98  $req = $helper->getPreservedRequest();
99  if ( $req ) {
100  $reqs[] = $req;
101  }
102  }
103  $res = $this->authManager->beginAuthentication( $reqs, $params['returnurl'] );
104  }
105 
106  // Remove CreateFromLoginAuthenticationRequest from $res->neededRequests.
107  // It's there so a RESTART treated as UI will work right, but showing
108  // it to the API client is just confusing.
110  $res->neededRequests, [ CreateFromLoginAuthenticationRequest::class ]
111  );
112 
113  $this->getResult()->addValue( null, 'clientlogin',
114  $helper->formatAuthenticationResponse( $res ) );
115  $helper->logAuthenticationResult( 'login', $res );
116  }
117 
118  public function isReadMode() {
119  return false;
120  }
121 
122  public function isWriteMode() {
123  // (T283394) Logging in triggers some database writes, so should be marked appropriately.
124  return true;
125  }
126 
127  public function needsToken() {
128  return 'login';
129  }
130 
131  public function getAllowedParams() {
132  return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LOGIN,
133  'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
134  );
135  }
136 
137  public function dynamicParameterDocumentation() {
138  return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LOGIN ];
139  }
140 
141  protected function getExamplesMessages() {
142  return [
143  'action=clientlogin&username=Example&password=ExamplePassword&'
144  . 'loginreturnurl=http://example.org/&logintoken=123ABC'
145  => 'apihelp-clientlogin-example-login',
146  'action=clientlogin&logincontinue=1&OATHToken=987654&logintoken=123ABC'
147  => 'apihelp-clientlogin-example-login2',
148  ];
149  }
150 
151  public function getHelpUrls() {
152  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Login';
153  }
154 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:47
ApiAuthManagerHelper\blacklistAuthenticationRequests
static blacklistAuthenticationRequests(array $reqs, array $remove)
Filter out authentication requests by class name.
Definition: ApiAuthManagerHelper.php:131
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1436
ApiClientLogin\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiClientLogin.php:64
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:628
ApiBase\makeMessage
static makeMessage( $msg, IContextSource $context, array $params=null)
Create a Message from a string or array.
Definition: ApiBase.php:1216
ApiAuthManagerHelper\getStandardParams
static getStandardParams( $action,... $wantedParams)
Fetch the standard parameters this helper recognizes.
Definition: ApiAuthManagerHelper.php:363
$res
$res
Definition: testCompression.php:57
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:55
ApiClientLogin\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiClientLogin.php:151
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:776
ApiBase\getModulePath
getModulePath()
Get the path to this module.
Definition: ApiBase.php:572
MediaWiki\Auth\AuthenticationResponse
This is a value object to hold authentication response data.
Definition: AuthenticationResponse.php:37
MediaWiki\Auth\CreateFromLoginAuthenticationRequest
This transfers state between the login and account creation flows.
Definition: CreateFromLoginAuthenticationRequest.php:35
ApiClientLogin\getFinalDescription
getFinalDescription()
Get final module description, after hooks have had a chance to tweak it as needed.
Definition: ApiClientLogin.php:51
ApiAuthManagerHelper
Helper class for AuthManager-using API modules.
Definition: ApiAuthManagerHelper.php:37
ApiClientLogin\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiClientLogin.php:141
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:764
ApiBase\requireAtLeastOneParameter
requireAtLeastOneParameter( $params,... $required)
Die if none of a certain set of parameters is set and not false.
Definition: ApiBase.php:961
ApiClientLogin\dynamicParameterDocumentation
dynamicParameterDocumentation()
Indicate if the module supports dynamically-determined parameters that cannot be included in self::ge...
Definition: ApiClientLogin.php:137
ApiBase\getModulePrefix
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:505
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:742
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1456
MediaWiki\Auth\AuthManager
This serves as the entry point to the authentication system.
Definition: AuthManager.php:102
ApiClientLogin\isReadMode
isReadMode()
Indicates whether this module requires read rights.
Definition: ApiClientLogin.php:118
ApiClientLogin
Log in to the wiki with AuthManager.
Definition: ApiClientLogin.php:32
ApiClientLogin\__construct
__construct(ApiMain $main, $action, AuthManager $authManager)
Definition: ApiClientLogin.php:42
ApiClientLogin\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiClientLogin.php:127
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:497
ApiClientLogin\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiClientLogin.php:131
ApiClientLogin\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiClientLogin.php:122
ApiClientLogin\$authManager
AuthManager $authManager
Definition: ApiClientLogin.php:35