|
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\Utility\NotFoundJSONResponse; |
|
15
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
16
|
|
|
use OCP\IRequest; |
|
17
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
18
|
|
|
use OCP\AppFramework\ApiController; |
|
19
|
|
|
use OCA\Passman\Service\VaultService; |
|
20
|
|
|
use OCA\Passman\Service\CredentialService; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
class VaultController extends ApiController { |
|
24
|
|
|
private $userId; |
|
25
|
|
|
private $vaultService; |
|
26
|
|
|
private $credentialService; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct($AppName, |
|
29
|
|
|
IRequest $request, |
|
30
|
|
|
$UserId, |
|
31
|
|
|
VaultService $vaultService, |
|
32
|
|
|
CredentialService $credentialService) { |
|
33
|
|
|
parent::__construct( |
|
34
|
|
|
$AppName, |
|
35
|
|
|
$request, |
|
36
|
|
|
'GET, POST, DELETE, PUT, PATCH', |
|
37
|
|
|
'Authorization, Content-Type, Accept', |
|
38
|
|
|
86400); |
|
39
|
|
|
$this->userId = $UserId; |
|
40
|
|
|
$this->vaultService = $vaultService; |
|
41
|
|
|
$this->credentialService = $credentialService; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @NoAdminRequired |
|
46
|
|
|
* @NoCSRFRequired |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function listVaults() { |
|
49
|
1 |
|
$result = array(); |
|
50
|
1 |
|
$vaults = $this->vaultService->getByUser($this->userId); |
|
51
|
|
|
|
|
52
|
1 |
|
$protected_credential_fields = array('getDescription', 'getEmail', 'getUsername', 'getPassword'); |
|
53
|
1 |
|
if ($vaults) { |
|
|
|
|
|
|
54
|
|
|
foreach ($vaults as $vault) { |
|
55
|
|
|
$credential = $this->credentialService->getRandomCredentialByVaultId($vault->getId(), $this->userId); |
|
56
|
|
|
$secret_field = $protected_credential_fields[array_rand($protected_credential_fields)]; |
|
57
|
|
|
array_push($result, array( |
|
58
|
|
|
'vault_id' => $vault->getId(), |
|
59
|
|
|
'guid' => $vault->getGuid(), |
|
60
|
|
|
'name' => $vault->getName(), |
|
61
|
|
|
'created' => $vault->getCreated(), |
|
62
|
|
|
'public_sharing_key' => $vault->getPublicSharingKey(), |
|
63
|
|
|
'last_access' => $vault->getlastAccess(), |
|
64
|
|
|
'challenge_password' => $credential->{$secret_field}() |
|
65
|
|
|
)); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
return new JSONResponse($result); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @NoAdminRequired |
|
74
|
|
|
* @NoCSRFRequired |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function create($vault_name) { |
|
77
|
1 |
|
$vault = $this->vaultService->createVault($vault_name, $this->userId); |
|
78
|
1 |
|
return new JSONResponse($vault); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @NoAdminRequired |
|
83
|
|
|
* @NoCSRFRequired |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function get($vault_guid) { |
|
86
|
|
|
//$vault_guid |
|
87
|
1 |
|
$vault = null; |
|
88
|
|
|
try { |
|
89
|
1 |
|
$vault = $this->vaultService->getByGuid($vault_guid, $this->userId); |
|
90
|
1 |
|
} catch (\Exception $e) { |
|
91
|
|
|
return new NotFoundJSONResponse(); |
|
92
|
|
|
} |
|
93
|
1 |
|
$result = array(); |
|
94
|
1 |
|
if ($vault) { |
|
95
|
|
|
$credentials = $this->credentialService->getCredentialsByVaultId($vault->getId(), $this->userId); |
|
96
|
|
|
|
|
97
|
|
|
$result = array( |
|
98
|
|
|
'vault_id' => $vault->getId(), |
|
99
|
|
|
'guid' => $vault->getGuid(), |
|
100
|
|
|
'name' => $vault->getName(), |
|
101
|
|
|
'created' => $vault->getCreated(), |
|
102
|
|
|
'private_sharing_key' => $vault->getPrivateSharingKey(), |
|
103
|
|
|
'public_sharing_key' => $vault->getPublicSharingKey(), |
|
104
|
|
|
'sharing_keys_generated' => $vault->getSharingKeysGenerated(), |
|
105
|
|
|
'vault_settings' => $vault->getVaultSettings(), |
|
106
|
|
|
'last_access' => $vault->getlastAccess() |
|
107
|
|
|
); |
|
108
|
|
|
$result['credentials'] = $credentials; |
|
109
|
|
|
|
|
110
|
|
|
$this->vaultService->setLastAccess($vault->getId(), $this->userId); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
1 |
|
return new JSONResponse($result); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @NoAdminRequired |
|
119
|
|
|
* @NoCSRFRequired |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function update($vault_guid, $name, $vault_settings) { |
|
122
|
1 |
|
$vault = $this->vaultService->getByGuid($vault_guid, $this->userId); |
|
123
|
1 |
|
if ($name && $vault) { |
|
124
|
|
|
$vault->setName($name); |
|
125
|
|
|
} |
|
126
|
1 |
|
if ($vault_settings && $vault) { |
|
127
|
|
|
$vault->setVaultSettings($vault_settings); |
|
128
|
|
|
} |
|
129
|
1 |
|
$this->vaultService->updateVault($vault); |
|
130
|
1 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @NoAdminRequired |
|
134
|
|
|
* @NoCSRFRequired |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function updateSharingKeys($vault_guid, $private_sharing_key, $public_sharing_key) { |
|
137
|
1 |
|
$vault = null; |
|
138
|
|
|
try { |
|
139
|
1 |
|
$vault = $this->vaultService->getByGuid($vault_guid, $this->userId); |
|
140
|
1 |
|
} catch (\Exception $e) { |
|
141
|
|
|
// No need to catch the execption |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1 |
|
if ($vault) { |
|
145
|
|
|
$this->vaultService->updateSharingKeys($vault->getId(), $private_sharing_key, $public_sharing_key); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @NoAdminRequired |
|
153
|
|
|
* @NoCSRFRequired |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function delete($vault_id) { |
|
156
|
1 |
|
return new JSONResponse($vault_id); |
|
157
|
|
|
} |
|
158
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.