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
29 * Unit to authenticate log-in attempts to the current wiki.
33 class ApiLogin
extends ApiBase
{
35 public function __construct( $main, $action ) {
36 parent
::__construct( $main, $action, 'lg' );
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 // If we're in JSON callback mode, no tokens can be obtained
50 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
51 $this->getResult()->addValue( null, 'login', array(
52 'result' => 'Aborted',
53 'reason' => 'Cannot log in when using a callback',
58 $params = $this->extractRequestParams();
62 // Init session if necessary
63 if ( session_id() == '' ) {
67 $context = new DerivativeContext( $this->getContext() );
68 $context->setRequest( new DerivativeRequest(
69 $this->getContext()->getRequest(),
71 'wpName' => $params['name'],
72 'wpPassword' => $params['password'],
73 'wpDomain' => $params['domain'],
74 'wpLoginToken' => $params['token'],
78 $loginForm = new LoginForm();
79 $loginForm->setContext( $context );
81 global $wgCookiePrefix, $wgPasswordAttemptThrottle;
83 $authRes = $loginForm->authenticateUserData();
85 case LoginForm
::SUCCESS
:
86 $user = $context->getUser();
87 $this->getContext()->setUser( $user );
88 $user->setOption( 'rememberpassword', 1 );
89 $user->setCookies( $this->getRequest() );
91 ApiQueryInfo
::resetTokenCache();
94 // @todo FIXME: Split back and frontend from this hook.
95 // @todo FIXME: This hook should be placed in the backend
97 wfRunHooks( 'UserLoginComplete', array( &$user, &$injected_html ) );
99 $result['result'] = 'Success';
100 $result['lguserid'] = intval( $user->getId() );
101 $result['lgusername'] = $user->getName();
102 $result['lgtoken'] = $user->getToken();
103 $result['cookieprefix'] = $wgCookiePrefix;
104 $result['sessionid'] = session_id();
107 case LoginForm
::NEED_TOKEN
:
108 $result['result'] = 'NeedToken';
109 $result['token'] = $loginForm->getLoginToken();
110 $result['cookieprefix'] = $wgCookiePrefix;
111 $result['sessionid'] = session_id();
114 case LoginForm
::WRONG_TOKEN
:
115 $result['result'] = 'WrongToken';
118 case LoginForm
::NO_NAME
:
119 $result['result'] = 'NoName';
122 case LoginForm
::ILLEGAL
:
123 $result['result'] = 'Illegal';
126 case LoginForm
::WRONG_PLUGIN_PASS
:
127 $result['result'] = 'WrongPluginPass';
130 case LoginForm
::NOT_EXISTS
:
131 $result['result'] = 'NotExists';
134 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;"
135 case LoginForm
::WRONG_PASS
:
136 $result['result'] = 'WrongPass';
139 case LoginForm
::EMPTY_PASS
:
140 $result['result'] = 'EmptyPass';
143 case LoginForm
::CREATE_BLOCKED
:
144 $result['result'] = 'CreateBlocked';
145 $result['details'] = 'Your IP address is blocked from account creation';
148 case LoginForm
::THROTTLED
:
149 $result['result'] = 'Throttled';
150 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
153 case LoginForm
::USER_BLOCKED
:
154 $result['result'] = 'Blocked';
157 case LoginForm
::ABORTED
:
158 $result['result'] = 'Aborted';
159 $result['reason'] = $loginForm->mAbortLoginErrorMsg
;
163 ApiBase
::dieDebug( __METHOD__
, "Unhandled case value: {$authRes}" );
166 $this->getResult()->addValue( null, 'login', $result );
169 public function mustBePosted() {
173 public function isReadMode() {
177 public function getAllowedParams() {
186 public function getParamDescription() {
188 'name' => 'User Name',
189 'password' => 'Password',
190 'domain' => 'Domain (optional)',
191 'token' => 'Login token obtained in first request',
195 public function getResultProperties() {
199 ApiBase
::PROP_TYPE
=> array(
216 ApiBase
::PROP_TYPE
=> 'integer',
217 ApiBase
::PROP_NULLABLE
=> true
219 'lgusername' => array(
220 ApiBase
::PROP_TYPE
=> 'string',
221 ApiBase
::PROP_NULLABLE
=> true
224 ApiBase
::PROP_TYPE
=> 'string',
225 ApiBase
::PROP_NULLABLE
=> true
227 'cookieprefix' => array(
228 ApiBase
::PROP_TYPE
=> 'string',
229 ApiBase
::PROP_NULLABLE
=> true
231 'sessionid' => array(
232 ApiBase
::PROP_TYPE
=> 'string',
233 ApiBase
::PROP_NULLABLE
=> true
236 ApiBase
::PROP_TYPE
=> 'string',
237 ApiBase
::PROP_NULLABLE
=> true
240 ApiBase
::PROP_TYPE
=> 'string',
241 ApiBase
::PROP_NULLABLE
=> true
244 ApiBase
::PROP_TYPE
=> 'integer',
245 ApiBase
::PROP_NULLABLE
=> true
248 ApiBase
::PROP_TYPE
=> 'string',
249 ApiBase
::PROP_NULLABLE
=> true
255 public function getDescription() {
257 'Log in and get the authentication tokens.',
258 'In the event of a successful log-in, a cookie will be attached',
259 'to your session. In the event of a failed log-in, you will not',
260 'be able to attempt another log-in through this method for 5 seconds.',
261 'This is to prevent password guessing by automated password crackers'
265 public function getPossibleErrors() {
266 return array_merge( parent
::getPossibleErrors(), array(
267 array( 'code' => 'NeedToken', 'info' => 'You need to resubmit your login with the specified token. See https://bugzilla.wikimedia.org/show_bug.cgi?id=23076' ),
268 array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
269 array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
270 array( 'code' => 'Illegal', 'info' => 'You provided an illegal username' ),
271 array( 'code' => 'NotExists', 'info' => 'The username you provided doesn\'t exist' ),
272 array( 'code' => 'EmptyPass', 'info' => 'You didn\'t set the lgpassword parameter or you left it empty' ),
273 array( 'code' => 'WrongPass', 'info' => 'The password you provided is incorrect' ),
274 array( 'code' => 'WrongPluginPass', 'info' => 'Same as "WrongPass", returned when an authentication plugin rather than MediaWiki itself rejected the password' ),
275 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' ),
276 array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
277 array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
281 public function getExamples() {
283 'api.php?action=login&lgname=user&lgpassword=password'
287 public function getHelpUrls() {
288 return 'https://www.mediawiki.org/wiki/API:Login';