MountPublicLinkController.php 7.1 KB

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