2014-05-19 00:05:59 +00:00
|
|
|
<?php
|
2018-04-11 01:29:26 +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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2019-03-14 14:39:10 +00:00
|
|
|
namespace MediaWiki\Extension\OATHAuth;
|
|
|
|
|
|
|
|
use BagOStuff;
|
|
|
|
use ConfigException;
|
2020-01-14 08:27:29 +00:00
|
|
|
use FormatJson;
|
2022-12-31 18:48:09 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
|
|
|
use MediaWiki\User\CentralId\CentralIdLookupFactory;
|
2020-01-14 08:27:29 +00:00
|
|
|
use MWException;
|
2022-12-31 18:48:09 +00:00
|
|
|
use Psr\Log\LoggerAwareInterface;
|
2020-01-14 08:27:29 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-11-12 08:47:12 +00:00
|
|
|
use RequestContext;
|
2020-01-14 08:27:29 +00:00
|
|
|
use User;
|
2022-06-10 23:12:26 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2022-12-31 18:48:09 +00:00
|
|
|
use Wikimedia\Rdbms\LBFactory;
|
2018-04-05 09:29:45 +00:00
|
|
|
|
2022-12-31 18:48:09 +00:00
|
|
|
class OATHUserRepository implements LoggerAwareInterface {
|
|
|
|
/** @var ServiceOptions */
|
|
|
|
private ServiceOptions $options;
|
|
|
|
|
|
|
|
/** @var LBFactory */
|
|
|
|
private LBFactory $lbFactory;
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2016-06-15 10:07:01 +00:00
|
|
|
/** @var BagOStuff */
|
2022-12-31 18:48:09 +00:00
|
|
|
private BagOStuff $cache;
|
2016-06-15 10:07:01 +00:00
|
|
|
|
2022-12-31 18:48:09 +00:00
|
|
|
/** @var OATHAuth */
|
|
|
|
private OATHAuth $auth;
|
|
|
|
|
|
|
|
/** @var CentralIdLookupFactory */
|
|
|
|
private CentralIdLookupFactory $centralIdLookupFactory;
|
2019-03-14 14:39:10 +00:00
|
|
|
|
2018-12-17 23:56:47 +00:00
|
|
|
/** @var LoggerInterface */
|
2022-12-31 18:48:09 +00:00
|
|
|
private LoggerInterface $logger;
|
|
|
|
|
|
|
|
/** @internal Only public for service wiring use. */
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
'OATHAuthDatabase',
|
|
|
|
];
|
2018-12-17 23:56:47 +00:00
|
|
|
|
2016-09-30 21:13:57 +00:00
|
|
|
/**
|
|
|
|
* OATHUserRepository constructor.
|
2022-12-31 18:48:09 +00:00
|
|
|
* @param ServiceOptions $options
|
|
|
|
* @param LBFactory $lbFactory
|
2016-06-15 10:07:01 +00:00
|
|
|
* @param BagOStuff $cache
|
2019-03-14 14:39:10 +00:00
|
|
|
* @param OATHAuth $auth
|
2022-12-31 18:48:09 +00:00
|
|
|
* @param CentralIdLookupFactory $centralIdLookupFactory
|
|
|
|
* @param LoggerInterface $logger
|
2016-09-30 21:13:57 +00:00
|
|
|
*/
|
2022-12-31 18:48:09 +00:00
|
|
|
public function __construct(
|
|
|
|
ServiceOptions $options,
|
|
|
|
LBFactory $lbFactory,
|
|
|
|
BagOStuff $cache,
|
|
|
|
OATHAuth $auth,
|
|
|
|
CentralIdLookupFactory $centralIdLookupFactory,
|
|
|
|
LoggerInterface $logger
|
|
|
|
) {
|
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
$this->options = $options;
|
|
|
|
$this->lbFactory = $lbFactory;
|
2016-06-15 10:07:01 +00:00
|
|
|
$this->cache = $cache;
|
2019-03-14 14:39:10 +00:00
|
|
|
$this->auth = $auth;
|
2022-12-31 18:48:09 +00:00
|
|
|
$this->centralIdLookupFactory = $centralIdLookupFactory;
|
|
|
|
$this->setLogger( $logger );
|
2018-12-17 23:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
*/
|
|
|
|
public function setLogger( LoggerInterface $logger ) {
|
|
|
|
$this->logger = $logger;
|
2014-05-19 00:05:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 23:18:35 +00:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @return OATHUser
|
2019-03-14 14:39:10 +00:00
|
|
|
* @throws \ConfigException
|
|
|
|
* @throws \MWException
|
2016-09-16 23:18:35 +00:00
|
|
|
*/
|
2014-05-19 00:05:59 +00:00
|
|
|
public function findByUser( User $user ) {
|
2016-06-15 10:07:01 +00:00
|
|
|
$oathUser = $this->cache->get( $user->getName() );
|
|
|
|
if ( !$oathUser ) {
|
2019-12-21 19:18:31 +00:00
|
|
|
$oathUser = new OATHUser( $user, [] );
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2022-12-31 18:48:09 +00:00
|
|
|
$uid = $this->centralIdLookupFactory->getLookup()
|
2021-08-13 16:19:35 +00:00
|
|
|
->centralIdFromLocalUser( $user );
|
2017-09-24 05:30:19 +00:00
|
|
|
$res = $this->getDB( DB_REPLICA )->selectRow(
|
2016-06-15 10:07:01 +00:00
|
|
|
'oathauth_users',
|
2022-03-21 23:59:51 +00:00
|
|
|
[ 'module', 'data' ],
|
2016-06-15 10:07:01 +00:00
|
|
|
[ 'id' => $uid ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
if ( $res ) {
|
2022-03-21 23:59:51 +00:00
|
|
|
$module = $this->auth->getModuleByKey( $res->module );
|
2019-03-14 14:39:10 +00:00
|
|
|
if ( $module === null ) {
|
|
|
|
throw new MWException( 'oathauth-module-invalid' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$oathUser->setModule( $module );
|
2019-12-21 19:18:31 +00:00
|
|
|
$decodedData = FormatJson::decode( $res->data, true );
|
2022-03-31 20:46:04 +00:00
|
|
|
if ( is_array( $decodedData['keys'] ) ) {
|
2019-05-15 06:04:12 +00:00
|
|
|
foreach ( $decodedData['keys'] as $keyData ) {
|
|
|
|
$key = $module->newKey( $keyData );
|
|
|
|
$oathUser->addKey( $key );
|
|
|
|
}
|
|
|
|
}
|
2016-06-15 10:07:01 +00:00
|
|
|
}
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2016-06-15 10:07:01 +00:00
|
|
|
$this->cache->set( $user->getName(), $oathUser );
|
|
|
|
}
|
2014-05-19 00:05:59 +00:00
|
|
|
return $oathUser;
|
|
|
|
}
|
|
|
|
|
2016-09-30 21:13:57 +00:00
|
|
|
/**
|
|
|
|
* @param OATHUser $user
|
2019-11-12 08:47:12 +00:00
|
|
|
* @param string|null $clientInfo
|
2019-03-14 14:39:10 +00:00
|
|
|
* @throws ConfigException
|
|
|
|
* @throws MWException
|
2016-09-30 21:13:57 +00:00
|
|
|
*/
|
2019-11-12 08:47:12 +00:00
|
|
|
public function persist( OATHUser $user, $clientInfo = null ) {
|
|
|
|
if ( !$clientInfo ) {
|
|
|
|
$clientInfo = RequestContext::getMain()->getRequest()->getIP();
|
|
|
|
}
|
2018-12-17 23:56:47 +00:00
|
|
|
$prevUser = $this->findByUser( $user->getUser() );
|
2019-03-14 14:39:10 +00:00
|
|
|
$data = $user->getModule()->getDataFromUser( $user );
|
2018-12-17 23:56:47 +00:00
|
|
|
|
2021-05-12 23:25:56 +00:00
|
|
|
$this->getDB( DB_PRIMARY )->replace(
|
2014-05-19 00:05:59 +00:00
|
|
|
'oathauth_users',
|
2020-04-17 22:12:12 +00:00
|
|
|
'id',
|
2016-09-16 23:18:35 +00:00
|
|
|
[
|
2022-12-31 18:48:09 +00:00
|
|
|
'id' => $this->centralIdLookupFactory->getLookup()
|
2021-08-13 16:19:35 +00:00
|
|
|
->centralIdFromLocalUser( $user->getUser() ),
|
2019-03-14 14:39:10 +00:00
|
|
|
'module' => $user->getModule()->getName(),
|
|
|
|
'data' => FormatJson::encode( $data )
|
2016-09-16 23:18:35 +00:00
|
|
|
],
|
2014-05-19 00:05:59 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2018-12-17 23:56:47 +00:00
|
|
|
|
|
|
|
$userName = $user->getUser()->getName();
|
|
|
|
$this->cache->set( $userName, $user );
|
|
|
|
|
|
|
|
if ( $prevUser !== false ) {
|
|
|
|
$this->logger->info( 'OATHAuth updated for {user} from {clientip}', [
|
|
|
|
'user' => $userName,
|
|
|
|
'clientip' => $clientInfo,
|
2022-02-18 00:10:53 +00:00
|
|
|
'oldoathtype' => $prevUser->getModule()->getName(),
|
|
|
|
'newoathtype' => $user->getModule()->getName(),
|
2018-12-17 23:56:47 +00:00
|
|
|
] );
|
|
|
|
} else {
|
|
|
|
// If findByUser() has returned false, there was no user row or cache entry
|
|
|
|
$this->logger->info( 'OATHAuth enabled for {user} from {clientip}', [
|
|
|
|
'user' => $userName,
|
|
|
|
'clientip' => $clientInfo,
|
2022-02-18 00:10:53 +00:00
|
|
|
'oathtype' => $user->getModule()->getName(),
|
2018-12-17 23:56:47 +00:00
|
|
|
] );
|
2022-02-17 15:15:34 +00:00
|
|
|
Notifications\Manager::notifyEnabled( $user );
|
2018-12-17 23:56:47 +00:00
|
|
|
}
|
2014-05-19 00:05:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 21:13:57 +00:00
|
|
|
/**
|
|
|
|
* @param OATHUser $user
|
2018-12-17 23:56:47 +00:00
|
|
|
* @param string $clientInfo
|
2022-02-16 09:15:02 +00:00
|
|
|
* @param bool $self Whether they disabled it themselves
|
2016-09-30 21:13:57 +00:00
|
|
|
*/
|
2022-02-16 09:15:02 +00:00
|
|
|
public function remove( OATHUser $user, $clientInfo, bool $self ) {
|
2021-05-12 23:25:56 +00:00
|
|
|
$this->getDB( DB_PRIMARY )->delete(
|
2014-05-19 00:05:59 +00:00
|
|
|
'oathauth_users',
|
2022-12-31 18:48:09 +00:00
|
|
|
[ 'id' => $this->centralIdLookupFactory->getLookup()
|
2021-08-13 16:19:35 +00:00
|
|
|
->centralIdFromLocalUser( $user->getUser() ) ],
|
2014-05-19 00:05:59 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2018-12-17 23:56:47 +00:00
|
|
|
|
|
|
|
$userName = $user->getUser()->getName();
|
|
|
|
$this->cache->delete( $userName );
|
|
|
|
|
|
|
|
$this->logger->info( 'OATHAuth disabled for {user} from {clientip}', [
|
|
|
|
'user' => $userName,
|
|
|
|
'clientip' => $clientInfo,
|
2022-02-18 00:10:53 +00:00
|
|
|
'oathtype' => $user->getModule()->getName(),
|
2018-12-17 23:56:47 +00:00
|
|
|
] );
|
2022-02-16 09:15:02 +00:00
|
|
|
Notifications\Manager::notifyDisabled( $user, $self );
|
2014-05-19 00:05:59 +00:00
|
|
|
}
|
2016-05-31 20:24:18 +00:00
|
|
|
|
|
|
|
/**
|
2021-05-12 23:25:56 +00:00
|
|
|
* @param int $index DB_PRIMARY/DB_REPLICA
|
2022-06-10 23:12:26 +00:00
|
|
|
* @return IDatabase
|
2016-05-31 20:24:18 +00:00
|
|
|
*/
|
2022-12-31 18:48:09 +00:00
|
|
|
private function getDB( int $index ): IDatabase {
|
|
|
|
$db = $this->options->get( 'OATHAuthDatabase' );
|
|
|
|
return $this->lbFactory->getMainLB( $db )->getConnectionRef( $index, [], $db );
|
2016-05-31 20:24:18 +00:00
|
|
|
}
|
2014-05-19 00:05:59 +00:00
|
|
|
}
|