1
0

CloudFederationProviderManager.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Bjoern Schiessle <bjoern@schiessle.org>
  5. *
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Maxence Lange <maxence@artificial-owl.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Federation;
  27. use OC\AppFramework\Http;
  28. use OCP\App\IAppManager;
  29. use OCP\Federation\Exceptions\ProviderDoesNotExistsException;
  30. use OCP\Federation\ICloudFederationNotification;
  31. use OCP\Federation\ICloudFederationProvider;
  32. use OCP\Federation\ICloudFederationProviderManager;
  33. use OCP\Federation\ICloudFederationShare;
  34. use OCP\Federation\ICloudIdManager;
  35. use OCP\Http\Client\IClientService;
  36. use OCP\Http\Client\IResponse;
  37. use OCP\IConfig;
  38. use OCP\OCM\Exceptions\OCMProviderException;
  39. use OCP\OCM\IOCMDiscoveryService;
  40. use Psr\Log\LoggerInterface;
  41. /**
  42. * Class Manager
  43. *
  44. * Manage Cloud Federation Providers
  45. *
  46. * @package OC\Federation
  47. */
  48. class CloudFederationProviderManager implements ICloudFederationProviderManager {
  49. /** @var array list of available cloud federation providers */
  50. private array $cloudFederationProvider = [];
  51. public function __construct(
  52. private IConfig $config,
  53. private IAppManager $appManager,
  54. private IClientService $httpClientService,
  55. private ICloudIdManager $cloudIdManager,
  56. private IOCMDiscoveryService $discoveryService,
  57. private LoggerInterface $logger
  58. ) {
  59. }
  60. /**
  61. * Registers an callback function which must return an cloud federation provider
  62. *
  63. * @param string $resourceType which resource type does the provider handles
  64. * @param string $displayName user facing name of the federated share provider
  65. * @param callable $callback
  66. */
  67. public function addCloudFederationProvider($resourceType, $displayName, callable $callback) {
  68. $this->cloudFederationProvider[$resourceType] = [
  69. 'resourceType' => $resourceType,
  70. 'displayName' => $displayName,
  71. 'callback' => $callback,
  72. ];
  73. }
  74. /**
  75. * remove cloud federation provider
  76. *
  77. * @param string $providerId
  78. */
  79. public function removeCloudFederationProvider($providerId) {
  80. unset($this->cloudFederationProvider[$providerId]);
  81. }
  82. /**
  83. * get a list of all cloudFederationProviders
  84. *
  85. * @return array [resourceType => ['resourceType' => $resourceType, 'displayName' => $displayName, 'callback' => callback]]
  86. */
  87. public function getAllCloudFederationProviders() {
  88. return $this->cloudFederationProvider;
  89. }
  90. /**
  91. * get a specific cloud federation provider
  92. *
  93. * @param string $resourceType
  94. * @return ICloudFederationProvider
  95. * @throws ProviderDoesNotExistsException
  96. */
  97. public function getCloudFederationProvider($resourceType) {
  98. if (isset($this->cloudFederationProvider[$resourceType])) {
  99. return call_user_func($this->cloudFederationProvider[$resourceType]['callback']);
  100. } else {
  101. throw new ProviderDoesNotExistsException($resourceType);
  102. }
  103. }
  104. /**
  105. * @deprecated 29.0.0 - Use {@see sendCloudShare()} instead and handle errors manually
  106. */
  107. public function sendShare(ICloudFederationShare $share) {
  108. $cloudID = $this->cloudIdManager->resolveCloudId($share->getShareWith());
  109. try {
  110. $ocmProvider = $this->discoveryService->discover($cloudID->getRemote());
  111. } catch (OCMProviderException $e) {
  112. return false;
  113. }
  114. $client = $this->httpClientService->newClient();
  115. try {
  116. $response = $client->post($ocmProvider->getEndPoint() . '/shares', [
  117. 'body' => json_encode($share->getShare()),
  118. 'headers' => ['content-type' => 'application/json'],
  119. 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates', false),
  120. 'timeout' => 10,
  121. 'connect_timeout' => 10,
  122. ]);
  123. if ($response->getStatusCode() === Http::STATUS_CREATED) {
  124. $result = json_decode($response->getBody(), true);
  125. return (is_array($result)) ? $result : [];
  126. }
  127. } catch (\Exception $e) {
  128. $this->logger->debug($e->getMessage(), ['exception' => $e]);
  129. // if flat re-sharing is not supported by the remote server
  130. // we re-throw the exception and fall back to the old behaviour.
  131. // (flat re-shares has been introduced in Nextcloud 9.1)
  132. if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
  133. throw $e;
  134. }
  135. }
  136. return false;
  137. }
  138. /**
  139. * @param ICloudFederationShare $share
  140. * @return IResponse
  141. * @throws OCMProviderException
  142. */
  143. public function sendCloudShare(ICloudFederationShare $share): IResponse {
  144. $cloudID = $this->cloudIdManager->resolveCloudId($share->getShareWith());
  145. $ocmProvider = $this->discoveryService->discover($cloudID->getRemote());
  146. $client = $this->httpClientService->newClient();
  147. try {
  148. return $client->post($ocmProvider->getEndPoint() . '/shares', [
  149. 'body' => json_encode($share->getShare()),
  150. 'headers' => ['content-type' => 'application/json'],
  151. 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates', false),
  152. 'timeout' => 10,
  153. 'connect_timeout' => 10,
  154. ]);
  155. } catch (\Throwable $e) {
  156. $this->logger->error('Error while sending share to federation server: ' . $e->getMessage(), ['exception' => $e]);
  157. try {
  158. return $client->getResponseFromThrowable($e);
  159. } catch (\Throwable $e) {
  160. throw new OCMProviderException($e->getMessage(), $e->getCode(), $e);
  161. }
  162. }
  163. }
  164. /**
  165. * @param string $url
  166. * @param ICloudFederationNotification $notification
  167. * @return array|false
  168. * @deprecated 29.0.0 - Use {@see sendCloudNotification()} instead and handle errors manually
  169. */
  170. public function sendNotification($url, ICloudFederationNotification $notification) {
  171. try {
  172. $ocmProvider = $this->discoveryService->discover($url);
  173. } catch (OCMProviderException $e) {
  174. return false;
  175. }
  176. $client = $this->httpClientService->newClient();
  177. try {
  178. $response = $client->post($ocmProvider->getEndPoint() . '/notifications', [
  179. 'body' => json_encode($notification->getMessage()),
  180. 'headers' => ['content-type' => 'application/json'],
  181. 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates', false),
  182. 'timeout' => 10,
  183. 'connect_timeout' => 10,
  184. ]);
  185. if ($response->getStatusCode() === Http::STATUS_CREATED) {
  186. $result = json_decode($response->getBody(), true);
  187. return (is_array($result)) ? $result : [];
  188. }
  189. } catch (\Exception $e) {
  190. // log the error and return false
  191. $this->logger->error('error while sending notification for federated share: ' . $e->getMessage(), ['exception' => $e]);
  192. }
  193. return false;
  194. }
  195. /**
  196. * @param string $url
  197. * @param ICloudFederationNotification $notification
  198. * @return IResponse
  199. * @throws OCMProviderException
  200. */
  201. public function sendCloudNotification(string $url, ICloudFederationNotification $notification): IResponse {
  202. $ocmProvider = $this->discoveryService->discover($url);
  203. $client = $this->httpClientService->newClient();
  204. try {
  205. return $client->post($ocmProvider->getEndPoint() . '/notifications', [
  206. 'body' => json_encode($notification->getMessage()),
  207. 'headers' => ['content-type' => 'application/json'],
  208. 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates', false),
  209. 'timeout' => 10,
  210. 'connect_timeout' => 10,
  211. ]);
  212. } catch (\Throwable $e) {
  213. $this->logger->error('Error while sending notification to federation server: ' . $e->getMessage(), ['exception' => $e]);
  214. try {
  215. return $client->getResponseFromThrowable($e);
  216. } catch (\Throwable $e) {
  217. throw new OCMProviderException($e->getMessage(), $e->getCode(), $e);
  218. }
  219. }
  220. }
  221. /**
  222. * check if the new cloud federation API is ready to be used
  223. *
  224. * @return bool
  225. */
  226. public function isReady() {
  227. return $this->appManager->isEnabledForUser('cloud_federation_api');
  228. }
  229. }