You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MountPublicLinkController.php 7.4KB

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