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