You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.6 KiB

9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Federation\BackgroundJob;
  26. use GuzzleHttp\Exception\ClientException;
  27. use OC\BackgroundJob\JobList;
  28. use OC\BackgroundJob\Job;
  29. use OCA\Federation\DbHandler;
  30. use OCA\Federation\TrustedServers;
  31. use OCP\AppFramework\Http;
  32. use OCP\BackgroundJob\IJobList;
  33. use OCP\Http\Client\IClient;
  34. use OCP\ILogger;
  35. use OCP\IURLGenerator;
  36. use OCP\OCS\IDiscoveryService;
  37. /**
  38. * Class RequestSharedSecret
  39. *
  40. * Ask remote Nextcloud to request a sharedSecret from this server
  41. *
  42. * @package OCA\Federation\Backgroundjob
  43. */
  44. class RequestSharedSecret extends Job {
  45. /** @var IClient */
  46. private $httpClient;
  47. /** @var IJobList */
  48. private $jobList;
  49. /** @var IURLGenerator */
  50. private $urlGenerator;
  51. /** @var DbHandler */
  52. private $dbHandler;
  53. /** @var TrustedServers */
  54. private $trustedServers;
  55. /** @var IDiscoveryService */
  56. private $ocsDiscoveryService;
  57. /** @var ILogger */
  58. private $logger;
  59. /** @var bool */
  60. protected $retainJob = false;
  61. private $format = '?format=json';
  62. private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret';
  63. /**
  64. * RequestSharedSecret constructor.
  65. *
  66. * @param IClient $httpClient
  67. * @param IURLGenerator $urlGenerator
  68. * @param IJobList $jobList
  69. * @param TrustedServers $trustedServers
  70. * @param DbHandler $dbHandler
  71. * @param IDiscoveryService $ocsDiscoveryService
  72. */
  73. public function __construct(
  74. IClient $httpClient = null,
  75. IURLGenerator $urlGenerator = null,
  76. IJobList $jobList = null,
  77. TrustedServers $trustedServers = null,
  78. DbHandler $dbHandler = null,
  79. IDiscoveryService $ocsDiscoveryService = null
  80. ) {
  81. $this->httpClient = $httpClient ? $httpClient : \OC::$server->getHTTPClientService()->newClient();
  82. $this->jobList = $jobList ? $jobList : \OC::$server->getJobList();
  83. $this->urlGenerator = $urlGenerator ? $urlGenerator : \OC::$server->getURLGenerator();
  84. $this->dbHandler = $dbHandler ? $dbHandler : new DbHandler(\OC::$server->getDatabaseConnection(), \OC::$server->getL10N('federation'));
  85. $this->logger = \OC::$server->getLogger();
  86. $this->ocsDiscoveryService = $ocsDiscoveryService ? $ocsDiscoveryService : \OC::$server->query(\OCP\OCS\IDiscoveryService::class);
  87. if ($trustedServers) {
  88. $this->trustedServers = $trustedServers;
  89. } else {
  90. $this->trustedServers = new TrustedServers(
  91. $this->dbHandler,
  92. \OC::$server->getHTTPClientService(),
  93. $this->logger,
  94. $this->jobList,
  95. \OC::$server->getSecureRandom(),
  96. \OC::$server->getConfig(),
  97. \OC::$server->getEventDispatcher()
  98. );
  99. }
  100. }
  101. /**
  102. * run the job, then remove it from the joblist
  103. *
  104. * @param JobList $jobList
  105. * @param ILogger|null $logger
  106. */
  107. public function execute($jobList, ILogger $logger = null) {
  108. $target = $this->argument['url'];
  109. // only execute if target is still in the list of trusted domains
  110. if ($this->trustedServers->isTrustedServer($target)) {
  111. $this->parentExecute($jobList, $logger);
  112. }
  113. if (!$this->retainJob) {
  114. $jobList->remove($this, $this->argument);
  115. }
  116. }
  117. /**
  118. * call execute() method of parent
  119. *
  120. * @param JobList $jobList
  121. * @param ILogger $logger
  122. */
  123. protected function parentExecute($jobList, $logger) {
  124. parent::execute($jobList, $logger);
  125. }
  126. protected function run($argument) {
  127. $target = $argument['url'];
  128. $source = $this->urlGenerator->getAbsoluteURL('/');
  129. $source = rtrim($source, '/');
  130. $token = $argument['token'];
  131. $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING');
  132. $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint;
  133. // make sure that we have a well formated url
  134. $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format;
  135. try {
  136. $result = $this->httpClient->post(
  137. $url,
  138. [
  139. 'body' => [
  140. 'url' => $source,
  141. 'token' => $token,
  142. ],
  143. 'timeout' => 3,
  144. 'connect_timeout' => 3,
  145. ]
  146. );
  147. $status = $result->getStatusCode();
  148. } catch (ClientException $e) {
  149. $status = $e->getCode();
  150. if ($status === Http::STATUS_FORBIDDEN) {
  151. $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']);
  152. } else {
  153. $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']);
  154. }
  155. } catch (\Exception $e) {
  156. $status = Http::STATUS_INTERNAL_SERVER_ERROR;
  157. $this->logger->logException($e, ['app' => 'federation']);
  158. }
  159. // if we received a unexpected response we try again later
  160. if (
  161. $status !== Http::STATUS_OK
  162. && $status !== Http::STATUS_FORBIDDEN
  163. ) {
  164. $this->retainJob = true;
  165. }
  166. if ($status === Http::STATUS_FORBIDDEN) {
  167. // clear token if remote server refuses to ask for shared secret
  168. $this->dbHandler->addToken($target, '');
  169. }
  170. }
  171. }