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',
59 $params = $this->extractRequestParams();
63 // Init session if necessary
64 if ( session_id() == '' ) {
68 $context = new DerivativeContext( $this->getContext() );
69 $context->setRequest( new DerivativeRequest(
70 $this->getContext()->getRequest(),
72 'wpName' => $params['name'],
73 'wpPassword' => $params['password'],
74 'wpDomain' => $params['domain'],
75 'wpLoginToken' => $params['token'],
79 $loginForm = new LoginForm();
80 $loginForm->setContext( $context );
82 global $wgCookiePrefix, $wgPasswordAttemptThrottle;
84 $authRes = $loginForm->authenticateUserData();
86 case LoginForm
::SUCCESS
:
87 $user = $context->getUser();
88 $this->getContext()->setUser( $user );
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 // bug 20223 - Treat a temporary password as wrong. Per SpecialUserLogin:
135 // The e-mailed temporary password should not be used for actual logins.
136 case LoginForm
::RESET_PASS
:
137 case LoginForm
::WRONG_PASS
:
138 $result['result'] = 'WrongPass';
141 case LoginForm
::EMPTY_PASS
:
142 $result['result'] = 'EmptyPass';
145 case LoginForm
::CREATE_BLOCKED
:
146 $result['result'] = 'CreateBlocked';
147 $result['details'] = 'Your IP address is blocked from account creation';
150 case LoginForm
::THROTTLED
:
151 $result['result'] = 'Throttled';
152 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
155 case LoginForm
::USER_BLOCKED
:
156 $result['result'] = 'Blocked';
159 case LoginForm
::ABORTED
:
160 $result['result'] = 'Aborted';
161 $result['reason'] = $loginForm->mAbortLoginErrorMsg
;
165 ApiBase
::dieDebug( __METHOD__
, "Unhandled case value: {$authRes}" );
168 $this->getResult()->addValue( null, 'login', $result );
171 public function mustBePosted() {
175 public function isReadMode() {
179 public function getAllowedParams() {
188 public function getParamDescription() {
190 'name' => 'User Name',
191 'password' => 'Password',
192 'domain' => 'Domain (optional)',
193 'token' => 'Login token obtained in first request',
197 public function getResultProperties() {
201 ApiBase
::PROP_TYPE
=> array(
218 ApiBase
::PROP_TYPE
=> 'integer',
219 ApiBase
::PROP_NULLABLE
=> true
221 'lgusername' => array(
222 ApiBase
::PROP_TYPE
=> 'string',
223 ApiBase
::PROP_NULLABLE
=> true
226 ApiBase
::PROP_TYPE
=> 'string',
227 ApiBase
::PROP_NULLABLE
=> true
229 'cookieprefix' => array(
230 ApiBase
::PROP_TYPE
=> 'string',
231 ApiBase
::PROP_NULLABLE
=> true
233 'sessionid' => array(
234 ApiBase
::PROP_TYPE
=> 'string',
235 ApiBase
::PROP_NULLABLE
=> true
238 ApiBase
::PROP_TYPE
=> 'string',
239 ApiBase
::PROP_NULLABLE
=> true
242 ApiBase
::PROP_TYPE
=> 'string',
243 ApiBase
::PROP_NULLABLE
=> true
246 ApiBase
::PROP_TYPE
=> 'integer',
247 ApiBase
::PROP_NULLABLE
=> true
250 ApiBase
::PROP_TYPE
=> 'string',
251 ApiBase
::PROP_NULLABLE
=> true
257 public function getDescription() {
259 'Log in and get the authentication tokens.',
260 'In the event of a successful log-in, a cookie will be attached to your session.',
261 'In the event of a failed log-in, you will not be able to attempt another log-in',
262 'through this method for 5 seconds. This is to prevent password guessing by',
263 'automated password crackers.'
267 public function getPossibleErrors() {
268 return array_merge( parent
::getPossibleErrors(), array(
270 'code' => 'NeedToken', 'info' => 'You need to resubmit your ' .
271 'login with the specified token. See ' .
272 'https://bugzilla.wikimedia.org/show_bug.cgi?id=23076'
274 array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
275 array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
276 array( 'code' => 'Illegal', 'info' => 'You provided an illegal username' ),
277 array( 'code' => 'NotExists', 'info' => 'The username you provided doesn\'t exist' ),
279 'code' => 'EmptyPass',
280 'info' => 'You didn\'t set the lgpassword parameter or you left it empty'
282 array( 'code' => 'WrongPass', 'info' => 'The password you provided is incorrect' ),
284 'code' => 'WrongPluginPass',
285 'info' => 'Same as "WrongPass", returned when an authentication ' .
286 'plugin rather than MediaWiki itself rejected the password'
289 'code' => 'CreateBlocked',
290 'info' => 'The wiki tried to automatically create a new account ' .
291 'for you, but your IP address has been blocked from account creation'
293 array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
294 array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
298 public function getExamples() {
300 'api.php?action=login&lgname=user&lgpassword=password'
304 public function getHelpUrls() {
305 return 'https://www.mediawiki.org/wiki/API:Login';