Merge pull request #5923 from nextcloud/expire-federation-jobs

Expire federation jobs
This commit is contained in:
Roeland Jago Douma 2017-08-02 16:15:28 +02:00 committed by GitHub
commit fbfd371493
15 changed files with 400 additions and 219 deletions

View File

@ -24,75 +24,29 @@
namespace OCA\Federation\AppInfo; namespace OCA\Federation\AppInfo;
use OCA\Federation\Controller\SettingsController;
use OCA\Federation\DAV\FedAuth; use OCA\Federation\DAV\FedAuth;
use OCA\Federation\DbHandler;
use OCA\Federation\Hooks; use OCA\Federation\Hooks;
use OCA\Federation\Middleware\AddServerMiddleware; use OCA\Federation\Middleware\AddServerMiddleware;
use OCA\Federation\SyncFederationAddressBooks; use OCP\AppFramework\App;
use OCA\Federation\TrustedServers;
use OCP\AppFramework\IAppContainer;
use OCP\SabrePluginEvent; use OCP\SabrePluginEvent;
use OCP\Util; use OCP\Util;
use Sabre\DAV\Auth\Plugin; use Sabre\DAV\Auth\Plugin;
use Sabre\DAV\Server;
class Application extends \OCP\AppFramework\App { class Application extends App {
/** /**
* @param array $urlParams * @param array $urlParams
*/ */
public function __construct($urlParams = array()) { public function __construct($urlParams = []) {
parent::__construct('federation', $urlParams); parent::__construct('federation', $urlParams);
$this->registerService();
$this->registerMiddleware(); $this->registerMiddleware();
} }
private function registerService() {
$container = $this->getContainer();
$container->registerService('addServerMiddleware', function(IAppContainer $c) {
return new AddServerMiddleware(
$c->getAppName(),
\OC::$server->getL10N($c->getAppName()),
\OC::$server->getLogger()
);
});
$container->registerService('DbHandler', function(IAppContainer $c) {
return new DbHandler(
\OC::$server->getDatabaseConnection(),
\OC::$server->getL10N($c->getAppName())
);
});
$container->registerService('TrustedServers', function(IAppContainer $c) {
$server = $c->getServer();
return new TrustedServers(
$c->query('DbHandler'),
$server->getHTTPClientService(),
$server->getLogger(),
$server->getJobList(),
$server->getSecureRandom(),
$server->getConfig(),
$server->getEventDispatcher()
);
});
$container->registerService('SettingsController', function (IAppContainer $c) {
$server = $c->getServer();
return new SettingsController(
$c->getAppName(),
$server->getRequest(),
$server->getL10N($c->getAppName()),
$c->query('TrustedServers')
);
});
}
private function registerMiddleware() { private function registerMiddleware() {
$container = $this->getContainer(); $container = $this->getContainer();
$container->registerMiddleware('addServerMiddleware'); $container->registerAlias('AddServerMiddleware', AddServerMiddleware::class);
$container->registerMiddleWare('AddServerMiddleware');
} }
/** /**
@ -102,7 +56,7 @@ class Application extends \OCP\AppFramework\App {
public function registerHooks() { public function registerHooks() {
$container = $this->getContainer(); $container = $this->getContainer();
$hooksManager = new Hooks($container->query('TrustedServers')); $hooksManager = $container->query(Hooks::class);
Util::connectHook( Util::connectHook(
'OCP\Share', 'OCP\Share',
@ -111,28 +65,18 @@ class Application extends \OCP\AppFramework\App {
'addServerHook' 'addServerHook'
); );
$dispatcher = $this->getContainer()->getServer()->getEventDispatcher(); $dispatcher = $container->getServer()->getEventDispatcher();
$dispatcher->addListener('OCA\DAV\Connector\Sabre::authInit', function($event) use($container) { $dispatcher->addListener('OCA\DAV\Connector\Sabre::authInit', function($event) use($container) {
if ($event instanceof SabrePluginEvent) { if ($event instanceof SabrePluginEvent) {
$authPlugin = $event->getServer()->getPlugin('auth'); $server = $event->getServer();
if ($authPlugin instanceof Plugin) { if ($server instanceof Server) {
$h = new DbHandler($container->getServer()->getDatabaseConnection(), $authPlugin = $server->getPlugin('auth');
$container->getServer()->getL10N('federation') if ($authPlugin instanceof Plugin) {
); $authPlugin->addBackend($container->query(FedAuth::class));
$authPlugin->addBackend(new FedAuth($h)); }
} }
} }
}); });
} }
/**
* @return SyncFederationAddressBooks
*/
public function getSyncService() {
$syncService = \OC::$server->query('CardDAVSyncService');
$dbHandler = $this->getContainer()->query('DbHandler');
$discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class);
return new SyncFederationAddressBooks($dbHandler, $syncService, $discoveryService);
}
} }

View File

