OCSAuthAPIController.php 6.1 KB

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