1
0

OCMDiscoveryService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\OCM;
  8. use GuzzleHttp\Exception\ConnectException;
  9. use JsonException;
  10. use OCP\AppFramework\Http;
  11. use OCP\Http\Client\IClientService;
  12. use OCP\ICache;
  13. use OCP\ICacheFactory;
  14. use OCP\IConfig;
  15. use OCP\OCM\Exceptions\OCMProviderException;
  16. use OCP\OCM\IOCMDiscoveryService;
  17. use OCP\OCM\IOCMProvider;
  18. use Psr\Log\LoggerInterface;
  19. /**
  20. * @since 28.0.0
  21. */
  22. class OCMDiscoveryService implements IOCMDiscoveryService {
  23. private ICache $cache;
  24. public function __construct(
  25. ICacheFactory $cacheFactory,
  26. private IClientService $clientService,
  27. private IConfig $config,
  28. private IOCMProvider $provider,
  29. private LoggerInterface $logger,
  30. ) {
  31. $this->cache = $cacheFactory->createDistributed('ocm-discovery');
  32. }
  33. /**
  34. * @param string $remote
  35. * @param bool $skipCache
  36. *
  37. * @return IOCMProvider
  38. * @throws OCMProviderException
  39. */
  40. public function discover(string $remote, bool $skipCache = false): IOCMProvider {
  41. $remote = rtrim($remote, '/');
  42. if (!str_starts_with($remote, 'http://') && !str_starts_with($remote, 'https://')) {
  43. // if scheme not specified, we test both;
  44. try {
  45. return $this->discover('https://' . $remote, $skipCache);
  46. } catch (OCMProviderException|ConnectException) {
  47. return $this->discover('http://' . $remote, $skipCache);
  48. }
  49. }
  50. if (!$skipCache) {
  51. try {
  52. $cached = $this->cache->get($remote);
  53. if ($cached === false) {
  54. throw new OCMProviderException('Previous discovery failed.');
  55. }
  56. $this->provider->import(json_decode($cached ?? '', true, 8, JSON_THROW_ON_ERROR) ?? []);
  57. return $this->provider;
  58. } catch (JsonException|OCMProviderException $e) {
  59. // we ignore cache on issues
  60. }
  61. }
  62. $client = $this->clientService->newClient();
  63. try {
  64. $options = [
  65. 'timeout' => 10,
  66. 'connect_timeout' => 10,
  67. ];
  68. if ($this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates') === true) {
  69. $options['verify'] = false;
  70. }
  71. $response = $client->get($remote . '/ocm-provider/', $options);
  72. if ($response->getStatusCode() === Http::STATUS_OK) {
  73. $body = $response->getBody();
  74. // update provider with data returned by the request
  75. $this->provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
  76. $this->cache->set($remote, $body, 60 * 60 * 24);
  77. }
  78. } catch (JsonException|OCMProviderException $e) {
  79. $this->cache->set($remote, false, 5 * 60);
  80. throw new OCMProviderException('data returned by remote seems invalid - ' . ($body ?? ''));
  81. } catch (\Exception $e) {
  82. $this->cache->set($remote, false, 5 * 60);
  83. $this->logger->warning('error while discovering ocm provider', [
  84. 'exception' => $e,
  85. 'remote' => $remote
  86. ]);
  87. throw new OCMProviderException('error while requesting remote ocm provider');
  88. }
  89. return $this->provider;
  90. }
  91. }