MediaWiki master
ApiClientLogin.php
Go to the documentation of this file.
1<?php
26
32class ApiClientLogin extends ApiBase {
33
34 private AuthManager $authManager;
35
41 public function __construct(
42 ApiMain $main,
43 $action,
44 AuthManager $authManager
45 ) {
46 parent::__construct( $main, $action, 'login' );
47 $this->authManager = $authManager;
48 }
49
50 public function getFinalDescription() {
51 // A bit of a hack to append 'api-help-authmanager-general-usage'
52 $msgs = parent::getFinalDescription();
53 $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
54 $this->getModulePrefix(),
55 $this->getModuleName(),
56 $this->getModulePath(),
57 AuthManager::ACTION_LOGIN,
58 $this->needsToken(),
59 ] );
60 return $msgs;
61 }
62
63 public function execute() {
64 $params = $this->extractRequestParams();
65
66 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
67
68 if ( $params['returnurl'] !== null ) {
69 $bits = wfParseUrl( $params['returnurl'] );
70 if ( !$bits || $bits['scheme'] === '' ) {
71 $encParamName = $this->encodeParamName( 'returnurl' );
72 $this->dieWithError(
73 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
74 "badurl_{$encParamName}"
75 );
76 }
77 }
78
79 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
80
81 // Make sure it's possible to log in
82 if ( !$this->authManager->canAuthenticateNow() ) {
83 $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LOGIN ) );
84 $this->getResult()->addValue( null, 'clientlogin',
85 $helper->formatAuthenticationResponse( $res ) );
86 $helper->logAuthenticationResult( 'login', $res );
87 return;
88 }
89
90 // Perform the login step
91 if ( $params['continue'] ) {
92 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN_CONTINUE );
93 $res = $this->authManager->continueAuthentication( $reqs );
94 } else {
95 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN );
96 if ( $params['preservestate'] ) {
97 $req = $helper->getPreservedRequest();
98 if ( $req ) {
99 $reqs[] = $req;
100 }
101 }
102 $res = $this->authManager->beginAuthentication( $reqs, $params['returnurl'] );
103 }
104
105 // Remove CreateFromLoginAuthenticationRequest from $res->neededRequests.
106 // It's there so a RESTART treated as UI will work right, but showing
107 // it to the API client is just confusing.
109 $res->neededRequests, [ CreateFromLoginAuthenticationRequest::class ]
110 );
111
112 $this->getResult()->addValue( null, 'clientlogin',
113 $helper->formatAuthenticationResponse( $res ) );
114 $helper->logAuthenticationResult( 'login', $res );
115 }
116
117 public function isReadMode() {
118 return false;
119 }
120
121 public function isWriteMode() {
122 // (T283394) Logging in triggers some database writes, so should be marked appropriately.
123 return true;
124 }
125
126 public function needsToken() {
127 return 'login';
128 }
129
130 public function getAllowedParams() {
131 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LOGIN,
132 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
133 );
134 }
135
137 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LOGIN ];
138 }
139
140 protected function getExamplesMessages() {
141 return [
142 'action=clientlogin&username=Example&password=ExamplePassword&'
143 . 'loginreturnurl=http://example.org/&logintoken=123ABC'
144 => 'apihelp-clientlogin-example-login',
145 'action=clientlogin&logincontinue=1&OATHToken=987654&logintoken=123ABC'
146 => 'apihelp-clientlogin-example-login2',
147 ];
148 }
149
150 public function getHelpUrls() {
151 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Login';
152 }
153}
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
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:64
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1533
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:541
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:789
static makeMessage( $msg, IContextSource $context, array $params=null)
Create a Message from a string or array.
Definition ApiBase.php:1285
requireAtLeastOneParameter( $params,... $required)
Die if 0 of a certain set of parameters is set and not false.
Definition ApiBase.php:1010
getResult()
Get the result object.
Definition ApiBase.php:671
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:811
getModulePath()
Get the path to this module.
Definition ApiBase.php:611
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:532
Log in to the wiki with AuthManager.
isWriteMode()
Indicates whether this module requires write mode.
getFinalDescription()
Get the 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:65
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.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
getContext()
Get the base IContextSource object.