MediaWiki  master
ApiClientLogin.php
Go to the documentation of this file.
1 <?php
27 
33 class 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 
121  public function dynamicParameterDocumentation() {
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 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:44
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:40
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1415
ApiClientLogin\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiClientLogin.php:52
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:546
ApiBase\makeMessage
static makeMessage( $msg, IContextSource $context, array $params=null)
Create a Message from a string or array.
Definition: ApiBase.php:1201
ApiAuthManagerHelper\getStandardParams
static getStandardParams( $action,... $wantedParams)
Fetch the standard parameters this helper recognizes.
Definition: ApiAuthManagerHelper.php:362
$res
$res
Definition: testCompression.php:57
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:50
ApiClientLogin\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiClientLogin.php:135
ApiClientLogin\__construct
__construct(ApiMain $main, $action)
Definition: ApiClientLogin.php:35
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:792
ApiBase\getModulePath
getModulePath()
Get the path to this module.
Definition: ApiBase.php:490
MediaWiki\Auth\AuthenticationResponse
This is a value object to hold authentication response data.
Definition: AuthenticationResponse.php:37
MediaWiki\Auth\CreateFromLoginAuthenticationRequest
This transfers state between the login and account creation flows.
Definition: CreateFromLoginAuthenticationRequest.php:34
ApiClientLogin\getFinalDescription
getFinalDescription()
Get final module description, after hooks have had a chance to tweak it as needed.
Definition: ApiClientLogin.php:39
ApiAuthManagerHelper
Helper class for AuthManager-using API modules.
Definition: ApiAuthManagerHelper.php:37
ApiClientLogin\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiClientLogin.php:125
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:695
ApiBase\requireAtLeastOneParameter
requireAtLeastOneParameter( $params,... $required)
Die if none of a certain set of parameters is set and not false.
Definition: ApiBase.php:892
ApiClientLogin\dynamicParameterDocumentation
dynamicParameterDocumentation()
Indicate if the module supports dynamically-determined parameters that cannot be included in self::ge...
Definition: ApiClientLogin.php:121
ApiBase\getModulePrefix
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:434
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:673
ApiAuthManagerHelper\blacklistAuthenticationRequests
static blacklistAuthenticationRequests(array $reqs, array $blacklist)
Filter out authentication requests by class name.
Definition: ApiAuthManagerHelper.php:129
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1490
MediaWiki\Auth\AuthManager
This serves as the entry point to the authentication system.
Definition: AuthManager.php:88
ApiClientLogin\isReadMode
isReadMode()
Indicates whether this module requires read rights.
Definition: ApiClientLogin.php:107
ApiClientLogin
Log in to the wiki with AuthManager.
Definition: ApiClientLogin.php:33
ApiClientLogin\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiClientLogin.php:111
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:426
ApiClientLogin\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiClientLogin.php:115