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