1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Robin Appelman <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Files_External\Lib\Auth\Password; |
23
|
|
|
|
24
|
|
|
use OCA\Files_External\Lib\Auth\IUserProvided; |
25
|
|
|
use OCA\Files_External\Lib\DefinitionParameter; |
26
|
|
|
use OCA\Files_External\Service\BackendService; |
27
|
|
|
use OCP\IL10N; |
28
|
|
|
use OCP\IUser; |
29
|
|
|
use OCA\Files_External\Lib\Auth\AuthMechanism; |
30
|
|
|
use OCA\Files_External\Lib\StorageConfig; |
31
|
|
|
use OCP\Security\ICredentialsManager; |
32
|
|
|
use OCP\Files\Storage; |
33
|
|
|
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* User provided Username and Password |
37
|
|
|
*/ |
38
|
|
|
class UserProvided extends AuthMechanism implements IUserProvided { |
39
|
|
|
|
40
|
|
|
const CREDENTIALS_IDENTIFIER_PREFIX = 'password::userprovided/'; |
41
|
|
|
|
42
|
|
|
/** @var ICredentialsManager */ |
43
|
|
|
protected $credentialsManager; |
44
|
|
|
|
45
|
|
|
public function __construct(IL10N $l, ICredentialsManager $credentialsManager) { |
46
|
|
|
$this->credentialsManager = $credentialsManager; |
47
|
|
|
|
48
|
|
|
$this |
49
|
|
|
->setIdentifier('password::userprovided') |
50
|
|
|
->setVisibility(BackendService::VISIBILITY_ADMIN) |
51
|
|
|
->setScheme(self::SCHEME_PASSWORD) |
52
|
|
|
->setText($l->t('User entered, store in database')) |
53
|
|
|
->addParameters([ |
54
|
|
|
(new DefinitionParameter('user', $l->t('Username'))) |
55
|
|
|
->setFlag(DefinitionParameter::FLAG_USER_PROVIDED), |
56
|
|
|
(new DefinitionParameter('password', $l->t('Password'))) |
57
|
|
|
->setType(DefinitionParameter::VALUE_PASSWORD) |
58
|
|
|
->setFlag(DefinitionParameter::FLAG_USER_PROVIDED), |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function getCredentialsIdentifier($storageId) { |
63
|
|
|
return self::CREDENTIALS_IDENTIFIER_PREFIX . $storageId; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function saveBackendOptions(IUser $user, $id, array $options) { |
67
|
|
|
$this->credentialsManager->store($user->getUID(), $this->getCredentialsIdentifier($id), [ |
68
|
|
|
'user' => $options['user'], // explicitly copy the fields we want instead of just passing the entire $options array |
69
|
|
|
'password' => $options['password'] // this way we prevent users from being able to modify any other field |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) { |
|
|
|
|
74
|
|
|
if (!isset($user)) { |
75
|
|
|
throw new InsufficientDataForMeaningfulAnswerException('No credentials saved'); |
76
|
|
|
} |
77
|
|
|
$uid = $user->getUID(); |
78
|
|
|
$credentials = $this->credentialsManager->retrieve($uid, $this->getCredentialsIdentifier($storage->getId())); |
79
|
|
|
|
80
|
|
|
if (!isset($credentials)) { |
81
|
|
|
throw new InsufficientDataForMeaningfulAnswerException('No credentials saved'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$storage->setBackendOption('user', $credentials['user']); |
85
|
|
|
$storage->setBackendOption('password', $credentials['password']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.