AuthMechanism.php 3.4 KB

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