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;
|
2023-10-10 21:42:36 +00:00
|
|
|
use MediaWiki\Extension\OATHAuth\Notifications\Manager;
|
2022-12-31 18:48:09 +00:00
|
|
|
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;
|
2022-12-31 23:49:18 +00:00
|
|
|
use RuntimeException;
|
2020-01-14 08:27:29 +00:00
|
|
|
use User;
|
2018-04-05 09:29:45 +00:00
|
|
|
|
2022-12-31 18:48:09 +00:00
|
|
|
class OATHUserRepository implements LoggerAwareInterface {
|
2022-12-31 21:32:29 +00:00
|
|
|
private OATHAuthDatabase $database;
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2022-12-31 18:48:09 +00:00
|
|
|
private BagOStuff $cache;
|
2016-06-15 10:07:01 +00:00
|
|
|
|
2022-12-31 21:06:34 +00:00
|
|
|
private OATHAuthModuleRegistry $moduleRegistry;
|
2022-12-31 18:48:09 +00:00
|
|
|
|
|
|
|
private CentralIdLookupFactory $centralIdLookupFactory;
|
2019-03-14 14:39:10 +00:00
|
|
|
|
2022-12-31 18:48:09 +00:00
|
|
|
private LoggerInterface $logger;
|
|
|
|
|
|
|
|
public function __construct(
|
2022-12-31 21:32:29 +00:00
|
|
|
OATHAuthDatabase $database,
|
2022-12-31 18:48:09 +00:00
|
|
|
BagOStuff $cache,
|
2022-12-31 21:06:34 +00:00
|
|
|
OATHAuthModuleRegistry $moduleRegistry,
|
2022-12-31 18:48:09 +00:00
|
|
|
CentralIdLookupFactory $centralIdLookupFactory,
|
|
|
|
LoggerInterface $logger
|
|
|
|
) {
|
2022-12-31 21:32:29 +00:00
|
|
|
$this->database = $database;
|
2016-06-15 10:07:01 +00:00
|
|
|
$this->cache = $cache;
|
2022-12-31 21:06:34 +00:00
|
|
|
$this->moduleRegistry = $moduleRegistry;
|
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
|
2023-10-10 21:42:36 +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 );
|
2022-12-31 23:49:18 +00:00
|
|
|
|
2023-01-30 21:01:56 +00:00
|
|
|
$res = $this->database->getDB( DB_REPLICA )->newSelectQueryBuilder()
|
|
|
|
->select( [
|
|
|
|
'oad_data',
|
|
|
|
'oat_name',
|
|
|
|
] )
|
|
|
|
->from( 'oathauth_devices' )
|
|
|
|
->join( 'oathauth_types', null, [ 'oat_id = oad_type' ] )
|
|
|
|
->where( [ 'oad_user' => $uid ] )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchResultSet();
|
|
|
|
|
|
|
|
$module = null;
|
|
|
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
if ( $module && $row->oat_name !== $module->getName() ) {
|
|
|
|
// Not supported by current application-layer code.
|
2023-11-20 22:09:51 +00:00
|
|
|
throw new RuntimeException( "user {$uid} has multiple different oathauth modules defined" );
|
2022-12-31 23:49:18 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 21:01:56 +00:00
|
|
|
if ( !$module ) {
|
|
|
|
$module = $this->moduleRegistry->getModuleByKey( $row->oat_name );
|
|
|
|
$oathUser->setModule( $module );
|
2022-12-31 23:49:18 +00:00
|
|
|
|
2023-01-30 21:01:56 +00:00
|
|
|
if ( !$module ) {
|
|
|
|
throw new MWException( 'oathauth-module-invalid' );
|
2022-12-31 23:49:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:01:56 +00:00
|
|
|
$keyData = FormatJson::decode( $row->oad_data, true );
|
|
|
|
$oathUser->addKey( $module->newKey( $keyData ) );
|
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() );
|
2022-12-31 23:49:18 +00:00
|
|
|
$userId = $this->centralIdLookupFactory->getLookup()->centralIdFromLocalUser( $user->getUser() );
|
|
|
|
|
2023-01-30 21:01:56 +00:00
|
|
|
$moduleId = $this->moduleRegistry->getModuleId( $user->getModule()->getName() );
|
|
|
|
$rows = [];
|
|
|
|
foreach ( $user->getKeys() as $key ) {
|
|
|
|
$rows[] = [
|
|
|
|
'oad_user' => $userId,
|
|
|
|
'oad_type' => $moduleId,
|
|
|
|
'oad_data' => FormatJson::encode( $key->jsonSerialize() )
|
2022-12-31 23:49:18 +00:00
|
|
|
];
|
|
|
|
}
|
2018-12-17 23:56:47 +00:00
|
|
|
|
2023-01-30 21:01:56 +00:00
|
|
|
$dbw = $this->database->getDB( DB_PRIMARY );
|
|
|
|
$dbw->startAtomic( __METHOD__ );
|
|
|
|
|
|
|
|
// TODO: only update changed rows
|
|
|
|
$dbw->delete(
|
|
|
|
'oathauth_devices',
|
|
|
|
[ 'oad_user' => $userId ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
$dbw->insert(
|
|
|
|
'oathauth_devices',
|
|
|
|
$rows,
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
$dbw->endAtomic( __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
|
|
|
] );
|
2023-11-20 22:09:51 +00:00
|
|
|
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
|
2023-10-10 21:42:36 +00:00
|
|
|
* @param bool $self Whether the user disabled the 2FA 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 ) {
|
2022-12-31 23:49:18 +00:00
|
|
|
$userId = $this->centralIdLookupFactory->getLookup()
|
|
|
|
->centralIdFromLocalUser( $user->getUser() );
|
2023-01-30 21:01:56 +00:00
|
|
|
$this->database->getDB( DB_PRIMARY )->delete(
|
|
|
|
'oathauth_devices',
|
|
|
|
[ 'oad_user' => $userId ],
|
|
|
|
__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
|
|
|
] );
|
2023-10-10 21:42:36 +00:00
|
|
|
Manager::notifyDisabled( $user, $self );
|
2014-05-19 00:05:59 +00:00
|
|
|
}
|
|
|
|
}
|