UserProvided.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2015, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_External\Lib\Auth\Password;
  26. use OCA\Files_External\Lib\Auth\AuthMechanism;
  27. use OCA\Files_External\Lib\Auth\IUserProvided;
  28. use OCA\Files_External\Lib\DefinitionParameter;
  29. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  30. use OCA\Files_External\Lib\StorageConfig;
  31. use OCA\Files_External\Service\BackendService;
  32. use OCP\IL10N;
  33. use OCP\IUser;
  34. use OCP\Security\ICredentialsManager;
  35. /**
  36. * User provided Username and Password
  37. */
  38. class UserProvided extends AuthMechanism implements IUserProvided {
  39. public const CREDENTIALS_IDENTIFIER_PREFIX = 'password::userprovided/';
  40. /** @var ICredentialsManager */
  41. protected $credentialsManager;
  42. public function __construct(IL10N $l, ICredentialsManager $credentialsManager) {
  43. $this->credentialsManager = $credentialsManager;
  44. $this
  45. ->setIdentifier('password::userprovided')
  46. ->setVisibility(BackendService::VISIBILITY_ADMIN)
  47. ->setScheme(self::SCHEME_PASSWORD)
  48. ->setText($l->t('User entered, store in database'))
  49. ->addParameters([
  50. (new DefinitionParameter('user', $l->t('Username')))
  51. ->setFlag(DefinitionParameter::FLAG_USER_PROVIDED),
  52. (new DefinitionParameter('password', $l->t('Password')))
  53. ->setType(DefinitionParameter::VALUE_PASSWORD)
  54. ->setFlag(DefinitionParameter::FLAG_USER_PROVIDED),
  55. ]);
  56. }
  57. private function getCredentialsIdentifier($storageId) {
  58. return self::CREDENTIALS_IDENTIFIER_PREFIX . $storageId;
  59. }
  60. public function saveBackendOptions(IUser $user, $mountId, array $options) {
  61. $this->credentialsManager->store($user->getUID(), $this->getCredentialsIdentifier($mountId), [
  62. 'user' => $options['user'], // explicitly copy the fields we want instead of just passing the entire $options array
  63. 'password' => $options['password'] // this way we prevent users from being able to modify any other field
  64. ]);
  65. }
  66. public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
  67. if (!isset($user)) {
  68. throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
  69. }
  70. $uid = $user->getUID();
  71. $credentials = $this->credentialsManager->retrieve($uid, $this->getCredentialsIdentifier($storage->getId()));
  72. if (!isset($credentials)) {
  73. throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
  74. }
  75. $storage->setBackendOption('user', $credentials['user']);
  76. $storage->setBackendOption('password', $credentials['password']);
  77. }
  78. }