Merge "Set context when using UserrightsPage"
[mediawiki.git] / includes / api / ApiLogin.php
blobb936d3beea6a95c5248e2086c5973529399247b3
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 /**
29 * Unit to authenticate log-in attempts to the current wiki.
31 * @ingroup API
33 class ApiLogin extends ApiBase {
35 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action, 'lg' );
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.
48 public function execute() {
49 $params = $this->extractRequestParams();
51 $result = array();
53 // Init session if necessary
54 if ( session_id() == '' ) {
55 wfSetupSession();
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' => ''
68 ) );
69 $loginForm = new LoginForm();
70 $loginForm->setContext( $context );
72 global $wgCookiePrefix, $wgPasswordAttemptThrottle;
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() );
82 ApiQueryInfo::resetTokenCache();
84 // Run hooks.
85 // @todo FIXME: Split back and frontend from this hook.
86 // @todo FIXME: This hook should be placed in the backend
87 $injected_html = '';
88 wfRunHooks( 'UserLoginComplete', array( &$user, &$injected_html ) );
90 $result['result'] = 'Success';
91 $result['lguserid'] = intval( $user->getId() );
92 $result['lgusername'] = $user->getName();
93 $result['lgtoken'] = $user->getToken();
94 $result['cookieprefix'] = $wgCookiePrefix;
95 $result['sessionid'] = session_id();
96 break;
98 case LoginForm::NEED_TOKEN:
99 $result['result'] = 'NeedToken';
100 $result['token'] = $loginForm->getLoginToken();
101 $result['cookieprefix'] = $wgCookiePrefix;
102 $result['sessionid'] = session_id();
103 break;
105 case LoginForm::WRONG_TOKEN:
106 $result['result'] = 'WrongToken';
107 break;
109 case LoginForm::NO_NAME:
110 $result['result'] = 'NoName';
111 break;
113 case LoginForm::ILLEGAL:
114 $result['result'] = 'Illegal';
115 break;
117 case LoginForm::WRONG_PLUGIN_PASS:
118 $result['result'] = 'WrongPluginPass';
119 break;
121 case LoginForm::NOT_EXISTS:
122 $result['result'] = 'NotExists';
123 break;
125 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;"
126 case LoginForm::WRONG_PASS:
127 $result['result'] = 'WrongPass';
128 break;
130 case LoginForm::EMPTY_PASS:
131 $result['result'] = 'EmptyPass';
132 break;
134 case LoginForm::CREATE_BLOCKED:
135 $result['result'] = 'CreateBlocked';
136 $result['details'] = 'Your IP address is blocked from account creation';
137 break;
139 case LoginForm::THROTTLED:
140 $result['result'] = 'Throttled';
141 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
142 break;
144 case LoginForm::USER_BLOCKED:
145 $result['result'] = 'Blocked';
146 break;
148 case LoginForm::ABORTED:
149 $result['result'] = 'Aborted';
150 $result['reason'] = $loginForm->mAbortLoginErrorMsg;
151 break;
153 default:
154 ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
157 $this->getResult()->addValue( null, 'login', $result );
160 public function mustBePosted() {
161 return true;
164 public function isReadMode() {
165 return false;
168 public function getAllowedParams() {
169 return array(
170 'name' => null,
171 'password' => null,
172 'domain' => null,
173 'token' => null,
177 public function getParamDescription() {
178 return array(
179 'name' => 'User Name',
180 'password' => 'Password',
181 'domain' => 'Domain (optional)',
182 'token' => 'Login token obtained in first request',
186 public function getResultProperties() {
187 return array(
188 '' => array(
189 'result' => array(
190 ApiBase::PROP_TYPE => array(
191 'Success',
192 'NeedToken',
193 'WrongToken',
194 'NoName',
195 'Illegal',
196 'WrongPluginPass',
197 'NotExists',
198 'WrongPass',
199 'EmptyPass',
200 'CreateBlocked',
201 'Throttled',
202 'Blocked',
203 'Aborted'
206 'lguserid' => array(
207 ApiBase::PROP_TYPE => 'integer',
208 ApiBase::PROP_NULLABLE => true
210 'lgusername' => array(
211 ApiBase::PROP_TYPE => 'string',
212 ApiBase::PROP_NULLABLE => true
214 'lgtoken' => array(
215 ApiBase::PROP_TYPE => 'string',
216 ApiBase::PROP_NULLABLE => true
218 'cookieprefix' => array(
219 ApiBase::PROP_TYPE => 'string',
220 ApiBase::PROP_NULLABLE => true
222 'sessionid' => array(
223 ApiBase::PROP_TYPE => 'string',
224 ApiBase::PROP_NULLABLE => true
226 'token' => array(
227 ApiBase::PROP_TYPE => 'string',
228 ApiBase::PROP_NULLABLE => true
230 'details' => array(
231 ApiBase::PROP_TYPE => 'string',
232 ApiBase::PROP_NULLABLE => true
234 'wait' => array(
235 ApiBase::PROP_TYPE => 'integer',
236 ApiBase::PROP_NULLABLE => true
238 'reason' => array(
239 ApiBase::PROP_TYPE => 'string',
240 ApiBase::PROP_NULLABLE => true
246 public function getDescription() {
247 return array(
248 'Log in and get the authentication tokens. ',
249 'In the event of a successful log-in, a cookie will be attached',
250 'to your session. In the event of a failed log-in, you will not ',
251 'be able to attempt another log-in through this method for 5 seconds.',
252 'This is to prevent password guessing by automated password crackers'
256 public function getPossibleErrors() {
257 return array_merge( parent::getPossibleErrors(), array(
258 array( 'code' => 'NeedToken', 'info' => 'You need to resubmit your login with the specified token. See https://bugzilla.wikimedia.org/show_bug.cgi?id=23076' ),
259 array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
260 array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
261 array( 'code' => 'Illegal', 'info' => ' You provided an illegal username' ),
262 array( 'code' => 'NotExists', 'info' => ' The username you provided doesn\'t exist' ),
263 array( 'code' => 'EmptyPass', 'info' => ' You didn\'t set the lgpassword parameter or you left it empty' ),
264 array( 'code' => 'WrongPass', 'info' => ' The password you provided is incorrect' ),
265 array( 'code' => 'WrongPluginPass', 'info' => 'Same as "WrongPass", returned when an authentication plugin rather than MediaWiki itself rejected the password' ),
266 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' ),
267 array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
268 array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
269 ) );
272 public function getExamples() {
273 return array(
274 'api.php?action=login&lgname=user&lgpassword=password'
278 public function getHelpUrls() {
279 return 'https://www.mediawiki.org/wiki/API:Login';