@ -32,8 +32,10 @@ use OC\BackgroundJob\Job;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient; use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse; use OCP\Http\Client\IResponse;
use OCP\ILogger; use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
@ -46,7 +48,7 @@ use OCP\OCS\IDiscoveryService;
* *
* @package OCA\Federation\Backgroundjob * @package OCA\Federation\Backgroundjob
*/ */
class GetSharedSecret extends Job{ class GetSharedSecret extends Job {
/** @var IClient */ /** @var IClient */
private $httpClient; private $httpClient;
@ -69,6 +71,9 @@ class GetSharedSecret extends Job{
/** @var ILogger */ /** @var ILogger */
private $logger; private $logger;
/** @var ITimeFactory */
private $timeFactory;
/** @var bool */ /** @var bool */
protected $retainJob = false; protected $retainJob = false;
@ -76,45 +81,39 @@ class GetSharedSecret extends Job{
private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret'; private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret';
/** @var int 30 day = 2592000sec */
private $maxLifespan = 2592000;
/** /**
* RequestSharedSecret constructor. * RequestSharedSecret constructor.
* *
* @param IClient $httpClient * @param IClientService $httpClientService
* @param IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param IJobList $jobList * @param IJobList $jobList
* @param TrustedServers $trustedServers * @param TrustedServers $trustedServers
* @param ILogger $logger * @param ILogger $logger
* @param DbHandler $dbHandler * @param DbHandler $dbHandler
* @param IDiscoveryService $ocsDiscoveryService * @param IDiscoveryService $ocsDiscoveryService
* @param ITimeFactory $timeFactory
*/ */
public function __construct( public function __construct(
IClient $httpClient = null, IClientService $httpClientService,
IURLGenerator $urlGenerator = null, IURLGenerator $urlGenerator,
IJobList $jobList = null, IJobList $jobList,
TrustedServers $trustedServers = null, TrustedServers $trustedServers,
ILogger $logger = null, ILogger $logger,
DbHandler $dbHandler = null, DbHandler $dbHandler,
IDiscoveryService $ocsDiscoveryService = null IDiscoveryService $ocsDiscoveryService,
ITimeFactory $timeFactory
) { ) {
$this->logger = $logger ? $logger : \OC::$server->getLogger(); $this->logger = $logger;
$this->httpClient = $httpClient ? $httpClient : \OC::$server->getHTTPClientService()->newClient(); $this->httpClient = $httpClientService->newClient();
$this->jobList = $jobList ? $jobList : \OC::$server->getJobList(); $this->jobList = $jobList;
$this->urlGenerator = $urlGenerator ? $urlGenerator : \OC::$server->getURLGenerator(); $this->urlGenerator = $urlGenerator;
$this->dbHandler = $dbHandler ? $dbHandler : new DbHandler(\OC::$server->getDatabaseConnection(), \OC::$server->getL10N('federation')); $this->dbHandler = $dbHandler;
$this->ocsDiscoveryService = $ocsDiscoveryService ? $ocsDiscoveryService : \OC::$server->query(\OCP\OCS\IDiscoveryService::class); $this->ocsDiscoveryService = $ocsDiscoveryService;
if ($trustedServers) { $this->trustedServers = $trustedServers;
$this->trustedServers = $trustedServers; $this->timeFactory = $timeFactory;
} else {
$this->trustedServers = new TrustedServers(
$this->dbHandler,
\OC::$server->getHTTPClientService(),
$this->logger,
$this->jobList,
\OC::$server->getSecureRandom(),
\OC::$server->getConfig(),
\OC::$server->getEventDispatcher()
);
}
} }
/** /**
@ -130,8 +129,10 @@ class GetSharedSecret extends Job{
$this->parentExecute($jobList, $logger); $this->parentExecute($jobList, $logger);
} }
if (!$this->retainJob) { $jobList->remove($this, $this->argument);
$jobList->remove($this, $this->argument);
if ($this->retainJob) {
$this->reAddJob($this->argument);
} }
} }
@ -147,14 +148,24 @@ class GetSharedSecret extends Job{
protected function run($argument) { protected function run($argument) {
$target = $argument['url']; $target = $argument['url'];
$created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime();
$currentTime = $this->timeFactory->getTime();
$source = $this->urlGenerator->getAbsoluteURL('/'); $source = $this->urlGenerator->getAbsoluteURL('/');
$source = rtrim($source, '/'); $source = rtrim($source, '/');
$token = $argument['token']; $token = $argument['token'];
// kill job after 30 days of trying
$deadline = $currentTime - $this->maxLifespan;
if ($created < $deadline) {
$this->retainJob = false;
$this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE);
return;
}
$endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING');
$endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint;
// make sure that we have a well formated url // make sure that we have a well formatted url
$url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format;
$result = null; $result = null;
@ -215,4 +226,23 @@ class GetSharedSecret extends Job{
} }
} }
/**
* re-add background job
*
* @param array $argument
*/
protected function reAddJob(array $argument) {
$url = $argument['url'];
$created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime();
$token = $argument['token'];
$this->jobList->add(
GetSharedSecret::class,
[
'url' => $url,
'token' => $token,
'created' => $created
]
);
}
} }

View File

@ -33,8 +33,10 @@ use OC\BackgroundJob\Job;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient; use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\ILogger; use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\OCS\IDiscoveryService; use OCP\OCS\IDiscoveryService;
@ -69,6 +71,9 @@ class RequestSharedSecret extends Job {
/** @var ILogger */ /** @var ILogger */
private $logger; private $logger;
/** @var ITimeFactory */
private $timeFactory;
/** @var bool */ /** @var bool */
protected $retainJob = false; protected $retainJob = false;
@ -76,43 +81,39 @@ class RequestSharedSecret extends Job {
private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret'; private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret';
/** @var int 30 day = 2592000sec */
private $maxLifespan = 2592000;
/** /**
* RequestSharedSecret constructor. * RequestSharedSecret constructor.
* *
* @param IClient $httpClient * @param IClientService $httpClientService
* @param IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param IJobList $jobList * @param IJobList $jobList
* @param TrustedServers $trustedServers * @param TrustedServers $trustedServers
* @param DbHandler $dbHandler * @param DbHandler $dbHandler
* @param IDiscoveryService $ocsDiscoveryService * @param IDiscoveryService $ocsDiscoveryService
* @param ILogger $logger
* @param ITimeFactory $timeFactory
*/ */
public function __construct( public function __construct(
IClient $httpClient = null, IClientService $httpClientService,
IURLGenerator $urlGenerator = null, IURLGenerator $urlGenerator,
IJobList $jobList = null, IJobList $jobList,
TrustedServers $trustedServers = null, TrustedServers $trustedServers,
DbHandler $dbHandler = null, DbHandler $dbHandler,
IDiscoveryService $ocsDiscoveryService = null IDiscoveryService $ocsDiscoveryService,
ILogger $logger,
ITimeFactory $timeFactory
) { ) {
$this->httpClient = $httpClient ? $httpClient : \OC::$server->getHTTPClientService()->newClient(); $this->httpClient = $httpClientService->newClient();
$this->jobList = $jobList ? $jobList : \OC::$server->getJobList(); $this->jobList = $jobList;
$this->urlGenerator = $urlGenerator ? $urlGenerator : \OC::$server->getURLGenerator(); $this->urlGenerator = $urlGenerator;
$this->dbHandler = $dbHandler ? $dbHandler : new DbHandler(\OC::$server->getDatabaseConnection(), \OC::$server->getL10N('federation')); $this->dbHandler = $dbHandler;
$this->logger = \OC::$server->getLogger(); $this->logger = $logger;
$this->ocsDiscoveryService = $ocsDiscoveryService ? $ocsDiscoveryService : \OC::$server->query(\OCP\OCS\IDiscoveryService::class); $this->ocsDiscoveryService = $ocsDiscoveryService;
if ($trustedServers) { $this->trustedServers = $trustedServers;
$this->trustedServers = $trustedServers; $this->timeFactory = $timeFactory;
} else {
$this->trustedServers = new TrustedServers(
$this->dbHandler,
\OC::$server->getHTTPClientService(),
$this->logger,
$this->jobList,
\OC::$server->getSecureRandom(),
\OC::$server->getConfig(),
\OC::$server->getEventDispatcher()
);
}
} }
@ -129,8 +130,10 @@ class RequestSharedSecret extends Job {
$this->parentExecute($jobList, $logger); $this->parentExecute($jobList, $logger);
} }
if (!$this->retainJob) { $jobList->remove($this, $this->argument);
$jobList->remove($this, $this->argument);
if ($this->retainJob) {
$this->reAddJob($this->argument);
} }
} }
@ -147,10 +150,20 @@ class RequestSharedSecret extends Job {
protected function run($argument) { protected function run($argument) {
$target = $argument['url']; $target = $argument['url'];
$created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime();
$currentTime = $this->timeFactory->getTime();
$source = $this->urlGenerator->getAbsoluteURL('/'); $source = $this->urlGenerator->getAbsoluteURL('/');
$source = rtrim($source, '/'); $source = rtrim($source, '/');
$token = $argument['token']; $token = $argument['token'];
// kill job after 30 days of trying
$deadline = $currentTime - $this->maxLifespan;
if ($created < $deadline) {
$this->retainJob = false;
$this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE);
return;
}
$endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING');
$endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint;
@ -198,4 +211,24 @@ class RequestSharedSecret extends Job {
} }
} }
/**
* re-add background job
*
* @param array $argument
*/
protected function reAddJob(array $argument) {
$url = $argument['url'];
$created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime();
$token = $argument['token'];
$this->jobList->add(
RequestSharedSecret::class,
[
'url' => $url,
'token' => $token,
'created' => $created
]
);
}
} }

