userprovided.php 3.1 KB

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