MountPublicLinkController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\FederatedFileSharing\Controller;
  32. use OC\HintException;
  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\JSONResponse;
  38. use OCP\Constants;
  39. use OCP\Federation\ICloudIdManager;
  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\Share\IShare;
  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. $response = new JSONResponse(['message' => $e->getHint()], Http::STATUS_BAD_REQUEST);
  131. $response->throttle();
  132. return $response;
  133. }
  134. // make sure that user is authenticated in case of a password protected link
  135. $storedPassword = $share->getPassword();
  136. $authenticated = $this->session->get('public_link_authenticated') === $share->getId() ||
  137. $this->shareManager->checkPassword($share, $password);
  138. if (!empty($storedPassword) && !$authenticated) {
  139. $response = new JSONResponse(
  140. ['message' => 'No permission to access the share'],
  141. Http::STATUS_BAD_REQUEST
  142. );
  143. $response->throttle();
  144. return $response;
  145. }
  146. if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
  147. $response = new JSONResponse(
  148. ['message' => 'Mounting file drop not supported'],
  149. Http::STATUS_BAD_REQUEST
  150. );
  151. $response->throttle();
  152. return $response;
  153. }
  154. $share->setSharedWith($shareWith);
  155. $share->setShareType(IShare::TYPE_REMOTE);
  156. try {
  157. $this->federatedShareProvider->create($share);
  158. } catch (\Exception $e) {
  159. \OC::$server->getLogger()->logException($e, [
  160. 'level' => ILogger::WARN,
  161. 'app' => 'federatedfilesharing',
  162. ]);
  163. return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
  164. }
  165. return new JSONResponse(['remoteUrl' => $server]);
  166. }
  167. /**
  168. * ask other server to get a federated share
  169. *
  170. * @NoAdminRequired
  171. *
  172. * @param string $token
  173. * @param string $remote
  174. * @param string $password
  175. * @param string $owner (only for legacy reasons, can be removed with legacyMountPublicLink())
  176. * @param string $ownerDisplayName (only for legacy reasons, can be removed with legacyMountPublicLink())
  177. * @param string $name (only for legacy reasons, can be removed with legacyMountPublicLink())
  178. * @return JSONResponse
  179. */
  180. public function askForFederatedShare($token, $remote, $password = '', $owner = '', $ownerDisplayName = '', $name = '') {
  181. // check if server admin allows to mount public links from other servers
  182. if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) {
  183. return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST);
  184. }
  185. $cloudId = $this->cloudIdManager->getCloudId($this->userSession->getUser()->getUID(), $this->addressHandler->generateRemoteURL());
  186. $httpClient = $this->clientService->newClient();
  187. try {
  188. $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/createFederatedShare',
  189. [
  190. 'body' =>
  191. [
  192. 'token' => $token,
  193. 'shareWith' => rtrim($cloudId->getId(), '/'),
  194. 'password' => $password
  195. ],
  196. 'connect_timeout' => 10,
  197. ]
  198. );
  199. } catch (\Exception $e) {
  200. if (empty($password)) {
  201. $message = $this->l->t("Couldn't establish a federated share.");
  202. } else {
  203. $message = $this->l->t("Couldn't establish a federated share, maybe the password was wrong.");
  204. }
  205. return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
  206. }
  207. $body = $response->getBody();
  208. $result = json_decode($body, true);
  209. if (is_array($result) && isset($result['remoteUrl'])) {
  210. return new JSONResponse(['message' => $this->l->t('Federated Share request sent, you will receive an invitation. Check your notifications.')]);
  211. }
  212. // if we doesn't get the expected response we assume that we try to add
  213. // a federated share from a Nextcloud <= 9 server
  214. $message = $this->l->t("Couldn't establish a federated share, it looks like the server to federate with is too old (Nextcloud <= 9).");
  215. return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
  216. }
  217. }