2016-03-28 08:26:48 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
|
|
|
|
2023-08-30 23:01:31 +00:00
|
|
|
// phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName
|
|
|
|
|
2017-06-15 22:56:12 +00:00
|
|
|
namespace LoginNotify;
|
|
|
|
|
2016-07-06 22:06:27 +00:00
|
|
|
use MediaWiki\Auth\AuthenticationResponse;
|
2023-08-15 15:45:07 +00:00
|
|
|
use MediaWiki\Auth\Hook\AuthManagerLoginAuthenticateAuditHook;
|
|
|
|
use MediaWiki\Auth\Hook\LocalUserCreatedHook;
|
2023-08-30 23:01:31 +00:00
|
|
|
use MediaWiki\Hook\RecentChange_saveHook;
|
2023-08-24 01:26:30 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2023-08-30 23:01:31 +00:00
|
|
|
use RecentChange;
|
2017-06-15 22:56:12 +00:00
|
|
|
use User;
|
2016-07-06 22:06:27 +00:00
|
|
|
|
2023-08-15 15:45:07 +00:00
|
|
|
class Hooks implements
|
|
|
|
AuthManagerLoginAuthenticateAuditHook,
|
2023-08-30 23:01:31 +00:00
|
|
|
LocalUserCreatedHook,
|
|
|
|
RecentChange_saveHook
|
2023-08-15 15:45:07 +00:00
|
|
|
{
|
2023-08-24 01:26:30 +00:00
|
|
|
/** @var UserFactory */
|
|
|
|
private $userFactory;
|
|
|
|
|
|
|
|
public function __construct( UserFactory $userFactory ) {
|
|
|
|
$this->userFactory = $userFactory;
|
|
|
|
}
|
|
|
|
|
2016-03-28 08:26:48 +00:00
|
|
|
/**
|
2017-10-07 01:40:02 +00:00
|
|
|
* Hook for login auditing
|
2016-07-06 22:06:27 +00:00
|
|
|
*
|
2017-03-23 03:28:30 +00:00
|
|
|
* @param AuthenticationResponse $ret Is login successful?
|
|
|
|
* @param User|null $user User object on successful auth
|
2023-08-31 17:54:29 +00:00
|
|
|
* @param string|null $username Username for failed attempts.
|
2023-08-15 15:45:07 +00:00
|
|
|
* @param string[] $extraData
|
2016-07-06 22:06:27 +00:00
|
|
|
*/
|
2023-08-15 15:45:07 +00:00
|
|
|
public function onAuthManagerLoginAuthenticateAudit(
|
|
|
|
$ret, $user, $username, $extraData
|
2016-07-06 22:06:27 +00:00
|
|
|
) {
|
2023-08-31 17:54:29 +00:00
|
|
|
if ( !$user && $username !== null ) {
|
|
|
|
$user = $this->userFactory->newFromName( $username, UserFactory::RIGOR_USABLE );
|
2016-07-06 22:06:27 +00:00
|
|
|
}
|
2023-08-31 17:54:29 +00:00
|
|
|
|
|
|
|
if ( !$user ) {
|
2016-07-06 22:06:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $ret->status === AuthenticationResponse::PASS ) {
|
2023-08-31 17:54:29 +00:00
|
|
|
self::doSuccessfulLogin( $user );
|
2018-05-07 16:32:37 +00:00
|
|
|
} elseif (
|
|
|
|
$ret->status === AuthenticationResponse::FAIL
|
|
|
|
&& $ret->message->getKey() !== 'login-throttled'
|
|
|
|
) {
|
2023-08-31 17:54:29 +00:00
|
|
|
self::doFailedLogin( $user );
|
2016-07-06 22:06:27 +00:00
|
|
|
}
|
|
|
|
// Other statuses include Abstain, Redirect, or UI. We ignore such
|
|
|
|
// statuses.
|
|
|
|
}
|
|
|
|
|
2017-03-23 03:28:30 +00:00
|
|
|
/**
|
|
|
|
* Handle a successful login (clear the attempt counter, send a notice, and record the
|
|
|
|
* current IP address as known).
|
|
|
|
*
|
|
|
|
* @param User $user The user who logged in.
|
|
|
|
*/
|
2018-01-01 22:22:04 +00:00
|
|
|
public static function doSuccessfulLogin( User $user ) {
|
2023-08-24 06:56:04 +00:00
|
|
|
$loginNotify = LoginNotify::getInstance();
|
2016-07-06 22:06:27 +00:00
|
|
|
$loginNotify->clearCounters( $user );
|
|
|
|
$loginNotify->sendSuccessNotice( $user );
|
2023-08-30 23:01:31 +00:00
|
|
|
$loginNotify->recordKnownWithCookie( $user );
|
2016-07-06 22:06:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 03:28:30 +00:00
|
|
|
/**
|
|
|
|
* Handle a failed login (record the failure).
|
|
|
|
*
|
|
|
|
* @param User $user The user that failed to log in.
|
|
|
|
*/
|
2018-01-01 22:22:04 +00:00
|
|
|
public static function doFailedLogin( User $user ) {
|
2023-08-24 06:56:04 +00:00
|
|
|
$loginNotify = LoginNotify::getInstance();
|
2016-07-06 22:06:27 +00:00
|
|
|
$loginNotify->recordFailure( $user );
|
2016-03-28 08:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-07 01:40:02 +00:00
|
|
|
* Hook handler for new account creation.
|
2017-01-23 06:37:08 +00:00
|
|
|
*
|
|
|
|
* Called immediately after a local user has been created and saved to the database
|
|
|
|
*
|
|
|
|
* @todo This still sets cookies if user creates account well logged in as someone else.
|
|
|
|
* @param User $user User created
|
2017-08-09 20:20:03 +00:00
|
|
|
* @param bool $autocreated Whether this was an auto-created account
|
2017-01-23 06:37:08 +00:00
|
|
|
*/
|
2023-08-15 15:45:07 +00:00
|
|
|
public function onLocalUserCreated( $user, $autocreated ) {
|
2017-01-23 06:37:08 +00:00
|
|
|
if ( !$autocreated ) {
|
2023-08-24 06:56:04 +00:00
|
|
|
$loginNotify = LoginNotify::getInstance();
|
2023-08-30 23:01:31 +00:00
|
|
|
$loginNotify->recordKnownWithCookie( $user );
|
2016-03-28 08:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-30 23:01:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param RecentChange $recentChange
|
|
|
|
*/
|
|
|
|
public function onRecentChange_save( $recentChange ) {
|
|
|
|
$loginNotify = LoginNotify::getInstance();
|
|
|
|
$user = $this->userFactory->newFromUserIdentity( $recentChange->getPerformerIdentity() );
|
|
|
|
$loginNotify->recordKnown( $user );
|
|
|
|
}
|
2016-03-28 08:26:48 +00:00
|
|
|
}
|