AuthMechanism.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Lib\Auth;
  8. use OCA\Files_External\Lib\FrontendDefinitionTrait;
  9. use OCA\Files_External\Lib\IdentifierTrait;
  10. use OCA\Files_External\Lib\IFrontendDefinition;
  11. use OCA\Files_External\Lib\IIdentifier;
  12. use OCA\Files_External\Lib\StorageConfig;
  13. use OCA\Files_External\Lib\StorageModifierTrait;
  14. use OCA\Files_External\Lib\VisibilityTrait;
  15. /**
  16. * Authentication mechanism
  17. *
  18. * An authentication mechanism can have services injected during construction,
  19. * such as \OCP\IDB for database operations. This allows an authentication
  20. * mechanism to perform advanced operations based on provided information.
  21. *
  22. * An authentication scheme defines the parameter interface, common to the
  23. * storage implementation, the backend and the authentication mechanism.
  24. * A storage implementation expects parameters according to the authentication
  25. * scheme, which are provided from the authentication mechanism.
  26. *
  27. * This class uses the following traits:
  28. * - VisibilityTrait
  29. * Restrict usage to admin-only/none
  30. * - FrontendDefinitionTrait
  31. * Specify configuration parameters and other definitions
  32. * - StorageModifierTrait
  33. * Object can affect storage mounting
  34. */
  35. class AuthMechanism implements \JsonSerializable, IIdentifier, IFrontendDefinition {
  36. /** Standard authentication schemes */
  37. public const SCHEME_NULL = 'null';
  38. public const SCHEME_BUILTIN = 'builtin';
  39. public const SCHEME_PASSWORD = 'password';
  40. public const SCHEME_OAUTH1 = 'oauth1';
  41. public const SCHEME_OAUTH2 = 'oauth2';
  42. public const SCHEME_PUBLICKEY = 'publickey';
  43. public const SCHEME_OPENSTACK = 'openstack';
  44. public const SCHEME_SMB = 'smb';
  45. use VisibilityTrait;
  46. use FrontendDefinitionTrait;
  47. use StorageModifierTrait;
  48. use IdentifierTrait;
  49. /** @var string */
  50. protected $scheme;
  51. /**
  52. * Get the authentication scheme implemented
  53. * See self::SCHEME_* constants
  54. *
  55. * @return string
  56. */
  57. public function getScheme() {
  58. return $this->scheme;
  59. }
  60. /**
  61. * @param string $scheme
  62. * @return $this
  63. */
  64. public function setScheme($scheme) {
  65. $this->scheme = $scheme;
  66. return $this;
  67. }
  68. /**
  69. * Serialize into JSON for client-side JS
  70. */
  71. public function jsonSerialize(): array {
  72. $data = $this->jsonSerializeDefinition();
  73. $data += $this->jsonSerializeIdentifier();
  74. $data['scheme'] = $this->getScheme();
  75. $data['visibility'] = $this->getVisibility();
  76. return $data;
  77. }
  78. /**
  79. * Check if parameters are satisfied in a StorageConfig
  80. *
  81. * @param StorageConfig $storage
  82. * @return bool
  83. */
  84. public function validateStorage(StorageConfig $storage) {
  85. // does the backend actually support this scheme
  86. $supportedSchemes = $storage->getBackend()->getAuthSchemes();
  87. if (!isset($supportedSchemes[$this->getScheme()])) {
  88. return false;
  89. }
  90. return $this->validateStorageDefinition($storage);
  91. }
  92. }