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 MediaWiki\Logger\LoggerFactory;
|
2018-12-17 23:56:47 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2018-05-20 17:44:50 +00:00
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
2018-04-05 09:29:45 +00:00
|
|
|
use Wikimedia\Rdbms\DBConnRef;
|
2019-03-14 14:39:10 +00:00
|
|
|
use FormatJson;
|
|
|
|
use CentralIdLookup;
|
|
|
|
use MWException;
|
|
|
|
use BagOStuff;
|
|
|
|
use ConfigException;
|
|
|
|
use User;
|
|
|
|
use stdClass;
|
2019-11-12 08:47:12 +00:00
|
|
|
use RequestContext;
|
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 ) {
|
|
|
|
$oathUser = new OATHUser( $user, null );
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2016-06-15 10:07:01 +00:00
|
|
|
$uid = CentralIdLookup::factory()->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',
|
|
|
|
'*',
|
|
|
|
[ 'id' => $uid ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
if ( $res ) {
|
2019-03-14 14:39:10 +00:00
|
|
|
$data = $res->data;
|
|
|
|
$moduleKey = $res->module;
|
|
|
|
if ( $this->isLegacy( $res ) ) {
|
|
|
|
$module = $this->auth->getModuleByKey( 'totp' );
|
|
|
|
$data = $this->checkAndResolveLegacy( $data, $res );
|
|
|
|
} else {
|
|
|
|
$module = $this->auth->getModuleByKey( $moduleKey );
|
|
|
|
}
|
|
|
|
if ( $module === null ) {
|
|
|
|
// For sanity
|
|
|
|
throw new MWException( 'oathauth-module-invalid' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$oathUser->setModule( $module );
|
2019-05-15 06:04:12 +00:00
|
|
|
$decodedData = FormatJson::decode( $res->data, 1 );
|
|
|
|
if ( !isset( $decodedData['keys'] ) && $module->getName() === 'totp' ) {
|
|
|
|
// Legacy single-key setup
|
|
|
|
$key = $module->newKey( $decodedData );
|
|
|
|
$oathUser->addKey( $key );
|
|
|
|
} elseif ( is_array( $decodedData['keys'] ) ) {
|
|
|
|
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
|
|
|
|
2016-05-31 20:24:18 +00:00
|
|
|
$this->getDB( DB_MASTER )->replace(
|
2014-05-19 00:05:59 +00:00
|
|
|
'oathauth_users',
|
2016-09-16 23:18:35 +00:00
|
|
|
[ 'id' ],
|
|
|
|
[
|
2016-04-01 20:45:40 +00:00
|
|
|
'id' => CentralIdLookup::factory()->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,
|
|
|
|
] );
|
|
|
|
} 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,
|
|
|
|
] );
|
|
|
|
}
|
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
|
2016-09-30 21:13:57 +00:00
|
|
|
*/
|
2018-12-17 23:56:47 +00:00
|
|
|
public function remove( OATHUser $user, $clientInfo ) {
|
2016-05-31 20:24:18 +00:00
|
|
|
$this->getDB( DB_MASTER )->delete(
|
2014-05-19 00:05:59 +00:00
|
|
|
'oathauth_users',
|
2016-09-16 23:18:35 +00:00
|
|
|
[ 'id' => CentralIdLookup::factory()->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,
|
|
|
|
] );
|
2014-05-19 00:05:59 +00:00
|
|
|
}
|
2016-05-31 20:24:18 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-17 23:56:47 +00:00
|
|
|
* @param int $index DB_MASTER/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
|
|
|
}
|
2019-03-14 14:39:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param stdClass $row
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isLegacy( $row ) {
|
|
|
|
if ( $row->module !== '' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( property_exists( $row, 'secret' ) && $row->secret !== null ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the DB data is in the new format,
|
|
|
|
* if not converts old data to new
|
|
|
|
*
|
|
|
|
* @param string $data
|
|
|
|
* @param stdClass $row
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function checkAndResolveLegacy( $data, $row ) {
|
|
|
|
if ( $data ) {
|
|
|
|
// New data exists - no action required
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
if ( property_exists( $row, 'secret' ) && property_exists( $row, 'scratch_tokens' ) ) {
|
|
|
|
return FormatJson::encode( [
|
|
|
|
'secret' => $row->secret,
|
|
|
|
'scratch_tokens' => $row->scratch_tokens
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
2014-05-19 00:05:59 +00:00
|
|
|
}
|