OCSAuthAPIController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Federation\Controller;
  8. use OCA\Federation\DbHandler;
  9. use OCA\Federation\TrustedServers;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\OpenAPI;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\AppFramework\OCS\OCSForbiddenException;
  14. use OCP\AppFramework\OCSController;
  15. use OCP\AppFramework\Utility\ITimeFactory;
  16. use OCP\BackgroundJob\IJobList;
  17. use OCP\IRequest;
  18. use OCP\Security\Bruteforce\IThrottler;
  19. use OCP\Security\ISecureRandom;
  20. use Psr\Log\LoggerInterface;
  21. /**
  22. * Class OCSAuthAPI
  23. *
  24. * OCS API end-points to exchange shared secret between two connected Nextclouds
  25. *
  26. * @package OCA\Federation\Controller
  27. */
  28. #[OpenAPI(scope: OpenAPI::SCOPE_FEDERATION)]
  29. class OCSAuthAPIController extends OCSController {
  30. private ISecureRandom $secureRandom;
  31. private IJobList $jobList;
  32. private TrustedServers $trustedServers;
  33. private DbHandler $dbHandler;
  34. private LoggerInterface $logger;
  35. private ITimeFactory $timeFactory;
  36. private IThrottler $throttler;
  37. public function __construct(
  38. string $appName,
  39. IRequest $request,
  40. ISecureRandom $secureRandom,
  41. IJobList $jobList,
  42. TrustedServers $trustedServers,
  43. DbHandler $dbHandler,
  44. LoggerInterface $logger,
  45. ITimeFactory $timeFactory,
  46. IThrottler $throttler
  47. ) {
  48. parent::__construct($appName, $request);
  49. $this->secureRandom = $secureRandom;
  50. $this->jobList = $jobList;
  51. $this->trustedServers = $trustedServers;
  52. $this->dbHandler = $dbHandler;
  53. $this->logger = $logger;
  54. $this->timeFactory = $timeFactory;
  55. $this->throttler = $throttler;
  56. }
  57. /**
  58. * Request received to ask remote server for a shared secret, for legacy end-points
  59. *
  60. * @NoCSRFRequired
  61. * @PublicPage
  62. * @BruteForceProtection(action=federationSharedSecret)
  63. *
  64. * @param string $url URL of the server
  65. * @param string $token Token of the server
  66. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  67. * @throws OCSForbiddenException Requesting shared secret is not allowed
  68. *
  69. * 200: Shared secret requested successfully
  70. */
  71. public function requestSharedSecretLegacy(string $url, string $token): DataResponse {
  72. return $this->requestSharedSecret($url, $token);
  73. }
  74. /**
  75. * Create shared secret and return it, for legacy end-points
  76. *
  77. * @NoCSRFRequired
  78. * @PublicPage
  79. * @BruteForceProtection(action=federationSharedSecret)
  80. *
  81. * @param string $url URL of the server
  82. * @param string $token Token of the server
  83. * @return DataResponse<Http::STATUS_OK, array{sharedSecret: string}, array{}>
  84. * @throws OCSForbiddenException Getting shared secret is not allowed
  85. *
  86. * 200: Shared secret returned
  87. */
  88. public function getSharedSecretLegacy(string $url, string $token): DataResponse {
  89. return $this->getSharedSecret($url, $token);
  90. }
  91. /**
  92. * Request received to ask remote server for a shared secret
  93. *
  94. * @NoCSRFRequired
  95. * @PublicPage
  96. * @BruteForceProtection(action=federationSharedSecret)
  97. *
  98. * @param string $url URL of the server
  99. * @param string $token Token of the server
  100. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  101. * @throws OCSForbiddenException Requesting shared secret is not allowed
  102. *
  103. * 200: Shared secret requested successfully
  104. */
  105. public function requestSharedSecret(string $url, string $token): DataResponse {
  106. if ($this->trustedServers->isTrustedServer($url) === false) {
  107. $this->throttler->registerAttempt('federationSharedSecret', $this->request->getRemoteAddress());
  108. $this->logger->error('remote server not trusted (' . $url . ') while requesting shared secret', ['app' => 'federation']);
  109. throw new OCSForbiddenException();
  110. }
  111. // if both server initiated the exchange of the shared secret the greater
  112. // token wins
  113. $localToken = $this->dbHandler->getToken($url);
  114. if (strcmp($localToken, $token) > 0) {
  115. $this->logger->info(
  116. 'remote server (' . $url . ') presented lower token. We will initiate the exchange of the shared secret.',
  117. ['app' => 'federation']
  118. );
  119. throw new OCSForbiddenException();
  120. }
  121. $this->jobList->add(
  122. 'OCA\Federation\BackgroundJob\GetSharedSecret',
  123. [
  124. 'url' => $url,
  125. 'token' => $token,
  126. 'created' => $this->timeFactory->getTime()
  127. ]
  128. );
  129. return new DataResponse();
  130. }
  131. /**
  132. * Create shared secret and return it
  133. *
  134. * @NoCSRFRequired
  135. * @PublicPage
  136. * @BruteForceProtection(action=federationSharedSecret)
  137. *
  138. * @param string $url URL of the server
  139. * @param string $token Token of the server
  140. * @return DataResponse<Http::STATUS_OK, array{sharedSecret: string}, array{}>
  141. * @throws OCSForbiddenException Getting shared secret is not allowed
  142. *
  143. * 200: Shared secret returned
  144. */
  145. public function getSharedSecret(string $url, string $token): DataResponse {
  146. if ($this->trustedServers->isTrustedServer($url) === false) {
  147. $this->throttler->registerAttempt('federationSharedSecret', $this->request->getRemoteAddress());
  148. $this->logger->error('remote server not trusted (' . $url . ') while getting shared secret', ['app' => 'federation']);
  149. throw new OCSForbiddenException();
  150. }
  151. if ($this->isValidToken($url, $token) === false) {
  152. $this->throttler->registerAttempt('federationSharedSecret', $this->request->getRemoteAddress());
  153. $expectedToken = $this->dbHandler->getToken($url);
  154. $this->logger->error(
  155. 'remote server (' . $url . ') didn\'t send a valid token (got "' . $token . '" but expected "'. $expectedToken . '") while getting shared secret',
  156. ['app' => 'federation']
  157. );
  158. throw new OCSForbiddenException();
  159. }
  160. $sharedSecret = $this->secureRandom->generate(32);
  161. $this->trustedServers->addSharedSecret($url, $sharedSecret);
  162. return new DataResponse([
  163. 'sharedSecret' => $sharedSecret
  164. ]);
  165. }
  166. protected function isValidToken(string $url, string $token): bool {
  167. $storedToken = $this->dbHandler->getToken($url);
  168. return hash_equals($storedToken, $token);
  169. }
  170. }