OCMDiscoveryService.php 3.1 KB

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