OCSAuthAPIController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  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\Controller;
  30. use OCA\Federation\DbHandler;
  31. use OCA\Federation\TrustedServers;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\AppFramework\OCS\OCSForbiddenException;
  34. use OCP\AppFramework\OCSController;
  35. use OCP\AppFramework\Utility\ITimeFactory;
  36. use OCP\BackgroundJob\IJobList;
  37. use OCP\IRequest;
  38. use OCP\Security\ISecureRandom;
  39. use Psr\Log\LoggerInterface;
  40. /**
  41. * Class OCSAuthAPI
  42. *
  43. * OCS API end-points to exchange shared secret between two connected Nextclouds
  44. *
  45. * @package OCA\Federation\Controller
  46. */
  47. class OCSAuthAPIController extends OCSController {
  48. private ISecureRandom $secureRandom;
  49. private IJobList $jobList;
  50. private TrustedServers $trustedServers;
  51. private DbHandler $dbHandler;
  52. private LoggerInterface $logger;
  53. private ITimeFactory $timeFactory;
  54. public function __construct(
  55. string $appName,
  56. IRequest $request,
  57. ISecureRandom $secureRandom,
  58. IJobList $jobList,
  59. TrustedServers $trustedServers,
  60. DbHandler $dbHandler,
  61. LoggerInterface $logger,
  62. ITimeFactory $timeFactory
  63. ) {
  64. parent::__construct($appName, $request);
  65. $this->secureRandom = $secureRandom;
  66. $this->jobList = $jobList;
  67. $this->trustedServers = $trustedServers;
  68. $this->dbHandler = $dbHandler;
  69. $this->logger = $logger;
  70. $this->timeFactory = $timeFactory;
  71. }
  72. /**
  73. * Request received to ask remote server for a shared secret, for legacy end-points
  74. *
  75. * @NoCSRFRequired
  76. * @PublicPage
  77. * @throws OCSForbiddenException
  78. */
  79. public function requestSharedSecretLegacy(string $url, string $token): DataResponse {
  80. return $this->requestSharedSecret($url, $token);
  81. }
  82. /**
  83. * Create shared secret and return it, for legacy end-points
  84. *
  85. * @NoCSRFRequired
  86. * @PublicPage
  87. * @throws OCSForbiddenException
  88. */
  89. public function getSharedSecretLegacy(string $url, string $token): DataResponse {
  90. return $this->getSharedSecret($url, $token);
  91. }
  92. /**
  93. * Request received to ask remote server for a shared secret
  94. *
  95. * @NoCSRFRequired
  96. * @PublicPage
  97. * @throws OCSForbiddenException
  98. */
  99. public function requestSharedSecret(string $url, string $token): DataResponse {
  100. if ($this->trustedServers->isTrustedServer($url) === false) {
  101. $this->logger->error('remote server not trusted (' . $url . ') while requesting shared secret', ['app' => 'federation']);
  102. throw new OCSForbiddenException();
  103. }
  104. // if both server initiated the exchange of the shared secret the greater
  105. // token wins
  106. $localToken = $this->dbHandler->getToken($url);
  107. if (strcmp($localToken, $token) > 0) {
  108. $this->logger->info(
  109. 'remote server (' . $url . ') presented lower token. We will initiate the exchange of the shared secret.',
  110. ['app' => 'federation']
  111. );
  112. throw new OCSForbiddenException();
  113. }
  114. $this->jobList->add(
  115. 'OCA\Federation\BackgroundJob\GetSharedSecret',
  116. [
  117. 'url' => $url,
  118. 'token' => $token,
  119. 'created' => $this->timeFactory->getTime()
  120. ]
  121. );
  122. return new DataResponse();
  123. }
  124. /**
  125. * Create shared secret and return it
  126. *
  127. * @NoCSRFRequired
  128. * @PublicPage
  129. * @throws OCSForbiddenException
  130. */
  131. public function getSharedSecret(string $url, string $token): DataResponse {
  132. if ($this->trustedServers->isTrustedServer($url) === false) {
  133. $this->logger->error('remote server not trusted (' . $url . ') while getting shared secret', ['app' => 'federation']);
  134. throw new OCSForbiddenException();
  135. }
  136. if ($this->isValidToken($url, $token) === false) {
  137. $expectedToken = $this->dbHandler->getToken($url);
  138. $this->logger->error(
  139. 'remote server (' . $url . ') didn\'t send a valid token (got "' . $token . '" but expected "'. $expectedToken . '") while getting shared secret',
  140. ['app' => 'federation']
  141. );
  142. throw new OCSForbiddenException();
  143. }
  144. $sharedSecret = $this->secureRandom->generate(32);
  145. $this->trustedServers->addSharedSecret($url, $sharedSecret);
  146. return new DataResponse([
  147. 'sharedSecret' => $sharedSecret
  148. ]);
  149. }
  150. protected function isValidToken(string $url, string $token): bool {
  151. $storedToken = $this->dbHandler->getToken($url);
  152. return hash_equals($storedToken, $token);
  153. }
  154. }