Welcome to mirror list, hosted at ThFree Co, Russian Federation.

credentialcontroller.php « controller - github.com/nextcloud/passman.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 87c029c6e032660b221d65cf6f82f1fccbfd4186 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
/**
 * Nextcloud - passman
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Sander Brand <brantje@gmail.com>
 * @copyright Sander Brand 2016
 */

namespace OCA\Passman\Controller;

use OCA\Passman\Activity;
use OCA\Passman\Db\Credential;
use OCA\Passman\Db\SharingACL;
use OCA\Passman\Service\ActivityService;
use OCA\Passman\Service\CredentialRevisionService;
use OCA\Passman\Service\CredentialService;
use OCA\Passman\Service\SettingsService;
use OCA\Passman\Service\ShareService;
use OCA\Passman\Utility\NotFoundJSONResponse;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;


class CredentialController extends ApiController {
	private $userId;
	private $credentialService;
	private $activityService;
	private $credentialRevisionService;
	private $sharingService;
	private $settings;

	public function __construct($AppName,
	                            IRequest $request,
								$userId,
		                        CredentialService $credentialService,
		                        ActivityService $activityService,
		                        CredentialRevisionService $credentialRevisionService,
		                        ShareService $sharingService,
		                        SettingsService $settings

	) {
		parent::__construct(
			$AppName,
			$request,
			'GET, POST, DELETE, PUT, PATCH, OPTIONS',
			'Authorization, Content-Type, Accept',
			86400);
		$this->userId = $userId;
		$this->credentialService = $credentialService;
		$this->activityService = $activityService;
		$this->credentialRevisionService = $credentialRevisionService;
		$this->sharingService = $sharingService;
		$this->settings = $settings;
	}


	/**
	 * @NoAdminRequired
	 * @NoCSRFRequired
	 */
	public function createCredential($changed, $created,
	                                 $credential_id, $custom_fields, $delete_time,
	                                 $description, $email, $expire_time, $favicon, $files, $guid,
	                                 $hidden, $icon, $label, $otp, $password, $renew_interval,
	                                 $tags, $url, $username, $vault_id, $compromised) {
		$credential = array(
			'credential_id' => $credential_id,
			'guid' => $guid,
			'user_id' => $this->userId,
			'vault_id' => $vault_id,
			'label' => $label,
			'description' => $description,
			'created' => $created,
			'changed' => $changed,
			'tags' => $tags,
			'email' => $email,
			'username' => $username,
			'password' => $password,
			'url' => $url,
			'icon' => json_encode($icon),
			'favicon' => $favicon,
			'renew_interval' => $renew_interval,
			'expire_time' => $expire_time,
			'delete_time' => $delete_time,
			'files' => $files,
			'custom_fields' => $custom_fields,
			'otp' => $otp,
			'hidden' => $hidden,
			'compromised' => $compromised
		);

		$credential = $this->credentialService->createCredential($credential);
		$link = ''; // @TODO create direct link to credential
		if (!$credential->getHidden()) {
			$this->activityService->add(
				Activity::SUBJECT_ITEM_CREATED_SELF, array($label, $this->userId),
				'', array(),
				$link, $this->userId, Activity::TYPE_ITEM_ACTION);
		}

		return new JSONResponse($this->credentialService->getCredentialByGUID($credential->getGuid()));
	}

	/**
	 * @NoAdminRequired
	 * @NoCSRFRequired
	 */
	public function getCredential($credential_guid) {
		$credential = $this->credentialService->getCredentialByGUID($credential_guid, $this->userId);
		return new JSONResponse($credential);
	}

