AuthMechanism.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Robin McCorkell <robin@mccorkell.me.uk>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_External\Lib\Auth;
  26. use OCA\Files_External\Lib\FrontendDefinitionTrait;
  27. use OCA\Files_External\Lib\IdentifierTrait;
  28. use OCA\Files_External\Lib\StorageConfig;
  29. use OCA\Files_External\Lib\StorageModifierTrait;
  30. use OCA\Files_External\Lib\VisibilityTrait;
  31. use OCA\Files_External\Lib\IIdentifier;
  32. use OCA\Files_External\Lib\IFrontendDefinition;
  33. /**
  34. * Authentication mechanism
  35. *
  36. * An authentication mechanism can have services injected during construction,
  37. * such as \OCP\IDB for database operations. This allows an authentication
  38. * mechanism to perform advanced operations based on provided information.
  39. *
  40. * An authentication scheme defines the parameter interface, common to the
  41. * storage implementation, the backend and the authentication mechanism.
  42. * A storage implementation expects parameters according to the authentication
  43. * scheme, which are provided from the authentication mechanism.
  44. *
  45. * This class uses the following traits:
  46. * - VisibilityTrait
  47. * Restrict usage to admin-only/none
  48. * - FrontendDefinitionTrait
  49. * Specify configuration parameters and other definitions
  50. * - StorageModifierTrait
  51. * Object can affect storage mounting
  52. */
  53. class AuthMechanism implements \JsonSerializable, IIdentifier, IFrontendDefinition {
  54. /** Standard authentication schemes */
  55. public const SCHEME_NULL = 'null';
  56. public const SCHEME_BUILTIN = 'builtin';
  57. public const SCHEME_PASSWORD = 'password';
  58. public const SCHEME_OAUTH1 = 'oauth1';
  59. public const SCHEME_OAUTH2 = 'oauth2';
  60. public const SCHEME_PUBLICKEY = 'publickey';
  61. public const SCHEME_OPENSTACK = 'openstack';
  62. public const SCHEME_SMB = 'smb';
  63. use VisibilityTrait;
  64. use FrontendDefinitionTrait;
  65. use StorageModifierTrait;
  66. use IdentifierTrait;
  67. /** @var string */
  68. protected $scheme;
  69. /**
  70. * Get the authentication scheme implemented
  71. * See self::SCHEME_* constants
  72. *
  73. * @return string
  74. */
  75. public function getScheme() {
  76. return $this->scheme;
  77. }
  78. /**
  79. * @param string $scheme
  80. * @return $this
  81. */
  82. public function setScheme($scheme) {
  83. $this->scheme = $scheme;
  84. return $this;
  85. }
  86. /**
  87. * Serialize into JSON for client-side JS
  88. */
  89. public function jsonSerialize(): array {
  90. $data = $this->jsonSerializeDefinition();
  91. $data += $this->jsonSerializeIdentifier();
  92. $data['scheme'] = $this->getScheme();
  93. $data['visibility'] = $this->getVisibility();
  94. return $data;
  95. }
  96. /**
  97. * Check if parameters are satisfied in a StorageConfig
  98. *
  99. * @param StorageConfig $storage
  100. * @return bool
  101. */
  102. public function validateStorage(StorageConfig $storage) {
  103. // does the backend actually support this scheme
  104. $supportedSchemes = $storage->getBackend()->getAuthSchemes();
  105. if (!isset($supportedSchemes[$this->getScheme()])) {
  106. return false;
  107. }
  108. return $this->validateStorageDefinition($storage);
  109. }
  110. }