Allow putting the app ID in the password for bot passwords
[lhc/web/wiklou.git] / includes / api / ApiLogin.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 19, 2006
6 *
7 * Copyright © 2006-2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
8 * Daniel Cannon (cannon dot danielc at gmail dot com)
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 */
27
28 use MediaWiki\Auth\AuthManager;
29 use MediaWiki\Auth\AuthenticationRequest;
30 use MediaWiki\Auth\AuthenticationResponse;
31 use MediaWiki\Logger\LoggerFactory;
32
33 /**
34 * Unit to authenticate log-in attempts to the current wiki.
35 *
36 * @ingroup API
37 */
38 class ApiLogin extends ApiBase {
39
40 public function __construct( ApiMain $main, $action ) {
41 parent::__construct( $main, $action, 'lg' );
42 }
43
44 protected function getDescriptionMessage() {
45 if ( $this->getConfig()->get( 'EnableBotPasswords' ) ) {
46 return 'apihelp-login-description';
47 } else {
48 return 'apihelp-login-description-nobotpasswords';
49 }
50 }
51
52 /**
53 * Executes the log-in attempt using the parameters passed. If
54 * the log-in succeeds, it attaches a cookie to the session
55 * and outputs the user id, username, and session token. If a
56 * log-in fails, as the result of a bad password, a nonexistent
57 * user, or any other reason, the host is cached with an expiry
58 * and no log-in attempts will be accepted until that expiry
59 * is reached. The expiry is $this->mLoginThrottle.
60 */
61 public function execute() {
62 // If we're in a mode that breaks the same-origin policy, no tokens can
63 // be obtained
64 if ( $this->lacksSameOriginSecurity() ) {
65 $this->getResult()->addValue( null, 'login', [
66 'result' => 'Aborted',
67 'reason' => 'Cannot log in when the same-origin policy is not applied',
68 ] );
69
70 return;
71 }
72
73 $params = $this->extractRequestParams();
74
75 $result = [];
76
77 // Make sure session is persisted
78 $session = MediaWiki\Session\SessionManager::getGlobalSession();
79 $session->persist();
80
81 // Make sure it's possible to log in
82 if ( !$session->canSetUser() ) {
83 $this->getResult()->addValue( null, 'login', [
84 'result' => 'Aborted',
85 'reason' => 'Cannot log in when using ' .
86 $session->getProvider()->describe( Language::factory( 'en' ) ),
87 ] );
88
89 return;
90 }
91
92 $authRes = false;
93 $context = new DerivativeContext( $this->getContext() );
94 $loginType = 'N/A';
95
96 // Check login token
97 $token = $session->getToken( '', 'login' );
98 if ( $token->wasNew() || !$params['token'] ) {
99 $authRes = 'NeedToken';
100 } elseif ( !$token->match( $params['token'] ) ) {
101 $authRes = 'WrongToken';
102 }
103
104 // Try bot passwords
105 if (
106 $authRes === false && $this->getConfig()->get( 'EnableBotPasswords' ) &&
107 ( $botLoginData = BotPassword::canonicalizeLoginData( $params['name'], $params['password'] ) )
108 ) {
109 $status = BotPassword::login(
110 $botLoginData[0], $botLoginData[1], $this->getRequest()
111 );
112 if ( $status->isOK() ) {
113 $session = $status->getValue();
114 $authRes = 'Success';
115 $loginType = 'BotPassword';
116 } elseif ( !$botLoginData[2] ) {
117 $authRes = 'Failed';
118 $message = $status->getMessage();
119 LoggerFactory::getInstance( 'authentication' )->info(
120 'BotPassword login failed: ' . $status->getWikiText( false, false, 'en' )
121 );
122 }
123 }
124
125 if ( $authRes === false ) {
126 // Simplified AuthManager login, for backwards compatibility
127 $manager = AuthManager::singleton();
128 $reqs = AuthenticationRequest::loadRequestsFromSubmission(
129 $manager->getAuthenticationRequests( AuthManager::ACTION_LOGIN, $this->getUser() ),
130 [
131 'username' => $params['name'],
132 'password' => $params['password'],
133 'domain' => $params['domain'],
134 'rememberMe' => true,
135 ]
136 );
137 $res = AuthManager::singleton()->beginAuthentication( $reqs, 'null:' );
138 switch ( $res->status ) {
139 case AuthenticationResponse::PASS:
140 if ( $this->getConfig()->get( 'EnableBotPasswords' ) ) {
141 $warn = 'Main-account login via action=login is deprecated and may stop working ' .
142 'without warning.';
143 $warn .= ' To continue login with action=login, see [[Special:BotPasswords]].';
144 $warn .= ' To safely continue using main-account login, see action=clientlogin.';
145 } else {
146 $warn = 'Login via action=login is deprecated and may stop working without warning.';
147 $warn .= ' To safely log in, see action=clientlogin.';
148 }
149 $this->setWarning( $warn );
150 $authRes = 'Success';
151 $loginType = 'AuthManager';
152 break;
153
154 case AuthenticationResponse::FAIL:
155 // Hope it's not a PreAuthenticationProvider that failed...
156 $authRes = 'Failed';
157 $message = $res->message;
158 \MediaWiki\Logger\LoggerFactory::getInstance( 'authentication' )
159 ->info( __METHOD__ . ': Authentication failed: '
160 . $message->inLanguage( 'en' )->plain() );
161 break;
162
163 default:
164 \MediaWiki\Logger\LoggerFactory::getInstance( 'authentication' )
165 ->info( __METHOD__ . ': Authentication failed due to unsupported response type: '
166 . $res->status, $this->getAuthenticationResponseLogData( $res ) );
167 $authRes = 'Aborted';
168 break;
169 }
170 }
171
172 $result['result'] = $authRes;
173 switch ( $authRes ) {
174 case 'Success':
175 $user = $session->getUser();
176
177 ApiQueryInfo::resetTokenCache();
178
179 // Deprecated hook
180 $injected_html = '';
181 Hooks::run( 'UserLoginComplete', [ &$user, &$injected_html, true ] );
182
183 $result['lguserid'] = intval( $user->getId() );
184 $result['lgusername'] = $user->getName();
185
186 // @todo: These are deprecated, and should be removed at some
187 // point (1.28 at the earliest, and see T121527). They were ok
188 // when the core cookie-based login was the only thing, but
189 // CentralAuth broke that a while back and
190 // SessionManager/AuthManager *really* break it.
191 $result['lgtoken'] = $user->getToken();
192 $result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
193 $result['sessionid'] = $session->getId();
194 break;
195
196 case 'NeedToken':
197 $result['token'] = $token->toString();
198 $this->setWarning( 'Fetching a token via action=login is deprecated. ' .
199 'Use action=query&meta=tokens&type=login instead.' );
200 $this->logFeatureUsage( 'action=login&!lgtoken' );
201
202 // @todo: See above about deprecation
203 $result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
204 $result['sessionid'] = $session->getId();
205 break;
206
207 case 'WrongToken':
208 break;
209
210 case 'Failed':
211 $result['reason'] = $message->useDatabase( 'false' )->inLanguage( 'en' )->text();
212 break;
213
214 case 'Aborted':
215 $result['reason'] = 'Authentication requires user interaction, ' .
216 'which is not supported by action=login.';
217 if ( $this->getConfig()->get( 'EnableBotPasswords' ) ) {
218 $result['reason'] .= ' To be able to login with action=login, see [[Special:BotPasswords]].';
219 $result['reason'] .= ' To continue using main-account login, see action=clientlogin.';
220 } else {
221 $result['reason'] .= ' To log in, see action=clientlogin.';
222 }
223 break;
224
225 default:
226 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
227 }
228
229 $this->getResult()->addValue( null, 'login', $result );
230
231 if ( $loginType === 'LoginForm' && isset( LoginForm::$statusCodes[$authRes] ) ) {
232 $authRes = LoginForm::$statusCodes[$authRes];
233 }
234 LoggerFactory::getInstance( 'authevents' )->info( 'Login attempt', [
235 'event' => 'login',
236 'successful' => $authRes === 'Success',
237 'loginType' => $loginType,
238 'status' => $authRes,
239 ] );
240 }
241
242 public function isDeprecated() {
243 return !$this->getConfig()->get( 'EnableBotPasswords' );
244 }
245
246 public function mustBePosted() {
247 return true;
248 }
249
250 public function isReadMode() {
251 return false;
252 }
253
254 public function getAllowedParams() {
255 return [
256 'name' => null,
257 'password' => [
258 ApiBase::PARAM_TYPE => 'password',
259 ],
260 'domain' => null,
261 'token' => [
262 ApiBase::PARAM_TYPE => 'string',
263 ApiBase::PARAM_REQUIRED => false, // for BC
264 ApiBase::PARAM_HELP_MSG => [ 'api-help-param-token', 'login' ],
265 ],
266 ];
267 }
268
269 protected function getExamplesMessages() {
270 return [
271 'action=login&lgname=user&lgpassword=password'
272 => 'apihelp-login-example-gettoken',
273 'action=login&lgname=user&lgpassword=password&lgtoken=123ABC'
274 => 'apihelp-login-example-login',
275 ];
276 }
277
278 public function getHelpUrls() {
279 return 'https://www.mediawiki.org/wiki/API:Login';
280 }
281
282 /**
283 * Turns an AuthenticationResponse into a hash suitable for passing to Logger
284 * @param AuthenticationResponse $response
285 * @return array
286 */
287 protected function getAuthenticationResponseLogData( AuthenticationResponse $response ) {
288 $ret = [
289 'status' => $response->status,
290 ];
291 if ( $response->message ) {
292 $ret['message'] = $response->message->inLanguage( 'en' )->plain();
293 };
294 $reqs = [
295 'neededRequests' => $response->neededRequests,
296 'createRequest' => $response->createRequest,
297 'linkRequest' => $response->linkRequest,
298 ];
299 foreach ( $reqs as $k => $v ) {
300 if ( $v ) {
301 $v = is_array( $v ) ? $v : [ $v ];
302 $reqClasses = array_unique( array_map( 'get_class', $v ) );
303 sort( $reqClasses );
304 $ret[$k] = implode( ', ', $reqClasses );
305 }
306 }
307 return $ret;
308 }
309 }