OCMDiscoveryService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023, Maxence Lange <maxence@artificial-owl.com>
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\OCM;
  25. use JsonException;
  26. use OCP\AppFramework\Http;
  27. use OCP\Http\Client\IClientService;
  28. use OCP\ICache;
  29. use OCP\ICacheFactory;
  30. use OCP\IConfig;
  31. use OCP\OCM\Exceptions\OCMProviderException;
  32. use OCP\OCM\IOCMDiscoveryService;
  33. use OCP\OCM\IOCMProvider;
  34. use Psr\Log\LoggerInterface;
  35. /**
  36. * @since 28.0.0
  37. */
  38. class OCMDiscoveryService implements IOCMDiscoveryService {
  39. private ICache $cache;
  40. private array $supportedAPIVersion =
  41. [
  42. '1.0-proposal1',
  43. '1.0',
  44. '1.1'
  45. ];
  46. public function __construct(
  47. ICacheFactory $cacheFactory,
  48. private IClientService $clientService,
  49. private IConfig $config,
  50. private IOCMProvider $provider,
  51. private LoggerInterface $logger,
  52. ) {
  53. $this->cache = $cacheFactory->createDistributed('ocm-discovery');
  54. }
  55. /**
  56. * @param string $remote
  57. * @param bool $skipCache
  58. *
  59. * @return IOCMProvider
  60. * @throws OCMProviderException
  61. */
  62. public function discover(string $remote, bool $skipCache = false): IOCMProvider {
  63. $remote = rtrim($remote, '/');
  64. if (!$skipCache) {
  65. try {
  66. $this->provider->import(json_decode($this->cache->get($remote) ?? '', true, 8, JSON_THROW_ON_ERROR) ?? []);
  67. if ($this->supportedAPIVersion($this->provider->getApiVersion())) {
  68. return $this->provider; // if cache looks valid, we use it
  69. }
  70. } catch (JsonException|OCMProviderException $e) {
  71. // we ignore cache on issues
  72. }
  73. }
  74. $client = $this->clientService->newClient();
  75. try {
  76. $response = $client->get(
  77. $remote . '/ocm-provider/',
  78. [
  79. 'timeout' => 10,
  80. 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates'),
  81. 'connect_timeout' => 10,
  82. ]
  83. );
  84. if ($response->getStatusCode() === Http::STATUS_OK) {
  85. $body = $response->getBody();
  86. // update provider with data returned by the request
  87. $this->provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
  88. $this->cache->set($remote, $body, 60 * 60 * 24);
  89. }
  90. } catch (JsonException|OCMProviderException $e) {
  91. throw new OCMProviderException('data returned by remote seems invalid - ' . ($body ?? ''));
  92. } catch (\Exception $e) {
  93. $this->logger->warning('error while discovering ocm provider', [
  94. 'exception' => $e,
  95. 'remote' => $remote
  96. ]);
  97. throw new OCMProviderException('error while requesting remote ocm provider');
  98. }
  99. if (!$this->supportedAPIVersion($this->provider->getApiVersion())) {
  100. throw new OCMProviderException('API version not supported');
  101. }
  102. return $this->provider;
  103. }
  104. /**
  105. * Check the version from remote is supported.
  106. * The minor version of the API will be ignored:
  107. * 1.0.1 is identified as 1.0
  108. *
  109. * @param string $version
  110. *
  111. * @return bool
  112. */
  113. private function supportedAPIVersion(string $version): bool {
  114. $dot1 = strpos($version, '.');
  115. $dot2 = strpos($version, '.', $dot1 + 1);
  116. if ($dot2 > 0) {
  117. $version = substr($version, 0, $dot2);
  118. }
  119. return (in_array($version, $this->supportedAPIVersion));
  120. }
  121. }