MediaWiki REL1_39
ApiClientLogin.php
Go to the documentation of this file.
1<?php
26
32class ApiClientLogin extends ApiBase {
33
35 private $authManager;
36
42 public function __construct(
43 ApiMain $main,
44 $action,
45 AuthManager $authManager
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 $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LOGIN ) );
85 $this->getResult()->addValue( null, 'clientlogin',
86 $helper->formatAuthenticationResponse( $res ) );
87 $helper->logAuthenticationResult( 'login', $res );
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
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}
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfEscapeWikiText( $text)
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:56
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1454
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:506
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:743
static makeMessage( $msg, IContextSource $context, array $params=null)
Create a Message from a string or array.
Definition ApiBase.php:1221
requireAtLeastOneParameter( $params,... $required)
Die if none of a certain set of parameters is set and not false.
Definition ApiBase.php:963
getResult()
Get the result object.
Definition ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:765
getModulePath()
Get the path to this module.
Definition ApiBase.php:573
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:498
Log in to the wiki with AuthManager.
isWriteMode()
Indicates whether this module requires write mode.
getFinalDescription()
Get final module description, after hooks have had a chance to tweak it as needed.
getHelpUrls()
Return links to more detailed help pages about the module.
isReadMode()
Indicates whether this module requires read rights.
dynamicParameterDocumentation()
Indicate if the module supports dynamically-determined parameters that cannot be included in self::ge...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getExamplesMessages()
Returns usage examples for this module.
needsToken()
Returns the token type this module requires in order to execute.
__construct(ApiMain $main, $action, AuthManager $authManager)
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:52
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
getContext()
Get the base IContextSource object.
This serves as the entry point to the authentication system.
This is a value object to hold authentication response data.
This transfers state between the login and account creation flows.