* changed display function for length to Linker::formatRevisionSize
[mediawiki.git] / includes / api / ApiLogin.php
blob5eb7ce45c1f188a72cfe03fb0f78e56b21b56a59
1 <?php
2 /**
5 * Created on Sep 19, 2006
7 * Copyright © 2006-2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com,
8 * Daniel Cannon (cannon dot danielc at gmail dot com)
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.
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.
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
25 * @file
28 if ( !defined( 'MEDIAWIKI' ) ) {
29 // Eclipse helper - will be ignored in production
30 require_once( 'ApiBase.php' );
33 /**
34 * Unit to authenticate log-in attempts to the current wiki.
36 * @ingroup API
38 class ApiLogin extends ApiBase {
40 public function __construct( $main, $action ) {
41 parent::__construct( $main, $action, 'lg' );
44 /**
45 * Executes the log-in attempt using the parameters passed. If
46 * the log-in succeeeds, it attaches a cookie to the session
47 * and outputs the user id, username, and session token. If a
48 * log-in fails, as the result of a bad password, a nonexistent
49 * user, or any other reason, the host is cached with an expiry
50 * and no log-in attempts will be accepted until that expiry
51 * is reached. The expiry is $this->mLoginThrottle.
53 public function execute() {
54 $params = $this->extractRequestParams();
56 $result = array();
58 $req = new FauxRequest( array(
59 'wpName' => $params['name'],
60 'wpPassword' => $params['password'],
61 'wpDomain' => $params['domain'],
62 'wpLoginToken' => $params['token'],
63 'wpRemember' => ''
64 ) );
66 // Init session if necessary
67 if ( session_id() == '' ) {
68 wfSetupSession();
71 $loginForm = new LoginForm( $req );
73 global $wgCookiePrefix, $wgUser, $wgPasswordAttemptThrottle;
75 $authRes = $loginForm->authenticateUserData();
76 switch ( $authRes ) {
77 case LoginForm::SUCCESS:
78 $wgUser->setOption( 'rememberpassword', 1 );
79 $wgUser->setCookies( $this->getMain()->getRequest() );
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 ) );
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;
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;
101 case LoginForm::WRONG_TOKEN:
102 $result['result'] = 'WrongToken';
103 break;
105 case LoginForm::NO_NAME:
106 $result['result'] = 'NoName';
107 break;
109 case LoginForm::ILLEGAL:
110 $result['result'] = 'Illegal';
111 break;
113 case LoginForm::WRONG_PLUGIN_PASS:
114 $result['result'] = 'WrongPluginPass';
115 break;
117 case LoginForm::NOT_EXISTS:
118 $result['result'] = 'NotExists';
119 break;
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;
126 case LoginForm::EMPTY_PASS:
127 $result['result'] = 'EmptyPass';
128 break;
130 case LoginForm::CREATE_BLOCKED:
131 $result['result'] = 'CreateBlocked';
132 $result['details'] = 'Your IP address is blocked from account creation';
133 break;
135 case LoginForm::THROTTLED:
136 $result['result'] = 'Throttled';
137 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
138 break;
140 case LoginForm::USER_BLOCKED:
141 $result['result'] = 'Blocked';
142 break;
144 case LoginForm::ABORTED:
145 $result['result'] = 'Aborted';
146 $result['reason'] = $loginForm->mAbortLoginErrorMsg;
147 break;
149 default:
150 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
153 $this->getResult()->addValue( null, 'login', $result );
156 public function mustBePosted() {
157 return true;
160 public function isReadMode() {
161 return false;
164 public function getAllowedParams() {
165 return array(
166 'name' => null,
167 'password' => null,
168 'domain' => null,
169 'token' => null,
173 public function getParamDescription() {
174 return array(
175 'name' => 'User Name',
176 'password' => 'Password',
177 'domain' => 'Domain (optional)',
178 'token' => 'Login token obtained in first request',
182 public function getDescription() {
183 return array(
184 'Log in and get the authentication tokens. ',
185 'In the event of a successful log-in, a cookie will be attached',
186 'to your session. In the event of a failed log-in, you will not ',
187 'be able to attempt another log-in through this method for 5 seconds.',
188 'This is to prevent password guessing by automated password crackers'
192 public function getPossibleErrors() {
193 return array_merge( parent::getPossibleErrors(), array(
194 array( 'code' => 'NeedToken', 'info' => 'You need to resubmit your login with the specified token. See https://bugzilla.wikimedia.org/show_bug.cgi?id=23076' ),
195 array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
196 array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
197 array( 'code' => 'Illegal', 'info' => ' You provided an illegal username' ),
198 array( 'code' => 'NotExists', 'info' => ' The username you provided doesn\'t exist' ),
199 array( 'code' => 'EmptyPass', 'info' => ' You didn\'t set the lgpassword parameter or you left it empty' ),
200 array( 'code' => 'WrongPass', 'info' => ' The password you provided is incorrect' ),
201 array( 'code' => 'WrongPluginPass', 'info' => 'Same as `WrongPass", returned when an authentication plugin rather than MediaWiki itself rejected the password' ),
202 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' ),
203 array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
204 array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
205 ) );
208 protected function getExamples() {
209 return array(
210 'api.php?action=login&lgname=user&lgpassword=password'
214 public function getVersion() {
215 return __CLASS__ . ': $Id$';