Merge "Added "maxPartitionsTry" option to JobQueueFederated"
[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( $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 return;
56 }
57
58 $params = $this->extractRequestParams();
59
60 $result = array();
61
62 // Init session if necessary
63 if ( session_id() == '' ) {
64 wfSetupSession();
65 }
66
67 $context = new DerivativeContext( $this->getContext() );
68 $context->setRequest( new DerivativeRequest(
69 $this->getContext()->getRequest(),
70 array(
71 'wpName' => $params['name'],
72 'wpPassword' => $params['password'],
73 'wpDomain' => $params['domain'],
74 'wpLoginToken' => $params['token'],
75 'wpRemember' => ''
76 )
77 ) );
78 $loginForm = new LoginForm();
79 $loginForm->setContext( $context );
80
81 global $wgCookiePrefix, $wgPasswordAttemptThrottle;
82
83 $authRes = $loginForm->authenticateUserData();
84 switch ( $authRes ) {
85 case LoginForm::SUCCESS:
86 $user = $context->getUser();
87 $this->getContext()->setUser( $user );
88 $user->setOption( 'rememberpassword', 1 );
89 $user->setCookies( $this->getRequest() );
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 case LoginForm::RESET_PASS: // bug 20223 - Treat a temporary password as wrong. Per SpecialUserLogin - "The e-mailed temporary password should not be used for actual logins;"
135 case LoginForm::WRONG_PASS:
136 $result['result'] = 'WrongPass';
137 break;
138
139 case LoginForm::EMPTY_PASS:
140 $result['result'] = 'EmptyPass';
141 break;
142
143 case LoginForm::CREATE_BLOCKED:
144 $result['result'] = 'CreateBlocked';
145 $result['details'] = 'Your IP address is blocked from account creation';
146 break;
147
148 case LoginForm::THROTTLED:
149 $result['result'] = 'Throttled';
150 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
151 break;
152
153 case LoginForm::USER_BLOCKED:
154 $result['result'] = 'Blocked';
155 break;
156
157 case LoginForm::ABORTED:
158 $result['result'] = 'Aborted';
159 $result['reason'] = $loginForm->mAbortLoginErrorMsg;
160 break;
161
162 default:
163 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
164 }
165
166 $this->getResult()->addValue( null, 'login', $result );
167 }
168
169 public function mustBePosted() {
170 return true;
171 }
172
173 public function isReadMode() {
174 return false;
175 }
176
177 public function getAllowedParams() {
178 return array(
179 'name' => null,
180 'password' => null,
181 'domain' => null,
182 'token' => null,
183 );
184 }
185
186 public function getParamDescription() {
187 return array(
188 'name' => 'User Name',
189 'password' => 'Password',
190 'domain' => 'Domain (optional)',
191 'token' => 'Login token obtained in first request',
192 );
193 }
194
195 public function getResultProperties() {
196 return array(
197 '' => array(
198 'result' => array(
199 ApiBase::PROP_TYPE => array(
200 'Success',
201 'NeedToken',
202 'WrongToken',
203 'NoName',
204 'Illegal',
205 'WrongPluginPass',
206 'NotExists',
207 'WrongPass',
208 'EmptyPass',
209 'CreateBlocked',
210 'Throttled',
211 'Blocked',
212 'Aborted'
213 )
214 ),
215 'lguserid' => array(
216 ApiBase::PROP_TYPE => 'integer',
217 ApiBase::PROP_NULLABLE => true
218 ),
219 'lgusername' => array(
220 ApiBase::PROP_TYPE => 'string',
221 ApiBase::PROP_NULLABLE => true
222 ),
223 'lgtoken' => array(
224 ApiBase::PROP_TYPE => 'string',
225 ApiBase::PROP_NULLABLE => true
226 ),
227 'cookieprefix' => array(
228 ApiBase::PROP_TYPE => 'string',
229 ApiBase::PROP_NULLABLE => true
230 ),
231 'sessionid' => array(
232 ApiBase::PROP_TYPE => 'string',
233 ApiBase::PROP_NULLABLE => true
234 ),
235 'token' => array(
236 ApiBase::PROP_TYPE => 'string',
237 ApiBase::PROP_NULLABLE => true
238 ),
239 'details' => array(
240 ApiBase::PROP_TYPE => 'string',
241 ApiBase::PROP_NULLABLE => true
242 ),
243 'wait' => array(
244 ApiBase::PROP_TYPE => 'integer',
245 ApiBase::PROP_NULLABLE => true
246 ),
247 'reason' => array(
248 ApiBase::PROP_TYPE => 'string',
249 ApiBase::PROP_NULLABLE => true
250 )
251 )
252 );
253 }
254
255 public function getDescription() {
256 return array(
257 'Log in and get the authentication tokens.',
258 'In the event of a successful log-in, a cookie will be attached',
259 'to your session. In the event of a failed log-in, you will not',
260 'be able to attempt another log-in through this method for 5 seconds.',
261 'This is to prevent password guessing by automated password crackers'
262 );
263 }
264
265 public function getPossibleErrors() {
266 return array_merge( parent::getPossibleErrors(), array(
267 array( 'code' => 'NeedToken', 'info' => 'You need to resubmit your login with the specified token. See https://bugzilla.wikimedia.org/show_bug.cgi?id=23076' ),
268 array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
269 array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
270 array( 'code' => 'Illegal', 'info' => 'You provided an illegal username' ),
271 array( 'code' => 'NotExists', 'info' => 'The username you provided doesn\'t exist' ),
272 array( 'code' => 'EmptyPass', 'info' => 'You didn\'t set the lgpassword parameter or you left it empty' ),
273 array( 'code' => 'WrongPass', 'info' => 'The password you provided is incorrect' ),
274 array( 'code' => 'WrongPluginPass', 'info' => 'Same as "WrongPass", returned when an authentication plugin rather than MediaWiki itself rejected the password' ),
275 array( 'code' => 'CreateBlocked', 'info' => 'The wiki tried to automatically create a new account for you, but your IP address has been blocked from account creation' ),
276 array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
277 array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
278 ) );
279 }
280
281 public function getExamples() {
282 return array(
283 'api.php?action=login&lgname=user&lgpassword=password'
284 );
285 }
286
287 public function getHelpUrls() {
288 return 'https://www.mediawiki.org/wiki/API:Login';
289 }
290 }