MediaWiki master
ApiClientLogin.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
29
35class ApiClientLogin extends ApiBase {
36
37 private AuthManager $authManager;
38 private UrlUtils $urlUtils;
39
40 public function __construct(
41 ApiMain $main,
42 string $action,
43 AuthManager $authManager,
44 UrlUtils $urlUtils
45 ) {
46 parent::__construct( $main, $action, 'login' );
47 $this->authManager = $authManager;
48 $this->urlUtils = $urlUtils;
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[] = $this->msg( 'api-help-authmanager-general-usage',
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() {
66 $performer = $this->getUser();
67
68 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
69
70 if ( $params['returnurl'] !== null ) {
71 $bits = $this->urlUtils->parse( $params['returnurl'] );
72 if ( !$bits || $bits['scheme'] === '' ) {
73 $encParamName = $this->encodeParamName( 'returnurl' );
74 $this->dieWithError(
75 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
76 "badurl_{$encParamName}"
77 );
78 }
79 }
80
81 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
82
83 // Make sure it's possible to log in
84 if ( !$this->authManager->canAuthenticateNow() ) {
85 $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LOGIN ) );
86 $this->getResult()->addValue( null, 'clientlogin',
87 $helper->formatAuthenticationResponse( $res ) );
88 $helper->logAuthenticationResult( 'login', $performer, $res );
89 return;
90 }
91
92 // Perform the login step
93 if ( $params['continue'] ) {
94 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN_CONTINUE );
95 $res = $this->authManager->continueAuthentication( $reqs );
96 } else {
97 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN );
98 if ( $params['preservestate'] ) {
99 $req = $helper->getPreservedRequest();
100 if ( $req ) {
101 $reqs[] = $req;
102 }
103 }
104 $res = $this->authManager->beginAuthentication( $reqs, $params['returnurl'] );
105 }
106
107 // Remove CreateFromLoginAuthenticationRequest from $res->neededRequests.
108 // It's there so a RESTART treated as UI will work right, but showing
109 // it to the API client is just confusing.
111 $res->neededRequests, [ CreateFromLoginAuthenticationRequest::class ]
112 );
113
114 $this->getResult()->addValue( null, 'clientlogin',
115 $helper->formatAuthenticationResponse( $res ) );
116 $helper->logAuthenticationResult( 'login', $performer, $res );
117 }
118
119 public function isReadMode() {
120 return false;
121 }
122
123 public function isWriteMode() {
124 // (T283394) Logging in triggers some database writes, so should be marked appropriately.
125 return true;
126 }
127
128 public function needsToken() {
129 return 'login';
130 }
131
132 public function getAllowedParams() {
133 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LOGIN,
134 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
135 );
136 }
137
139 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LOGIN ];
140 }
141
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
152 public function getHelpUrls() {
153 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Login';
154 }
155}
156
158class_alias( ApiClientLogin::class, 'ApiClientLogin' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
array $params
The job parameters.
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:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1531
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:580
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
requireAtLeastOneParameter( $params,... $required)
Die if 0 of a certain set of parameters is set and not false.
Definition ApiBase.php:1050
getModulePath()
Get the path to this module.
Definition ApiBase.php:650
getResult()
Get the result object.
Definition ApiBase.php:710
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:829
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
Log in to the wiki with AuthManager.
isReadMode()
Indicates whether this module requires read rights.
getExamplesMessages()
Returns usage examples for this module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
isWriteMode()
Indicates whether this module requires write access to the wiki.
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.
getHelpUrls()
Return links to more detailed help pages about the module.
__construct(ApiMain $main, string $action, AuthManager $authManager, 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:78
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()
A service to expand, parse, and otherwise manipulate URLs.
Definition UrlUtils.php:16