diff options
Diffstat (limited to 'apps/federation/lib/BackgroundJob/RequestSharedSecret.php')
-rw-r--r-- | apps/federation/lib/BackgroundJob/RequestSharedSecret.php | 89 |
1 files changed, 61 insertions, 28 deletions
diff --git a/apps/federation/lib/BackgroundJob/RequestSharedSecret.php b/apps/federation/lib/BackgroundJob/RequestSharedSecret.php index 77d0234ef74..7effb838d8b 100644 --- a/apps/federation/lib/BackgroundJob/RequestSharedSecret.php +++ b/apps/federation/lib/BackgroundJob/RequestSharedSecret.php @@ -33,8 +33,10 @@ use OC\BackgroundJob\Job; use OCA\Federation\DbHandler; use OCA\Federation\TrustedServers; use OCP\AppFramework\Http; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJobList; use OCP\Http\Client\IClient; +use OCP\Http\Client\IClientService; use OCP\ILogger; use OCP\IURLGenerator; use OCP\OCS\IDiscoveryService; @@ -69,6 +71,9 @@ class RequestSharedSecret extends Job { /** @var ILogger */ private $logger; + /** @var ITimeFactory */ + private $timeFactory; + /** @var bool */ protected $retainJob = false; @@ -76,43 +81,39 @@ class RequestSharedSecret extends Job { private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret'; + /** @var int 30 day = 2592000sec */ + private $maxLifespan = 2592000; + /** * RequestSharedSecret constructor. * - * @param IClient $httpClient + * @param IClientService $httpClientService * @param IURLGenerator $urlGenerator * @param IJobList $jobList * @param TrustedServers $trustedServers * @param DbHandler $dbHandler * @param IDiscoveryService $ocsDiscoveryService + * @param ILogger $logger + * @param ITimeFactory $timeFactory */ public function __construct( - IClient $httpClient = null, - IURLGenerator $urlGenerator = null, - IJobList $jobList = null, - TrustedServers $trustedServers = null, - DbHandler $dbHandler = null, - IDiscoveryService $ocsDiscoveryService = null + IClientService $httpClientService, + IURLGenerator $urlGenerator, + IJobList $jobList, + TrustedServers $trustedServers, + DbHandler $dbHandler, + IDiscoveryService $ocsDiscoveryService, + ILogger $logger, + ITimeFactory $timeFactory ) { - $this->httpClient = $httpClient ? $httpClient : \OC::$server->getHTTPClientService()->newClient(); - $this->jobList = $jobList ? $jobList : \OC::$server->getJobList(); - $this->urlGenerator = $urlGenerator ? $urlGenerator : \OC::$server->getURLGenerator(); - $this->dbHandler = $dbHandler ? $dbHandler : new DbHandler(\OC::$server->getDatabaseConnection(), \OC::$server->getL10N('federation')); - $this->logger = \OC::$server->getLogger(); - $this->ocsDiscoveryService = $ocsDiscoveryService ? $ocsDiscoveryService : \OC::$server->query(\OCP\OCS\IDiscoveryService::class); - if ($trustedServers) { - $this->trustedServers = $trustedServers; - } else { - $this->trustedServers = new TrustedServers( - $this->dbHandler, - \OC::$server->getHTTPClientService(), - $this->logger, - $this->jobList, - \OC::$server->getSecureRandom(), - \OC::$server->getConfig(), - \OC::$server->getEventDispatcher() - ); - } + $this->httpClient = $httpClientService->newClient(); + $this->jobList = $jobList; + $this->urlGenerator = $urlGenerator; + $this->dbHandler = $dbHandler; + $this->logger = $logger; + $this->ocsDiscoveryService = $ocsDiscoveryService; + $this->trustedServers = $trustedServers; + $this->timeFactory = $timeFactory; } @@ -129,8 +130,10 @@ class RequestSharedSecret extends Job { $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) { $target = $argument['url']; + $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); + $currentTime = $this->timeFactory->getTime(); $source = $this->urlGenerator->getAbsoluteURL('/'); $source = rtrim($source, '/'); $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'); $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 + ] + ); + } } |