globalauth.php 2.6 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\Service\BackendService;
  23. use OCP\IL10N;
  24. use OCP\IUser;
  25. use OCA\Files_External\Lib\Auth\AuthMechanism;
  26. use OCA\Files_External\Lib\StorageConfig;
  27. use OCP\Security\ICredentialsManager;
  28. use OCP\Files\Storage;
  29. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  30. /**
  31. * Global Username and Password
  32. */
  33. class GlobalAuth extends AuthMechanism {
  34. const CREDENTIALS_IDENTIFIER = 'password::global';
  35. /** @var ICredentialsManager */
  36. protected $credentialsManager;
  37. public function __construct(IL10N $l, ICredentialsManager $credentialsManager) {
  38. $this->credentialsManager = $credentialsManager;
  39. $this
  40. ->setIdentifier('password::global')
  41. ->setVisibility(BackendService::VISIBILITY_DEFAULT)
  42. ->setScheme(self::SCHEME_PASSWORD)
  43. ->setText($l->t('Global Credentails'));
  44. }
  45. public function getAuth($uid) {
  46. $auth = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);
  47. if (!is_array($auth)) {
  48. return [
  49. 'user' => '',
  50. 'password' => ''
  51. ];
  52. } else {
  53. return $auth;
  54. }
  55. }
  56. public function saveAuth($uid, $user, $password) {
  57. $this->credentialsManager->store($uid, self::CREDENTIALS_IDENTIFIER, [
  58. 'user' => $user,
  59. 'password' => $password
  60. ]);
  61. }
  62. public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
  63. if ($storage->getType() === StorageConfig::MOUNT_TYPE_ADMIN) {
  64. $uid = '';
  65. } elseif (is_null($user)) {
  66. throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
  67. } else {
  68. $uid = $user->getUID();
  69. }
  70. $credentials = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);
  71. if (is_array($credentials)) {
  72. $storage->setBackendOption('user', $credentials['user']);
  73. $storage->setBackendOption('password', $credentials['password']);
  74. }
  75. }
  76. }