1
0

MountPublicLinkController.php 7.1 KB

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