Merge "Added some extra tests for ORMRow class"
[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 succeeeds, 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 $params = $this->extractRequestParams();
50
51 $result = array();
52
53 // Init session if necessary
54 if ( session_id() == '' ) {
55 wfSetupSession();
56 }
57
58 $context = new DerivativeContext( $this->getContext() );
59 $context->setRequest( new DerivativeRequest(
60 $this->getContext()->getRequest(),
61 array(
62 'wpName' => $params['name'],
63 'wpPassword' => $params['password'],
64 'wpDomain' => $params['domain'],
65 'wpLoginToken' => $params['token'],
66 'wpRemember' => ''
67 )
68 ) );
69 $loginForm = new LoginForm();
70 $loginForm->setContext( $context );
71
72 global $wgCookiePrefix, $wgPasswordAttemptThrottle;
73
74 $authRes = $loginForm->authenticateUserData();
75 switch ( $authRes ) {
76 case LoginForm::SUCCESS:
77 $user = $context->getUser();
78 $this->getContext()->setUser( $user );
79 $user->setOption( 'rememberpassword', 1 );
80 $user->setCookies( $this->getRequest() );
81
82 // Run hooks.
83 // @todo FIXME: Split back and frontend from this hook.
84 // @todo FIXME: This hook should be placed in the backend
85 $injected_html = '';
86 wfRunHooks( 'UserLoginComplete', array( &$user, &$injected_html ) );
87
88 $result['result'] = 'Success';
89 $result['lguserid'] = intval( $user->getId() );
90 $result['lgusername'] = $user->getName();
91 $result['lgtoken'] = $user->getToken();
92 $result['cookieprefix'] = $wgCookiePrefix;
93 $result['sessionid'] = session_id();
94 break;
95
96 case LoginForm::NEED_TOKEN:
97 $result['result'] = 'NeedToken';
98 $result['token'] = $loginForm->getLoginToken();
99 $result['cookieprefix'] = $wgCookiePrefix;
100 $result['sessionid'] = session_id();
101 break;
102
103 case LoginForm::WRONG_TOKEN:
104 $result['result'] = 'WrongToken';
105 break;
106
107 case LoginForm::NO_NAME:
108 $result['result'] = 'NoName';
109 break;
110
111 case LoginForm::ILLEGAL:
112 $result['result'] = 'Illegal';
113 break;
114
115 case LoginForm::WRONG_PLUGIN_PASS:
116 $result['result'] = 'WrongPluginPass';
117 break;
118
119 case LoginForm::NOT_EXISTS:
120 $result['result'] = 'NotExists';
121 break;
122
123 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;"
124 case LoginForm::WRONG_PASS:
125 $result['result'] = 'WrongPass';
126 break;
127
128 case LoginForm::EMPTY_PASS:
129 $result['result'] = 'EmptyPass';
130 break;
131
132 case LoginForm::CREATE_BLOCKED:
133 $result['result'] = 'CreateBlocked';
134 $result['details'] = 'Your IP address is blocked from account creation';
135 break;
136
137 case LoginForm::THROTTLED:
138 $result['result'] = 'Throttled';
139 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
140 break;
141
142 case LoginForm::USER_BLOCKED:
143 $result['result'] = 'Blocked';
144 break;
145
146 case LoginForm::ABORTED:
147 $result['result'] = 'Aborted';
148 $result['reason'] = $loginForm->mAbortLoginErrorMsg;
149 break;
150
151 default:
152 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
153 }
154
155 $this->getResult()->addValue( null, 'login', $result );
156 }
157
158 public function mustBePosted() {
159 return true;
160 }
161
162 public function isReadMode() {
163 return false;
164 }
165
166 public function getAllowedParams() {
167 return array(
168 'name' => null,
169 'password' => null,
170 'domain' => null,
171 'token' => null,
172 );
173 }
174
175 public function getParamDescription() {
176 return array(
177 'name' => 'User Name',
178 'password' => 'Password',
179 'domain' => 'Domain (optional)',
180 'token' => 'Login token obtained in first request',
181 );
182 }
183
184 public function getResultProperties() {
185 return array(
186 '' => array(
187 'result' => array(
188 ApiBase::PROP_TYPE => array(
189 'Success',
190 'NeedToken',
191 'WrongToken',
192 'NoName',
193 'Illegal',
194 'WrongPluginPass',
195 'NotExists',
196 'WrongPass',
197 'EmptyPass',
198 'CreateBlocked',
199 'Throttled',
200 'Blocked',
201 'Aborted'
202 )
203 ),
204 'lguserid' => array(
205 ApiBase::PROP_TYPE => 'integer',
206 ApiBase::PROP_NULLABLE => true
207 ),
208 'lgusername' => array(
209 ApiBase::PROP_TYPE => 'string',
210 ApiBase::PROP_NULLABLE => true
211 ),
212 'lgtoken' => array(
213 ApiBase::PROP_TYPE => 'string',
214 ApiBase::PROP_NULLABLE => true
215 ),
216 'cookieprefix' => array(
217 ApiBase::PROP_TYPE => 'string',
218 ApiBase::PROP_NULLABLE => true
219 ),
220 'sessionid' => array(
221 ApiBase::PROP_TYPE => 'string',
222 ApiBase::PROP_NULLABLE => true
223 ),
224 'token' => array(
225 ApiBase::PROP_TYPE => 'string',
226 ApiBase::PROP_NULLABLE => true
227 ),
228 'details' => array(
229 ApiBase::PROP_TYPE => 'string',
230 ApiBase::PROP_NULLABLE => true
231 ),
232 'wait' => array(
233 ApiBase::PROP_TYPE => 'integer',
234 ApiBase::PROP_NULLABLE => true
235 ),
236 'reason' => array(
237 ApiBase::PROP_TYPE => 'string',
238 ApiBase::PROP_NULLABLE => true
239 )
240 )
241 );
242 }
243
244 public function getDescription() {
245 return array(
246 'Log in and get the authentication tokens. ',
247 'In the event of a successful log-in, a cookie will be attached',
248 'to your session. In the event of a failed log-in, you will not ',
249 'be able to attempt another log-in through this method for 5 seconds.',
250 'This is to prevent password guessing by automated password crackers'
251 );
252 }
253
254 public function getPossibleErrors() {
255 return array_merge( parent::getPossibleErrors(), array(
256 array( 'code' => 'NeedToken', 'info' => 'You need to resubmit your login with the specified token. See https://bugzilla.wikimedia.org/show_bug.cgi?id=23076' ),
257 array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
258 array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
259 array( 'code' => 'Illegal', 'info' => ' You provided an illegal username' ),
260 array( 'code' => 'NotExists', 'info' => ' The username you provided doesn\'t exist' ),
261 array( 'code' => 'EmptyPass', 'info' => ' You didn\'t set the lgpassword parameter or you left it empty' ),
262 array( 'code' => 'WrongPass', 'info' => ' The password you provided is incorrect' ),
263 array( 'code' => 'WrongPluginPass', 'info' => 'Same as "WrongPass", returned when an authentication plugin rather than MediaWiki itself rejected the password' ),
264 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' ),
265 array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
266 array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
267 ) );
268 }
269
270 public function getExamples() {
271 return array(
272 'api.php?action=login&lgname=user&lgpassword=password'
273 );
274 }
275
276 public function getHelpUrls() {
277 return 'https://www.mediawiki.org/wiki/API:Login';
278 }
279
280 public function getVersion() {
281 return __CLASS__ . ': $Id$';
282 }
283 }