UserGlobalAuth.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\Files_External\Lib\Auth\Password;
  28. use OCA\Files_External\Lib\Auth\AuthMechanism;
  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 Global Username and Password
  37. */
  38. class UserGlobalAuth extends AuthMechanism {
  39. private const CREDENTIALS_IDENTIFIER = 'password::global';
  40. /** @var ICredentialsManager */
  41. protected $credentialsManager;
  42. public function __construct(IL10N $l, ICredentialsManager $credentialsManager) {
  43. $this->credentialsManager = $credentialsManager;
  44. $this
  45. ->setIdentifier('password::global::user')
  46. ->setVisibility(BackendService::VISIBILITY_DEFAULT)
  47. ->setScheme(self::SCHEME_PASSWORD)
  48. ->setText($l->t('Global credentials, user entered'));
  49. }
  50. public function saveBackendOptions(IUser $user, $id, $backendOptions) {
  51. // backendOptions are set when invoked via Files app
  52. // but they are not set when invoked via ext storage settings
  53. if (!isset($backendOptions['user']) && !isset($backendOptions['password'])) {
  54. return;
  55. }
  56. // make sure we're not setting any unexpected keys
  57. $credentials = [
  58. 'user' => $backendOptions['user'],
  59. 'password' => $backendOptions['password'],
  60. ];
  61. $this->credentialsManager->store($user->getUID(), self::CREDENTIALS_IDENTIFIER, $credentials);
  62. }
  63. public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
  64. if ($user === null) {
  65. throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
  66. }
  67. $uid = $user->getUID();
  68. $credentials = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);
  69. if (is_array($credentials)) {
  70. $storage->setBackendOption('user', $credentials['user']);
  71. $storage->setBackendOption('password', $credentials['password']);
  72. }
  73. }
  74. }