OCMDiscoveryService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. $response = $client->get(
  64. $remote . '/ocm-provider/',
  65. [
  66. 'timeout' => 10,
  67. 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates'),
  68. 'connect_timeout' => 10,
  69. ]
  70. );
  71. if ($response->getStatusCode() === Http::STATUS_OK) {
  72. $body = $response->getBody();
  73. // update provider with data returned by the request
  74. $this->provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
  75. $this->cache->set($remote, $body, 60 * 60 * 24);
  76. }
  77. } catch (JsonException|OCMProviderException $e) {
  78. $this->cache->set($remote, false, 5 * 60);
  79. throw new OCMProviderException('data returned by remote seems invalid - ' . ($body ?? ''));
  80. } catch (\Exception $e) {
  81. $this->cache->set($remote, false, 5 * 60);
  82. $this->logger->warning('error while discovering ocm provider', [
  83. 'exception' => $e,
  84. 'remote' => $remote
  85. ]);
  86. throw new OCMProviderException('error while requesting remote ocm provider');
  87. }
  88. if (!$this->supportedAPIVersion($this->provider->getApiVersion())) {
  89. $this->cache->set($remote, false, 5 * 60);
  90. throw new OCMProviderException('API version not supported');
  91. }
  92. return $this->provider;
  93. }
  94. /**
  95. * Check the version from remote is supported.
  96. * The minor version of the API will be ignored:
  97. * 1.0.1 is identified as 1.0
  98. *
  99. * @param string $version
  100. *
  101. * @return bool
  102. */
  103. private function supportedAPIVersion(string $version): bool {
  104. $dot1 = strpos($version, '.');
  105. $dot2 = strpos($version, '.', $dot1 + 1);
  106. if ($dot2 > 0) {
  107. $version = substr($version, 0, $dot2);
  108. }
  109. return (in_array($version, $this->supportedAPIVersion));
  110. }
  111. }