Completed
Pull Request — master (#32303)
by Victor
09:58
created

Notifications::sendDeclineShare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Björn Schießle <[email protected]>
4
 * @author Joas Schilling <[email protected]>
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Thomas Müller <[email protected]>
7
 * @author Vincent Petry <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2018, ownCloud GmbH
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OCA\FederatedFileSharing;
27
28
use OCA\FederatedFileSharing\Ocm\NotificationManager;
29
use OCP\AppFramework\Http;
30
use OCP\BackgroundJob\IJobList;
31
use OCP\Http\Client\IClientService;
32
use OCP\IConfig;
33
34
class Notifications {
35
	const RESPONSE_FORMAT = 'json'; // default response format for ocs calls
36
37
	/** @var AddressHandler */
38
	private $addressHandler;
39
40
	/** @var IClientService */
41
	private $httpClientService;
42
43
	/** @var DiscoveryManager */
44
	private $discoveryManager;
45 1
46
	/** @var IJobList  */
47
	private $jobList;
48
49 1
	/** @var IConfig */
50 1
	private $config;
51 1
52
	/**
53
	 * @param AddressHandler $addressHandler
54
	 * @param IClientService $httpClientService
55
	 * @param DiscoveryManager $discoveryManager
56
	 * @param IJobList $jobList
57
	 * @param IConfig $config
58
	 */
59
	public function __construct(
60
		AddressHandler $addressHandler,
61
		IClientService $httpClientService,
62
		DiscoveryManager $discoveryManager,
63
		IJobList $jobList,
64
		IConfig $config
65
	) {
66
		$this->addressHandler = $addressHandler;
67
		$this->httpClientService = $httpClientService;
68
		$this->discoveryManager = $discoveryManager;
69
		$this->jobList = $jobList;
70
		$this->config = $config;
71
	}
72
73
	/**
74
	 * send server-to-server share to remote server
75
	 *
76
	 * @param Address $shareWithAddress
77
	 * @param Address $ownerAddress
78
	 * @param Address $sharedByAddress
79
	 * @param string $token
80
	 * @param string $name
81
	 * @param int $remote_id
82
	 *
83
	 * @return bool
84
	 *
85
	 * @throws \OC\HintException
86
	 * @throws \OC\ServerNotAvailableException
87
	 */
88
	public function sendRemoteShare(Address $shareWithAddress,
89
		Address $ownerAddress,
90
		Address $sharedByAddress,
91
		$token,
92
		$name,
93
		$remote_id
94
	) {
95
		$remoteShareSuccess = false;
96
		if ($shareWithAddress->getUserId() && $shareWithAddress->getCloudId()) {
97
			$remoteShareSuccess = $this->sendOcmRemoteShare(
98
				$shareWithAddress, $ownerAddress, $sharedByAddress, $token, $name, $remote_id
99
			);
100
			if (!$remoteShareSuccess) {
101
				$remoteShareSuccess = $this->sendPreOcmRemoteShare(
102
					$shareWithAddress, $ownerAddress, $sharedByAddress, $token, $name, $remote_id
103
				);
104
			}
105
		}
106
		if ($remoteShareSuccess) {
107
			\OC_Hook::emit(
108
				'OCP\Share',
109
				'federated_share_added',
110
				['server' => $shareWithAddress->getHostName()]
111
			);
112
		}
113
		return $remoteShareSuccess;
114
	}
115
116
	/**
117
	 * ask owner to re-share the file with the given user
118
	 *
119
	 * @param string $token
120
	 * @param int $id remote Id
121
	 * @param int $shareId internal share Id
122
	 * @param string $remote remote address of the owner
123
	 * @param string $shareWith
124
	 * @param int $permission
125
	 * @return bool
126
	 * @throws \Exception
127
	 */
128
	public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission) {
129
		$ocmNotificationManager = new NotificationManager();
130
		$data = [
131
			'shareWith' => $shareWith,
132
			'senderId' => $shareId
133
		];
134
		$ocmNotification = $ocmNotificationManager->convertToOcmFileNotification($id, $token, 'reshare', $data);
135
		$ocmFields = $ocmNotification->toArray();
136
137
		$url = \rtrim(
138
			$this->addressHandler->removeProtocolFromUrl($remote),
139
			'/'
140
		);
141
		$result = $this->tryHttpPostToShareEndpoint($url, '/notifications', $ocmFields, true);
142
		if (isset($result['statusCode']) && $result['statusCode'] === Http::STATUS_CREATED) {
143
			$response = \json_decode($result['result'], true);
144
			if (\is_array($response) && isset($response['sharedSecret'], $response['providerId'])) {
145
				return [
146
					$response['sharedSecret'],
147
					(int) $response['providerId']
148
				];
149
			}
150
			return true;
151
		}
152
153
		$fields = [
154
			'shareWith' => $shareWith,
155
			'token' => $token,
156
			'permission' => $permission,
157
			'remoteId' => $shareId
158
		];
159
160
		$url = $this->addressHandler->removeProtocolFromUrl($remote);
161
		$result = $this->tryHttpPostToShareEndpoint(\rtrim($url, '/'), '/' . $id . '/reshare', $fields);
162
		$status = \json_decode($result['result'], true);
163
164
		$httpRequestSuccessful = $result['success'];
165
		$validToken = isset($status['ocs']['data']['token']) && \is_string($status['ocs']['data']['token']);
166
		$validRemoteId = isset($status['ocs']['data']['remoteId']);
167
168
		if ($httpRequestSuccessful && $this->isOcsStatusOk($status) && $validToken && $validRemoteId) {
169
			return [
170
				$status['ocs']['data']['token'],
171
				(int)$status['ocs']['data']['remoteId']
172
			];
173
		}
174
175
		return false;
176
	}
177
178
	/**
179
	 * send server-to-server unshare to remote server
180
	 *
181
	 * @param string $remote url
182
	 * @param int $id share id
183
	 * @param string $token
184
	 * @return bool
185
	 */
186
	public function sendRemoteUnShare($remote, $id, $token) {
187
		return $this->sendUpdateToRemote($remote, $id, $token, 'unshare');
188
	}
189
190
	/**
191
	 * send server-to-server unshare to remote server
192
	 *
193
	 * @param string $remote url
194
	 * @param int $id share id
195
	 * @param string $token
196
	 * @return bool
197
	 */
198
	public function sendRevokeShare($remote, $id, $token) {
199
		return $this->sendUpdateToRemote($remote, $id, $token, 'revoke');
200
	}
201
202
	/**
203
	 * send notification to remote server if the permissions was changed
204
	 *
205
	 * @param string $remote
206
	 * @param int $remoteId
207
	 * @param string $token
208
	 * @param int $permissions
209
	 * @return bool
210
	 */
211
	public function sendPermissionChange($remote, $remoteId, $token, $permissions) {
212
		return $this->sendUpdateToRemote($remote, $remoteId, $token, 'permissions', ['permissions' => $permissions]);
213
	}
214
215
	/**
216
	 * forward accept reShare to remote server
217
	 *
218
	 * @param string $remote
219
	 * @param int $remoteId
220
	 * @param string $token
221
	 */
222
	public function sendAcceptShare($remote, $remoteId, $token) {
223
		$this->sendUpdateToRemote($remote, $remoteId, $token, 'accept');
224
	}
225
226
	/**
227
	 * forward decline reShare to remote server
228
	 *
229
	 * @param string $remote
230
	 * @param int $remoteId
231
	 * @param string $token
232
	 */
233
	public function sendDeclineShare($remote, $remoteId, $token) {
234
		$this->sendUpdateToRemote($remote, $remoteId, $token, 'decline');
235
	}
236
237
	/**
238
	 * inform remote server whether server-to-server share was accepted/declined
239
	 *
240
	 * @param string $remote
241
	 * @param int $remoteId Share id on the remote host
242
	 * @param string $token
243
	 * @param string $action possible actions:
244
	 * 	                     accept, decline, unshare, revoke, permissions
245
	 * @param array $data
246
	 * @param int $try
247
	 *
248
	 * @return boolean
249
	 *
250
	 * @throws \Exception
251
	 */
252
	public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) {
253
		$ocmNotificationManager = new NotificationManager();
254
		$ocmNotification = $ocmNotificationManager->convertToOcmFileNotification($remoteId, $token, $action, $data);
255
		$ocmFields = $ocmNotification->toArray();
256
		$url = \rtrim(
257
			$this->addressHandler->removeProtocolFromUrl($remote),
258
			'/'
259
		);
260
		$result = $this->tryHttpPostToShareEndpoint($url, '/notifications', $ocmFields, true);
261
		if (isset($result['statusCode']) && $result['statusCode'] === Http::STATUS_CREATED) {
262
			return true;
263
		}
264
265
		$fields = ['token' => $token];
266
		foreach ($data as $key => $value) {
267
			$fields[$key] = $value;
268
		}
269
270
		$url = \rtrim(
271
			$this->addressHandler->removeProtocolFromUrl($remote),
272
			'/'
273
		);
274
		$result = $this->tryHttpPostToShareEndpoint($url, '/' . $remoteId . '/' . $action, $fields);
275
		$status = \json_decode($result['result'], true);
276
277
		if ($result['success'] && $this->isOcsStatusOk($status)) {
278
			return true;
279
		} elseif ($try === 0) {
280
			// only add new job on first try
281
			$this->jobList->add('OCA\FederatedFileSharing\BackgroundJob\RetryJob',
282
				[
283
					'remote' => $remote,
284
					'remoteId' => $remoteId,
285
					'token' => $token,
286
					'action' => $action,
287
					'data' => \json_encode($data),
288
					'try' => $try,
289
					'lastRun' => $this->getTimestamp()
290
				]
291
			);
292
		}
293
294
		return false;
295
	}
