authmechanism.php 3.3 KB

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