1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nextcloud - passman |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Sander Brand <[email protected]> |
9
|
|
|
* @copyright Sander Brand 2016 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Passman\Controller; |
13
|
|
|
|
14
|
|
|
use OCA\Passman\Service\DeleteVaultRequestService; |
15
|
|
|
use OCA\Passman\Service\EncryptService; |
16
|
|
|
use OCA\Passman\Service\SettingsService; |
17
|
|
|
use OCA\Passman\Utility\NotFoundJSONResponse; |
18
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
19
|
|
|
use OCP\IRequest; |
20
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
21
|
|
|
use OCP\AppFramework\ApiController; |
22
|
|
|
use OCA\Passman\Service\VaultService; |
23
|
|
|
use OCA\Passman\Service\CredentialService; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class VaultController extends ApiController { |
27
|
|
|
private $userId; |
28
|
|
|
private $vaultService; |
29
|
|
|
private $credentialService; |
30
|
|
|
private $settings; |
31
|
|
|
private $deleteVaultRequestService; |
32
|
|
|
|
33
|
|
View Code Duplication |
public function __construct($AppName, |
|
|
|
|
34
|
|
|
IRequest $request, |
35
|
|
|
$UserId, |
36
|
|
|
VaultService $vaultService, |
37
|
|
|
CredentialService $credentialService, |
38
|
|
|
DeleteVaultRequestService $deleteVaultRequestService, |
39
|
|
|
SettingsService $settings) { |
40
|
|
|
parent::__construct( |
41
|
|
|
$AppName, |
42
|
|
|
$request, |
43
|
|
|
'GET, POST, DELETE, PUT, PATCH, OPTIONS', |
44
|
|
|
'Authorization, Content-Type, Accept', |
45
|
|
|
86400); |
46
|
|
|
$this->userId = $UserId; |
47
|
|
|
$this->vaultService = $vaultService; |
48
|
|
|
$this->credentialService = $credentialService; |
49
|
|
|
$this->deleteVaultRequestService = $deleteVaultRequestService; |
50
|
|
|
$this->settings = $settings; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @NoAdminRequired |
55
|
|
|
* @NoCSRFRequired |
56
|
|
|
*/ |
57
|
1 |
|
public function listVaults() { |
58
|
1 |
|
$result = array(); |
59
|
1 |
|
$vaults = $this->vaultService->getByUser($this->userId); |
60
|
|
|
|
61
|
1 |
|
$protected_credential_fields = array('getDescription', 'getEmail', 'getUsername', 'getPassword'); |
62
|
1 |
|
if (isset($vaults)) { |
63
|
|
|
foreach ($vaults as $vault) { |
64
|
|
|
$credential = $this->credentialService->getRandomCredentialByVaultId($vault->getId(), $this->userId); |
65
|
|
|
$secret_field = $protected_credential_fields[array_rand($protected_credential_fields)]; |
66
|
|
|
if(isset($credential)) { |
67
|
|
|
array_push($result, array( |
68
|
|
|
'vault_id' => $vault->getId(), |
69
|
|
|
'guid' => $vault->getGuid(), |
70
|
|
|
'name' => $vault->getName(), |
71
|
|
|
'created' => $vault->getCreated(), |
72
|
|
|
'public_sharing_key' => $vault->getPublicSharingKey(), |
73
|
|
|
'last_access' => $vault->getlastAccess(), |
74
|
|
|
'challenge_password' => $credential->{$secret_field}(), |
75
|
|
|
'delete_request_pending' => ($this->deleteVaultRequestService->getDeleteRequestForVault($vault->getGuid())) ? true : false |
76
|
|
|
)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return new JSONResponse($result); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @NoAdminRequired |
86
|
|
|
* @NoCSRFRequired |
87
|
|
|
*/ |
88
|
1 |
|
public function create($vault_name) { |
89
|
1 |
|
$vault = $this->vaultService->createVault($vault_name, $this->userId); |
90
|
1 |
|
return new JSONResponse($vault); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @NoAdminRequired |
95
|
|
|
* @NoCSRFRequired |
96
|
|
|
*/ |
97
|
1 |
|
public function get($vault_guid) { |
98
|
1 |
|
$vault = null; |
99
|
|
|
try { |
100
|
1 |
|
$vault = $this->vaultService->getByGuid($vault_guid, $this->userId); |
101
|
1 |
|
} catch (\Exception $e) { |
102
|
|
|
return new NotFoundJSONResponse(); |
103
|
|
|
} |
104
|
1 |
|
$result = array(); |
105
|
1 |
|
if (isset($vault)) { |
106
|
|
|
$credentials = $this->credentialService->getCredentialsByVaultId($vault->getId(), $this->userId); |
107
|
|
|
|
108
|
|
|
$result = array( |
109
|
|
|
'vault_id' => $vault->getId(), |
110
|
|
|
'guid' => $vault->getGuid(), |
111
|
|
|
'name' => $vault->getName(), |
112
|
|
|
'created' => $vault->getCreated(), |
113
|
|
|
'private_sharing_key' => $vault->getPrivateSharingKey(), |
114
|
|
|
'public_sharing_key' => $vault->getPublicSharingKey(), |
115
|
|
|
'sharing_keys_generated' => $vault->getSharingKeysGenerated(), |
116
|
|
|
'vault_settings' => $vault->getVaultSettings(), |
117
|
|
|
'last_access' => $vault->getlastAccess(), |
118
|
|
|
'delete_request_pending' => ($this->deleteVaultRequestService->getDeleteRequestForVault($vault->getGuid())) ? true : false |
119
|
|
|
); |
120
|
|
|
$result['credentials'] = $credentials; |
121
|
|
|
|
122
|
|
|
$this->vaultService->setLastAccess($vault->getId(), $this->userId); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
1 |
|
return new JSONResponse($result); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @NoAdminRequired |
131
|
|
|
* @NoCSRFRequired |
132
|
|
|
*/ |
133
|
1 |
|
public function update($vault_guid, $name, $vault_settings) { |
134
|
1 |
|
$vault = $this->vaultService->getByGuid($vault_guid, $this->userId); |
135
|
1 |
|
if ($name && $vault) { |
136
|
|
|
$vault->setName($name); |
137
|
|
|
} |
138
|
1 |
|
if ($vault_settings && $vault) { |
139
|
|
|
$vault->setVaultSettings($vault_settings); |
140
|
|
|
} |
141
|
1 |
|
$this->vaultService->updateVault($vault); |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @NoAdminRequired |
146
|
|
|
* @NoCSRFRequired |
147
|
|
|
*/ |
148
|
1 |
|
public function updateSharingKeys($vault_guid, $private_sharing_key, $public_sharing_key) { |
149
|
1 |
|
$vault = null; |
150
|
|
|
try { |
151
|
1 |
|
$vault = $this->vaultService->getByGuid($vault_guid, $this->userId); |
152
|
1 |
|
} catch (\Exception $e) { |
153
|
|
|
// No need to catch the execption |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
if ($vault) { |
157
|
|
|
$this->vaultService->updateSharingKeys($vault->getId(), $private_sharing_key, $public_sharing_key); |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
return; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @NoAdminRequired |
165
|
|
|
* @NoCSRFRequired |
166
|
|
|
*/ |
167
|
1 |
|
public function delete($vault_guid) { |
168
|
1 |
|
$this->vaultService->deleteVault($vault_guid, $this->userId); |
169
|
1 |
|
return new JSONResponse(array('ok' => true)); |
170
|
|
|
} |
171
|
|
|
} |
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.