MediaWiki REL1_35
ApiClientLogin.php
Go to the documentation of this file.
1<?php
27
33class ApiClientLogin extends ApiBase {
34
35 public function __construct( ApiMain $main, $action ) {
36 parent::__construct( $main, $action, 'login' );
37 }
38
39 public function getFinalDescription() {
40 // A bit of a hack to append 'api-help-authmanager-general-usage'
41 $msgs = parent::getFinalDescription();
42 $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
43 $this->getModulePrefix(),
44 $this->getModuleName(),
45 $this->getModulePath(),
46 AuthManager::ACTION_LOGIN,
47 $this->needsToken(),
48 ] );
49 return $msgs;
50 }
51
52 public function execute() {
53 $params = $this->extractRequestParams();
54
55 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
56
57 if ( $params['returnurl'] !== null ) {
58 $bits = wfParseUrl( $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 $manager = MediaWikiServices::getInstance()->getAuthManager();
69 $helper = new ApiAuthManagerHelper( $this, $manager );
70
71 // Make sure it's possible to log in
72 if ( !$manager->canAuthenticateNow() ) {
73 $this->getResult()->addValue( null, 'clientlogin', $helper->formatAuthenticationResponse(
74 AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LOGIN ) )
75 ) );
76 $helper->logAuthenticationResult( 'login', 'userlogin-cannot-' . AuthManager::ACTION_LOGIN );
77 return;
78 }
79
80 // Perform the login step
81 if ( $params['continue'] ) {
82 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN_CONTINUE );
83 $res = $manager->continueAuthentication( $reqs );
84 } else {
85 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN );
86 if ( $params['preservestate'] ) {
87 $req = $helper->getPreservedRequest();
88 if ( $req ) {
89 $reqs[] = $req;
90 }
91 }
92 $res = $manager->beginAuthentication( $reqs, $params['returnurl'] );
93 }
94
95 // Remove CreateFromLoginAuthenticationRequest from $res->neededRequests.
96 // It's there so a RESTART treated as UI will work right, but showing
97 // it to the API client is just confusing.
99 $res->neededRequests, [ CreateFromLoginAuthenticationRequest::class ]
100 );
101
102 $this->getResult()->addValue( null, 'clientlogin',
103 $helper->formatAuthenticationResponse( $res ) );
104 $helper->logAuthenticationResult( 'login', $res );
105 }
106
107 public function isReadMode() {
108 return false;
109 }
110
111 public function needsToken() {
112 return 'login';
113 }
114
115 public function getAllowedParams() {
116 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LOGIN,
117 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
118 );
119 }
120
122 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LOGIN ];
123 }
124
125 protected function getExamplesMessages() {
126 return [
127 'action=clientlogin&username=Example&password=ExamplePassword&'
128 . 'loginreturnurl=http://example.org/&logintoken=123ABC'
129 => 'apihelp-clientlogin-example-login',
130 'action=clientlogin&logincontinue=1&OATHToken=987654&logintoken=123ABC'
131 => 'apihelp-clientlogin-example-login2',
132 ];
133 }
134
135 public function getHelpUrls() {
136 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Login';
137 }
138}
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 $blacklist)
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:52
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1437
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:507
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:750
static makeMessage( $msg, IContextSource $context, array $params=null)
Create a Message from a string or array.
Definition ApiBase.php:1223
requireAtLeastOneParameter( $params,... $required)
Die if none of a certain set of parameters is set and not false.
Definition ApiBase.php:969
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
getModulePath()
Get the path to this module.
Definition ApiBase.php:564
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
Log in to the wiki with AuthManager.
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 Stable to override.
__construct(ApiMain $main, $action)
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.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:47
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.