StorageAuthBase.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\Files_External\Command;
  23. use OC\Core\Command\Base;
  24. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  25. use OCA\Files_External\Lib\StorageConfig;
  26. use OCA\Files_External\NotFoundException;
  27. use OCA\Files_External\Service\GlobalStoragesService;
  28. use OCP\Files\Storage\IStorage;
  29. use OCP\Files\StorageNotAvailableException;
  30. use OCP\IUserManager;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. abstract class StorageAuthBase extends Base {
  34. public function __construct(
  35. protected GlobalStoragesService $globalService,
  36. protected IUserManager $userManager,
  37. ) {
  38. parent::__construct();
  39. }
  40. private function getUserOption(InputInterface $input): ?string {
  41. if ($input->getOption('user')) {
  42. return (string)$input->getOption('user');
  43. }
  44. return $_ENV['NOTIFY_USER'] ?? $_SERVER['NOTIFY_USER'] ?? null;
  45. }
  46. private function getPasswordOption(InputInterface $input): ?string {
  47. if ($input->getOption('password')) {
  48. return (string)$input->getOption('password');
  49. }
  50. return $_ENV['NOTIFY_PASSWORD'] ?? $_SERVER['NOTIFY_PASSWORD'] ?? null;
  51. }
  52. /**
  53. * @param InputInterface $input
  54. * @param OutputInterface $output
  55. * @return array
  56. * @psalm-return array{0: StorageConfig, 1: IStorage}|array{0: null, 1: null}
  57. */
  58. protected function createStorage(InputInterface $input, OutputInterface $output): array {
  59. try {
  60. /** @var StorageConfig|null $mount */
  61. $mount = $this->globalService->getStorage($input->getArgument('mount_id'));
  62. } catch (NotFoundException $e) {
  63. $output->writeln('<error>Mount not found</error>');
  64. return [null, null];
  65. }
  66. if (is_null($mount)) {
  67. $output->writeln('<error>Mount not found</error>');
  68. return [null, null];
  69. }
  70. $noAuth = false;
  71. $userOption = $this->getUserOption($input);
  72. $passwordOption = $this->getPasswordOption($input);
  73. // if only the user is provided, we get the user object to pass along to the auth backend
  74. // this allows using saved user credentials
  75. $user = ($userOption && !$passwordOption) ? $this->userManager->get($userOption) : null;
  76. try {
  77. $authBackend = $mount->getAuthMechanism();
  78. $authBackend->manipulateStorageConfig($mount, $user);
  79. } catch (InsufficientDataForMeaningfulAnswerException $e) {
  80. $noAuth = true;
  81. } catch (StorageNotAvailableException $e) {
  82. $noAuth = true;
  83. }
  84. if ($userOption) {
  85. $mount->setBackendOption('user', $userOption);
  86. }
  87. if ($passwordOption) {
  88. $mount->setBackendOption('password', $passwordOption);
  89. }
  90. try {
  91. $backend = $mount->getBackend();
  92. $backend->manipulateStorageConfig($mount, $user);
  93. } catch (InsufficientDataForMeaningfulAnswerException $e) {
  94. $noAuth = true;
  95. } catch (StorageNotAvailableException $e) {
  96. $noAuth = true;
  97. }
  98. try {
  99. $class = $mount->getBackend()->getStorageClass();
  100. /** @var IStorage $storage */
  101. $storage = new $class($mount->getBackendOptions());
  102. if (!$storage->test()) {
  103. throw new \Exception();
  104. }
  105. return [$mount, $storage];
  106. } catch (\Exception $e) {
  107. $output->writeln('<error>Error while trying to create storage</error>');
  108. if ($noAuth) {
  109. $output->writeln('<error>Username and/or password required</error>');
  110. }
  111. return [null, null];
  112. }
  113. }
  114. }