Update git submodules
[mediawiki.git] / includes / api / ApiClientLogin.php
blobdc64f572f77a34d5cb06fcb28a20de55a6837710
1 <?php
2 /**
3 * Copyright © 2016 Wikimedia Foundation and contributors
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\AuthenticationResponse;
24 use MediaWiki\Auth\AuthManager;
25 use MediaWiki\Auth\CreateFromLoginAuthenticationRequest;
27 /**
28 * Log in to the wiki with AuthManager
30 * @ingroup API
32 class ApiClientLogin extends ApiBase {
34 private AuthManager $authManager;
36 /**
37 * @param ApiMain $main
38 * @param string $action
39 * @param AuthManager $authManager
41 public function __construct(
42 ApiMain $main,
43 $action,
44 AuthManager $authManager
45 ) {
46 parent::__construct( $main, $action, 'login' );
47 $this->authManager = $authManager;
50 public function getFinalDescription() {
51 // A bit of a hack to append 'api-help-authmanager-general-usage'
52 $msgs = parent::getFinalDescription();
53 $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
54 $this->getModulePrefix(),
55 $this->getModuleName(),
56 $this->getModulePath(),
57 AuthManager::ACTION_LOGIN,
58 $this->needsToken(),
59 ] );
60 return $msgs;
63 public function execute() {
64 $params = $this->extractRequestParams();
66 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
68 if ( $params['returnurl'] !== null ) {
69 $bits = wfParseUrl( $params['returnurl'] );
70 if ( !$bits || $bits['scheme'] === '' ) {
71 $encParamName = $this->encodeParamName( 'returnurl' );
72 $this->dieWithError(
73 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
74 "badurl_{$encParamName}"
79 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
81 // Make sure it's possible to log in
82 if ( !$this->authManager->canAuthenticateNow() ) {
83 $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LOGIN ) );
84 $this->getResult()->addValue( null, 'clientlogin',
85 $helper->formatAuthenticationResponse( $res ) );
86 $helper->logAuthenticationResult( 'login', $res );
87 return;
90 // Perform the login step
91 if ( $params['continue'] ) {
92 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN_CONTINUE );
93 $res = $this->authManager->continueAuthentication( $reqs );
94 } else {
95 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LOGIN );
96 if ( $params['preservestate'] ) {
97 $req = $helper->getPreservedRequest();
98 if ( $req ) {
99 $reqs[] = $req;
102 $res = $this->authManager->beginAuthentication( $reqs, $params['returnurl'] );
105 // Remove CreateFromLoginAuthenticationRequest from $res->neededRequests.
106 // It's there so a RESTART treated as UI will work right, but showing
107 // it to the API client is just confusing.
108 $res->neededRequests = ApiAuthManagerHelper::blacklistAuthenticationRequests(
109 $res->neededRequests, [ CreateFromLoginAuthenticationRequest::class ]
112 $this->getResult()->addValue( null, 'clientlogin',
113 $helper->formatAuthenticationResponse( $res ) );
114 $helper->logAuthenticationResult( 'login', $res );
117 public function isReadMode() {
118 return false;
121 public function isWriteMode() {
122 // (T283394) Logging in triggers some database writes, so should be marked appropriately.
123 return true;
126 public function needsToken() {
127 return 'login';
130 public function getAllowedParams() {
131 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LOGIN,
132 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
136 public function dynamicParameterDocumentation() {
137 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LOGIN ];
140 protected function getExamplesMessages() {
141 return [
142 'action=clientlogin&username=Example&password=ExamplePassword&'
143 . 'loginreturnurl=http://example.org/&logintoken=123ABC'
144 => 'apihelp-clientlogin-example-login',
145 'action=clientlogin&logincontinue=1&OATHToken=987654&logintoken=123ABC'
146 => 'apihelp-clientlogin-example-login2',
150 public function getHelpUrls() {
151 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Login';