/** * run the job, then remove it from the joblist * * @param JobList $jobList * @param ILogger $logger */ public function execute($jobList, ILogger $logger = null) { $jobList->remove($this, $this->argument); $target = $this->argument['url']; // only execute if target is still in the list of trusted domains if ($this->trustedServers->isTrustedServer($target)) { $this->parentExecute($jobList, $logger); } }
protected function run($argument) { $target = $argument['url']; $source = $this->urlGenerator->getAbsoluteURL('/'); $source = rtrim($source, '/'); $token = $argument['token']; try { $result = $this->httpClient->get($target . $this->endPoint, ['query' => ['url' => $source, 'token' => $token], 'timeout' => 3, 'connect_timeout' => 3]); $status = $result->getStatusCode(); } catch (ClientException $e) { $status = $e->getCode(); $this->logger->logException($e); } // if we received a unexpected response we try again later if ($status !== Http::STATUS_OK && $status !== Http::STATUS_FORBIDDEN) { $this->jobList->add('OCA\\Federation\\BackgroundJob\\GetSharedSecret', $argument); } else { // reset token if we received a valid response $this->dbHandler->addToken($target, ''); } if ($status === Http::STATUS_OK) { $body = $result->getBody(); $result = json_decode($body, true); if (isset($result['ocs']['data']['sharedSecret'])) { $this->trustedServers->addSharedSecret($target, $result['ocs']['data']['sharedSecret']); } else { $this->logger->error('remote server "' . $target . '"" does not return a valid shared secret', ['app' => 'federation']); $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); } } }
/** * @expectedException \Exception * @expectedExceptionMessage simulated exception */ public function testIsOwnCloudServerFail() { $server = 'server1'; $this->httpClientService->expects($this->once())->method('newClient')->willReturn($this->httpClient); $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')->willReturnCallback(function () { throw new \Exception('simulated exception'); }); $this->trustedServers->isOwnCloudServer($server); }
/** * create shared secret and return it * * @return \OC_OCS_Result */ public function getSharedSecret() { $url = $this->request->getParam('url'); $token = $this->request->getParam('token'); if ($this->trustedServers->isTrustedServer($url) === false || $this->isValidToken($url, $token) === false) { return new \OC_OCS_Result(null, HTTP::STATUS_FORBIDDEN); } $sharedSecret = $this->secureRandom->getMediumStrengthGenerator()->generate(32); $this->trustedServers->addSharedSecret($url, $sharedSecret); // reset token after the exchange of the shared secret was successful $this->dbHandler->addToken($url, ''); return new \OC_OCS_Result(['sharedSecret' => $sharedSecret], Http::STATUS_OK); }
/** * check if the server should be added to the list of trusted servers or not * * @param string $url * @return bool * @throws HintException */ protected function checkServer($url) { if ($this->trustedServers->isTrustedServer($url) === true) { $message = 'Server is already in the list of trusted servers.'; $hint = $this->l->t('Server is already in the list of trusted servers.'); throw new HintException($message, $hint); } if ($this->trustedServers->isOwnCloudServer($url) === false) { $message = 'No ownCloud server found'; $hint = $this->l->t('No ownCloud server found'); throw new HintException($message, $hint); } return true; }
/** * create shared secret and return it * * @return \OC_OCS_Result */ public function getSharedSecret() { $url = $this->request->getParam('url'); $token = $this->request->getParam('token'); if ($this->trustedServers->isTrustedServer($url) === false) { $this->logger->log(\OCP\Util::ERROR, 'remote server not trusted (' . $url . ') while getting shared secret'); return new \OC_OCS_Result(null, HTTP::STATUS_FORBIDDEN); } if ($this->isValidToken($url, $token) === false) { $this->logger->log(\OCP\Util::ERROR, 'remote server (' . $url . ') didn\'t send a valid token (got ' . $token . ') while getting shared secret'); return new \OC_OCS_Result(null, HTTP::STATUS_FORBIDDEN); } $sharedSecret = $this->secureRandom->generate(32); $this->trustedServers->addSharedSecret($url, $sharedSecret); // reset token after the exchange of the shared secret was successful $this->dbHandler->addToken($url, ''); return new \OC_OCS_Result(['sharedSecret' => $sharedSecret], Http::STATUS_OK); }
protected function run($argument) { $target = $argument['url']; $source = $this->urlGenerator->getAbsoluteURL('/'); $source = rtrim($source, '/'); $token = $argument['token']; $result = null; try { $result = $this->httpClient->get($target . $this->endPoint, ['query' => ['url' => $source, 'token' => $token], 'timeout' => 3, 'connect_timeout' => 3]); $status = $result->getStatusCode(); } catch (ClientException $e) { $status = $e->getCode(); if ($status === Http::STATUS_FORBIDDEN) { $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); } else { $this->logger->logException($e, ['app' => 'federation']); } } catch (\Exception $e) { $status = Http::STATUS_INTERNAL_SERVER_ERROR; $this->logger->logException($e, ['app' => 'federation']); } // if we received a unexpected response we try again later if ($status !== Http::STATUS_OK && $status !== Http::STATUS_FORBIDDEN) { $this->retainJob = true; } else { // reset token if we received a valid response $this->dbHandler->addToken($target, ''); } if ($status === Http::STATUS_OK && $result instanceof IResponse) { $body = $result->getBody(); $result = json_decode($body, true); if (isset($result['ocs']['data']['sharedSecret'])) { $this->trustedServers->addSharedSecret($target, $result['ocs']['data']['sharedSecret']); } else { $this->logger->error('remote server "' . $target . '"" does not return a valid shared secret', ['app' => 'federation']); $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); } } }