You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoginCredentials.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2015, ownCloud, Inc.
  4. *
  5. * @author blizzz <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_External\Lib\Auth\Password;
  29. use OCA\Files_External\Lib\Auth\AuthMechanism;
  30. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  31. use OCA\Files_External\Lib\StorageConfig;
  32. use OCA\Files_External\Listener\StorePasswordListener;
  33. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  34. use OCP\Authentication\LoginCredentials\IStore as CredentialsStore;
  35. use OCP\EventDispatcher\IEventDispatcher;
  36. use OCP\IL10N;
  37. use OCP\ISession;
  38. use OCP\IUser;
  39. use OCP\IUserBackend;
  40. use OCP\LDAP\ILDAPProviderFactory;
  41. use OCP\Security\ICredentialsManager;
  42. use OCP\User\Events\PasswordUpdatedEvent;
  43. use OCP\User\Events\UserLoggedInEvent;
  44. /**
  45. * Username and password from login credentials, saved in DB
  46. */
  47. class LoginCredentials extends AuthMechanism {
  48. public const CREDENTIALS_IDENTIFIER = 'password::logincredentials/credentials';
  49. /** @var ISession */
  50. protected $session;
  51. /** @var ICredentialsManager */
  52. protected $credentialsManager;
  53. /** @var CredentialsStore */
  54. private $credentialsStore;
  55. /** @var ILDAPProviderFactory */
  56. private $ldapFactory;
  57. public function __construct(
  58. IL10N $l,
  59. ISession $session,
  60. ICredentialsManager $credentialsManager,
  61. CredentialsStore $credentialsStore,
  62. IEventDispatcher $eventDispatcher,
  63. ILDAPProviderFactory $ldapFactory
  64. ) {
  65. $this->session = $session;
  66. $this->credentialsManager = $credentialsManager;
  67. $this->credentialsStore = $credentialsStore;
  68. $this->ldapFactory = $ldapFactory;
  69. $this
  70. ->setIdentifier('password::logincredentials')
  71. ->setScheme(self::SCHEME_PASSWORD)
  72. ->setText($l->t('Log-in credentials, save in database'))
  73. ->addParameters([
  74. ]);
  75. $eventDispatcher->addServiceListener(UserLoggedInEvent::class, StorePasswordListener::class);
  76. $eventDispatcher->addServiceListener(PasswordUpdatedEvent::class, StorePasswordListener::class);
  77. }
  78. private function getCredentials(IUser $user): array {
  79. $credentials = $this->credentialsManager->retrieve($user->getUID(), self::CREDENTIALS_IDENTIFIER);
  80. if (is_null($credentials)) {
  81. // nothing saved in db, try to get it from the session and save it
  82. try {
  83. $sessionCredentials = $this->credentialsStore->getLoginCredentials();
  84. if ($sessionCredentials->getUID() !== $user->getUID()) {
  85. // Can't take the credentials from the session as they are not the same user
  86. throw new CredentialsUnavailableException();
  87. }
  88. $credentials = [
  89. 'user' => $sessionCredentials->getLoginName(),
  90. 'password' => $sessionCredentials->getPassword(),
  91. ];
  92. $this->credentialsManager->store($user->getUID(), self::CREDENTIALS_IDENTIFIER, $credentials);
  93. } catch (CredentialsUnavailableException $e) {
  94. throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
  95. }
  96. }
  97. return $credentials;
  98. }
  99. public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) {
  100. if (!isset($user)) {
  101. throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
  102. }
  103. $credentials = $this->getCredentials($user);
  104. $loginKey = $storage->getBackendOption("login_ldap_attr");
  105. if ($loginKey) {
  106. $backend = $user->getBackend();
  107. if ($backend instanceof IUserBackend && $backend->getBackendName() === 'LDAP') {
  108. $value = $this->getLdapPropertyForUser($user, $loginKey);
  109. if ($value === null) {
  110. throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute not set for user ' . $user->getUID());
  111. }
  112. $storage->setBackendOption('user', $value);
  113. } else {
  114. throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute configured but user ' . $user->getUID() . ' is not an ldap user');
  115. }
  116. } else {
  117. $storage->setBackendOption('user', $credentials['user']);
  118. }
  119. $storage->setBackendOption('password', $credentials['password']);
  120. }
  121. private function getLdapPropertyForUser(IUser $user, string $property): ?string {
  122. return $this->ldapFactory->getLDAPProvider()->getUserAttribute($user->getUID(), $property);
  123. }
  124. }