View File

@ -36,7 +36,7 @@ class SyncFederationAddressBooks extends Command {
/** /**
* @param \OCA\Federation\SyncFederationAddressBooks $syncService * @param \OCA\Federation\SyncFederationAddressBooks $syncService
*/ */
function __construct(\OCA\Federation\SyncFederationAddressBooks $syncService) { public function __construct(\OCA\Federation\SyncFederationAddressBooks $syncService) {
parent::__construct(); parent::__construct();
$this->syncService = $syncService; $this->syncService = $syncService;

View File

@ -32,6 +32,7 @@ use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\OCS\OCSForbiddenException; use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCSController; use OCP\AppFramework\OCSController;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\ILogger; use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
@ -61,6 +62,9 @@ class OCSAuthAPIController extends OCSController{
/** @var ILogger */ /** @var ILogger */
private $logger; private $logger;
/** @var ITimeFactory */
private $timeFactory;
/** /**
* OCSAuthAPI constructor. * OCSAuthAPI constructor.
* *
@ -71,6 +75,7 @@ class OCSAuthAPIController extends OCSController{
* @param TrustedServers $trustedServers * @param TrustedServers $trustedServers
* @param DbHandler $dbHandler * @param DbHandler $dbHandler
* @param ILogger $logger * @param ILogger $logger
* @param ITimeFactory $timeFactory
*/ */
public function __construct( public function __construct(
$appName, $appName,
@ -79,7 +84,8 @@ class OCSAuthAPIController extends OCSController{
IJobList $jobList, IJobList $jobList,
TrustedServers $trustedServers, TrustedServers $trustedServers,
DbHandler $dbHandler, DbHandler $dbHandler,
ILogger $logger ILogger $logger,
ITimeFactory $timeFactory
) { ) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
@ -88,6 +94,7 @@ class OCSAuthAPIController extends OCSController{
$this->trustedServers = $trustedServers; $this->trustedServers = $trustedServers;
$this->dbHandler = $dbHandler; $this->dbHandler = $dbHandler;
$this->logger = $logger; $this->logger = $logger;
$this->timeFactory = $timeFactory;
} }
/** /**
@ -149,20 +156,12 @@ class OCSAuthAPIController extends OCSController{
throw new OCSForbiddenException(); throw new OCSForbiddenException();
} }
// we ask for the shared secret so we no longer have to ask the other server
// to request the shared secret
$this->jobList->remove('OCA\Federation\BackgroundJob\RequestSharedSecret',
[
'url' => $url,
'token' => $localToken
]
);
$this->jobList->add( $this->jobList->add(
'OCA\Federation\BackgroundJob\GetSharedSecret', 'OCA\Federation\BackgroundJob\GetSharedSecret',
[ [
'url' => $url, 'url' => $url,
'token' => $token, 'token' => $token,
'created' => $this->timeFactory->getTime()
] ]
); );
@ -210,5 +209,4 @@ class OCSAuthAPIController extends OCSController{
$storedToken = $this->dbHandler->getToken($url); $storedToken = $this->dbHandler->getToken($url);
return hash_equals($storedToken, $token); return hash_equals($storedToken, $token);
} }
} }

View File

@ -26,7 +26,6 @@ namespace OCA\Federation\Controller;
use OC\HintException; use OC\HintException;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;

View File

@ -63,6 +63,6 @@ class FedAuth extends AbstractBasic {
/** /**
* @inheritdoc * @inheritdoc
*/ */
function challenge(RequestInterface $request, ResponseInterface $response) { public function challenge(RequestInterface $request, ResponseInterface $response) {
} }
} }

View File

@ -88,11 +88,11 @@ class DbHandler {
if ($result) { if ($result) {
return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable); return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable);
} else {
$message = 'Internal failure, Could not add trusted server: ' . $url;
$message_t = $this->IL10N->t('Could not add server');
throw new HintException($message, $message_t);
} }
$message = 'Internal failure, Could not add trusted server: ' . $url;
$message_t = $this->IL10N->t('Could not add server');
throw new HintException($message, $message_t);
} }
/** /**
@ -137,8 +137,11 @@ class DbHandler {
*/ */
public function getAllServer() { public function getAllServer() {
$query = $this->connection->getQueryBuilder(); $query = $this->connection->getQueryBuilder();
$query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])->from($this->dbTable); $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])
$result = $query->execute()->fetchAll(); ->from($this->dbTable);
$statement = $query->execute();
$result = $statement->fetchAll();
$statement->closeCursor();
return $result; return $result;
} }
@ -151,10 +154,13 @@ class DbHandler {
public function serverExists($url) { public function serverExists($url) {
$hash = $this->hash($url); $hash = $this->hash($url);
$query = $this->connection->getQueryBuilder(); $query = $this->connection->getQueryBuilder();
$query->select('url')->from($this->dbTable) $query->select('url')
->from($this->dbTable)
->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
->setParameter('url_hash', $hash); ->setParameter('url_hash', $hash);
$result = $query->execute()->fetchAll(); $statement = $query->execute();
$result = $statement->fetchAll();
$statement->closeCursor();
return !empty($result); return !empty($result);
} }
@ -190,7 +196,9 @@ class DbHandler {
->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
->setParameter('url_hash', $hash); ->setParameter('url_hash', $hash);
$result = $query->execute()->fetch(); $statement = $query->execute();
$result = $statement->fetch();
$statement->closeCursor();
if (!isset($result['token'])) { if (!isset($result['token'])) {
throw new \Exception('No token found for: ' . $url); throw new \Exception('No token found for: ' . $url);
@ -229,7 +237,9 @@ class DbHandler {
->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
->setParameter('url_hash', $hash); ->setParameter('url_hash', $hash);
$result = $query->execute()->fetch(); $statement = $query->execute();
$result = $statement->fetch();
$statement->closeCursor();
return $result['shared_secret']; return $result['shared_secret'];
} }
@ -265,7 +275,9 @@ class DbHandler {
->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
->setParameter('url_hash', $hash); ->setParameter('url_hash', $hash);
$result = $query->execute()->fetch(); $statement = $query->execute();
$result = $statement->fetch();
$statement->closeCursor();
return (int)$result['status']; return (int)$result['status'];
} }
@ -314,7 +326,9 @@ class DbHandler {
$query->select('url')->from($this->dbTable) $query->select('url')->from($this->dbTable)
->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password))); ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password)));
$result = $query->execute()->fetch(); $statement = $query->execute();
$result = $statement->fetch();
$statement->closeCursor();
return !empty($result); return !empty($result);
} }

