Failed Conditions
Pull Request — master (#682)
by
unknown
06:28
created

lib/Service/CredentialService.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Passman\Service;
25
26
use OCA\Passman\Db\Credential;
27
use OCA\Passman\Db\CredentialMapper;
28
use OCA\Passman\Db\SharingACL;
29
use OCA\Passman\Db\SharingACLMapper;
30
use OCP\AppFramework\Db\DoesNotExistException;
31
use OCP\AppFramework\Db\Entity;
32
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
33
use OCP\IConfig;
34
35
36
class CredentialService {
37
38
	private CredentialMapper $credentialMapper;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
39
	private SharingACLMapper $sharingACL;
40
	private EncryptService $encryptService;
41
	private $server_key;
42
43
	public function __construct(CredentialMapper $credentialMapper, SharingACLMapper $sharingACL, EncryptService $encryptService, IConfig $config) {
44
		$this->credentialMapper = $credentialMapper;
45
		$this->sharingACL = $sharingACL;
46
		$this->encryptService = $encryptService;
47
		$this->server_key = $config->getSystemValue('passwordsalt', '');
48
	}
49
50
	/**
51
	 * Create a new credential
52
	 *
53
	 * @param array $credential
54
	 * @return Credential
55
	 * @throws \Exception
56
	 */
57
	public function createCredential(array $credential) {
58
		$credential = $this->encryptService->encryptCredential($credential);
59
		return $this->credentialMapper->create($credential);
60
	}
61
62
	/**
63
	 * Update credential
64
	 *
65
	 * @param array $credential
66
	 * @param false $useRawUser
67
	 * @return Credential|Entity
68
	 * @throws DoesNotExistException
69
	 * @throws MultipleObjectsReturnedException
70
	 */
71
	public function updateCredential(array $credential, $useRawUser = false) {
72
		$credential = $this->encryptService->encryptCredential($credential);
73
		return $this->credentialMapper->updateCredential($credential, $useRawUser);
74
	}
75
76
	/**
77
	 * Update credential
78
	 *
79
	 * @param Credential $credential
80
	 * @return Credential|Entity
81
	 * @throws DoesNotExistException
82
	 * @throws MultipleObjectsReturnedException
83
	 */
84
	public function upd(Credential $credential) {
85
		$credential = $this->encryptService->encryptCredential($credential);
86
		return $this->credentialMapper->updateCredential($credential->jsonSerialize(), false);
87
	}
88
89
	/**
90
	 * Delete credential
91
	 *
92
	 * @param Credential $credential
93
	 * @return Entity
94
	 */
95
	public function deleteCredential(Credential $credential) {
96
		return $this->credentialMapper->deleteCredential($credential);
97
	}
98
99
	/**
100
	 * Get credentials by vault id
101
	 *
102
	 * @param int $vault_id
103
	 * @param string $user_id
104
	 * @return Credential[]
105
	 */
106
	public function getCredentialsByVaultId(int $vault_id, string $user_id) {
107
		$credentials = $this->credentialMapper->getCredentialsByVaultId($vault_id, $user_id);
108
		foreach ($credentials as $index => $credential) {
109
			$credentials[$index] = $this->encryptService->decryptCredential($credential);
110
		}
111
		return $credentials;
112
	}
113
114
	/**
115
	 * Get a random credential from given vault
116
	 *
117
	 * @param int $vault_id
118
	 * @param string $user_id
119
	 * @return mixed
120
	 */
121
	public function getRandomCredentialByVaultId(int $vault_id, string $user_id) {
122
		$credentials = $this->credentialMapper->getRandomCredentialByVaultId($vault_id, $user_id);
123
		foreach ($credentials as $index => $credential) {
124
			$credentials[$index] = $this->encryptService->decryptCredential($credential);
125
		}
126
		return array_pop($credentials);
127
	}
128
129
	/**
130
	 * Get expired credentials.
131
	 *
132
	 * @param int $timestamp
133
	 * @return Credential[]
134
	 */
135
	public function getExpiredCredentials(int $timestamp) {
136
		$credentials = $this->credentialMapper->getExpiredCredentials($timestamp);
137
		foreach ($credentials as $index => $credential) {
138
			$credentials[$index] = $this->encryptService->decryptCredential($credential);
139
		}
140
		return $credentials;
141
	}
142
143
	/**
144
	 * Get a single credential.
145
	 *
146
	 * @param int $credential_id
147
	 * @param string $user_id
148
	 * @return array|Credential
149
	 * @throws DoesNotExistException
150
	 * @throws MultipleObjectsReturnedException
151
	 */
152
	public function getCredentialById(int $credential_id, string $user_id) {
153
		$credential = $this->credentialMapper->getCredentialById($credential_id);
154
		if ($credential->getUserId() === $user_id) {
155
			return $this->encryptService->decryptCredential($credential);
156
		} else {
157
			$acl = $this->sharingACL->getItemACL($user_id, $credential->getGuid());
158
			if ($acl->hasPermission(SharingACL::READ)) {
159
				return $this->encryptService->decryptCredential($credential);
160
			} else {
161
				throw new DoesNotExistException("Did expect one result but found none when executing");
162
			}
163
		}
164
	}
165
166
	/**
167
	 * Get credential label by credential id.
168
	 *
169
	 * @param int $credential_id
170
	 * @return array|Credential
171
	 * @throws DoesNotExistException
172
	 * @throws MultipleObjectsReturnedException
173
	 */
174
	public function getCredentialLabelById(int $credential_id) {
175
		$credential = $this->credentialMapper->getCredentialLabelById($credential_id);
176
		return $this->encryptService->decryptCredential($credential);
177
	}
178
179
	/**
180
	 * Get credential by guid
181
	 *
182
	 * @param string $credential_guid
183
	 * @param string|null $user_id
184
	 * @return array|Credential
185
	 * @throws DoesNotExistException
186
	 * @throws MultipleObjectsReturnedException
187
	 */
188
	public function getCredentialByGUID(string $credential_guid, string $user_id = null) {
189
		$credential = $this->credentialMapper->getCredentialByGUID($credential_guid, $user_id);
190
		return $this->encryptService->decryptCredential($credential);
191
	}
192
}
193