MediaWiki master
ApiClientLogin.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
15
21class ApiClientLogin extends ApiBase {
22
23 public function __construct(
24 ApiMain $main,
25 string $action,
26 private readonly AuthManager $authManager,
27 private readonly UrlUtils $urlUtils,
28 ) {
29 parent::__construct( $main, $action, 'login' );
30 }
31
33 public function getFinalDescription() {
34 // A bit of a hack to append 'api-help-authmanager-general-usage'
35 $msgs = parent::getFinalDescription();
36 $msgs[] = $this->msg( 'api-help-authmanager-general-usage',
37 $this->getModulePrefix(),
38 $this->getModuleName(),
39 $this->getModulePath(),
40 AuthManager::ACTION_LOGIN,
41 $this->needsToken(),
42 );
43 return $msgs;
44 }
45
46 public function execute() {
47 $params = $this->extractRequestParams();
48 $performer = $this->getUser();
49
50 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
51
52 if ( $params['returnurl'] !== null ) {
53 $bits = $this->urlUtils->parse( $params['returnurl'] );
54 if ( !$bits || $bits['scheme'] === '' ) {
55 $encParamName = $this->encodeParamName( 'returnurl' );
56 $this->dieWithError(
57 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
58 "badurl_{$encParamName}"
59 );
60 }
61 }
62 $requestOptions = [];
63 if ( $params['reauthenticate'] !== null ) {
64 $this->requireMaxOneParameter( $params, 'reauthenticate', 'continue' );
65 if ( $this->getUser()->isAnon() ) {
66 $this->dieWithError( 'apierror-mustbeloggedin-reauthenticate' );
67 }
68 $requestOptions['securityLevel'] = $params['reauthenticate'];
69 }
70
71 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
72
73 // Make sure it's possible to log in
74 if ( !$this->authManager->canAuthenticateNow() ) {
75 $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LOGIN ) );
76 $this->getResult()->addValue( null, 'clientlogin',
77 $helper->formatAuthenticationResponse( $res ) );
78 $helper->logAuthenticationResult( 'login', $performer, $res );
79 return;
80 }
81
82 // Perform the login step
83 if ( $params['continue'] ) {
84 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN_CONTINUE );
85 $res = $this->authManager->continueAuthentication( $reqs );
86 } else {
87 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN, $requestOptions );
88 if ( $params['preservestate'] ) {
89 $req = $helper->getPreservedRequest();
90 if ( $req ) {
91 $reqs[] = $req;
92 }
93 }
94 $res = $this->authManager->beginAuthentication( $reqs, $params['returnurl'] );
95 }
96
97 // Remove CreateFromLoginAuthenticationRequest from $res->neededRequests.
98 // It's there so a RESTART treated as UI will work right, but showing
99 // it to the API client is just confusing.
101 $res->neededRequests, [ CreateFromLoginAuthenticationRequest::class ]
102 );
103
104 $this->getResult()->addValue( null, 'clientlogin',
105 $helper->formatAuthenticationResponse( $res ) );
106 $helper->logAuthenticationResult( 'login', $performer, $res );
107 }
108
110 public function isReadMode() {
111 return false;
112 }
113
115 public function isWriteMode() {
116 // (T283394) Logging in triggers some database writes, so should be marked appropriately.
117 return true;
118 }
119
121 public function needsToken() {
122 return 'login';
123 }
124
126 public function getAllowedParams() {
127 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LOGIN,
128 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
129 ) + [
130 'reauthenticate' => [
131 ApiBase::PARAM_TYPE => 'string',
132 ],
133 ];
134 }
135
138 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LOGIN ];
139 }
140
142 protected function getExamplesMessages() {
143 return [
144 'action=clientlogin&username=Example&password=ExamplePassword&'
145 . 'loginreturnurl=http://example.org/&logintoken=123ABC'
146 => 'apihelp-clientlogin-example-login',
147 'action=clientlogin&logincontinue=1&OATHToken=987654&logintoken=123ABC'
148 => 'apihelp-clientlogin-example-login2',
149 ];
150 }
151
153 public function getHelpUrls() {
154 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Login';
155 }
156}
157
159class_alias( ApiClientLogin::class, 'ApiClientLogin' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Helper class for AuthManager-using API modules.
static getStandardParams( $action,... $wantedParams)
Fetch the standard parameters this helper recognizes.
static blacklistAuthenticationRequests(array $reqs, array $remove)
Filter out authentication requests by class name.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:60
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:566
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
requireAtLeastOneParameter( $params,... $required)
Die if 0 of a certain set of parameters is set and not false.
Definition ApiBase.php:1039
getModulePath()
Get the path to this module.
Definition ApiBase.php:636
getResult()
Get the result object.
Definition ApiBase.php:696
requireMaxOneParameter( $params,... $required)
Dies if more than one parameter from a certain set of parameters are set and not false.
Definition ApiBase.php:1012
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:815
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
Log in to the wiki with AuthManager.
isReadMode()
Indicates whether this module requires read rights.to override bool
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
needsToken()
Returns the token type this module requires in order to execute.Modules are strongly encouraged to us...
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
__construct(ApiMain $main, string $action, private readonly AuthManager $authManager, private readonly UrlUtils $urlUtils,)
getFinalDescription()
Get the final module description, after hooks have had a chance to tweak it as needed....
dynamicParameterDocumentation()
Indicate if the module supports dynamically-determined parameters that cannot be included in self::ge...
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
AuthManager is the authentication system in MediaWiki and serves entry point for authentication.
This is a value object to hold authentication response data.
This transfers state between the login and account creation flows.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
A service to expand, parse, and otherwise manipulate URLs.
Definition UrlUtils.php:16