View File

@ -44,6 +44,11 @@ class AddServerMiddleware extends Middleware {
/** @var ILogger */ /** @var ILogger */
protected $logger; protected $logger;
/**
* @param string $appName
* @param IL10N $l
* @param ILogger $logger
*/
public function __construct($appName, IL10N $l, ILogger $logger) { public function __construct($appName, IL10N $l, ILogger $logger) {
$this->appName = $appName; $this->appName = $appName;
$this->l = $l; $this->l = $l;

View File

@ -24,20 +24,31 @@ namespace OCA\Federation;
use OC\BackgroundJob\TimedJob; use OC\BackgroundJob\TimedJob;
use OCA\Federation\AppInfo\Application; use OCA\Federation\AppInfo\Application;
use OCP\ILogger;
class SyncJob extends TimedJob { class SyncJob extends TimedJob {
public function __construct() { /** @var SyncFederationAddressBooks */
protected $syncService;
/** @var ILogger */
protected $logger;
/**
* @param SyncFederationAddressBooks $syncService
* @param ILogger $logger
*/
public function __construct(SyncFederationAddressBooks $syncService, ILogger $logger) {
// Run once a day // Run once a day
$this->setInterval(24 * 60 * 60); $this->setInterval(24 * 60 * 60);
$this->syncService = $syncService;
$this->logger = $logger;
} }
protected function run($argument) { protected function run($argument) {
$app = new Application(); $this->syncService->syncThemAll(function($url, $ex) {
$ss = $app->getSyncService();
$ss->syncThemAll(function($url, $ex) {
if ($ex instanceof \Exception) { if ($ex instanceof \Exception) {
\OC::$server->getLogger()->error("Error while syncing $url : " . $ex->getMessage(), ['app' => 'fed-sync']); $this->logger->error("Error while syncing $url : " . $ex->getMessage(), ['app' => 'fed-sync']);
} }
}); });
} }

View File

@ -28,6 +28,7 @@ namespace OCA\Federation;
use OC\HintException; use OC\HintException;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
use OCP\IConfig; use OCP\IConfig;
@ -68,6 +69,9 @@ class TrustedServers {
/** @var EventDispatcherInterface */ /** @var EventDispatcherInterface */
private $dispatcher; private $dispatcher;
/** @var ITimeFactory */
private $timeFactory;
/** /**
* @param DbHandler $dbHandler * @param DbHandler $dbHandler
* @param IClientService $httpClientService * @param IClientService $httpClientService
@ -76,6 +80,7 @@ class TrustedServers {
* @param ISecureRandom $secureRandom * @param ISecureRandom $secureRandom
* @param IConfig $config * @param IConfig $config
* @param EventDispatcherInterface $dispatcher * @param EventDispatcherInterface $dispatcher
* @param ITimeFactory $timeFactory
*/ */
public function __construct( public function __construct(
DbHandler $dbHandler, DbHandler $dbHandler,
@ -84,7 +89,8 @@ class TrustedServers {
IJobList $jobList, IJobList $jobList,
ISecureRandom $secureRandom, ISecureRandom $secureRandom,
IConfig $config, IConfig $config,
EventDispatcherInterface $dispatcher EventDispatcherInterface $dispatcher,
ITimeFactory $timeFactory
) { ) {
$this->dbHandler = $dbHandler; $this->dbHandler = $dbHandler;
$this->httpClientService = $httpClientService; $this->httpClientService = $httpClientService;
@ -93,6 +99,7 @@ class TrustedServers {
$this->secureRandom = $secureRandom; $this->secureRandom = $secureRandom;
$this->config = $config; $this->config = $config;
$this->dispatcher = $dispatcher; $this->dispatcher = $dispatcher;
$this->timeFactory = $timeFactory;
} }
/** /**
@ -111,7 +118,8 @@ class TrustedServers {
'OCA\Federation\BackgroundJob\RequestSharedSecret', 'OCA\Federation\BackgroundJob\RequestSharedSecret',
[ [
'url' => $url, 'url' => $url,
'token' => $token 'token' => $token,
'created' => $this->timeFactory->getTime()
] ]
); );
} }

View File

@ -31,8 +31,10 @@ use OCA\Files_Sharing\Tests\TestCase;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient; use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse; use OCP\Http\Client\IResponse;
use OCP\ILogger; use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
@ -47,36 +49,43 @@ use OCP\OCS\IDiscoveryService;
*/ */
class GetSharedSecretTest extends TestCase { class GetSharedSecretTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject | IClient */ /** @var \PHPUnit_Framework_MockObject_MockObject|IClient */
private $httpClient; private $httpClient;
/** @var \PHPUnit_Framework_MockObject_MockObject | IJobList */ /** @var \PHPUnit_Framework_MockObject_MockObject|IClientService */
private $httpClientService;
/** @var \PHPUnit_Framework_MockObject_MockObject|IJobList */
private $jobList; private $jobList;
/** @var \PHPUnit_Framework_MockObject_MockObject | IURLGenerator */ /** @var \PHPUnit_Framework_MockObject_MockObject|IURLGenerator */
private $urlGenerator; private $urlGenerator;
/** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers */ /** @var \PHPUnit_Framework_MockObject_MockObject|TrustedServers */
private $trustedServers; private $trustedServers;
/** @var \PHPUnit_Framework_MockObject_MockObject | DbHandler */ /** @var \PHPUnit_Framework_MockObject_MockObject|DbHandler */
private $dbHandler; private $dbHandler;
/** @var \PHPUnit_Framework_MockObject_MockObject | ILogger */ /** @var \PHPUnit_Framework_MockObject_MockObject|ILogger */
private $logger; private $logger;
/** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */ /** @var \PHPUnit_Framework_MockObject_MockObject|IResponse */
private $response; private $response;
/** @var \PHPUnit_Framework_MockObject_MockObject | IDiscoveryService */ /** @var \PHPUnit_Framework_MockObject_MockObject|IDiscoveryService */
private $discoverService; private $discoverService;
/** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
private $timeFactory;
/** @var GetSharedSecret */ /** @var GetSharedSecret */
private $getSharedSecret; private $getSharedSecret;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->httpClientService = $this->createMock(IClientService::class);
$this->httpClient = $this->getMockBuilder(IClient::class)->getMock(); $this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
$this->jobList = $this->getMockBuilder(IJobList::class)->getMock(); $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
$this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock(); $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
@ -87,17 +96,20 @@ class GetSharedSecretTest extends TestCase {
$this->logger = $this->getMockBuilder(ILogger::class)->getMock(); $this->logger = $this->getMockBuilder(ILogger::class)->getMock();
$this->response = $this->getMockBuilder(IResponse::class)->getMock(); $this->response = $this->getMockBuilder(IResponse::class)->getMock();
$this->discoverService = $this->getMockBuilder(IDiscoveryService::class)->getMock(); $this->discoverService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->discoverService->expects($this->any())->method('discover')->willReturn([]); $this->discoverService->expects($this->any())->method('discover')->willReturn([]);
$this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
$this->getSharedSecret = new GetSharedSecret( $this->getSharedSecret = new GetSharedSecret(
$this->httpClient, $this->httpClientService,
$this->urlGenerator, $this->urlGenerator,
$this->jobList, $this->jobList,
$this->trustedServers, $this->trustedServers,
$this->logger, $this->logger,
$this->dbHandler, $this->dbHandler,
$this->discoverService $this->discoverService,
$this->timeFactory
); );
} }
@ -112,16 +124,17 @@ class GetSharedSecretTest extends TestCase {
$getSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\GetSharedSecret') $getSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\GetSharedSecret')
->setConstructorArgs( ->setConstructorArgs(
[ [
$this->httpClient, $this->httpClientService,
$this->urlGenerator, $this->urlGenerator,
$this->jobList, $this->jobList,
$this->trustedServers, $this->trustedServers,
$this->logger, $this->logger,
$this->dbHandler, $this->dbHandler,
$this->discoverService $this->discoverService,
$this->timeFactory
] ]
)->setMethods(['parentExecute'])->getMock(); )->setMethods(['parentExecute'])->getMock();
$this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url']]); $this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
$this->trustedServers->expects($this->once())->method('isTrustedServer') $this->trustedServers->expects($this->once())->method('isTrustedServer')
->with('url')->willReturn($isTrustedServer); ->with('url')->willReturn($isTrustedServer);
@ -131,10 +144,23 @@ class GetSharedSecretTest extends TestCase {
$getSharedSecret->expects($this->never())->method('parentExecute'); $getSharedSecret->expects($this->never())->method('parentExecute');
} }
$this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]); $this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
$this->jobList->expects($this->once())->method('remove');
$this->timeFactory->method('getTime')->willReturn(42);
if ($retainBackgroundJob) { if ($retainBackgroundJob) {
$this->jobList->expects($this->never())->method('remove'); $this->jobList->expects($this->once())
->method('add')
->with(
GetSharedSecret::class,
[
'url' => 'url',
'token' => 'token',
'created' => 42,
]
);
} else { } else {
$this->jobList->expects($this->once())->method('remove'); $this->jobList->expects($this->never())->method('add');
} }
$getSharedSecret->execute($this->jobList); $getSharedSecret->execute($this->jobList);
@ -155,13 +181,15 @@ class GetSharedSecretTest extends TestCase {
* @param int $statusCode * @param int $statusCode
*/ */
public function testRun($statusCode) { public function testRun($statusCode) {
$target = 'targetURL'; $target = 'targetURL';
$source = 'sourceURL'; $source = 'sourceURL';
$token = 'token'; $token = 'token';
$argument = ['url' => $target, 'token' => $token]; $argument = ['url' => $target, 'token' => $token];
$this->timeFactory->method('getTime')
->willReturn(42);
$this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/') $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
->willReturn($source); ->willReturn($source);
$this->httpClient->expects($this->once())->method('get') $this->httpClient->expects($this->once())->method('get')
@ -208,7 +236,6 @@ class GetSharedSecretTest extends TestCase {
} else { } else {
$this->assertFalse($this->invokePrivate($this->getSharedSecret, 'retainJob')); $this->assertFalse($this->invokePrivate($this->getSharedSecret, 'retainJob'));
} }
} }
public function dataTestRun() { public function dataTestRun() {
@ -219,4 +246,33 @@ class GetSharedSecretTest extends TestCase {
]; ];
} }
public function testRunExpired() {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
$created = 42;
$argument = [
'url' => $target,
'token' => $token,
'created' => $created,
];
$this->urlGenerator->expects($this->once())
->method('getAbsoluteURL')
->with('/')
->willReturn($source);
$this->timeFactory->method('getTime')
->willReturn($created + 2592000 + 1);
$this->trustedServers->expects($this->once())
->method('setServerStatus')
->with(
$target,
TrustedServers::STATUS_FAILURE
);
$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
}
} }

View File

@ -29,42 +29,55 @@ use OCA\Federation\BackgroundJob\RequestSharedSecret;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient; use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse; use OCP\Http\Client\IResponse;
use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\OCS\IDiscoveryService; use OCP\OCS\IDiscoveryService;
use Test\TestCase; use Test\TestCase;
class RequestSharedSecretTest extends TestCase { class RequestSharedSecretTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject | IClient */ /** @var \PHPUnit_Framework_MockObject_MockObject|IClientService */
private $httpClientService;
/** @var \PHPUnit_Framework_MockObject_MockObject|IClient */
private $httpClient; private $httpClient;
/** @var \PHPUnit_Framework_MockObject_MockObject | IJobList */ /** @var \PHPUnit_Framework_MockObject_MockObject|IJobList */
private $jobList; private $jobList;
/** @var \PHPUnit_Framework_MockObject_MockObject | IURLGenerator */ /** @var \PHPUnit_Framework_MockObject_MockObject|IURLGenerator */
private $urlGenerator; private $urlGenerator;
/** @var \PHPUnit_Framework_MockObject_MockObject | DbHandler */ /** @var \PHPUnit_Framework_MockObject_MockObject|DbHandler */
private $dbHandler; private $dbHandler;
/** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers */ /** @var \PHPUnit_Framework_MockObject_MockObject|TrustedServers */
private $trustedServers; private $trustedServers;
/** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */ /** @var \PHPUnit_Framework_MockObject_MockObject|IResponse */
private $response; private $response;
/** @var \PHPUnit_Framework_MockObject_MockObject | IDiscoveryService */ /** @var \PHPUnit_Framework_MockObject_MockObject|IDiscoveryService */
private $discoveryService; private $discoveryService;
/** @var \PHPUnit_Framework_MockObject_MockObject|ILogger */
private $logger;
/** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
private $timeFactory;
/** @var RequestSharedSecret */ /** @var RequestSharedSecret */
private $requestSharedSecret; private $requestSharedSecret;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->httpClientService = $this->createMock(IClientService::class);
$this->httpClient = $this->getMockBuilder(IClient::class)->getMock(); $this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
$this->jobList = $this->getMockBuilder(IJobList::class)->getMock(); $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
$this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock(); $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
@ -74,16 +87,21 @@ class RequestSharedSecretTest extends TestCase {
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->response = $this->getMockBuilder(IResponse::class)->getMock(); $this->response = $this->getMockBuilder(IResponse::class)->getMock();
$this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock(); $this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
$this->logger = $this->createMock(ILogger::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->discoveryService->expects($this->any())->method('discover')->willReturn([]); $this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
$this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
$this->requestSharedSecret = new RequestSharedSecret( $this->requestSharedSecret = new RequestSharedSecret(
$this->httpClient, $this->httpClientService,
$this->urlGenerator, $this->urlGenerator,
$this->jobList, $this->jobList,
$this->trustedServers, $this->trustedServers,
$this->dbHandler, $this->dbHandler,
$this->discoveryService $this->discoveryService,
$this->logger,
$this->timeFactory
); );
} }
@ -98,15 +116,17 @@ class RequestSharedSecretTest extends TestCase {
$requestSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\RequestSharedSecret') $requestSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\RequestSharedSecret')
->setConstructorArgs( ->setConstructorArgs(
[ [
$this->httpClient, $this->httpClientService,
$this->urlGenerator, $this->urlGenerator,
$this->jobList, $this->jobList,
$this->trustedServers, $this->trustedServers,
$this->dbHandler, $this->dbHandler,
$this->discoveryService $this->discoveryService,
$this->logger,
$this->timeFactory
] ]
)->setMethods(['parentExecute'])->getMock(); )->setMethods(['parentExecute'])->getMock();
$this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url']]); $this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
$this->trustedServers->expects($this->once())->method('isTrustedServer') $this->trustedServers->expects($this->once())->method('isTrustedServer')
->with('url')->willReturn($isTrustedServer); ->with('url')->willReturn($isTrustedServer);
@ -116,10 +136,23 @@ class RequestSharedSecretTest extends TestCase {
$requestSharedSecret->expects($this->never())->method('parentExecute'); $requestSharedSecret->expects($this->never())->method('parentExecute');
} }
$this->invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]); $this->invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]);
$this->jobList->expects($this->once())->method('remove');
$this->timeFactory->method('getTime')->willReturn(42);
if ($retainBackgroundJob) { if ($retainBackgroundJob) {
$this->jobList->expects($this->never())->method('remove'); $this->jobList->expects($this->once())
->method('add')
->with(
RequestSharedSecret::class,
[
'url' => 'url',
'token' => 'token',
'created' => 42,
]
);
} else { } else {
$this->jobList->expects($this->once())->method('remove'); $this->jobList->expects($this->never())->method('add');
} }
$requestSharedSecret->execute($this->jobList); $requestSharedSecret->execute($this->jobList);
@ -147,6 +180,8 @@ class RequestSharedSecretTest extends TestCase {
$argument = ['url' => $target, 'token' => $token]; $argument = ['url' => $target, 'token' => $token];
$this->timeFactory->method('getTime')->willReturn(42);
$this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/') $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
->willReturn($source); ->willReturn($source);
$this->httpClient->expects($this->once())->method('post') $this->httpClient->expects($this->once())->method('post')
@ -195,4 +230,34 @@ class RequestSharedSecretTest extends TestCase {
[Http::STATUS_CONFLICT], [Http::STATUS_CONFLICT],
]; ];
} }
public function testRunExpired() {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
$created = 42;
$argument = [
'url' => $target,
'token' => $token,
'created' => $created,
];
$this->urlGenerator->expects($this->once())
->method('getAbsoluteURL')
->with('/')
->willReturn($source);
$this->timeFactory->method('getTime')
->willReturn($created + 2592000 + 1);
$this->trustedServers->expects($this->once())
->method('setServerStatus')
->with(
$target,
TrustedServers::STATUS_FAILURE
);
$this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
}
} }