296
297
	/**
298
	 * return current timestamp
299
	 *
300
	 * @return int
301
	 */
302
	protected function getTimestamp() {
303
		return \time();
304
	}
305
306
	/**
307
	 * try http post first with https and then with http as a fallback
308
	 *
309
	 * @param string $remoteDomain
310
	 * @param string $urlSuffix
311
	 * @param array $fields post parameters
312
	 * @param bool $useOcm send request to OCM endpoint instead of OCS
313
	 *
314
	 * @return array
315
	 *
316
	 * @throws \Exception
317
	 */
318
	protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $useOcm = false) {
319
		$client = $this->httpClientService->newClient();
320
		$protocol = 'https://';
321
		$result = [
322
			'success' => false,
323
			'result' => '',
324
		];
325
		$try = 0;
326
327
		while ($result['success'] === false && $try < 2) {
328
			if ($useOcm) {
329
				$endpoint = $this->discoveryManager->getOcmShareEndpoint($protocol . $remoteDomain);
330
				$endpoint .= $urlSuffix;
331
			} else {
332
				$relativePath = $this->discoveryManager->getShareEndpoint($protocol . $remoteDomain);
333
				$endpoint = $protocol . $remoteDomain . $relativePath . $urlSuffix . '?format=' . self::RESPONSE_FORMAT;
334
			}
335
336
			try {
337
				$response = $client->post($endpoint, [
338
					'body' => $fields,
339
					'timeout' => 10,
340
					'connect_timeout' => 10,
341
				]);
342
				$result['result'] = $response->getBody();
343
				$result['statusCode'] = $response->getStatusCode();
344
				$result['success'] = true;
345
				break;
346
			} catch (\Exception $e) {
347
				// if flat re-sharing is not supported by the remote server
348
				// we re-throw the exception and fall back to the old behaviour.
349
				// (flat re-shares has been introduced in ownCloud 9.1)
350
				if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
351
					throw $e;
352
				}
353
				$allowHttpFallback = $this->config->getSystemValue('sharing.federation.allowHttpFallback', false) === true;
354
				if (!$allowHttpFallback) {
355
					break;
356
				}
357
				$try++;
358
				$protocol = 'http://';
359
			}
360
		}
