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;
|
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2021-08-13 16:19:35 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-01-14 08:27:29 +00:00
|
|
|
use MWException;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2019-11-12 08:47:12 +00:00
|
|
|
use RequestContext;
|
2020-01-14 08:27:29 +00:00
|
|
|
use User;
|
|
|
|
use Wikimedia\Rdbms\DBConnRef;
|
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
2018-04-05 09:29:45 +00:00
|
|
|
|
2014-05-19 00:05:59 +00:00
|
|
|
class OATHUserRepository {
|
2018-05-20 17:44:50 +00:00
|
|
|
/** @var ILoadBalancer */
|
2016-05-31 20:24:18 +00:00
|
|
|
protected $lb;
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2016-06-15 10:07:01 +00:00
|
|
|
/** @var BagOStuff */
|
|
|
|
protected $cache;
|
|
|
|
|
2019-03-14 14:39:10 +00:00
|
|
|
/**
|
|
|
|
* @var OATHAuth
|
|
|
|
*/
|
|
|
|
protected $auth;
|
|
|
|
|
2018-12-17 23:56:47 +00:00
|
|
|
/** @var LoggerInterface */
|
|
|
|
private $logger;
|
|
|
|
|
2016-09-30 21:13:57 +00:00
|
|
|
/**
|
|
|
|
* OATHUserRepository constructor.
|
2018-05-20 17:44:50 +00:00
|
|
|
* @param ILoadBalancer $lb
|
2016-06-15 10:07:01 +00:00
|
|
|
* @param BagOStuff $cache
|
2019-03-14 14:39:10 +00:00
|
|
|
* @param OATHAuth $auth
|
2016-09-30 21:13:57 +00:00
|
|
|
*/
|
2019-03-14 14:39:10 +00:00
|
|
|
public function __construct( ILoadBalancer $lb, BagOStuff $cache, OATHAuth $auth ) {
|
2016-05-31 20:24:18 +00:00
|
|
|
$this->lb = $lb;
|
2016-06-15 10:07:01 +00:00
|
|
|
$this->cache = $cache;
|
2019-03-14 14:39:10 +00:00
|
|
|
$this->auth = $auth;
|
2018-12-17 23:56:47 +00:00
|
|
|
|
2019-03-14 14:39:10 +00:00
|
|
|
$this->setLogger( LoggerFactory::getInstance( 'authentication' ) );
|
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
|
|
|
|
2021-08-13 16:19:35 +00:00
|
|
|
$uid = MediaWikiServices::getInstance()
|
|
|
|
->getCentralIdLookupFactory()
|
|
|
|
->getLookup()
|
|
|
|
->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-26 09:44:50 +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
|
|
|
[
|
2021-08-13 16:19:35 +00:00
|
|
|
'id' => MediaWikiServices::getInstance()
|
|
|
|
->getCentralIdLookupFactory()
|
|
|
|
->getLookup()
|
|
|
|
->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',
|
2021-08-13 16:19:35 +00:00
|
|
|
[ 'id' => MediaWikiServices::getInstance()
|
|
|
|
->getCentralIdLookupFactory()
|
|
|
|
->getLookup()
|
|
|
|
->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
|
2016-05-31 20:24:18 +00:00
|
|
|
* @return DBConnRef
|
|
|
|
*/
|
|
|
|
private function getDB( $index ) {
|
|
|
|
global $wgOATHAuthDatabase;
|
|
|
|
|
2016-09-16 23:18:35 +00:00
|
|
|
return $this->lb->getConnectionRef( $index, [], $wgOATHAuthDatabase );
|
2016-05-31 20:24:18 +00:00
|
|
|
}
|
2014-05-19 00:05:59 +00:00
|
|
|
}
|