Capabilities.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\CloudFederationAPI;
  8. use NCU\Security\Signature\Exceptions\IdentityNotFoundException;
  9. use NCU\Security\Signature\Exceptions\SignatoryException;
  10. use OC\OCM\OCMSignatoryManager;
  11. use OCP\Capabilities\ICapability;
  12. use OCP\IAppConfig;
  13. use OCP\IURLGenerator;
  14. use OCP\OCM\Exceptions\OCMArgumentException;
  15. use OCP\OCM\IOCMProvider;
  16. use Psr\Log\LoggerInterface;
  17. class Capabilities implements ICapability {
  18. public const API_VERSION = '1.1'; // informative, real version.
  19. public function __construct(
  20. private IURLGenerator $urlGenerator,
  21. private IAppConfig $appConfig,
  22. private IOCMProvider $provider,
  23. private readonly OCMSignatoryManager $ocmSignatoryManager,
  24. private readonly LoggerInterface $logger,
  25. ) {
  26. }
  27. /**
  28. * Function an app uses to return the capabilities
  29. *
  30. * @return array{
  31. * ocm: array{
  32. * apiVersion: '1.0-proposal1',
  33. * enabled: bool,
  34. * endPoint: string,
  35. * publicKey: array{
  36. * keyId: string,
  37. * publicKeyPem: string,
  38. * },
  39. * resourceTypes: list<array{
  40. * name: string,
  41. * shareTypes: list<string>,
  42. * protocols: array<string, string>
  43. * }>,
  44. * version: string
  45. * }
  46. * }
  47. * @throws OCMArgumentException
  48. */
  49. public function getCapabilities() {
  50. $url = $this->urlGenerator->linkToRouteAbsolute('cloud_federation_api.requesthandlercontroller.addShare');
  51. $this->provider->setEnabled(true);
  52. $this->provider->setApiVersion(self::API_VERSION);
  53. $pos = strrpos($url, '/');
  54. if ($pos === false) {
  55. throw new OCMArgumentException('generated route should contains a slash character');
  56. }
  57. $this->provider->setEndPoint(substr($url, 0, $pos));
  58. $resource = $this->provider->createNewResourceType();
  59. $resource->setName('file')
  60. ->setShareTypes(['user', 'group'])
  61. ->setProtocols(['webdav' => '/public.php/webdav/']);
  62. $this->provider->addResourceType($resource);
  63. // Adding a public key to the ocm discovery
  64. try {
  65. if (!$this->appConfig->getValueBool('core', OCMSignatoryManager::APPCONFIG_SIGN_DISABLED, lazy: true)) {
  66. /**
  67. * @experimental 31.0.0
  68. * @psalm-suppress UndefinedInterfaceMethod
  69. */
  70. $this->provider->setSignatory($this->ocmSignatoryManager->getLocalSignatory());
  71. } else {
  72. $this->logger->debug('ocm public key feature disabled');
  73. }
  74. } catch (SignatoryException|IdentityNotFoundException $e) {
  75. $this->logger->warning('cannot generate local signatory', ['exception' => $e]);
  76. }
  77. return ['ocm' => json_decode(json_encode($this->provider->jsonSerialize()), true)];
  78. }
  79. }