	/**
	 * @NoAdminRequired
	 * @NoCSRFRequired
	 */
	public function updateCredential($changed, $created,
	                                 $credential_id, $custom_fields, $delete_time, $credential_guid,
	                                 $description, $email, $expire_time, $icon, $files, $guid,
	                                 $hidden, $label, $otp, $password, $renew_interval,
	                                 $tags, $url, $username, $vault_id, $revision_created, $shared_key, $acl, $unshare_action, $set_share_key, $skip_revision, $compromised) {


		$storedCredential = $this->credentialService->getCredentialByGUID($credential_guid);

		$credential = array(
			'credential_id' => $credential_id,
			'guid' => $guid,
			'label' => $label,
			'description' => $description,
			'created' => $created,
			'changed' => $changed,
			'vault_id' => $vault_id,
			'tags' => $tags,
			'email' => $email,
			'username' => $username,
			'password' => $password,
			'url' => $url,
			'icon' => json_encode($icon),
			'renew_interval' => $renew_interval,
			'expire_time' => $expire_time,
			'files' => $files,
			'custom_fields' => $custom_fields,
			'delete_time' => $delete_time,
			'hidden' => $hidden,
			'otp' => $otp,
			'user_id' => $storedCredential->getUserId(),
			'compromised' => $compromised
		);


		if (!hash_equals($storedCredential->getUserId(), $this->userId)) {
			$acl = $this->sharingService->getCredentialAclForUser($this->userId, $storedCredential->getGuid());
			if ($acl->hasPermission(SharingACL::WRITE)) {
				$credential['shared_key'] = $storedCredential->getSharedKey();
			} else {
				return new DataResponse(['msg' => 'Not authorized'], Http::STATUS_UNAUTHORIZED);
			}
			if (!$this->settings->isEnabled('user_sharing_enabled')) {
				return new DataResponse(['msg' => 'Not authorized'], Http::STATUS_UNAUTHORIZED);
			}
		}


		$link = ''; // @TODO create direct link to credential
		if ($revision_created) {
			$activity = 'item_apply_revision';
			$this->activityService->add(
				$activity . '_self', array($label, $this->userId, $revision_created),
				'', array(),
				$link, $this->userId, Activity::TYPE_ITEM_ACTION);
		} else if (($storedCredential->getDeleteTime() === 0) && (int)$delete_time > 0) {
			$activity = 'item_deleted';
			$this->activityService->add(
				$activity . '_self', array($label, $this->userId),
				'', array(),
				$link, $this->userId, Activity::TYPE_ITEM_ACTION);
		} else if (($storedCredential->getDeleteTime() > 0) && (int)$delete_time === 0) {
			$activity = 'item_recovered';
			$this->activityService->add(
				$activity . '_self', array($label, $this->userId),
				'', array(),
				$link, $this->userId, Activity::TYPE_ITEM_ACTION);
		} else if ($label !== $storedCredential->getLabel()) {
			$activity = 'item_renamed';
			$this->activityService->add(
				$activity . '_self', array($storedCredential->getLabel(), $label, $this->userId),
				'', array(),
				$link, $this->userId, Activity::TYPE_ITEM_RENAMED);
		} else {
			$activity = 'item_edited';
			$this->activityService->add(
				$activity . '_self', array($label, $this->userId),
				'', array(),
				$link, $this->userId, Activity::TYPE_ITEM_ACTION);
		}
		$acl_list = null;

		try {
			$acl_list = $this->sharingService->getCredentialAclList($storedCredential->getGuid());
		} catch (\Exception $exception) {
			// Just check if we have an acl list
		}
		if (!empty($acl_list)) {
			$params = array();
			switch ($activity) {
				case 'item_recovered':
				case 'item_deleted':
				case 'item_edited':
					$params = array($credential['label'], $this->userId);
					break;
				case 'item_apply_revision':
					$params = array($credential['label'], $this->userId, $revision_created);
					break;
				case 'item_renamed':
					$params = array($storedCredential->getLabel(), $label, $this->userId);
					break;
			}

			foreach ($acl_list as $sharingACL) {
				$target_user = $sharingACL->getUserId();
				if ($target_user === $this->userId) {
					continue;
				}
				$this->activityService->add(
					$activity, $params,
					'', array(),
					$link, $target_user, Activity::TYPE_ITEM_ACTION);
			}
			if (!hash_equals($this->userId, $storedCredential->getUserId())) {
				$this->activityService->add(
					$activity, $params,
					'', array(),
					$link, $storedCredential->getUserId(), Activity::TYPE_ITEM_ACTION);
			}
		}
		if ($set_share_key === true) {
			$storedCredential->setSharedKey($shared_key);
			$credential['shared_key'] = $shared_key;
		}
		if ($unshare_action === true) {
			$storedCredential->setSharedKey('');
			$credential['shared_key'] = '';
		}

		if (!isset($credential['shared_key'])) {
			$credential['shared_key'] = $storedCredential->getSharedKey();
		}

		if (!$skip_revision) {
			$this->credentialRevisionService->createRevision($storedCredential, $storedCredential->getUserId(), $credential_id, $this->userId);
		}

		$credential = $this->credentialService->updateCredential($credential);

		return new JSONResponse($this->credentialService->getCredentialByGUID($credential->getGuid()));
	}

	/**
	 * @NoAdminRequired
	 * @NoCSRFRequired
	 */
	public function deleteCredential($credential_guid) {
		try {
			$credential = $this->credentialService->getCredentialByGUID($credential_guid, $this->userId);
		} catch (\Exception $e) {
			return new NotFoundJSONResponse();
		}
		if ($credential instanceof Credential) {
			$result = $this->credentialService->deleteCredential($credential);
			//print_r($credential);
			$this->credentialService->deleteCredentialParts($credential, $this->userId);
		} else {
			$result = false;
		}
		return new JSONResponse($result);
	}

	/**
	 * @NoAdminRequired
	 * @NoCSRFRequired
	 * @throws \Exception
	 */
	public function getRevision($credential_guid) {
		try {
			$credential = $this->credentialService->getCredentialByGUID($credential_guid);
		} catch (\Exception $ex) {
			return new NotFoundJSONResponse();
		}
		// If the request was made by the owner of the credential
		if ($this->userId === $credential->getUserId()) {
			$result = $this->credentialRevisionService->getRevisions($credential->getId(), $this->userId);
		} else {
			$acl = $this->sharingService->getACL($this->userId, $credential_guid);
			if ($acl->hasPermission(SharingACL::HISTORY)) {
				$result = $this->credentialRevisionService->getRevisions($credential->getId());
			} else {
				return new NotFoundJSONResponse();
			}
		}

		return new JSONResponse($result);
	}

	/**
	 * @NoAdminRequired
	 * @NoCSRFRequired
	 */
	public function deleteRevision($credential_id, $revision_id) {
		$result = $this->credentialRevisionService->deleteRevision($revision_id, $this->userId);
		return new JSONResponse($result);
	}

	/**
	 * @NoAdminRequired
	 * @NoCSRFRequired
	 */
	public function updateRevision($revision_id, $credential_data) {
		$revision = null;
		try {
			$revision = $this->credentialRevisionService->getRevision($revision_id);
		} catch (\Exception $exception) {
			return new JSONResponse(array());
		}

		$revision->setCredentialData($credential_data);

		$this->credentialRevisionService->updateRevision($revision);
		return new JSONResponse(array());
	}
}