2016-06-21 15:58:35 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
2016-07-21 17:49:16 +03:00
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-07-21 17:49:16 +03:00
|
|
|
*
|
2016-06-21 15:58:35 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-06-21 15:58:35 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files_External\Lib\Auth\Password;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\Files_External\Lib\Auth\AuthMechanism;
|
|
|
|
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
|
|
|
|
use OCA\Files_External\Lib\StorageConfig;
|
2020-07-30 12:53:09 +03:00
|
|
|
use OCA\Files_External\Listener\StorePasswordListener;
|
2021-01-04 21:11:29 +03:00
|
|
|
use OCA\User_LDAP\IUserLDAP;
|
2020-05-08 17:38:13 +03:00
|
|
|
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
|
|
|
|
use OCP\Authentication\LoginCredentials\IStore as CredentialsStore;
|
2020-05-20 16:54:16 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\Security\ICredentialsManager;
|
2020-05-20 16:54:16 +03:00
|
|
|
use OCP\User\Events\PasswordUpdatedEvent;
|
|
|
|
use OCP\User\Events\UserLoggedInEvent;
|
2016-06-21 15:58:35 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Username and password from login credentials, saved in DB
|
|
|
|
*/
|
|
|
|
class LoginCredentials extends AuthMechanism {
|
2020-04-10 17:54:27 +03:00
|
|
|
public const CREDENTIALS_IDENTIFIER = 'password::logincredentials/credentials';
|
2016-06-21 15:58:35 +03:00
|
|
|
|
|
|
|
/** @var ISession */
|
|
|
|
protected $session;
|
|
|
|
|
|
|
|
/** @var ICredentialsManager */
|
|
|
|
protected $credentialsManager;
|
|
|
|
|
2020-05-08 17:38:13 +03:00
|
|
|
/** @var CredentialsStore */
|
|
|
|
private $credentialsStore;
|
|
|
|
|
2020-05-20 16:54:16 +03:00
|
|
|
public function __construct(IL10N $l, ISession $session, ICredentialsManager $credentialsManager, CredentialsStore $credentialsStore, IEventDispatcher $eventDispatcher) {
|
2016-06-21 15:58:35 +03:00
|
|
|
$this->session = $session;
|
|
|
|
$this->credentialsManager = $credentialsManager;
|
2020-05-08 17:38:13 +03:00
|
|
|
$this->credentialsStore = $credentialsStore;
|
2016-06-21 15:58:35 +03:00
|
|
|
|
|
|
|
$this
|
|
|
|
->setIdentifier('password::logincredentials')
|
|
|
|
->setScheme(self::SCHEME_PASSWORD)
|
|
|
|
->setText($l->t('Log-in credentials, save in database'))
|
|
|
|
->addParameters([
|
2020-05-08 17:38:13 +03:00
|
|
|
]);
|
2020-05-20 16:54:16 +03:00
|
|
|
|
2020-07-30 12:53:09 +03:00
|
|
|
$eventDispatcher->addServiceListener(UserLoggedInEvent::class, StorePasswordListener::class);
|
|
|
|
$eventDispatcher->addServiceListener(PasswordUpdatedEvent::class, StorePasswordListener::class);
|
2016-06-21 15:58:35 +03:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:38:13 +03:00
|
|
|
private function getCredentials(IUser $user): array {
|
|
|
|
$credentials = $this->credentialsManager->retrieve($user->getUID(), self::CREDENTIALS_IDENTIFIER);
|
|
|
|
|
|
|
|
if (is_null($credentials)) {
|
|
|
|
// nothing saved in db, try to get it from the session and save it
|
|
|
|
try {
|
|
|
|
$sessionCredentials = $this->credentialsStore->getLoginCredentials();
|
|
|
|
|
|
|
|
$credentials = [
|
|
|
|
'user' => $sessionCredentials->getLoginName(),
|
2021-01-04 21:11:29 +03:00
|
|
|
'password' => $sessionCredentials->getPassword(),
|
2020-05-08 17:38:13 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->credentialsManager->store($user->getUID(), self::CREDENTIALS_IDENTIFIER, $credentials);
|
|
|
|
} catch (CredentialsUnavailableException $e) {
|
|
|
|
throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $credentials;
|
2016-06-21 15:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
|
|
|
|
if (!isset($user)) {
|
|
|
|
throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
|
|
|
|
}
|
2020-05-08 17:38:13 +03:00
|
|
|
$credentials = $this->getCredentials($user);
|
2016-06-21 15:58:35 +03:00
|
|
|
|
2021-01-04 21:11:29 +03:00
|
|
|
$loginKey = $storage->getBackendOption("login_ldap_attr");
|
|
|
|
if ($loginKey) {
|
|
|
|
$backend = $user->getBackend();
|
|
|
|
if ($backend instanceof IUserLDAP) {
|
|
|
|
$value = $this->getLdapPropertyForUser($backend, $user, $loginKey);
|
|
|
|
if ($value === null) {
|
|
|
|
throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute not set for user ' . $user->getUID());
|
|
|
|
}
|
|
|
|
$storage->setBackendOption('user', $value);
|
|
|
|
} else {
|
|
|
|
throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute configured but user ' . $user->getUID() . ' is not an ldap user');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$storage->setBackendOption('user', $credentials['user']);
|
|
|
|
}
|
2016-06-21 15:58:35 +03:00
|
|
|
$storage->setBackendOption('password', $credentials['password']);
|
|
|
|
}
|
2021-01-04 21:11:29 +03:00
|
|
|
|
|
|
|
private function getLdapPropertyForUser(IUserLDAP $ldap, IUser $user, string $property): ?string {
|
|
|
|
$access = $ldap->getLDAPAccess($user->getUID());
|
|
|
|
$connection = $access->getConnection();
|
|
|
|
$key = "external_login::" . $user->getUID() . "::" . $property;
|
|
|
|
$cached = $connection->getFromCache($key);
|
|
|
|
|
|
|
|
if ($cached !== null) {
|
|
|
|
return $cached;
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = $access->readAttribute($access->username2dn($user->getUID()), $property);
|
|
|
|
if (count($value) > 0) {
|
|
|
|
$value = current($value);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$connection->writeToCache($key, $value);
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
2016-06-21 15:58:35 +03:00
|
|
|
}
|