UserProvided.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. public function __construct(
  23. IL10N $l,
  24. protected ICredentialsManager $credentialsManager,
  25. ) {
  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. if ($options['password'] === DefinitionParameter::UNMODIFIED_PLACEHOLDER) {
  44. $oldCredentials = $this->credentialsManager->retrieve($user->getUID(), $this->getCredentialsIdentifier($mountId));
  45. $options['password'] = $oldCredentials['password'];
  46. }
  47. $this->credentialsManager->store($user->getUID(), $this->getCredentialsIdentifier($mountId), [
  48. 'user' => $options['user'], // explicitly copy the fields we want instead of just passing the entire $options array
  49. 'password' => $options['password'] // this way we prevent users from being able to modify any other field
  50. ]);
  51. }
  52. /**
  53. * @return void
  54. */
  55. public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) {
  56. if (!isset($user)) {
  57. throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
  58. }
  59. $uid = $user->getUID();
  60. $credentials = $this->credentialsManager->retrieve($uid, $this->getCredentialsIdentifier($storage->getId()));
  61. if (!isset($credentials)) {
  62. throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
  63. }
  64. $storage->setBackendOption('user', $credentials['user']);
  65. $storage->setBackendOption('password', $credentials['password']);
  66. }
  67. }