MountPublicLinkController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016, Björn Schießle <bjoern@schiessle.org>
  5. *
  6. * @author Allan Nordhøy <epost@anotheragency.no>
  7. * @author Bjoern Schiessle <bjoern@schiessle.org>
  8. * @author Björn Schießle <bjoern@schiessle.org>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\FederatedFileSharing\Controller;
  29. use OC\Files\Filesystem;
  30. use OC\HintException;
  31. use OC\Share\Helper;
  32. use OCA\FederatedFileSharing\AddressHandler;
  33. use OCA\FederatedFileSharing\FederatedShareProvider;
  34. use OCA\Files_Sharing\External\Manager;
  35. use OCP\AppFramework\Controller;
  36. use OCP\AppFramework\Http;
  37. use OCP\AppFramework\Http\JSONResponse;
  38. use OCP\Federation\ICloudIdManager;
  39. use OCP\Files\StorageInvalidException;
  40. use OCP\Http\Client\IClientService;
  41. use OCP\IL10N;
  42. use OCP\ILogger;
  43. use OCP\IRequest;
  44. use OCP\ISession;
  45. use OCP\IUserSession;
  46. use OCP\Share\IManager;
  47. use OCP\Util;
  48. /**
  49. * Class MountPublicLinkController
  50. *
  51. * convert public links to federated shares
  52. *
  53. * @package OCA\FederatedFileSharing\Controller
  54. */
  55. class MountPublicLinkController extends Controller {
  56. /** @var FederatedShareProvider */
  57. private $federatedShareProvider;
  58. /** @var AddressHandler */
  59. private $addressHandler;
  60. /** @var IManager */
  61. private $shareManager;
  62. /** @var ISession */
  63. private $session;
  64. /** @var IL10N */
  65. private $l;
  66. /** @var IUserSession */
  67. private $userSession;
  68. /** @var IClientService */
  69. private $clientService;
  70. /** @var ICloudIdManager */
  71. private $cloudIdManager;
  72. /**
  73. * MountPublicLinkController constructor.
  74. *
  75. * @param string $appName
  76. * @param IRequest $request
  77. * @param FederatedShareProvider $federatedShareProvider
  78. * @param IManager $shareManager
  79. * @param AddressHandler $addressHandler
  80. * @param ISession $session
  81. * @param IL10N $l
  82. * @param IUserSession $userSession
  83. * @param IClientService $clientService
  84. * @param ICloudIdManager $cloudIdManager
  85. */
  86. public function __construct($appName,
  87. IRequest $request,
  88. FederatedShareProvider $federatedShareProvider,
  89. IManager $shareManager,
  90. AddressHandler $addressHandler,
  91. ISession $session,
  92. IL10N $l,
  93. IUserSession $userSession,
  94. IClientService $clientService,
  95. ICloudIdManager $cloudIdManager
  96. ) {
  97. parent::__construct($appName, $request);
  98. $this->federatedShareProvider = $federatedShareProvider;
  99. $this->shareManager = $shareManager;
  100. $this->addressHandler = $addressHandler;
  101. $this->session = $session;
  102. $this->l = $l;
  103. $this->userSession = $userSession;
  104. $this->clientService = $clientService;
  105. $this->cloudIdManager = $cloudIdManager;
  106. }
  107. /**
  108. * send federated share to a user of a public link
  109. *
  110. * @NoCSRFRequired
  111. * @PublicPage
  112. * @BruteForceProtection(action=publicLink2FederatedShare)
  113. *
  114. * @param string $shareWith
  115. * @param string $token
  116. * @param string $password
  117. * @return JSONResponse
  118. */
  119. public function createFederatedShare($shareWith, $token, $password = '') {
  120. if (!$this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) {
  121. return new JSONResponse(
  122. ['message' => 'This server doesn\'t support outgoing federated shares'],
  123. Http::STATUS_BAD_REQUEST
  124. );
  125. }
  126. try {
  127. list(, $server) = $this->addressHandler->splitUserRemote($shareWith);
  128. $share = $this->shareManager->getShareByToken($token);
  129. } catch (HintException $e) {
  130. return new JSONResponse(['message' => $e->getHint()], Http::STATUS_BAD_REQUEST);
  131. }
  132. // make sure that user is authenticated in case of a password protected link
  133. $storedPassword = $share->getPassword();
  134. $authenticated = $this->session->get('public_link_authenticated') === $share->getId() ||
  135. $this->shareManager->checkPassword($share, $password);
  136. if (!empty($storedPassword) && !$authenticated ) {
  137. $response = new JSONResponse(
  138. ['message' => 'No permission to access the share'],
  139. Http::STATUS_BAD_REQUEST
  140. );
  141. $response->throttle();
  142. return $response;
  143. }
  144. $share->setSharedWith($shareWith);
  145. try {
  146. $this->federatedShareProvider->create($share);
  147. } catch (\Exception $e) {
  148. \OC::$server->getLogger()->logException($e, [
  149. 'level' => ILogger::WARN,
  150. 'app' => 'federatedfilesharing',
  151. ]);
  152. return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
  153. }
  154. return new JSONResponse(['remoteUrl' => $server]);
  155. }
  156. /**
  157. * ask other server to get a federated share
  158. *
  159. * @NoAdminRequired
  160. *
  161. * @param string $token
  162. * @param string $remote
  163. * @param string $password
  164. * @param string $owner (only for legacy reasons, can be removed with legacyMountPublicLink())
  165. * @param string $ownerDisplayName (only for legacy reasons, can be removed with legacyMountPublicLink())
  166. * @param string $name (only for legacy reasons, can be removed with legacyMountPublicLink())
  167. * @return JSONResponse
  168. */
  169. public function askForFederatedShare($token, $remote, $password = '', $owner = '', $ownerDisplayName = '', $name = '') {
  170. // check if server admin allows to mount public links from other servers
  171. if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) {
  172. return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST);
  173. }
  174. $cloudId = $this->cloudIdManager->getCloudId($this->userSession->getUser()->getUID(), $this->addressHandler->generateRemoteURL());
  175. $httpClient = $this->clientService->newClient();
  176. try {
  177. $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/createFederatedShare',
  178. [
  179. 'body' =>
  180. [
  181. 'token' => $token,
  182. 'shareWith' => rtrim($cloudId->getId(), '/'),
  183. 'password' => $password
  184. ],
  185. 'connect_timeout' => 10,
  186. ]
  187. );
  188. } catch (\Exception $e) {
  189. if (empty($password)) {
  190. $message = $this->l->t("Couldn't establish a federated share.");
  191. } else {
  192. $message = $this->l->t("Couldn't establish a federated share, maybe the password was wrong.");
  193. }
  194. return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
  195. }
  196. $body = $response->getBody();
  197. $result = json_decode($body, true);
  198. if (is_array($result) && isset($result['remoteUrl'])) {
  199. return new JSONResponse(['message' => $this->l->t('Federated Share request sent, you will receive an invitation. Check your notifications.')]);
  200. }
  201. // if we doesn't get the expected response we assume that we try to add
  202. // a federated share from a Nextcloud <= 9 server
  203. $message = $this->l->t("Couldn't establish a federated share, it looks like the server to federate with is too old (Nextcloud <= 9).");
  204. return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
  205. }
  206. }