RequestSharedSecret.php 6.4 KB

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