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
28 if ( !defined( 'MEDIAWIKI' ) ) {
29 // Eclipse helper - will be ignored in production
30 require_once( 'ApiBase.php' );
34 * Unit to authenticate log-in attempts to the current wiki.
38 class ApiLogin
extends ApiBase
{
40 public function __construct( $main, $action ) {
41 parent
::__construct( $main, $action, 'lg' );
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();
58 // Init session if necessary
59 if ( session_id() == '' ) {
63 $context = new DerivativeContext( $this->getContext() );
64 $context->setRequest( new DerivativeRequest(
65 $this->getContext()->getRequest(),
67 'wpName' => $params['name'],
68 'wpPassword' => $params['password'],
69 'wpDomain' => $params['domain'],
70 'wpLoginToken' => $params['token'],
74 $loginForm = new LoginForm();
75 $loginForm->setContext( $context );
77 global $wgCookiePrefix, $wgPasswordAttemptThrottle;
79 $authRes = $loginForm->authenticateUserData();
81 case LoginForm
::SUCCESS
:
82 $user = $context->getUser();
83 $this->getContext()->setUser( $user );
84 $user->setOption( 'rememberpassword', 1 );
85 $user->setCookies( $this->getRequest() );
88 // @todo FIXME: Split back and frontend from this hook.
89 // @todo FIXME: This hook should be placed in the backend
91 wfRunHooks( 'UserLoginComplete', array( &$user, &$injected_html ) );
93 $result['result'] = 'Success';
94 $result['lguserid'] = intval( $user->getId() );
95 $result['lgusername'] = $user->getName();
96 $result['lgtoken'] = $user->getToken();
97 $result['cookieprefix'] = $wgCookiePrefix;
98 $result['sessionid'] = session_id();
101 case LoginForm
::NEED_TOKEN
:
102 $result['result'] = 'NeedToken';
103 $result['token'] = $loginForm->getLoginToken();
104 $result['cookieprefix'] = $wgCookiePrefix;
105 $result['sessionid'] = session_id();
108 case LoginForm
::WRONG_TOKEN
:
109 $result['result'] = 'WrongToken';
112 case LoginForm
::NO_NAME
:
113 $result['result'] = 'NoName';
116 case LoginForm
::ILLEGAL
:
117 $result['result'] = 'Illegal';
120 case LoginForm
::WRONG_PLUGIN_PASS
:
121 $result['result'] = 'WrongPluginPass';
124 case LoginForm
::NOT_EXISTS
:
125 $result['result'] = 'NotExists';
128 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;"
129 case LoginForm
::WRONG_PASS
:
130 $result['result'] = 'WrongPass';
133 case LoginForm
::EMPTY_PASS
:
134 $result['result'] = 'EmptyPass';
137 case LoginForm
::CREATE_BLOCKED
:
138 $result['result'] = 'CreateBlocked';
139 $result['details'] = 'Your IP address is blocked from account creation';
142 case LoginForm
::THROTTLED
:
143 $result['result'] = 'Throttled';
144 $result['wait'] = intval( $wgPasswordAttemptThrottle['seconds'] );
147 case LoginForm
::USER_BLOCKED
:
148 $result['result'] = 'Blocked';
151 case LoginForm
::ABORTED
:
152 $result['result'] = 'Aborted';
153 $result['reason'] = $loginForm->mAbortLoginErrorMsg
;
157 ApiBase
::dieDebug( __METHOD__
, "Unhandled case value: {$authRes}" );
160 $this->getResult()->addValue( null, 'login', $result );
163 public function mustBePosted() {
167 public function isReadMode() {
171 public function getAllowedParams() {
180 public function getParamDescription() {
182 'name' => 'User Name',
183 'password' => 'Password',
184 'domain' => 'Domain (optional)',
185 'token' => 'Login token obtained in first request',
189 public function getDescription() {
191 'Log in and get the authentication tokens. ',
192 'In the event of a successful log-in, a cookie will be attached',
193 'to your session. In the event of a failed log-in, you will not ',
194 'be able to attempt another log-in through this method for 5 seconds.',
195 'This is to prevent password guessing by automated password crackers'
199 public function getPossibleErrors() {
200 return array_merge( parent
::getPossibleErrors(), array(
201 array( 'code' => 'NeedToken', 'info' => 'You need to resubmit your login with the specified token. See https://bugzilla.wikimedia.org/show_bug.cgi?id=23076' ),
202 array( 'code' => 'WrongToken', 'info' => 'You specified an invalid token' ),
203 array( 'code' => 'NoName', 'info' => 'You didn\'t set the lgname parameter' ),
204 array( 'code' => 'Illegal', 'info' => ' You provided an illegal username' ),
205 array( 'code' => 'NotExists', 'info' => ' The username you provided doesn\'t exist' ),
206 array( 'code' => 'EmptyPass', 'info' => ' You didn\'t set the lgpassword parameter or you left it empty' ),
207 array( 'code' => 'WrongPass', 'info' => ' The password you provided is incorrect' ),
208 array( 'code' => 'WrongPluginPass', 'info' => 'Same as `WrongPass", returned when an authentication plugin rather than MediaWiki itself rejected the password' ),
209 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' ),
210 array( 'code' => 'Throttled', 'info' => 'You\'ve logged in too many times in a short time' ),
211 array( 'code' => 'Blocked', 'info' => 'User is blocked' ),
215 public function getExamples() {
217 'api.php?action=login&lgname=user&lgpassword=password'
221 public function getHelpUrls() {
222 return 'http://www.mediawiki.org/wiki/API:Login';
225 public function getVersion() {
226 return __CLASS__
. ': $Id$';