UserProvided.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2015 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\Auth\IUserProvided;
  10. use OCA\Files_External\Lib\DefinitionParameter;
  11. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  12. use OCA\Files_External\Lib\StorageConfig;
  13. use OCA\Files_External\Service\BackendService;
  14. use OCP\IL10N;
  15. use OCP\IUser;
  16. use OCP\Security\ICredentialsManager;
  17. /**
  18. * User provided Username and Password
  19. */
  20. class UserProvided extends AuthMechanism implements IUserProvided {
  21. public const CREDENTIALS_IDENTIFIER_PREFIX = 'password::userprovided/';
  22. /** @var ICredentialsManager */
  23. protected $credentialsManager;
  24. public function __construct(IL10N $l, ICredentialsManager $credentialsManager) {
  25. $this->credentialsManager = $credentialsManager;
  26. $this
  27. ->setIdentifier('password::userprovided')
  28. ->setVisibility(BackendService::VISIBILITY_ADMIN)
  29. ->setScheme(self::SCHEME_PASSWORD)
  30. ->setText($l->t('Manually entered, store in database'))
  31. ->addParameters([
  32. (new DefinitionParameter('user', $l->t('Login')))
  33. ->setFlag(DefinitionParameter::FLAG_USER_PROVIDED),
  34. (new DefinitionParameter('password', $l->t('Password')))
  35. ->setType(DefinitionParameter::VALUE_PASSWORD)
  36. ->setFlag(DefinitionParameter::FLAG_USER_PROVIDED),
  37. ]);
  38. }
  39. private function getCredentialsIdentifier($storageId) {
  40. return self::CREDENTIALS_IDENTIFIER_PREFIX . $storageId;
  41. }
  42. public function saveBackendOptions(IUser $user, $mountId, array $options) {
  43. $this->credentialsManager->store($user->getUID(), $this->getCredentialsIdentifier($mountId), [
  44. 'user' => $options['user'], // explicitly copy the fields we want instead of just passing the entire $options array
  45. 'password' => $options['password'] // this way we prevent users from being able to modify any other field
  46. ]);
  47. }
  48. /**
  49. * @return void
  50. */
  51. public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) {
  52. if (!isset($user)) {
  53. throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
  54. }
  55. $uid = $user->getUID();
  56. $credentials = $this->credentialsManager->retrieve($uid, $this->getCredentialsIdentifier($storage->getId()));
  57. if (!isset($credentials)) {
  58. throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
  59. }
  60. $storage->setBackendOption('user', $credentials['user']);
  61. $storage->setBackendOption('password', $credentials['password']);
  62. }
  63. }