Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / api / ApiClientLogin.php
blobcffccb15b54c2544c30a090ff568e7b25a1ceddd
1 <?php
2 /**
3 * Copyright © 2016 Brad Jorsch <bjorsch@wikimedia.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 use MediaWiki\Auth\AuthManager;
24 use MediaWiki\Auth\AuthenticationRequest;
25 use MediaWiki\Auth\AuthenticationResponse;
26 use MediaWiki\Auth\CreateFromLoginAuthenticationRequest;
28 /**
29 * Log in to the wiki with AuthManager
31 * @ingroup API
33 class ApiClientLogin extends ApiBase {
35 public function __construct( ApiMain $main, $action ) {
36 parent::__construct( $main, $action, 'login' );
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 self::needsToken(),
48 ] );
49 return $msgs;
52 public function execute() {
53 $params = $this->extractRequestParams();
55 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
57 if ( $params['returnurl'] !== null ) {
58 $bits = wfParseUrl( $params['returnurl'] );
59 if ( !$bits || $bits['scheme'] === '' ) {
60 $encParamName = $this->encodeParamName( 'returnurl' );
61 $this->dieUsage(
62 "Invalid value '{$params['returnurl']}' for url parameter $encParamName",
63 "badurl_{$encParamName}"
68 $helper = new ApiAuthManagerHelper( $this );
69 $manager = AuthManager::singleton();
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 return;
79 // Perform the login step
80 if ( $params['continue'] ) {
81 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN_CONTINUE );
82 $res = $manager->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;
91 $res = $manager->beginAuthentication( $reqs, $params['returnurl'] );
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.
97 $res->neededRequests = ApiAuthManagerHelper::blacklistAuthenticationRequests(
98 $res->neededRequests, [ CreateFromLoginAuthenticationRequest::class ]
101 $this->getResult()->addValue( null, 'clientlogin',
102 $helper->formatAuthenticationResponse( $res ) );
105 public function isReadMode() {
106 return false;
109 public function needsToken() {
110 return 'login';
113 public function getAllowedParams() {
114 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LOGIN,
115 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
119 public function dynamicParameterDocumentation() {
120 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LOGIN ];
123 protected function getExamplesMessages() {
124 return [
125 'action=clientlogin&username=Example&password=ExamplePassword&'
126 . 'loginreturnurl=http://example.org/&logintoken=123ABC'
127 => 'apihelp-clientlogin-example-login',
128 'action=clientlogin&logincontinue=1&OATHToken=987654&logintoken=123ABC'
129 => 'apihelp-clientlogin-example-login2',
133 public function getHelpUrls() {
134 return 'https://www.mediawiki.org/wiki/API:Login';