API login module: coding style cleanup, remove stray semicolon, use proper copyright...
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 'wpRemember' => ''
62 ) );
63
64 // Init session if necessary
65 if ( session_id() == '' ) {
66 wfSetupSession();
67 }
68
69 $loginForm = new LoginForm( $req );
70 switch ( $authRes = $loginForm->authenticateUserData() ) {
71 case LoginForm::SUCCESS:
72 global $wgUser, $wgCookiePrefix;
73
74 $wgUser->setOption( 'rememberpassword', 1 );
75 $wgUser->setCookies();
76
77 // Run hooks. FIXME: split back and frontend from this hook.
78 // FIXME: This hook should be placed in the backend
79 $injected_html = '';
80 wfRunHooks( 'UserLoginComplete', array( &$wgUser, &$injected_html ) );
81
82 $result['result'] = 'Success';
83 $result['lguserid'] = intval( $wgUser->getId() );
84 $result['lgusername'] = $wgUser->getName();
85 $result['lgtoken'] = $wgUser->getToken();
86 $result['cookieprefix'] = $wgCookiePrefix;
87 $result['sessionid'] = session_id();
88 break;
89
90 case LoginForm::NO_NAME:
91 $result['result'] = 'NoName';
92 break;
93
94 case LoginForm::ILLEGAL:
95 $result['result'] = 'Illegal';
96 break;
97
98 case LoginForm::WRONG_PLUGIN_PASS:
99 $result['result'] = 'WrongPluginPass';
100 break;
101
102 case LoginForm::NOT_EXISTS:
103 $result['result'] = 'NotExists';
104 break;
105
106 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;"
107 case LoginForm::WRONG_PASS:
108 $result['result'] = 'WrongPass';
109 break;
110
111 case LoginForm::EMPTY_PASS:
112 $result['result'] = 'EmptyPass';
113 break;
114
115 case LoginForm::CREATE_BLOCKED:
116 $result['result'] = 'CreateBlocked';
117 $result['details'] = 'Your IP address is blocked from account creation';
118 break;
119
120 case LoginForm::THROTTLED:
121 global $wgPasswordAttemptThrottle;
122 $result['result'] = 'Throttled';
123 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
124 break;
125
126 case LoginForm::USER_BLOCKED:
127 $result['result'] = 'Blocked';
128 break;
129
130 default:
131 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
132 }
133
134 $this->getResult()->addValue( null, 'login', $result );
135 }
136
137 public function mustBePosted() {
138 return true;
139 }
140
141 public function isReadMode() {
142 return false;
143 }
144
145 public function getAllowedParams() {
146 return array(
147 'name' => null,
148 'password' => null,
149 'domain' => null
150 );
151 }
152
153 public function getParamDescription() {
154 return array(
155 'name' => 'User Name',
156 'password' => 'Password',
157 'domain' => 'Domain (optional)'
158 );
159 }
160
161 public function getDescription() {
162 return array(
163 'This module is used to login and get the authentication tokens. ',
164 'In the event of a successful log-in, a cookie will be attached',
165 'to your session. In the event of a failed log-in, you will not ',
166 'be able to attempt another log-in through this method for 5 seconds.',
167 'This is to prevent password guessing by automated password crackers.'
168 );
169 }
170
171 public function getPossibleErrors() {
172 return array_merge( parent::getPossibleErrors(), array(
173 array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
174 array( 'code' => 'Illegal', 'info' => ' You provided an illegal username' ),
175 array( 'code' => 'NotExists', 'info' => ' The username you provided doesn\'t exist' ),
176 array( 'code' => 'EmptyPass', 'info' => ' You didn\'t set the lgpassword parameter or you left it empty' ),
177 array( 'code' => 'WrongPass', 'info' => ' The password you provided is incorrect' ),
178 array( 'code' => 'WrongPluginPass', 'info' => 'Same as `WrongPass", returned when an authentication plugin rather than MediaWiki itself rejected the password' ),
179 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' ),
180 array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
181 array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
182 ) );
183 }
184
185 protected function getExamples() {
186 return array(
187 'api.php?action=login&lgname=user&lgpassword=password'
188 );
189 }
190
191 public function getVersion() {
192 return __CLASS__ . ': $Id$';
193 }
194 }