2006-09-23 15:57:16 +00:00
|
|
|
<?php
|
2010-02-18 14:53:30 +00:00
|
|
|
/**
|
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\AuthenticationRequest;
|
|
|
|
|
use MediaWiki\Auth\AuthenticationResponse;
|
2020-01-10 00:00:51 +00:00
|
|
|
use MediaWiki\Auth\AuthManager;
|
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
|
|
|
}
|
|
|
|
|
|
2017-05-26 17:50:06 +00:00
|
|
|
protected function getExtendedDescription() {
|
2016-04-01 16:49:26 +00:00
|
|
|
if ( $this->getConfig()->get( 'EnableBotPasswords' ) ) {
|
2017-05-26 17:50:06 +00:00
|
|
|
return 'apihelp-login-extended-description';
|
2016-01-11 18:20:22 +00:00
|
|
|
} else {
|
2017-05-26 17:50:06 +00:00
|
|
|
return 'apihelp-login-extended-description-nobotpasswords';
|
2016-01-11 18:20:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 14:14:03 +00:00
|
|
|
/**
|
|
|
|
|
* Format a message for the response
|
|
|
|
|
* @param Message|string|array $message
|
|
|
|
|
* @return string|array
|
|
|
|
|
*/
|
|
|
|
|
private function formatMessage( $message ) {
|
|
|
|
|
$message = Message::newFromSpecifier( $message );
|
|
|
|
|
$errorFormatter = $this->getErrorFormatter();
|
|
|
|
|
if ( $errorFormatter instanceof ApiErrorFormatter_BackCompat ) {
|
|
|
|
|
return ApiErrorFormatter::stripMarkup(
|
|
|
|
|
$message->useDatabase( false )->inLanguage( 'en' )->text()
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return $errorFormatter->formatMessage( $message );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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',
|
2017-05-16 14:14:03 +00:00
|
|
|
'reason' => $this->formatMessage( 'api-login-fail-sameorigin' ),
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 14:05:44 +00:00
|
|
|
$this->requirePostedParameters( [ 'password', 'token' ] );
|
2016-08-18 17:36:11 +00:00
|
|
|
|
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',
|
2017-05-16 14:14:03 +00:00
|
|
|
'reason' => $this->formatMessage( [
|
|
|
|
|
'api-login-fail-badsessionprovider',
|
|
|
|
|
$session->getProvider()->describe( $this->getErrorFormatter()->getLanguage() ),
|
|
|
|
|
] )
|
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;
|
|
|
|
|
$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
|
2016-08-23 03:02:23 +00:00
|
|
|
if (
|
|
|
|
|
$authRes === false && $this->getConfig()->get( 'EnableBotPasswords' ) &&
|
|
|
|
|
( $botLoginData = BotPassword::canonicalizeLoginData( $params['name'], $params['password'] ) )
|
2016-02-01 20:44:03 +00:00
|
|
|
) {
|
|
|
|
|
$status = BotPassword::login(
|
2016-08-23 03:02:23 +00:00
|
|
|
$botLoginData[0], $botLoginData[1], $this->getRequest()
|
2016-02-01 20:44:03 +00:00
|
|
|
);
|
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';
|
2018-10-08 13:30:13 +00:00
|
|
|
} elseif (
|
2018-05-04 13:35:55 +00:00
|
|
|
$status->hasMessage( 'login-throttled' ) ||
|
2018-05-13 19:37:09 +00:00
|
|
|
$status->hasMessage( 'botpasswords-needs-reset' ) ||
|
|
|
|
|
$status->hasMessage( 'botpasswords-locked' )
|
2018-05-04 13:35:55 +00:00
|
|
|
) {
|
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
|
|
|
);
|
|
|
|
|
}
|
2018-10-08 13:30:13 +00:00
|
|
|
// For other errors, let's see if it's a valid non-bot login
|
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' ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->addDeprecation( 'apiwarn-deprecation-login-botpw', 'main-account-login' );
|
2016-04-01 16:49:26 +00:00
|
|
|
} else {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->addDeprecation( 'apiwarn-deprecation-login-nobotpw', 'main-account-login' );
|
2016-04-01 16:49:26 +00:00
|
|
|
}
|
|
|
|
|
$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
|
|
|
|
2019-02-25 00:38:33 +00:00
|
|
|
$result['lguserid'] = (int)$user->getId();
|
2011-10-26 23:27:01 +00:00
|
|
|
$result['lgusername'] = $user->getName();
|
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-10-19 16:54:25 +00:00
|
|
|
$this->addDeprecation( 'apiwarn-deprecation-login-token', 'action=login&!lgtoken' );
|
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':
|
2017-05-16 14:14:03 +00:00
|
|
|
$result['reason'] = $this->formatMessage( $message );
|
2016-01-11 18:20:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'Aborted':
|
2017-05-16 14:14:03 +00:00
|
|
|
$result['reason'] = $this->formatMessage(
|
|
|
|
|
$this->getConfig()->get( 'EnableBotPasswords' )
|
|
|
|
|
? 'api-login-fail-aborted'
|
|
|
|
|
: 'api-login-fail-aborted-nobotpw'
|
|
|
|
|
);
|
2016-01-11 18:20:22 +00:00
|
|
|
break;
|
|
|
|
|
|
2018-10-08 13:30:13 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
// Unreachable
|
2010-02-18 14:53:30 +00:00
|
|
|
default:
|
|
|
|
|
ApiBase::dieDebug( __METHOD__, "Unhandled case value: {$authRes}" );
|
2018-10-08 13:30:13 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
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-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-08-18 17:37:05 +00:00
|
|
|
ApiBase::PARAM_SENSITIVE => true,
|
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&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() {
|
2017-04-04 22:52:57 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/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 ) {
|
2020-02-14 20:31:29 +00:00
|
|
|
$ret['responseMessage'] = $response->message->inLanguage( 'en' )->plain();
|
2019-06-12 13:35:59 +00:00
|
|
|
}
|
2016-08-16 05:52:44 +00:00
|
|
|
$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
|
|
|
}
|