SessionCredentials.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Lib\Auth\Password;
  8. use OCA\Files_External\Lib\Auth\AuthMechanism;
  9. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  10. use OCA\Files_External\Lib\SessionStorageWrapper;
  11. use OCA\Files_External\Lib\StorageConfig;
  12. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  13. use OCP\Authentication\LoginCredentials\IStore as CredentialsStore;
  14. use OCP\Files\Storage\IStorage;
  15. use OCP\Files\StorageAuthException;
  16. use OCP\IL10N;
  17. use OCP\IUser;
  18. /**
  19. * Username and password from login credentials, saved in session
  20. */
  21. class SessionCredentials extends AuthMechanism {
  22. public function __construct(
  23. IL10N $l,
  24. private CredentialsStore $credentialsStore,
  25. ) {
  26. $this->setIdentifier('password::sessioncredentials')
  27. ->setScheme(self::SCHEME_PASSWORD)
  28. ->setText($l->t('Log-in credentials, save in session'))
  29. ->addParameters([]);
  30. }
  31. /**
  32. * @return void
  33. */
  34. public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) {
  35. try {
  36. $credentials = $this->credentialsStore->getLoginCredentials();
  37. } catch (CredentialsUnavailableException $e) {
  38. throw new InsufficientDataForMeaningfulAnswerException('No session credentials saved');
  39. }
  40. if ($user === null) {
  41. throw new StorageAuthException('Session unavailable');
  42. }
  43. if ($credentials->getUID() !== $user->getUID()) {
  44. throw new StorageAuthException('Session credentials for storage owner not available');
  45. }
  46. $storage->setBackendOption('user', $credentials->getLoginName());
  47. $storage->setBackendOption('password', $credentials->getPassword());
  48. }
  49. public function wrapStorage(IStorage $storage): IStorage {
  50. return new SessionStorageWrapper(['storage' => $storage]);
  51. }
  52. }