getsharedsecret.php 5.3 KB

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