Merge "Adding hlist module to mediawiki"
[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
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->setOption( 'rememberpassword', 1 );
90 $user->setCookies( $this->getRequest() );
91
92 ApiQueryInfo::resetTokenCache();
93
94 // Run hooks.
95 // @todo FIXME: Split back and frontend from this hook.
96 // @todo FIXME: This hook should be placed in the backend
97 $injected_html = '';
98 wfRunHooks( 'UserLoginComplete', array( &$user, &$injected_html ) );
99
100 $result['result'] = 'Success';
101 $result['lguserid'] = intval( $user->getId() );
102 $result['lgusername'] = $user->getName();
103 $result['lgtoken'] = $user->getToken();
104 $result['cookieprefix'] = $wgCookiePrefix;
105 $result['sessionid'] = session_id();
106 break;
107
108 case LoginForm::NEED_TOKEN:
109 $result['result'] = 'NeedToken';
110 $result['token'] = $loginForm->getLoginToken();
111 $result['cookieprefix'] = $wgCookiePrefix;
112 $result['sessionid'] = session_id();
113 break;
114
115 case LoginForm::WRONG_TOKEN:
116 $result['result'] = 'WrongToken';
117 break;
118
119 case LoginForm::NO_NAME:
120 $result['result'] = 'NoName';
121 break;
122
123 case LoginForm::ILLEGAL:
124 $result['result'] = 'Illegal';
125 break;
126
127 case LoginForm::WRONG_PLUGIN_PASS:
128 $result['result'] = 'WrongPluginPass';
129 break;
130
131 case LoginForm::NOT_EXISTS:
132 $result['result'] = 'NotExists';
133 break;
134
135 // bug 20223 - Treat a temporary password as wrong. Per SpecialUserLogin:
136 // The e-mailed temporary password should not be used for actual logins.
137 case LoginForm::RESET_PASS:
138 case LoginForm::WRONG_PASS:
139 $result['result'] = 'WrongPass';
140 break;
141
142 case LoginForm::EMPTY_PASS:
143 $result['result'] = 'EmptyPass';
144 break;
145
146 case LoginForm::CREATE_BLOCKED:
147 $result['result'] = 'CreateBlocked';
148 $result['details'] = 'Your IP address is blocked from account creation';
149 break;
150
151 case LoginForm::THROTTLED:
152 $result['result'] = 'Throttled';
153 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
154 break;
155
156 case LoginForm::USER_BLOCKED:
157 $result['result'] = 'Blocked';
158 break;
159
160 case LoginForm::ABORTED:
161 $result['result'] = 'Aborted';
162 $result['reason'] = $loginForm->mAbortLoginErrorMsg;
163 break;
164
165 default:
166 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
167 }
168
169 $this->getResult()->addValue( null, 'login', $result );
170 }
171
172 public function mustBePosted() {
173 return true;
174 }
175
176 public function isReadMode() {
177 return false;
178 }
179
180 public function getAllowedParams() {
181 return array(
182 'name' => null,
183 'password' => null,
184 'domain' => null,
185 'token' => null,
186 );
187 }
188
189 public function getParamDescription() {
190 return array(
191 'name' => 'User Name',
192 'password' => 'Password',
193 'domain' => 'Domain (optional)',
194 'token' => 'Login token obtained in first request',
195 );
196 }
197
198 public function getResultProperties() {
199 return array(
200 '' => array(
201 'result' => array(
202 ApiBase::PROP_TYPE => array(
203 'Success',
204 'NeedToken',
205 'WrongToken',
206 'NoName',
207 'Illegal',
208 'WrongPluginPass',
209 'NotExists',
210 'WrongPass',
211 'EmptyPass',
212 'CreateBlocked',
213 'Throttled',
214 'Blocked',
215 'Aborted'
216 )
217 ),
218 'lguserid' => array(
219 ApiBase::PROP_TYPE => 'integer',
220 ApiBase::PROP_NULLABLE => true
221 ),
222 'lgusername' => array(
223 ApiBase::PROP_TYPE => 'string',
224 ApiBase::PROP_NULLABLE => true
225 ),
226 'lgtoken' => array(
227 ApiBase::PROP_TYPE => 'string',
228 ApiBase::PROP_NULLABLE => true
229 ),
230 'cookieprefix' => array(
231 ApiBase::PROP_TYPE => 'string',
232 ApiBase::PROP_NULLABLE => true
233 ),
234 'sessionid' => array(
235 ApiBase::PROP_TYPE => 'string',
236 ApiBase::PROP_NULLABLE => true
237 ),
238 'token' => array(
239 ApiBase::PROP_TYPE => 'string',
240 ApiBase::PROP_NULLABLE => true
241 ),
242 'details' => array(
243 ApiBase::PROP_TYPE => 'string',
244 ApiBase::PROP_NULLABLE => true
245 ),
246 'wait' => array(
247 ApiBase::PROP_TYPE => 'integer',
248 ApiBase::PROP_NULLABLE => true
249 ),
250 'reason' => array(
251 ApiBase::PROP_TYPE => 'string',
252 ApiBase::PROP_NULLABLE => true
253 )
254 )
255 );
256 }
257
258 public function getDescription() {
259 return array(
260 'Log in and get the authentication tokens.',
261 'In the event of a successful log-in, a cookie will be attached to your session.',
262 'In the event of a failed log-in, you will not be able to attempt another log-in',
263 'through this method for 5 seconds. This is to prevent password guessing by',
264 'automated password crackers'
265 );
266 }
267
268 public function getPossibleErrors() {
269 return array_merge( parent::getPossibleErrors(), array(
270 array(
271 'code' => 'NeedToken', 'info' => 'You need to resubmit your ' .
272 'login with the specified token. See ' .
273 'https://bugzilla.wikimedia.org/show_bug.cgi?id=23076'
274 ),
275 array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
276 array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
277 array( 'code' => 'Illegal', 'info' => 'You provided an illegal username' ),
278 array( 'code' => 'NotExists', 'info' => 'The username you provided doesn\'t exist' ),
279 array(
280 'code' => 'EmptyPass',
281 'info' => 'You didn\'t set the lgpassword parameter or you left it empty'
282 ),
283 array( 'code' => 'WrongPass', 'info' => 'The password you provided is incorrect' ),
284 array(
285 'code' => 'WrongPluginPass',
286 'info' => 'Same as "WrongPass", returned when an authentication ' .
287 'plugin rather than MediaWiki itself rejected the password'
288 ),
289 array(
290 'code' => 'CreateBlocked',
291 'info' => 'The wiki tried to automatically create a new account ' .
292 'for you, but your IP address has been blocked from account creation'
293 ),
294 array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
295 array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
296 ) );
297 }
298
299 public function getExamples() {
300 return array(
301 'api.php?action=login&lgname=user&lgpassword=password'
302 );
303 }
304
305 public function getHelpUrls() {
306 return 'https://www.mediawiki.org/wiki/API:Login';
307 }
308 }