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( ApiMain
$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 $authRes = $loginForm->authenticateUserData();
84 case LoginForm
::SUCCESS
:
85 $user = $context->getUser();
86 $this->getContext()->setUser( $user );
87 $user->setCookies( $this->getRequest(), null, true );
89 ApiQueryInfo
::resetTokenCache();
92 // @todo FIXME: Split back and frontend from this hook.
93 // @todo FIXME: This hook should be placed in the backend
95 Hooks
::run( 'UserLoginComplete', array( &$user, &$injected_html ) );
97 $result['result'] = 'Success';
98 $result['lguserid'] = intval( $user->getId() );
99 $result['lgusername'] = $user->getName();
100 $result['lgtoken'] = $user->getToken();
101 $result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
102 $result['sessionid'] = session_id();
105 case LoginForm
::NEED_TOKEN
:
106 $result['result'] = 'NeedToken';
107 $result['token'] = $loginForm->getLoginToken();
108 $result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
109 $result['sessionid'] = session_id();
112 case LoginForm
::WRONG_TOKEN
:
113 $result['result'] = 'WrongToken';
116 case LoginForm
::NO_NAME
:
117 $result['result'] = 'NoName';
120 case LoginForm
::ILLEGAL
:
121 $result['result'] = 'Illegal';
124 case LoginForm
::WRONG_PLUGIN_PASS
:
125 $result['result'] = 'WrongPluginPass';
128 case LoginForm
::NOT_EXISTS
:
129 $result['result'] = 'NotExists';
132 // bug 20223 - Treat a temporary password as wrong. Per SpecialUserLogin:
133 // The e-mailed temporary password should not be used for actual logins.
134 case LoginForm
::RESET_PASS
:
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 $throttle = $this->getConfig()->get( 'PasswordAttemptThrottle' );
151 $result['wait'] = intval( $throttle['seconds'] );
154 case LoginForm
::USER_BLOCKED
:
155 $result['result'] = 'Blocked';
158 case LoginForm
::ABORTED
:
159 $result['result'] = 'Aborted';
160 $result['reason'] = $loginForm->mAbortLoginErrorMsg
;
164 ApiBase
::dieDebug( __METHOD__
, "Unhandled case value: {$authRes}" );
167 $this->getResult()->addValue( null, 'login', $result );
170 public function mustBePosted() {
174 public function isReadMode() {
178 public function getAllowedParams() {
187 protected function getExamplesMessages() {
189 'action=login&lgname=user&lgpassword=password'
190 => 'apihelp-login-example-gettoken',
191 'action=login&lgname=user&lgpassword=password&lgtoken=123ABC'
192 => 'apihelp-login-example-login',
196 public function getHelpUrls() {
197 return 'https://www.mediawiki.org/wiki/API:Login';