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 use MediaWiki\Logger\LoggerFactory
;
31 * Unit to authenticate log-in attempts to the current wiki.
35 class ApiLogin
extends ApiBase
{
37 public function __construct( ApiMain
$main, $action ) {
38 parent
::__construct( $main, $action, 'lg' );
42 * Executes the log-in attempt using the parameters passed. If
43 * the log-in succeeds, it attaches a cookie to the session
44 * and outputs the user id, username, and session token. If a
45 * log-in fails, as the result of a bad password, a nonexistent
46 * user, or any other reason, the host is cached with an expiry
47 * and no log-in attempts will be accepted until that expiry
48 * is reached. The expiry is $this->mLoginThrottle.
50 public function execute() {
51 // If we're in a mode that breaks the same-origin policy, no tokens can
53 if ( $this->lacksSameOriginSecurity() ) {
54 $this->getResult()->addValue( null, 'login', array(
55 'result' => 'Aborted',
56 'reason' => 'Cannot log in when the same-origin policy is not applied',
62 $params = $this->extractRequestParams();
66 // Make sure session is persisted
67 $session = MediaWiki\Session\SessionManager
::getGlobalSession();
70 // Make sure it's possible to log in
71 if ( !$session->canSetUser() ) {
72 $this->getResult()->addValue( null, 'login', array(
73 'result' => 'Aborted',
74 'reason' => 'Cannot log in when using ' .
75 $session->getProvider()->describe( Language
::factory( 'en' ) ),
82 $context = new DerivativeContext( $this->getContext() );
86 $token = LoginForm
::getLoginToken();
88 LoginForm
::setLoginToken();
89 $authRes = LoginForm
::NEED_TOKEN
;
90 } elseif ( !$params['token'] ) {
91 $authRes = LoginForm
::NEED_TOKEN
;
92 } elseif ( $token !== $params['token'] ) {
93 $authRes = LoginForm
::WRONG_TOKEN
;
97 if ( $authRes === false && $this->getConfig()->get( 'EnableBotPasswords' ) &&
98 strpos( $params['name'], BotPassword
::getSeparator() ) !== false
100 $status = BotPassword
::login(
101 $params['name'], $params['password'], $this->getRequest()
103 if ( $status->isOk() ) {
104 $session = $status->getValue();
105 $authRes = LoginForm
::SUCCESS
;
106 $loginType = 'BotPassword';
108 LoggerFactory
::getInstance( 'authmanager' )->info(
109 'BotPassword login failed: ' . $status->getWikiText()
115 if ( $authRes === false ) {
116 $context->setRequest( new DerivativeRequest(
117 $this->getContext()->getRequest(),
119 'wpName' => $params['name'],
120 'wpPassword' => $params['password'],
121 'wpDomain' => $params['domain'],
122 'wpLoginToken' => $params['token'],
126 $loginForm = new LoginForm();
127 $loginForm->setContext( $context );
128 $authRes = $loginForm->authenticateUserData();
129 $loginType = 'LoginForm';
132 switch ( $authRes ) {
133 case LoginForm
::SUCCESS
:
134 $user = $context->getUser();
135 $this->getContext()->setUser( $user );
136 $user->setCookies( $this->getRequest(), null, true );
138 ApiQueryInfo
::resetTokenCache();
141 // @todo FIXME: Split back and frontend from this hook.
142 // @todo FIXME: This hook should be placed in the backend
144 Hooks
::run( 'UserLoginComplete', array( &$user, &$injected_html ) );
146 $result['result'] = 'Success';
147 $result['lguserid'] = intval( $user->getId() );
148 $result['lgusername'] = $user->getName();
150 // @todo: These are deprecated, and should be removed at some
151 // point (1.28 at the earliest, and see T121527). They were ok
152 // when the core cookie-based login was the only thing, but
153 // CentralAuth broke that a while back and
154 // SessionManager/AuthManager are *really* going to break it.
155 $result['lgtoken'] = $user->getToken();
156 $result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
157 $result['sessionid'] = $session->getId();
160 case LoginForm
::NEED_TOKEN
:
161 $result['result'] = 'NeedToken';
162 $result['token'] = LoginForm
::getLoginToken();
164 // @todo: See above about deprecation
165 $result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
166 $result['sessionid'] = $session->getId();
169 case LoginForm
::WRONG_TOKEN
:
170 $result['result'] = 'WrongToken';
173 case LoginForm
::NO_NAME
:
174 $result['result'] = 'NoName';
177 case LoginForm
::ILLEGAL
:
178 $result['result'] = 'Illegal';
181 case LoginForm
::WRONG_PLUGIN_PASS
:
182 $result['result'] = 'WrongPluginPass';
185 case LoginForm
::NOT_EXISTS
:
186 $result['result'] = 'NotExists';
189 // bug 20223 - Treat a temporary password as wrong. Per SpecialUserLogin:
190 // The e-mailed temporary password should not be used for actual logins.
191 case LoginForm
::RESET_PASS
:
192 case LoginForm
::WRONG_PASS
:
193 $result['result'] = 'WrongPass';
196 case LoginForm
::EMPTY_PASS
:
197 $result['result'] = 'EmptyPass';
200 case LoginForm
::CREATE_BLOCKED
:
201 $result['result'] = 'CreateBlocked';
202 $result['details'] = 'Your IP address is blocked from account creation';
203 $block = $context->getUser()->getBlock();
205 $result = array_merge( $result, ApiQueryUserInfo
::getBlockInfo( $block ) );
209 case LoginForm
::THROTTLED
:
210 $result['result'] = 'Throttled';
211 $throttle = $this->getConfig()->get( 'PasswordAttemptThrottle' );
212 $result['wait'] = intval( $throttle['seconds'] );
215 case LoginForm
::USER_BLOCKED
:
216 $result['result'] = 'Blocked';
217 $block = User
::newFromName( $params['name'] )->getBlock();
219 $result = array_merge( $result, ApiQueryUserInfo
::getBlockInfo( $block ) );
223 case LoginForm
::ABORTED
:
224 $result['result'] = 'Aborted';
225 $result['reason'] = $loginForm->mAbortLoginErrorMsg
;
229 ApiBase
::dieDebug( __METHOD__
, "Unhandled case value: {$authRes}" );
232 $this->getResult()->addValue( null, 'login', $result );
234 LoggerFactory
::getInstance( 'authmanager' )->info( 'Login attempt', array(
236 'successful' => $authRes === LoginForm
::SUCCESS
,
237 'loginType' => $loginType,
238 'status' => LoginForm
::$statusCodes[$authRes],
242 public function mustBePosted() {
246 public function isReadMode() {
250 public function getAllowedParams() {
254 ApiBase
::PARAM_TYPE
=> 'password',
261 protected function getExamplesMessages() {
263 'action=login&lgname=user&lgpassword=password'
264 => 'apihelp-login-example-gettoken',
265 'action=login&lgname=user&lgpassword=password&lgtoken=123ABC'
266 => 'apihelp-login-example-login',
270 public function getHelpUrls() {
271 return 'https://www.mediawiki.org/wiki/API:Login';