GlobalAuth.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2015, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  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\InsufficientDataForMeaningfulAnswerException;
  28. use OCA\Files_External\Lib\StorageConfig;
  29. use OCA\Files_External\Service\BackendService;
  30. use OCP\IL10N;
  31. use OCP\IUser;
  32. use OCP\Security\ICredentialsManager;
  33. /**
  34. * Global Username and Password
  35. */
  36. class GlobalAuth extends AuthMechanism {
  37. public const CREDENTIALS_IDENTIFIER = 'password::global';
  38. /** @var ICredentialsManager */
  39. protected $credentialsManager;
  40. public function __construct(IL10N $l, ICredentialsManager $credentialsManager) {
  41. $this->credentialsManager = $credentialsManager;
  42. $this
  43. ->setIdentifier('password::global')
  44. ->setVisibility(BackendService::VISIBILITY_DEFAULT)
  45. ->setScheme(self::SCHEME_PASSWORD)
  46. ->setText($l->t('Global credentials'));
  47. }
  48. public function getAuth($uid) {
  49. $auth = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);
  50. if (!is_array($auth)) {
  51. return [
  52. 'user' => '',
  53. 'password' => ''
  54. ];
  55. } else {
  56. return $auth;
  57. }
  58. }
  59. public function saveAuth($uid, $user, $password) {
  60. $this->credentialsManager->store($uid, self::CREDENTIALS_IDENTIFIER, [
  61. 'user' => $user,
  62. 'password' => $password
  63. ]);
  64. }
  65. public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
  66. if ($storage->getType() === StorageConfig::MOUNT_TYPE_ADMIN) {
  67. $uid = '';
  68. } elseif (is_null($user)) {
  69. throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
  70. } else {
  71. $uid = $user->getUID();
  72. }
  73. $credentials = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);
  74. if (is_array($credentials)) {
  75. $storage->setBackendOption('user', $credentials['user']);
  76. $storage->setBackendOption('password', $credentials['password']);
  77. }
  78. }
  79. }