OCMDiscoveryService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 JsonException;
  9. use OCP\AppFramework\Http;
  10. use OCP\Http\Client\IClientService;
  11. use OCP\ICache;
  12. use OCP\ICacheFactory;
  13. use OCP\IConfig;
  14. use OCP\OCM\Exceptions\OCMProviderException;
  15. use OCP\OCM\IOCMDiscoveryService;
  16. use OCP\OCM\IOCMProvider;
  17. use Psr\Log\LoggerInterface;
  18. /**
  19. * @since 28.0.0
  20. */
  21. class OCMDiscoveryService implements IOCMDiscoveryService {
  22. private ICache $cache;
  23. private array $supportedAPIVersion =
  24. [
  25. '1.0-proposal1',
  26. '1.0',
  27. '1.1'
  28. ];
  29. public function __construct(
  30. ICacheFactory $cacheFactory,
  31. private IClientService $clientService,
  32. private IConfig $config,
  33. private IOCMProvider $provider,
  34. private LoggerInterface $logger,
  35. ) {
  36. $this->cache = $cacheFactory->createDistributed('ocm-discovery');
  37. }
  38. /**
  39. * @param string $remote
  40. * @param bool $skipCache
  41. *
  42. * @return IOCMProvider
  43. * @throws OCMProviderException
  44. */
  45. public function discover(string $remote, bool $skipCache = false): IOCMProvider {
  46. $remote = rtrim($remote, '/');
  47. if (!$skipCache) {
  48. try {
  49. $cached = $this->cache->get($remote);
  50. if ($cached === false) {
  51. throw new OCMProviderException('Previous discovery failed.');
  52. }
  53. $this->provider->import(json_decode($cached ?? '', true, 8, JSON_THROW_ON_ERROR) ?? []);
  54. if ($this->supportedAPIVersion($this->provider->getApiVersion())) {
  55. return $this->provider; // if cache looks valid, we use it
  56. }
  57. } catch (JsonException|OCMProviderException $e) {
  58. // we ignore cache on issues
  59. }
  60. }
  61. $client = $this->clientService->newClient();
  62. try {
  63. $options = [
  64. 'timeout' => 10,
  65. 'connect_timeout' => 10,
  66. ];
  67. if ($this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates') === true) {
  68. $options['verify'] = false;
  69. }
  70. $response = $client->get(
  71. $remote . '/ocm-provider/',
  72. $options,
  73. );
  74. if ($response->getStatusCode() === Http::STATUS_OK) {
  75. $body = $response->getBody();
  76. // update provider with data returned by the request
  77. $this->provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
  78. $this->cache->set($remote, $body, 60 * 60 * 24);
  79. }
  80. } catch (JsonException|OCMProviderException $e) {
  81. $this->cache->set($remote, false, 5 * 60);
  82. throw new OCMProviderException('data returned by remote seems invalid - ' . ($body ?? ''));
  83. } catch (\Exception $e) {
  84. $this->cache->set($remote, false, 5 * 60);
  85. $this->logger->warning('error while discovering ocm provider', [
  86. 'exception' => $e,
  87. 'remote' => $remote
  88. ]);
  89. throw new OCMProviderException('error while requesting remote ocm provider');
  90. }
  91. if (!$this->supportedAPIVersion($this->provider->getApiVersion())) {
  92. $this->cache->set($remote, false, 5 * 60);
  93. throw new OCMProviderException('API version not supported');
  94. }
  95. return $this->provider;
  96. }
  97. /**
  98. * Check the version from remote is supported.
  99. * The minor version of the API will be ignored:
  100. * 1.0.1 is identified as 1.0
  101. *
  102. * @param string $version
  103. *
  104. * @return bool
  105. */
  106. private function supportedAPIVersion(string $version): bool {
  107. $dot1 = strpos($version, '.');
  108. $dot2 = strpos($version, '.', $dot1 + 1);
  109. if ($dot2 > 0) {
  110. $version = substr($version, 0, $dot2);
  111. }
  112. return (in_array($version, $this->supportedAPIVersion));
  113. }
  114. }