2016-06-21 14:55:07 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-06-21 14:55:07 +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,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files_External\Lib\Auth\Password;
|
|
|
|
|
|
|
|
use OCA\Files_External\Service\BackendService;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCA\Files_External\Lib\Auth\AuthMechanism;
|
|
|
|
use OCA\Files_External\Lib\StorageConfig;
|
|
|
|
use OCP\Security\ICredentialsManager;
|
|
|
|
use OCP\Files\Storage;
|
|
|
|
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Global Username and Password
|
|
|
|
*/
|
|
|
|
class GlobalAuth extends AuthMechanism {
|
|
|
|
|
|
|
|
const CREDENTIALS_IDENTIFIER = 'password::global';
|
|
|
|
|
|
|
|
/** @var ICredentialsManager */
|
|
|
|
protected $credentialsManager;
|
|
|
|
|
|
|
|
public function __construct(IL10N $l, ICredentialsManager $credentialsManager) {
|
|
|
|
$this->credentialsManager = $credentialsManager;
|
|
|
|
|
|
|
|
$this
|
|
|
|
->setIdentifier('password::global')
|
|
|
|
->setVisibility(BackendService::VISIBILITY_DEFAULT)
|
|
|
|
->setScheme(self::SCHEME_PASSWORD)
|
2016-08-10 02:04:35 +03:00
|
|
|
->setText($l->t('Global credentials'));
|
2016-06-21 14:55:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAuth($uid) {
|
|
|
|
$auth = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);
|
|
|
|
if (!is_array($auth)) {
|
|
|
|
return [
|
|
|
|
'user' => '',
|
|
|
|
'password' => ''
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
return $auth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveAuth($uid, $user, $password) {
|
|
|
|
$this->credentialsManager->store($uid, self::CREDENTIALS_IDENTIFIER, [
|
|
|
|
'user' => $user,
|
|
|
|
'password' => $password
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
|
|
|
|
if ($storage->getType() === StorageConfig::MOUNT_TYPE_ADMIN) {
|
|
|
|
$uid = '';
|
|
|
|
} elseif (is_null($user)) {
|
|
|
|
throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
|
|
|
|
} else {
|
|
|
|
$uid = $user->getUID();
|
|
|
|
}
|
|
|
|
$credentials = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);
|
|
|
|
|
|
|
|
if (is_array($credentials)) {
|
|
|
|
$storage->setBackendOption('user', $credentials['user']);
|
|
|
|
$storage->setBackendOption('password', $credentials['password']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|