2016-06-21 08:58:35 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2024-06-06 03:55:47 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2015 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2016-06-21 08:58:35 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Files_External\Lib\Auth\Password;
|
|
|
|
|
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCA\Files_External\Lib\Auth\AuthMechanism;
|
2016-06-21 08:58:35 -04:00
|
|
|
use OCA\Files_External\Lib\Auth\IUserProvided;
|
|
|
|
|
use OCA\Files_External\Lib\DefinitionParameter;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
|
|
|
|
|
use OCA\Files_External\Lib\StorageConfig;
|
2016-06-21 08:58:35 -04:00
|
|
|
use OCA\Files_External\Service\BackendService;
|
|
|
|
|
use OCP\IL10N;
|
|
|
|
|
use OCP\IUser;
|
|
|
|
|
use OCP\Security\ICredentialsManager;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* User provided Username and Password
|
|
|
|
|
*/
|
|
|
|
|
class UserProvided extends AuthMechanism implements IUserProvided {
|
2020-04-10 10:54:27 -04:00
|
|
|
public const CREDENTIALS_IDENTIFIER_PREFIX = 'password::userprovided/';
|
2016-06-21 08:58:35 -04:00
|
|
|
|
|
|
|
|
/** @var ICredentialsManager */
|
|
|
|
|
protected $credentialsManager;
|
|
|
|
|
|
|
|
|
|
public function __construct(IL10N $l, ICredentialsManager $credentialsManager) {
|
|
|
|
|
$this->credentialsManager = $credentialsManager;
|
|
|
|
|
|
|
|
|
|
$this
|
|
|
|
|
->setIdentifier('password::userprovided')
|
|
|
|
|
->setVisibility(BackendService::VISIBILITY_ADMIN)
|
|
|
|
|
->setScheme(self::SCHEME_PASSWORD)
|
2022-09-21 11:44:32 -04:00
|
|
|
->setText($l->t('Manually entered, store in database'))
|
2016-06-21 08:58:35 -04:00
|
|
|
->addParameters([
|
2024-02-13 08:37:09 -05:00
|
|
|
(new DefinitionParameter('user', $l->t('Login')))
|
2016-06-21 08:58:35 -04:00
|
|
|
->setFlag(DefinitionParameter::FLAG_USER_PROVIDED),
|
|
|
|
|
(new DefinitionParameter('password', $l->t('Password')))
|
|
|
|
|
->setType(DefinitionParameter::VALUE_PASSWORD)
|
|
|
|
|
->setFlag(DefinitionParameter::FLAG_USER_PROVIDED),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getCredentialsIdentifier($storageId) {
|
|
|
|
|
return self::CREDENTIALS_IDENTIFIER_PREFIX . $storageId;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-19 11:54:00 -04:00
|
|
|
public function saveBackendOptions(IUser $user, $mountId, array $options) {
|
2024-07-29 21:05:27 -04:00
|
|
|
if ($options['password'] === DefinitionParameter::UNMODIFIED_PLACEHOLDER) {
|
|
|
|
|
$oldCredentials = $this->credentialsManager->retrieve($user->getUID(), $this->getCredentialsIdentifier($mountId));
|
|
|
|
|
$options['password'] = $oldCredentials['password'];
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-19 11:54:00 -04:00
|
|
|
$this->credentialsManager->store($user->getUID(), $this->getCredentialsIdentifier($mountId), [
|
2016-06-21 08:58:35 -04:00
|
|
|
'user' => $options['user'], // explicitly copy the fields we want instead of just passing the entire $options array
|
|
|
|
|
'password' => $options['password'] // this way we prevent users from being able to modify any other field
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 12:15:01 -04:00
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2024-03-28 11:13:19 -04:00
|
|
|
public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) {
|
2016-06-21 08:58:35 -04:00
|
|
|
if (!isset($user)) {
|
|
|
|
|
throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
|
|
|
|
|
}
|
|
|
|
|
$uid = $user->getUID();
|
|
|
|
|
$credentials = $this->credentialsManager->retrieve($uid, $this->getCredentialsIdentifier($storage->getId()));
|
|
|
|
|
|
|
|
|
|
if (!isset($credentials)) {
|
|
|
|
|
throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$storage->setBackendOption('user', $credentials['user']);
|
|
|
|
|
$storage->setBackendOption('password', $credentials['password']);
|
|
|
|
|
}
|
|
|
|
|
}
|