Also set the queue types map when rebuilding the ready queue map
[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 /**
29 * Unit to authenticate log-in attempts to the current wiki.
30 *
31 * @ingroup API
32 */
33 class ApiLogin extends ApiBase {
34
35 public function __construct( ApiMain $main, $action ) {
36 parent::__construct( $main, $action, 'lg' );
37 }
38
39 /**
40 * Executes the log-in attempt using the parameters passed. If
41 * the log-in succeeds, it attaches a cookie to the session
42 * and outputs the user id, username, and session token. If a
43 * log-in fails, as the result of a bad password, a nonexistent
44 * user, or any other reason, the host is cached with an expiry
45 * and no log-in attempts will be accepted until that expiry
46 * is reached. The expiry is $this->mLoginThrottle.
47 */
48 public function execute() {
49 // If we're in JSON callback mode, no tokens can be obtained
50 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
51 $this->getResult()->addValue( null, 'login', array(
52 'result' => 'Aborted',
53 'reason' => 'Cannot log in when using a callback',
54 ) );
55
56 return;
57 }
58
59 $params = $this->extractRequestParams();
60
61 $result = array();
62
63 // Init session if necessary
64 if ( session_id() == '' ) {
65 wfSetupSession();
66 }
67
68 $context = new DerivativeContext( $this->getContext() );
69 $context->setRequest( new DerivativeRequest(
70 $this->getContext()->getRequest(),
71 array(
72 'wpName' => $params['name'],
73 'wpPassword' => $params['password'],
74 'wpDomain' => $params['domain'],
75 'wpLoginToken' => $params['token'],
76 'wpRemember' => ''
77 )
78 ) );
79 $loginForm = new LoginForm();
80 $loginForm->setContext( $context );
81
82 global $wgCookiePrefix, $wgPasswordAttemptThrottle;
83
84 $authRes = $loginForm->authenticateUserData();
85 switch ( $authRes ) {
86 case LoginForm::SUCCESS:
87 $user = $context->getUser();
88 $this->getContext()->setUser( $user );
89 $user->setCookies( $this->getRequest(), null, true );
90
91 ApiQueryInfo::resetTokenCache();
92
93 // Run hooks.
94 // @todo FIXME: Split back and frontend from this hook.
95 // @todo FIXME: This hook should be placed in the backend
96 $injected_html = '';
97 wfRunHooks( 'UserLoginComplete', array( &$user, &$injected_html ) );
98
99 $result['result'] = 'Success';
100 $result['lguserid'] = intval( $user->getId() );
101 $result['lgusername'] = $user->getName();
102 $result['lgtoken'] = $user->getToken();
103 $result['cookieprefix'] = $wgCookiePrefix;
104 $result['sessionid'] = session_id();
105 break;
106
107 case LoginForm::NEED_TOKEN:
108 $result['result'] = 'NeedToken';
109 $result['token'] = $loginForm->getLoginToken();
110 $result['cookieprefix'] = $wgCookiePrefix;
111 $result['sessionid'] = session_id();
112 break;
113
114 case LoginForm::WRONG_TOKEN:
115 $result['result'] = 'WrongToken';
116 break;
117
118 case LoginForm::NO_NAME:
119 $result['result'] = 'NoName';
120 break;
121
122 case LoginForm::ILLEGAL:
123 $result['result'] = 'Illegal';
124 break;
125
126 case LoginForm::WRONG_PLUGIN_PASS:
127 $result['result'] = 'WrongPluginPass';
128 break;
129
130 case LoginForm::NOT_EXISTS:
131 $result['result'] = 'NotExists';
132 break;
133
134 // bug 20223 - Treat a temporary password as wrong. Per SpecialUserLogin:
135 // The e-mailed temporary password should not be used for actual logins.
136 case LoginForm::RESET_PASS:
137 case LoginForm::WRONG_PASS:
138 $result['result'] = 'WrongPass';
139 break;
140
141 case LoginForm::EMPTY_PASS:
142 $result['result'] = 'EmptyPass';
143 break;
144
145 case LoginForm::CREATE_BLOCKED:
146 $result['result'] = 'CreateBlocked';
147 $result['details'] = 'Your IP address is blocked from account creation';
148 break;
149
150 case LoginForm::THROTTLED:
151 $result['result'] = 'Throttled';
152 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
153 break;
154
155 case LoginForm::USER_BLOCKED:
156 $result['result'] = 'Blocked';
157 break;
158
159 case LoginForm::ABORTED:
160 $result['result'] = 'Aborted';
161 $result['reason'] = $loginForm->mAbortLoginErrorMsg;
162 break;
163
164 default:
165 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
166 }
167
168 $this->getResult()->addValue( null, 'login', $result );
169 }
170
171 public function mustBePosted() {
172 return true;
173 }
174
175 public function isReadMode() {
176 return false;
177 }
178
179 public function getAllowedParams() {
180 return array(
181 'name' => null,
182 'password' => null,
183 'domain' => null,
184 'token' => null,
185 );
186 }
187
188 public function getParamDescription() {
189 return array(
190 'name' => 'User Name',
191 'password' => 'Password',
192 'domain' => 'Domain (optional)',
193 'token' => 'Login token obtained in first request',
194 );
195 }
196
197 public function getResultProperties() {
198 return array(
199 '' => array(
200 'result' => array(
201 ApiBase::PROP_TYPE => array(
202 'Success',
203 'NeedToken',
204 'WrongToken',
205 'NoName',
206 'Illegal',
207 'WrongPluginPass',
208 'NotExists',
209 'WrongPass',
210 'EmptyPass',
211 'CreateBlocked',
212 'Throttled',
213 'Blocked',
214 'Aborted'
215 )
216 ),
217 'lguserid' => array(
218 ApiBase::PROP_TYPE => 'integer',
219 ApiBase::PROP_NULLABLE => true
220 ),
221 'lgusername' => array(
222 ApiBase::PROP_TYPE => 'string',
223 ApiBase::PROP_NULLABLE => true
224 ),
225 'lgtoken' => array(
226 ApiBase::PROP_TYPE => 'string',
227 ApiBase::PROP_NULLABLE => true
228 ),
229 'cookieprefix' => array(
230 ApiBase::PROP_TYPE => 'string',
231 ApiBase::PROP_NULLABLE => true
232 ),
233 'sessionid' => array(
234 ApiBase::PROP_TYPE => 'string',
235 ApiBase::PROP_NULLABLE => true
236 ),
237 'token' => array(
238 ApiBase::PROP_TYPE => 'string',
239 ApiBase::PROP_NULLABLE => true
240 ),
241 'details' => array(
242 ApiBase::PROP_TYPE => 'string',
243 ApiBase::PROP_NULLABLE => true
244 ),
245 'wait' => array(
246 ApiBase::PROP_TYPE => 'integer',
247 ApiBase::PROP_NULLABLE => true
248 ),
249 'reason' => array(
250 ApiBase::PROP_TYPE => 'string',
251 ApiBase::PROP_NULLABLE => true
252 )
253 )
254 );
255 }
256
257 public function getDescription() {
258 return array(
259 'Log in and get the authentication tokens.',
260 'In the event of a successful log-in, a cookie will be attached to your session.',
261 'In the event of a failed log-in, you will not be able to attempt another log-in',
262 'through this method for 5 seconds. This is to prevent password guessing by',
263 'automated password crackers.'
264 );
265 }
266
267 public function getPossibleErrors() {
268 return array_merge( parent::getPossibleErrors(), array(
269 array(
270 'code' => 'NeedToken', 'info' => 'You need to resubmit your ' .
271 'login with the specified token. See ' .
272 'https://bugzilla.wikimedia.org/show_bug.cgi?id=23076'
273 ),
274 array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
275 array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
276 array( 'code' => 'Illegal', 'info' => 'You provided an illegal username' ),
277 array( 'code' => 'NotExists', 'info' => 'The username you provided doesn\'t exist' ),
278 array(
279 'code' => 'EmptyPass',
280 'info' => 'You didn\'t set the lgpassword parameter or you left it empty'
281 ),
282 array( 'code' => 'WrongPass', 'info' => 'The password you provided is incorrect' ),
283 array(
284 'code' => 'WrongPluginPass',
285 'info' => 'Same as "WrongPass", returned when an authentication ' .
286 'plugin rather than MediaWiki itself rejected the password'
287 ),
288 array(
289 'code' => 'CreateBlocked',
290 'info' => 'The wiki tried to automatically create a new account ' .
291 'for you, but your IP address has been blocked from account creation'
292 ),
293 array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
294 array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
295 ) );
296 }
297
298 public function getExamples() {
299 return array(
300 'api.php?action=login&lgname=user&lgpassword=password'
301 );
302 }
303
304 public function getHelpUrls() {
305 return 'https://www.mediawiki.org/wiki/API:Login';
306 }
307 }