View File

@ -29,8 +29,8 @@ use OC\BackgroundJob\JobList;
use OCA\Federation\Controller\OCSAuthAPIController; use OCA\Federation\Controller\OCSAuthAPIController;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http;
use OCP\AppFramework\OCS\OCSForbiddenException; use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\ILogger; use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
use OCP\Security\ISecureRandom; use OCP\Security\ISecureRandom;
@ -38,40 +38,45 @@ use Test\TestCase;
class OCSAuthAPIControllerTest extends TestCase { class OCSAuthAPIControllerTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject | IRequest */ /** @var \PHPUnit_Framework_MockObject_MockObject|IRequest */
private $request; private $request;
/** @var \PHPUnit_Framework_MockObject_MockObject | ISecureRandom */ /** @var \PHPUnit_Framework_MockObject_MockObject|ISecureRandom */
private $secureRandom; private $secureRandom;
/** @var \PHPUnit_Framework_MockObject_MockObject | JobList */ /** @var \PHPUnit_Framework_MockObject_MockObject|JobList */
private $jobList; private $jobList;
/** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers */ /** @var \PHPUnit_Framework_MockObject_MockObject|TrustedServers */
private $trustedServers; private $trustedServers;
/** @var \PHPUnit_Framework_MockObject_MockObject | DbHandler */ /** @var \PHPUnit_Framework_MockObject_MockObject|DbHandler */
private $dbHandler; private $dbHandler;
/** @var \PHPUnit_Framework_MockObject_MockObject | ILogger */ /** @var \PHPUnit_Framework_MockObject_MockObject|ILogger */
private $logger; private $logger;
/** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
private $timeFactory;
/** @var OCSAuthAPIController */ /** @var OCSAuthAPIController */
private $ocsAuthApi; private $ocsAuthApi;
/** @var int simulated timestamp */
private $currentTime = 1234567;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->request = $this->getMockBuilder('OCP\IRequest')->getMock(); $this->request = $this->createMock(IRequest::class);
$this->secureRandom = $this->getMockBuilder('OCP\Security\ISecureRandom')->getMock(); $this->secureRandom = $this->createMock(ISecureRandom::class);
$this->trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers') $this->trustedServers = $this->createMock(TrustedServers::class);
->disableOriginalConstructor()->getMock(); $this->dbHandler = $this->createMock(DbHandler::class);
$this->dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler') $this->jobList = $this->createMock(JobList::class);
->disableOriginalConstructor()->getMock(); $this->logger = $this->createMock(ILogger::class);
$this->jobList = $this->getMockBuilder('OC\BackgroundJob\JobList') $this->timeFactory = $this->createMock(ITimeFactory::class);
->disableOriginalConstructor()->getMock();
$this->logger = $this->getMockBuilder('OCP\ILogger')
->disableOriginalConstructor()->getMock();
$this->ocsAuthApi = new OCSAuthAPIController( $this->ocsAuthApi = new OCSAuthAPIController(
'federation', 'federation',
@ -80,9 +85,13 @@ class OCSAuthAPIControllerTest extends TestCase {
$this->jobList, $this->jobList,
$this->trustedServers, $this->trustedServers,
$this->dbHandler, $this->dbHandler,
$this->logger $this->logger,
$this->timeFactory
); );
$this->timeFactory->method('getTime')
->willReturn($this->currentTime);
} }
/** /**
@ -105,9 +114,7 @@ class OCSAuthAPIControllerTest extends TestCase {
if ($ok) { if ($ok) {
$this->jobList->expects($this->once())->method('add') $this->jobList->expects($this->once())->method('add')
->with('OCA\Federation\BackgroundJob\GetSharedSecret', ['url' => $url, 'token' => $token]); ->with('OCA\Federation\BackgroundJob\GetSharedSecret', ['url' => $url, 'token' => $token, 'created' => $this->currentTime]);
$this->jobList->expects($this->once())->method('remove')
->with('OCA\Federation\BackgroundJob\RequestSharedSecret', ['url' => $url, 'token' => $localToken]);
} else { } else {
$this->jobList->expects($this->never())->method('add'); $this->jobList->expects($this->never())->method('add');
$this->jobList->expects($this->never())->method('remove'); $this->jobList->expects($this->never())->method('remove');
@ -151,7 +158,8 @@ class OCSAuthAPIControllerTest extends TestCase {
$this->jobList, $this->jobList,
$this->trustedServers, $this->trustedServers,
$this->dbHandler, $this->dbHandler,
$this->logger $this->logger,
$this->timeFactory
] ]
)->setMethods(['isValidToken'])->getMock(); )->setMethods(['isValidToken'])->getMock();

View File

@ -29,6 +29,7 @@ namespace OCA\Federation\Tests;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient; use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
@ -71,6 +72,9 @@ class TrustedServersTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject | EventDispatcherInterface */ /** @var \PHPUnit_Framework_MockObject_MockObject | EventDispatcherInterface */
private $dispatcher; private $dispatcher;
/** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
private $timeFactory;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
@ -85,6 +89,7 @@ class TrustedServersTest extends TestCase {
$this->jobList = $this->getMockBuilder(IJobList::class)->getMock(); $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
$this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->getMock(); $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->getMock();
$this->config = $this->getMockBuilder(IConfig::class)->getMock(); $this->config = $this->getMockBuilder(IConfig::class)->getMock();
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->trustedServers = new TrustedServers( $this->trustedServers = new TrustedServers(
$this->dbHandler, $this->dbHandler,
@ -93,7 +98,8 @@ class TrustedServersTest extends TestCase {
$this->jobList, $this->jobList,
$this->secureRandom, $this->secureRandom,
$this->config, $this->config,
$this->dispatcher $this->dispatcher,
$this->timeFactory
); );
} }
@ -114,13 +120,16 @@ class TrustedServersTest extends TestCase {
$this->jobList, $this->jobList,
$this->secureRandom, $this->secureRandom,
$this->config, $this->config,
$this->dispatcher $this->dispatcher,
$this->timeFactory
] ]
) )
->setMethods(['normalizeUrl', 'updateProtocol']) ->setMethods(['normalizeUrl', 'updateProtocol'])
->getMock(); ->getMock();
$trustedServers->expects($this->once())->method('updateProtocol') $trustedServers->expects($this->once())->method('updateProtocol')
->with('url')->willReturn('https://url'); ->with('url')->willReturn('https://url');
$this->timeFactory->method('getTime')
->willReturn(1234567);
$this->dbHandler->expects($this->once())->method('addServer')->with('https://url') $this->dbHandler->expects($this->once())->method('addServer')->with('https://url')
->willReturn($success); ->willReturn($success);
@ -130,7 +139,7 @@ class TrustedServersTest extends TestCase {
$this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token'); $this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token');
$this->jobList->expects($this->once())->method('add') $this->jobList->expects($this->once())->method('add')
->with('OCA\Federation\BackgroundJob\RequestSharedSecret', ->with('OCA\Federation\BackgroundJob\RequestSharedSecret',
['url' => 'https://url', 'token' => 'token']); ['url' => 'https://url', 'token' => 'token', 'created' => 1234567]);
} else { } else {
$this->jobList->expects($this->never())->method('add'); $this->jobList->expects($this->never())->method('add');
} }
@ -272,7 +281,8 @@ class TrustedServersTest extends TestCase {
$this->jobList, $this->jobList,
$this->secureRandom, $this->secureRandom,
$this->config, $this->config,
$this->dispatcher $this->dispatcher,
$this->timeFactory
] ]
) )
->setMethods(['checkOwnCloudVersion']) ->setMethods(['checkOwnCloudVersion'])