1
0

OCSAuthAPIController.php 6.1 KB

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