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 a mode that breaks the same-origin policy, no tokens can
51 if ( $this->lacksSameOriginSecurity() ) {
52 $this->getResult()->addValue( null, 'login', array(
53 'result' => 'Aborted',
54 'reason' => 'Cannot log in when the same-origin policy is not applied',
60 $params = $this->extractRequestParams();
64 // Init session if necessary
65 if ( session_id() == '' ) {
69 $context = new DerivativeContext( $this->getContext() );
70 $context->setRequest( new DerivativeRequest(
71 $this->getContext()->getRequest(),
73 'wpName' => $params['name'],
74 'wpPassword' => $params['password'],
75 'wpDomain' => $params['domain'],
76 'wpLoginToken' => $params['token'],
80 $loginForm = new LoginForm();
81 $loginForm->setContext( $context );
83 $authRes = $loginForm->authenticateUserData();
85 case LoginForm
::SUCCESS
:
86 $user = $context->getUser();
87 $this->getContext()->setUser( $user );
88 $user->setCookies( $this->getRequest(), null, true );
90 ApiQueryInfo
::resetTokenCache();
93 // @todo FIXME: Split back and frontend from this hook.
94 // @todo FIXME: This hook should be placed in the backend
96 Hooks
::run( 'UserLoginComplete', array( &$user, &$injected_html ) );
98 $result['result'] = 'Success';
99 $result['lguserid'] = intval( $user->getId() );
100 $result['lgusername'] = $user->getName();
101 $result['lgtoken'] = $user->getToken();
102 $result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
103 $result['sessionid'] = session_id();
106 case LoginForm
::NEED_TOKEN
:
107 $result['result'] = 'NeedToken';
108 $result['token'] = $loginForm->getLoginToken();
109 $result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
110 $result['sessionid'] = session_id();
113 case LoginForm
::WRONG_TOKEN
:
114 $result['result'] = 'WrongToken';
117 case LoginForm
::NO_NAME
:
118 $result['result'] = 'NoName';
121 case LoginForm
::ILLEGAL
:
122 $result['result'] = 'Illegal';
125 case LoginForm
::WRONG_PLUGIN_PASS
:
126 $result['result'] = 'WrongPluginPass';
129 case LoginForm
::NOT_EXISTS
:
130 $result['result'] = 'NotExists';
133 // bug 20223 - Treat a temporary password as wrong. Per SpecialUserLogin:
134 // The e-mailed temporary password should not be used for actual logins.
135 case LoginForm
::RESET_PASS
:
136 case LoginForm
::WRONG_PASS
:
137 $result['result'] = 'WrongPass';
140 case LoginForm
::EMPTY_PASS
:
141 $result['result'] = 'EmptyPass';
144 case LoginForm
::CREATE_BLOCKED
:
145 $result['result'] = 'CreateBlocked';
146 $result['details'] = 'Your IP address is blocked from account creation';
149 case LoginForm
::THROTTLED
:
150 $result['result'] = 'Throttled';
151 $throttle = $this->getConfig()->get( 'PasswordAttemptThrottle' );
152 $result['wait'] = intval( $throttle['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 protected function getExamplesMessages() {
190 'action=login&lgname=user&lgpassword=password'
191 => 'apihelp-login-example-gettoken',
192 'action=login&lgname=user&lgpassword=password&lgtoken=123ABC'
193 => 'apihelp-login-example-login',
197 public function getHelpUrls() {
198 return 'https://www.mediawiki.org/wiki/API:Login';