1
0

MountPublicLinkController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\FederatedFileSharing\Controller;
  8. use OCA\DAV\Connector\Sabre\PublicAuth;
  9. use OCA\FederatedFileSharing\AddressHandler;
  10. use OCA\FederatedFileSharing\FederatedShareProvider;
  11. use OCP\AppFramework\Controller;
  12. use OCP\AppFramework\Http;
  13. use OCP\AppFramework\Http\Attribute\OpenAPI;
  14. use OCP\AppFramework\Http\JSONResponse;
  15. use OCP\Constants;
  16. use OCP\Federation\ICloudIdManager;
  17. use OCP\HintException;
  18. use OCP\Http\Client\IClientService;
  19. use OCP\IL10N;
  20. use OCP\IRequest;
  21. use OCP\ISession;
  22. use OCP\IUserSession;
  23. use OCP\Share\IManager;
  24. use OCP\Share\IShare;
  25. use Psr\Log\LoggerInterface;
  26. /**
  27. * Class MountPublicLinkController
  28. *
  29. * convert public links to federated shares
  30. *
  31. * @package OCA\FederatedFileSharing\Controller
  32. */
  33. #[OpenAPI(scope: OpenAPI::SCOPE_FEDERATION)]
  34. class MountPublicLinkController extends Controller {
  35. /**
  36. * MountPublicLinkController constructor.
  37. */
  38. public function __construct(
  39. string $appName,
  40. IRequest $request,
  41. private FederatedShareProvider $federatedShareProvider,
  42. private IManager $shareManager,
  43. private AddressHandler $addressHandler,
  44. private ISession $session,
  45. private IL10N $l,
  46. private IUserSession $userSession,
  47. private IClientService $clientService,
  48. private ICloudIdManager $cloudIdManager,
  49. private LoggerInterface $logger,
  50. ) {
  51. parent::__construct($appName, $request);
  52. }
  53. /**
  54. * send federated share to a user of a public link
  55. *
  56. * @NoCSRFRequired
  57. * @PublicPage
  58. * @BruteForceProtection(action=publicLink2FederatedShare)
  59. *
  60. * @param string $shareWith Username to share with
  61. * @param string $token Token of the share
  62. * @param string $password Password of the share
  63. * @return JSONResponse<Http::STATUS_OK, array{remoteUrl: string}, array{}>|JSONResponse<Http::STATUS_BAD_REQUEST, array{message: string}, array{}>
  64. * 200: Remote URL returned
  65. * 400: Creating share is not possible
  66. */
  67. public function createFederatedShare($shareWith, $token, $password = '') {
  68. if (!$this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) {
  69. return new JSONResponse(
  70. ['message' => 'This server doesn\'t support outgoing federated shares'],
  71. Http::STATUS_BAD_REQUEST
  72. );
  73. }
  74. try {
  75. [, $server] = $this->addressHandler->splitUserRemote($shareWith);
  76. $share = $this->shareManager->getShareByToken($token);
  77. } catch (HintException $e) {
  78. $response = new JSONResponse(['message' => $e->getHint()], Http::STATUS_BAD_REQUEST);
  79. $response->throttle();
  80. return $response;
  81. }
  82. // make sure that user is authenticated in case of a password protected link
  83. $storedPassword = $share->getPassword();
  84. $authenticated = $this->session->get(PublicAuth::DAV_AUTHENTICATED) === $share->getId() ||
  85. $this->shareManager->checkPassword($share, $password);
  86. if (!empty($storedPassword) && !$authenticated) {
  87. $response = new JSONResponse(
  88. ['message' => 'No permission to access the share'],
  89. Http::STATUS_BAD_REQUEST
  90. );
  91. $response->throttle();
  92. return $response;
  93. }
  94. if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
  95. $response = new JSONResponse(
  96. ['message' => 'Mounting file drop not supported'],
  97. Http::STATUS_BAD_REQUEST
  98. );
  99. $response->throttle();
  100. return $response;
  101. }
  102. $share->setSharedWith($shareWith);
  103. $share->setShareType(IShare::TYPE_REMOTE);
  104. try {
  105. $this->federatedShareProvider->create($share);
  106. } catch (\Exception $e) {
  107. $this->logger->warning($e->getMessage(), [
  108. 'app' => 'federatedfilesharing',
  109. 'exception' => $e,
  110. ]);
  111. return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
  112. }
  113. return new JSONResponse(['remoteUrl' => $server]);
  114. }
  115. /**
  116. * ask other server to get a federated share
  117. *
  118. * @NoAdminRequired
  119. *
  120. * @param string $token
  121. * @param string $remote
  122. * @param string $password
  123. * @param string $owner (only for legacy reasons, can be removed with legacyMountPublicLink())
  124. * @param string $ownerDisplayName (only for legacy reasons, can be removed with legacyMountPublicLink())
  125. * @param string $name (only for legacy reasons, can be removed with legacyMountPublicLink())
  126. * @return JSONResponse
  127. */
  128. public function askForFederatedShare($token, $remote, $password = '', $owner = '', $ownerDisplayName = '', $name = '') {
  129. // check if server admin allows to mount public links from other servers
  130. if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) {
  131. return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST);
  132. }
  133. $cloudId = $this->cloudIdManager->getCloudId($this->userSession->getUser()->getUID(), $this->addressHandler->generateRemoteURL());
  134. $httpClient = $this->clientService->newClient();
  135. try {
  136. $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/createFederatedShare',
  137. [
  138. 'body' =>
  139. [
  140. 'token' => $token,
  141. 'shareWith' => rtrim($cloudId->getId(), '/'),
  142. 'password' => $password
  143. ],
  144. 'connect_timeout' => 10,
  145. ]
  146. );
  147. } catch (\Exception $e) {
  148. if (empty($password)) {
  149. $message = $this->l->t("Couldn't establish a federated share.");
  150. } else {
  151. $message = $this->l->t("Couldn't establish a federated share, maybe the password was wrong.");
  152. }
  153. return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
  154. }
  155. $body = $response->getBody();
  156. $result = json_decode($body, true);
  157. if (is_array($result) && isset($result['remoteUrl'])) {
  158. return new JSONResponse(['message' => $this->l->t('Federated Share request sent, you will receive an invitation. Check your notifications.')]);
  159. }
  160. // if we doesn't get the expected response we assume that we try to add
  161. // a federated share from a Nextcloud <= 9 server
  162. $message = $this->l->t("Couldn't establish a federated share, it looks like the server to federate with is too old (Nextcloud <= 9).");
  163. return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
  164. }
  165. }