You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.5 KiB

10 years ago
10 years ago
Authentication mechanisms for external storage backends A backend can now specify generic authentication schemes that it supports, instead of specifying the parameters for its authentication method directly. This allows multiple authentication mechanisms to be implemented for a single scheme, providing altered functionality. This commit introduces the backend framework for this feature, and so at this point the UI will be broken as the frontend does not specify the required information. Terminology: - authentication scheme Parameter interface for the authentication method. A backend supporting the 'password' scheme accepts two parameters, 'user' and 'password'. - authentication mechanism Specific mechanism implementing a scheme. Basic mechanisms may forward configuration options directly to the backend, more advanced ones may lookup parameters or retrieve them from the session New dropdown selector for external storage configurations to select the authentication mechanism to be used. Authentication mechanisms can have visibilities, just like backends. The API was extended too to make it easier to add/remove visibilities. In addition, the concept of 'allowed visibility' has been introduced, so a backend/auth mechanism can force a maximum visibility level (e.g. Local storage type) that cannot be overridden by configuration in the web UI. An authentication mechanism is a fully instantiated implementation. This allows an implementation to have dependencies injected into it, e.g. an \OCP\IDB for database operations. When a StorageConfig is being prepared for mounting, the authentication mechanism implementation has manipulateStorage() called, which inserts the relevant authentication method options into the storage ready for mounting.
10 years ago
Authentication mechanisms for external storage backends A backend can now specify generic authentication schemes that it supports, instead of specifying the parameters for its authentication method directly. This allows multiple authentication mechanisms to be implemented for a single scheme, providing altered functionality. This commit introduces the backend framework for this feature, and so at this point the UI will be broken as the frontend does not specify the required information. Terminology: - authentication scheme Parameter interface for the authentication method. A backend supporting the 'password' scheme accepts two parameters, 'user' and 'password'. - authentication mechanism Specific mechanism implementing a scheme. Basic mechanisms may forward configuration options directly to the backend, more advanced ones may lookup parameters or retrieve them from the session New dropdown selector for external storage configurations to select the authentication mechanism to be used. Authentication mechanisms can have visibilities, just like backends. The API was extended too to make it easier to add/remove visibilities. In addition, the concept of 'allowed visibility' has been introduced, so a backend/auth mechanism can force a maximum visibility level (e.g. Local storage type) that cannot be overridden by configuration in the web UI. An authentication mechanism is a fully instantiated implementation. This allows an implementation to have dependencies injected into it, e.g. an \OCP\IDB for database operations. When a StorageConfig is being prepared for mounting, the authentication mechanism implementation has manipulateStorage() called, which inserts the relevant authentication method options into the storage ready for mounting.
10 years ago
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Robin Appelman <icewind@owncloud.com>
  5. * @author Robin McCorkell <robin@mccorkell.me.uk>
  6. *
  7. * @copyright Copyright (c) 2016, ownCloud, Inc.
  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\Config;
  24. use OC\Files\Storage\Wrapper\Availability;
  25. use OCA\Files_external\Migration\StorageMigrator;
  26. use OCP\Files\Storage;
  27. use OC\Files\Mount\MountPoint;
  28. use OCP\Files\Storage\IStorageFactory;
  29. use OCA\Files_External\Lib\PersonalMount;
  30. use OCP\Files\Config\IMountProvider;
  31. use OCP\IUser;
  32. use OCA\Files_external\Service\UserStoragesService;
  33. use OCA\Files_External\Service\UserGlobalStoragesService;
  34. use OCA\Files_External\Lib\StorageConfig;
  35. use OCA\Files_External\Lib\FailedStorage;
  36. use OCP\Files\StorageNotAvailableException;
  37. /**
  38. * Make the old files_external config work with the new public mount config api
  39. */
  40. class ConfigAdapter implements IMountProvider {
  41. /** @var UserStoragesService */
  42. private $userStoragesService;
  43. /** @var UserGlobalStoragesService */
  44. private $userGlobalStoragesService;
  45. /** @var StorageMigrator */
  46. private $migrator;
  47. /**
  48. * @param UserStoragesService $userStoragesService
  49. * @param UserGlobalStoragesService $userGlobalStoragesService
  50. * @param StorageMigrator $migrator
  51. */
  52. public function __construct(
  53. UserStoragesService $userStoragesService,
  54. UserGlobalStoragesService $userGlobalStoragesService,
  55. StorageMigrator $migrator
  56. ) {
  57. $this->userStoragesService = $userStoragesService;
  58. $this->userGlobalStoragesService = $userGlobalStoragesService;
  59. $this->migrator = $migrator;
  60. }
  61. /**
  62. * Process storage ready for mounting
  63. *
  64. * @param StorageConfig $storage
  65. * @param IUser $user
  66. */
  67. private function prepareStorageConfig(StorageConfig &$storage, IUser $user) {
  68. foreach ($storage->getBackendOptions() as $option => $value) {
  69. $storage->setBackendOption($option, \OC_Mount_Config::setUserVars(
  70. $user->getUID(), $value
  71. ));
  72. }
  73. $objectStore = $storage->getBackendOption('objectstore');
  74. if ($objectStore) {
  75. $objectClass = $objectStore['class'];
  76. if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) {
  77. throw new \InvalidArgumentException('Invalid object store');
  78. }
  79. $storage->setBackendOption('objectstore', new $objectClass($objectStore));
  80. }
  81. $storage->getAuthMechanism()->manipulateStorageConfig($storage, $user);
  82. $storage->getBackend()->manipulateStorageConfig($storage, $user);
  83. }
  84. /**
  85. * Construct the storage implementation
  86. *
  87. * @param StorageConfig $storageConfig
  88. * @return Storage
  89. */
  90. private function constructStorage(StorageConfig $storageConfig) {
  91. $class = $storageConfig->getBackend()->getStorageClass();
  92. $storage = new $class($storageConfig->getBackendOptions());
  93. // auth mechanism should fire first
  94. $storage = $storageConfig->getBackend()->wrapStorage($storage);
  95. $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
  96. return $storage;
  97. }
  98. /**
  99. * Get all mountpoints applicable for the user
  100. *
  101. * @param \OCP\IUser $user
  102. * @param \OCP\Files\Storage\IStorageFactory $loader
  103. * @return \OCP\Files\Mount\IMountPoint[]
  104. */
  105. public function getMountsForUser(IUser $user, IStorageFactory $loader) {
  106. $this->migrator->migrateUser($user);
  107. $mounts = [];
  108. $this->userStoragesService->setUser($user);
  109. $this->userGlobalStoragesService->setUser($user);
  110. foreach ($this->userGlobalStoragesService->getUniqueStorages() as $storage) {
  111. try {
  112. $this->prepareStorageConfig($storage, $user);
  113. $impl = $this->constructStorage($storage);
  114. } catch (\Exception $e) {
  115. // propagate exception into filesystem
  116. $impl = new FailedStorage(['exception' => $e]);
  117. }
  118. try {
  119. $availability = $impl->getAvailability();
  120. if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
  121. $impl = new FailedStorage([
  122. 'exception' => new StorageNotAvailableException('Storage with mount id ' . $storage->getId() . ' is not available')
  123. ]);
  124. }
  125. } catch (\Exception $e) {
  126. // propagate exception into filesystem
  127. $impl = new FailedStorage(['exception' => $e]);
  128. }
  129. $mount = new MountPoint(
  130. $impl,
  131. '/' . $user->getUID() . '/files' . $storage->getMountPoint(),
  132. null,
  133. $loader,
  134. $storage->getMountOptions()
  135. );
  136. $mounts[$storage->getMountPoint()] = $mount;
  137. }
  138. foreach ($this->userStoragesService->getStorages() as $storage) {
  139. try {
  140. $this->prepareStorageConfig($storage, $user);
  141. $impl = $this->constructStorage($storage);
  142. } catch (\Exception $e) {
  143. // propagate exception into filesystem
  144. $impl = new FailedStorage(['exception' => $e]);
  145. }
  146. $mount = new PersonalMount(
  147. $this->userStoragesService,
  148. $storage->getId(),
  149. $impl,
  150. '/' . $user->getUID() . '/files' . $storage->getMountPoint(),
  151. null,
  152. $loader,
  153. $storage->getMountOptions()
  154. );
  155. $mounts[$storage->getMountPoint()] = $mount;
  156. }
  157. $this->userStoragesService->resetUser();
  158. $this->userGlobalStoragesService->resetUser();
  159. return $mounts;
  160. }
  161. }