Follow-up r69898: fix Undefined variable: wgCookiePrefix in /ApiLogin.php on line 94
[lhc/web/wiklou.git] / includes / api / ApiLogin.php
1 <?php
2
3 /**
4 * Created on Sep 19, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2006-2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com,
9 * Daniel Cannon (cannon dot danielc at gmail dot com)
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiBase.php' );
30 }
31
32 /**
33 * Unit to authenticate log-in attempts to the current wiki.
34 *
35 * @ingroup API
36 */
37 class ApiLogin extends ApiBase {
38
39 public function __construct( $main, $action ) {
40 parent::__construct( $main, $action, 'lg' );
41 }
42
43 /**
44 * Executes the log-in attempt using the parameters passed. If
45 * the log-in succeeeds, it attaches a cookie to the session
46 * and outputs the user id, username, and session token. If a
47 * log-in fails, as the result of a bad password, a nonexistent
48 * user, or any other reason, the host is cached with an expiry
49 * and no log-in attempts will be accepted until that expiry
50 * is reached. The expiry is $this->mLoginThrottle.
51 */
52 public function execute() {
53 $params = $this->extractRequestParams();
54
55 $result = array();
56
57 $req = new FauxRequest( array(
58 'wpName' => $params['name'],
59 'wpPassword' => $params['password'],
60 'wpDomain' => $params['domain'],
61 'wpLoginToken' => $params['token'],
62 'wpRemember' => ''
63 ) );
64
65 // Init session if necessary
66 if ( session_id() == '' ) {
67 wfSetupSession();
68 }
69
70 $loginForm = new LoginForm( $req );
71
72 global $wgCookiePrefix;
73
74 switch ( $authRes = $loginForm->authenticateUserData() ) {
75 case LoginForm::SUCCESS:
76 global $wgUser;
77
78 $wgUser->setOption( 'rememberpassword', 1 );
79 $wgUser->setCookies();
80
81 // Run hooks. FIXME: split back and frontend from this hook.
82 // FIXME: This hook should be placed in the backend
83 $injected_html = '';
84 wfRunHooks( 'UserLoginComplete', array( &$wgUser, &$injected_html ) );
85
86 $result['result'] = 'Success';
87 $result['lguserid'] = intval( $wgUser->getId() );
88 $result['lgusername'] = $wgUser->getName();
89 $result['lgtoken'] = $wgUser->getToken();
90 $result['cookieprefix'] = $wgCookiePrefix;
91 $result['sessionid'] = session_id();
92 break;
93
94 case LoginForm::NEED_TOKEN:
95 $result['result'] = 'NeedToken';
96 $result['token'] = $loginForm->getLoginToken();
97 $result['cookieprefix'] = $wgCookiePrefix;
98 $result['sessionid'] = session_id();
99 break;
100
101 case LoginForm::WRONG_TOKEN:
102 $result['result'] = 'WrongToken';
103 break;
104
105 case LoginForm::NO_NAME:
106 $result['result'] = 'NoName';
107 break;
108
109 case LoginForm::ILLEGAL:
110 $result['result'] = 'Illegal';
111 break;
112
113 case LoginForm::WRONG_PLUGIN_PASS:
114 $result['result'] = 'WrongPluginPass';
115 break;
116
117 case LoginForm::NOT_EXISTS:
118 $result['result'] = 'NotExists';
119 break;
120
121 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;"
122 case LoginForm::WRONG_PASS:
123 $result['result'] = 'WrongPass';
124 break;
125
126 case LoginForm::EMPTY_PASS:
127 $result['result'] = 'EmptyPass';
128 break;
129
130 case LoginForm::CREATE_BLOCKED:
131 $result['result'] = 'CreateBlocked';
132 $result['details'] = 'Your IP address is blocked from account creation';
133 break;
134
135 case LoginForm::THROTTLED:
136 global $wgPasswordAttemptThrottle;
137 $result['result'] = 'Throttled';
138 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
139 break;
140
141 case LoginForm::USER_BLOCKED:
142 $result['result'] = 'Blocked';
143 break;
144
145 default:
146 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
147 }
148
149 $this->getResult()->addValue( null, 'login', $result );
150 }
151
152 public function mustBePosted() {
153 return true;
154 }
155
156 public function isReadMode() {
157 return false;
158 }
159
160 public function getAllowedParams() {
161 return array(
162 'name' => null,
163 'password' => null,
164 'domain' => null,
165 'token' => null,
166 );
167 }
168
169 public function getParamDescription() {
170 return array(
171 'name' => 'User Name',
172 'password' => 'Password',
173 'domain' => 'Domain (optional)',
174 'token' => 'Login token obtained in first request',
175 );
176 }
177
178 public function getDescription() {
179 return array(
180 'This module is used to login and get the authentication tokens. ',
181 'In the event of a successful log-in, a cookie will be attached',
182 'to your session. In the event of a failed log-in, you will not ',
183 'be able to attempt another log-in through this method for 5 seconds.',
184 'This is to prevent password guessing by automated password crackers'
185 );
186 }
187
188 public function getPossibleErrors() {
189 return array_merge( parent::getPossibleErrors(), array(
190 array( 'code' => 'NeedToken', 'info' => 'You need to resubmit your login with the specified token. See https://bugzilla.wikimedia.org/show_bug.cgi?id=23076' ),
191 array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
192 array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
193 array( 'code' => 'Illegal', 'info' => ' You provided an illegal username' ),
194 array( 'code' => 'NotExists', 'info' => ' The username you provided doesn\'t exist' ),
195 array( 'code' => 'EmptyPass', 'info' => ' You didn\'t set the lgpassword parameter or you left it empty' ),
196 array( 'code' => 'WrongPass', 'info' => ' The password you provided is incorrect' ),
197 array( 'code' => 'WrongPluginPass', 'info' => 'Same as `WrongPass", returned when an authentication plugin rather than MediaWiki itself rejected the password' ),
198 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' ),
199 array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
200 array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
201 ) );
202 }
203
204 protected function getExamples() {
205 return array(
206 'api.php?action=login&lgname=user&lgpassword=password'
207 );
208 }
209
210 public function getVersion() {
211 return __CLASS__ . ': $Id$';
212 }
213 }