1
0

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