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>
|
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-05-08 17:38:13 +03:00
|
|
|
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
|
|
|
|
use OCP\Authentication\LoginCredentials\IStore as CredentialsStore;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\Security\ICredentialsManager;
|
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;
|
|
|
|
|
|
|
|
public function __construct(IL10N $l, ISession $session, ICredentialsManager $credentialsManager, CredentialsStore $credentialsStore) {
|
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
|
|
|
]);
|
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(),
|
|
|
|
'password' => $sessionCredentials->getPassword()
|
|
|
|
];
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
$storage->setBackendOption('user', $credentials['user']);
|
|
|
|
$storage->setBackendOption('password', $credentials['password']);
|
|
|
|
}
|
|
|
|
}
|