OCSAuthAPIController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Federation\Controller;
  27. use OCA\Federation\DbHandler;
  28. use OCA\Federation\TrustedServers;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\OCS\OCSForbiddenException;
  31. use OCP\AppFramework\OCSController;
  32. use OCP\BackgroundJob\IJobList;
  33. use OCP\ILogger;
  34. use OCP\IRequest;
  35. use OCP\Security\ISecureRandom;
  36. /**
  37. * Class OCSAuthAPI
  38. *
  39. * OCS API end-points to exchange shared secret between two connected ownClouds
  40. *
  41. * @package OCA\Federation\Controller
  42. */
  43. class OCSAuthAPIController extends OCSController{
  44. /** @var ISecureRandom */
  45. private $secureRandom;
  46. /** @var IJobList */
  47. private $jobList;
  48. /** @var TrustedServers */
  49. private $trustedServers;
  50. /** @var DbHandler */
  51. private $dbHandler;
  52. /** @var ILogger */
  53. private $logger;
  54. /**
  55. * OCSAuthAPI constructor.
  56. *
  57. * @param string $appName
  58. * @param IRequest $request
  59. * @param ISecureRandom $secureRandom
  60. * @param IJobList $jobList
  61. * @param TrustedServers $trustedServers
  62. * @param DbHandler $dbHandler
  63. * @param ILogger $logger
  64. */
  65. public function __construct(
  66. $appName,
  67. IRequest $request,
  68. ISecureRandom $secureRandom,
  69. IJobList $jobList,
  70. TrustedServers $trustedServers,
  71. DbHandler $dbHandler,
  72. ILogger $logger
  73. ) {
  74. parent::__construct($appName, $request);
  75. $this->secureRandom = $secureRandom;
  76. $this->jobList = $jobList;
  77. $this->trustedServers = $trustedServers;
  78. $this->dbHandler = $dbHandler;
  79. $this->logger = $logger;
  80. }
  81. /**
  82. * @NoCSRFRequired
  83. * @PublicPage
  84. *
  85. * request received to ask remote server for a shared secret
  86. *
  87. * @param string $url
  88. * @param string $token
  89. * @return Http\DataResponse
  90. * @throws OCSForbiddenException
  91. */
  92. public function requestSharedSecret($url, $token) {
  93. if ($this->trustedServers->isTrustedServer($url) === false) {
  94. $this->logger->error('remote server not trusted (' . $url . ') while requesting shared secret', ['app' => 'federation']);
  95. throw new OCSForbiddenException();
  96. }
  97. // if both server initiated the exchange of the shared secret the greater
  98. // token wins
  99. $localToken = $this->dbHandler->getToken($url);
  100. if (strcmp($localToken, $token) > 0) {
  101. $this->logger->info(
  102. 'remote server (' . $url . ') presented lower token. We will initiate the exchange of the shared secret.',
  103. ['app' => 'federation']
  104. );
  105. throw new OCSForbiddenException();
  106. }
  107. // we ask for the shared secret so we no longer have to ask the other server
  108. // to request the shared secret
  109. $this->jobList->remove('OCA\Federation\BackgroundJob\RequestSharedSecret',
  110. [
  111. 'url' => $url,
  112. 'token' => $localToken
  113. ]
  114. );
  115. $this->jobList->add(
  116. 'OCA\Federation\BackgroundJob\GetSharedSecret',
  117. [
  118. 'url' => $url,
  119. 'token' => $token,
  120. ]
  121. );
  122. return new Http\DataResponse();
  123. }
  124. /**
  125. * @NoCSRFRequired
  126. * @PublicPage
  127. *
  128. * create shared secret and return it
  129. *
  130. * @param string $url
  131. * @param string $token
  132. * @return Http\DataResponse
  133. * @throws OCSForbiddenException
  134. */
  135. public function getSharedSecret($url, $token) {
  136. if ($this->trustedServers->isTrustedServer($url) === false) {
  137. $this->logger->error('remote server not trusted (' . $url . ') while getting shared secret', ['app' => 'federation']);
  138. throw new OCSForbiddenException();
  139. }
  140. if ($this->isValidToken($url, $token) === false) {
  141. $expectedToken = $this->dbHandler->getToken($url);
  142. $this->logger->error(
  143. 'remote server (' . $url . ') didn\'t send a valid token (got "' . $token . '" but expected "'. $expectedToken . '") while getting shared secret',
  144. ['app' => 'federation']
  145. );
  146. throw new OCSForbiddenException();
  147. }
  148. $sharedSecret = $this->secureRandom->generate(32);
  149. $this->trustedServers->addSharedSecret($url, $sharedSecret);
  150. // reset token after the exchange of the shared secret was successful
  151. $this->dbHandler->addToken($url, '');
  152. return new Http\DataResponse([
  153. 'sharedSecret' => $sharedSecret
  154. ]);
  155. }
  156. protected function isValidToken($url, $token) {
  157. $storedToken = $this->dbHandler->getToken($url);
  158. return hash_equals($storedToken, $token);
  159. }
  160. }