361
362
		return $result;
363
	}
364
365
	protected function sendOcmRemoteShare(Address $shareWithAddress, Address $ownerAddress, Address $sharedByAddress, $token, $name, $remote_id) {
366
		$fields = [
367
			'shareWith' => $shareWithAddress->getCloudId(),
368
			'name' => $name,
369
			'providerId' => $remote_id,
370
			'owner' => $ownerAddress->getCloudId(),
371
			'ownerDisplayName' => $ownerAddress->getDisplayName(),
372
			'sender' => $sharedByAddress->getCloudId(),
373
			'senderDisplayName' => $sharedByAddress->getDisplayName(),
374
			'shareType' => 'user',
375
			'resourceType' => 'file',
376
			'protocol' => [
377
				'name' => 'webdav',
378
				'options' => [
379
					'sharedSecret' => $token
380
				]
381
			]
382
		];
383
384
		$url = $shareWithAddress->getHostName();
385
		$result = $this->tryHttpPostToShareEndpoint($url, '/shares', $fields, true);
386
387
		if (isset($result['statusCode']) && $result['statusCode'] === Http::STATUS_CREATED) {
388
			return true;
389
		}
390
		return false;
391
	}
392
393
	protected function sendPreOcmRemoteShare(Address $shareWithAddress, Address $ownerAddress, Address $sharedByAddress, $token, $name, $remote_id) {
394
		$fields = [
395
			'shareWith' => $shareWithAddress->getUserId(),
396
			'token' => $token,
397
			'name' => $name,
398
			'remoteId' => $remote_id,
399
			'owner' => $ownerAddress->getUserId(),
400
			'ownerFederatedId' => $ownerAddress->getCloudId(),
401
			'sharedBy' => $sharedByAddress->getUserId(),
402
			'sharedByFederatedId' => $sharedByAddress->getUserId(),
403
			'remote' => $this->addressHandler->generateRemoteURL(),
404
		];
405
		$url = $shareWithAddress->getHostName();
406
		$result = $this->tryHttpPostToShareEndpoint($url, '', $fields);
407
		$status = \json_decode($result['result'], true);
408
409
		if ($result['success'] && $this->isOcsStatusOk($status)) {
410
			return true;
411
		}
412
		return false;
413
	}
414
415
	/**
416
	 * Validate ocs response - 100 or 200 means success
417
	 *
418
	 * @param array $status
419
	 *
420
	 * @return bool
421
	 */
422
	private function isOcsStatusOk($status) {
423
		return \in_array($status['ocs']['meta']['statuscode'], [100, 200]);
424
	}
425
}
426