2006-09-23 15:57:16 +00:00
|
|
|
<?php
|
2010-02-18 14:53:30 +00:00
|
|
|
/**
|
2010-12-22 20:52:06 +00:00
|
|
|
*
|
2006-09-23 15:57:16 +00:00
|
|
|
*
|
2010-08-07 19:59:42 +00:00
|
|
|
* Created on Sep 19, 2006
|
|
|
|
|
*
|
2012-07-15 20:13:02 +00:00
|
|
|
* Copyright © 2006-2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
|
2007-05-27 23:50:24 +00:00
|
|
|
* Daniel Cannon (cannon dot danielc at gmail dot com)
|
2006-09-23 15:57:16 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-09-23 15:57:16 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2006-09-23 15:57:16 +00:00
|
|
|
*/
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2016-01-11 18:20:22 +00:00
|
|
|
use MediaWiki\Auth\AuthManager;
|
|
|
|
|
use MediaWiki\Auth\AuthenticationRequest;
|
|
|
|
|
use MediaWiki\Auth\AuthenticationResponse;
|
2015-04-21 08:33:40 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2006-09-23 15:57:16 +00:00
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
2007-05-27 23:50:24 +00:00
|
|
|
* Unit to authenticate log-in attempts to the current wiki.
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup API
|
2007-04-20 08:55:14 +00:00
|
|
|
*/
|
2006-09-23 15:57:16 +00:00
|
|
|
class ApiLogin extends ApiBase {
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2014-03-25 17:22:11 +00:00
|
|
|
public function __construct( ApiMain $main, $action ) {
|
2010-02-18 14:53:30 +00:00
|
|
|
parent::__construct( $main, $action, 'lg' );
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
2016-01-11 18:20:22 +00:00
|
|
|
protected function getDescriptionMessage() {
|
2016-04-01 16:49:26 +00:00
|
|
|
if ( $this->getConfig()->get( 'EnableBotPasswords' ) ) {
|
2016-01-11 18:20:22 +00:00
|
|
|
return 'apihelp-login-description';
|
|
|
|
|
} else {
|
|
|
|
|
return 'apihelp-login-description-nobotpasswords';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-27 23:50:24 +00:00
|
|
|
/**
|
|
|
|
|
* Executes the log-in attempt using the parameters passed. If
|
2013-03-11 03:45:51 +00:00
|
|
|
* the log-in succeeds, it attaches a cookie to the session
|
2007-05-27 23:50:24 +00:00
|
|
|
* and outputs the user id, username, and session token. If a
|
2009-01-01 02:02:03 +00:00
|
|
|
* log-in fails, as the result of a bad password, a nonexistent
|
2007-05-27 23:50:24 +00:00
|
|
|
* user, or any other reason, the host is cached with an expiry
|
|
|
|
|
* and no log-in attempts will be accepted until that expiry
|
|
|
|
|
* is reached. The expiry is $this->mLoginThrottle.
|
|
|
|
|
*/
|
2006-09-27 05:13:48 +00:00
|
|
|
public function execute() {
|
2014-12-17 11:09:04 +00:00
|
|
|
// If we're in a mode that breaks the same-origin policy, no tokens can
|
|
|
|
|
// be obtained
|
|
|
|
|
if ( $this->lacksSameOriginSecurity() ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->getResult()->addValue( null, 'login', [
|
2013-08-29 16:45:30 +00:00
|
|
|
'result' => 'Aborted',
|
2014-12-17 11:09:04 +00:00
|
|
|
'reason' => 'Cannot log in when the same-origin policy is not applied',
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2013-08-29 16:45:30 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-17 16:34:01 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2006-09-23 15:57:16 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$result = [];
|
2006-09-26 06:37:26 +00:00
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
// Make sure session is persisted
|
|
|
|
|
$session = MediaWiki\Session\SessionManager::getGlobalSession();
|
|
|
|
|
$session->persist();
|
|
|
|
|
|
|
|
|
|
// Make sure it's possible to log in
|
|
|
|
|
if ( !$session->canSetUser() ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->getResult()->addValue( null, 'login', [
|
2016-02-01 20:44:03 +00:00
|
|
|
'result' => 'Aborted',
|
|
|
|
|
'reason' => 'Cannot log in when using ' .
|
|
|
|
|
$session->getProvider()->describe( Language::factory( 'en' ) ),
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
return;
|
2007-12-04 08:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
$authRes = false;
|
2011-10-26 23:27:01 +00:00
|
|
|
$context = new DerivativeContext( $this->getContext() );
|
2016-02-01 20:44:03 +00:00
|
|
|
$loginType = 'N/A';
|
|
|
|
|
|
|
|
|
|
// Check login token
|
2016-01-11 18:20:22 +00:00
|
|
|
$token = $session->getToken( '', 'login' );
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( $token->wasNew() || !$params['token'] ) {
|
2016-01-11 18:20:22 +00:00
|
|
|
$authRes = 'NeedToken';
|
2016-02-01 20:44:03 +00:00
|
|
|
} elseif ( !$token->match( $params['token'] ) ) {
|
2016-01-11 18:20:22 +00:00
|
|
|
$authRes = 'WrongToken';
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try bot passwords
|
|
|
|
|
if ( $authRes === false && $this->getConfig()->get( 'EnableBotPasswords' ) &&
|
|
|
|
|
strpos( $params['name'], BotPassword::getSeparator() ) !== false
|
|
|
|
|
) {
|
|
|
|
|
$status = BotPassword::login(
|
|
|
|
|
$params['name'], $params['password'], $this->getRequest()
|
|
|
|
|
);
|
2016-02-20 20:20:37 +00:00
|
|
|
if ( $status->isOK() ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
$session = $status->getValue();
|
2016-01-11 18:20:22 +00:00
|
|
|
$authRes = 'Success';
|
2016-02-01 20:44:03 +00:00
|
|
|
$loginType = 'BotPassword';
|
|
|
|
|
} else {
|
2016-01-11 18:20:22 +00:00
|
|
|
$authRes = 'Failed';
|
|
|
|
|
$message = $status->getMessage();
|
2016-08-10 01:32:28 +00:00
|
|
|
LoggerFactory::getInstance( 'authentication' )->info(
|
2016-04-10 12:12:43 +00:00
|
|
|
'BotPassword login failed: ' . $status->getWikiText( false, false, 'en' )
|
2016-02-01 20:44:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $authRes === false ) {
|
2016-04-01 16:49:26 +00:00
|
|
|
// Simplified AuthManager login, for backwards compatibility
|
|
|
|
|
$manager = AuthManager::singleton();
|
|
|
|
|
$reqs = AuthenticationRequest::loadRequestsFromSubmission(
|
|
|
|
|
$manager->getAuthenticationRequests( AuthManager::ACTION_LOGIN, $this->getUser() ),
|
|
|
|
|
[
|
|
|
|
|
'username' => $params['name'],
|
|
|
|
|
'password' => $params['password'],
|
|
|
|
|
'domain' => $params['domain'],
|
|
|
|
|
'rememberMe' => true,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$res = AuthManager::singleton()->beginAuthentication( $reqs, 'null:' );
|
|
|
|
|
switch ( $res->status ) {
|
|
|
|
|
case AuthenticationResponse::PASS:
|
|
|
|
|
if ( $this->getConfig()->get( 'EnableBotPasswords' ) ) {
|
|
|
|
|
$warn = 'Main-account login via action=login is deprecated and may stop working ' .
|
|
|
|
|
'without warning.';
|
|
|
|
|
$warn .= ' To continue login with action=login, see [[Special:BotPasswords]].';
|
|
|
|
|
$warn .= ' To safely continue using main-account login, see action=clientlogin.';
|
|
|
|
|
} else {
|
|
|
|
|
$warn = 'Login via action=login is deprecated and may stop working without warning.';
|
|
|
|
|
$warn .= ' To safely log in, see action=clientlogin.';
|
|
|
|
|
}
|
|
|
|
|
$this->setWarning( $warn );
|
|
|
|
|
$authRes = 'Success';
|
|
|
|
|
$loginType = 'AuthManager';
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AuthenticationResponse::FAIL:
|
|
|
|
|
// Hope it's not a PreAuthenticationProvider that failed...
|
|
|
|
|
$authRes = 'Failed';
|
|
|
|
|
$message = $res->message;
|
|
|
|
|
\MediaWiki\Logger\LoggerFactory::getInstance( 'authentication' )
|
2016-08-16 05:52:44 +00:00
|
|
|
->info( __METHOD__ . ': Authentication failed: '
|
|
|
|
|
. $message->inLanguage( 'en' )->plain() );
|
2016-04-01 16:49:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2016-08-16 05:52:44 +00:00
|
|
|
\MediaWiki\Logger\LoggerFactory::getInstance( 'authentication' )
|
|
|
|
|
->info( __METHOD__ . ': Authentication failed due to unsupported response type: '
|
|
|
|
|
. $res->status, $this->getAuthenticationResponseLogData( $res ) );
|
2016-04-01 16:49:26 +00:00
|
|
|
$authRes = 'Aborted';
|
|
|
|
|
break;
|
2016-01-11 18:20:22 +00:00
|
|
|
}
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
2010-08-03 20:05:38 +00:00
|
|
|
|
2016-01-11 18:20:22 +00:00
|
|
|
$result['result'] = $authRes;
|
2011-03-15 00:34:44 +00:00
|
|
|
switch ( $authRes ) {
|
2016-01-11 18:20:22 +00:00
|
|
|
case 'Success':
|
2016-04-01 16:49:26 +00:00
|
|
|
$user = $session->getUser();
|
2009-09-26 00:49:32 +00:00
|
|
|
|
2012-06-22 20:37:26 +00:00
|
|
|
ApiQueryInfo::resetTokenCache();
|
|
|
|
|
|
2016-01-11 18:20:22 +00:00
|
|
|
// Deprecated hook
|
2009-09-26 00:49:32 +00:00
|
|
|
$injected_html = '';
|
2016-07-20 00:25:12 +00:00
|
|
|
Hooks::run( 'UserLoginComplete', [ &$user, &$injected_html, true ] );
|
2009-09-26 00:49:32 +00:00
|
|
|
|
2011-10-26 23:27:01 +00:00
|
|
|
$result['lguserid'] = intval( $user->getId() );
|
|
|
|
|
$result['lgusername'] = $user->getName();
|
2016-08-05 22:50:11 +00:00
|
|
|
|
|
|
|
|
// @todo: These are deprecated, and should be removed at some
|
|
|
|
|
// point (1.28 at the earliest, and see T121527). They were ok
|
|
|
|
|
// when the core cookie-based login was the only thing, but
|
|
|
|
|
// CentralAuth broke that a while back and
|
|
|
|
|
// SessionManager/AuthManager *really* break it.
|
|
|
|
|
$result['lgtoken'] = $user->getToken();
|
|
|
|
|
$result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
|
|
|
|
|
$result['sessionid'] = $session->getId();
|
2006-09-23 15:57:16 +00:00
|
|
|
break;
|
2010-07-23 07:33:40 +00:00
|
|
|
|
2016-01-11 18:20:22 +00:00
|
|
|
case 'NeedToken':
|
|
|
|
|
$result['token'] = $token->toString();
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->setWarning( 'Fetching a token via action=login is deprecated. ' .
|
|
|
|
|
'Use action=query&meta=tokens&type=login instead.' );
|
|
|
|
|
$this->logFeatureUsage( 'action=login&!lgtoken' );
|
2016-08-05 22:50:11 +00:00
|
|
|
|
|
|
|
|
// @todo: See above about deprecation
|
|
|
|
|
$result['cookieprefix'] = $this->getConfig()->get( 'CookiePrefix' );
|
|
|
|
|
$result['sessionid'] = $session->getId();
|
2010-04-07 00:05:33 +00:00
|
|
|
break;
|
2010-07-23 07:33:40 +00:00
|
|
|
|
2016-01-11 18:20:22 +00:00
|
|
|
case 'WrongToken':
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'Failed':
|
|
|
|
|
$result['reason'] = $message->useDatabase( 'false' )->inLanguage( 'en' )->text();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'Aborted':
|
|
|
|
|
$result['reason'] = 'Authentication requires user interaction, ' .
|
|
|
|
|
'which is not supported by action=login.';
|
|
|
|
|
if ( $this->getConfig()->get( 'EnableBotPasswords' ) ) {
|
|
|
|
|
$result['reason'] .= ' To be able to login with action=login, see [[Special:BotPasswords]].';
|
|
|
|
|
$result['reason'] .= ' To continue using main-account login, see action=clientlogin.';
|
|
|
|
|
} else {
|
|
|
|
|
$result['reason'] .= ' To log in, see action=clientlogin.';
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2010-02-18 14:53:30 +00:00
|
|
|
default:
|
|
|
|
|
ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
2006-09-26 06:37:26 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->getResult()->addValue( null, 'login', $result );
|
2015-04-21 08:33:40 +00:00
|
|
|
|
2016-01-11 18:20:22 +00:00
|
|
|
if ( $loginType === 'LoginForm' && isset( LoginForm::$statusCodes[$authRes] ) ) {
|
|
|
|
|
$authRes = LoginForm::$statusCodes[$authRes];
|
|
|
|
|
}
|
2016-08-10 01:32:28 +00:00
|
|
|
LoggerFactory::getInstance( 'authevents' )->info( 'Login attempt', [
|
2015-04-21 08:33:40 +00:00
|
|
|
'event' => 'login',
|
2016-01-11 18:20:22 +00:00
|
|
|
'successful' => $authRes === 'Success',
|
2016-02-01 20:44:03 +00:00
|
|
|
'loginType' => $loginType,
|
2016-01-11 18:20:22 +00:00
|
|
|
'status' => $authRes,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
2016-01-11 18:20:22 +00:00
|
|
|
public function isDeprecated() {
|
2016-04-01 16:49:26 +00:00
|
|
|
return !$this->getConfig()->get( 'EnableBotPasswords' );
|
2016-01-11 18:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-14 21:12:11 +00:00
|
|
|
public function mustBePosted() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2007-05-27 23:50:24 +00:00
|
|
|
|
2009-03-06 13:49:44 +00:00
|
|
|
public function isReadMode() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2006-10-17 02:11:29 +00:00
|
|
|
'name' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'password' => [
|
2015-05-07 16:39:55 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'password',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2010-04-07 00:05:33 +00:00
|
|
|
'domain' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'token' => [
|
2016-02-01 20:44:03 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
|
ApiBase::PARAM_REQUIRED => false, // for BC
|
2016-02-17 09:09:32 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG => [ 'api-help-param-token', 'login' ],
|
|
|
|
|
],
|
|
|
|
|
];
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=login&lgname=user&lgpassword=password'
|
|
|
|
|
=> 'apihelp-login-example-gettoken',
|
|
|
|
|
'action=login&lgname=user&lgpassword=password&lgtoken=123ABC'
|
|
|
|
|
=> 'apihelp-login-example-login',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2006-10-03 05:41:55 +00:00
|
|
|
}
|
2006-10-01 21:20:55 +00:00
|
|
|
|
2011-07-17 16:23:29 +00:00
|
|
|
public function getHelpUrls() {
|
2011-11-28 15:43:11 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/API:Login';
|
2011-07-17 16:18:09 +00:00
|
|
|
}
|
2016-08-16 05:52:44 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Turns an AuthenticationResponse into a hash suitable for passing to Logger
|
|
|
|
|
* @param AuthenticationResponse $response
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function getAuthenticationResponseLogData( AuthenticationResponse $response ) {
|
|
|
|
|
$ret = [
|
|
|
|
|
'status' => $response->status,
|
|
|
|
|
];
|
|
|
|
|
if ( $response->message ) {
|
|
|
|
|
$ret['message'] = $response->message->inLanguage( 'en' )->plain();
|
|
|
|
|
};
|
|
|
|
|
$reqs = [
|
|
|
|
|
'neededRequests' => $response->neededRequests,
|
|
|
|
|
'createRequest' => $response->createRequest,
|
|
|
|
|
'linkRequest' => $response->linkRequest,
|
|
|
|
|
];
|
|
|
|
|
foreach ( $reqs as $k => $v ) {
|
|
|
|
|
if ( $v ) {
|
|
|
|
|
$v = is_array( $v ) ? $v : [ $v ];
|
|
|
|
|
$reqClasses = array_unique( array_map( 'get_class', $v ) );
|
|
|
|
|
sort( $reqClasses );
|
|
|
|
|
$ret[$k] = implode( ', ', $reqClasses );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|