|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Circles - Bring cloud-users closer together. |
|
8
|
|
|
* |
|
9
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
10
|
|
|
* later. See the COPYING file. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Maxence Lange <[email protected]> |
|
13
|
|
|
* @copyright 2021 |
|
14
|
|
|
* @license GNU AGPL version 3 or any later version |
|
15
|
|
|
* |
|
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
19
|
|
|
* License, or (at your option) any later version. |
|
20
|
|
|
* |
|
21
|
|
|
* This program is distributed in the hope that it will be useful, |
|
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
24
|
|
|
* GNU Affero General Public License for more details. |
|
25
|
|
|
* |
|
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
namespace OCA\Circles\Service; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
use daita\MySmallPhpTools\ActivityPub\Nextcloud\nc21\NC21Signature; |
|
35
|
|
|
use daita\MySmallPhpTools\Exceptions\InvalidOriginException; |
|
36
|
|
|
use daita\MySmallPhpTools\Exceptions\MalformedArrayException; |
|
37
|
|
|
use daita\MySmallPhpTools\Exceptions\RequestNetworkException; |
|
38
|
|
|
use daita\MySmallPhpTools\Exceptions\RowNotFoundException; |
|
39
|
|
|
use daita\MySmallPhpTools\Exceptions\SignatoryException; |
|
40
|
|
|
use daita\MySmallPhpTools\Exceptions\SignatureException; |
|
41
|
|
|
use daita\MySmallPhpTools\Model\Nextcloud\nc21\NC21Request; |
|
42
|
|
|
use daita\MySmallPhpTools\Model\Nextcloud\nc21\NC21SignedRequest; |
|
43
|
|
|
use daita\MySmallPhpTools\Traits\Nextcloud\nc21\TNC21LocalSignatory; |
|
44
|
|
|
use daita\MySmallPhpTools\Traits\TStringTools; |
|
45
|
|
|
use OCA\Circles\Db\RemoteRequest; |
|
46
|
|
|
use OCA\Circles\Exceptions\RemoteNotFoundException; |
|
47
|
|
|
use OCA\Circles\Exceptions\RemoteUidException; |
|
48
|
|
|
use OCA\Circles\Model\AppService; |
|
49
|
|
|
use OCP\IURLGenerator; |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Class RemoteService |
|
54
|
|
|
* |
|
55
|
|
|
* @package OCA\Circles\Service |
|
56
|
|
|
*/ |
|
57
|
|
|
class RemoteService extends NC21Signature { |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
use TNC21LocalSignatory; |
|
61
|
|
|
use TStringTools; |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
const UPDATE_DATA = 'data'; |
|
65
|
|
|
const UPDATE_INSTANCE = 'instance'; |
|
66
|
|
|
const UPDATE_HREF = 'href'; |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** @var IURLGenerator */ |
|
70
|
|
|
private $urlGenerator; |
|
71
|
|
|
|
|
72
|
|
|
/** @var RemoteRequest */ |
|
73
|
|
|
private $remoteRequest; |
|
74
|
|
|
|
|
75
|
|
|
/** @var ConfigService */ |
|
76
|
|
|
private $configService; |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* RemoteService constructor. |
|
81
|
|
|
* |
|
82
|
|
|
* @param IURLGenerator $urlGenerator |
|
83
|
|
|
* @param RemoteRequest $remoteRequest |
|
84
|
|
|
* @param ConfigService $configService |
|
85
|
|
|
*/ |
|
86
|
|
|
public function __construct( |
|
87
|
|
|
IURLGenerator $urlGenerator, RemoteRequest $remoteRequest, ConfigService $configService |
|
88
|
|
|
) { |
|
89
|
|
|
$this->setup('app', 'circles'); |
|
90
|
|
|
|
|
91
|
|
|
$this->urlGenerator = $urlGenerator; |
|
92
|
|
|
$this->remoteRequest = $remoteRequest; |
|
93
|
|
|
$this->configService = $configService; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param bool $generate |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $confirmKey |
|
101
|
|
|
* |
|
102
|
|
|
* @return AppService |
|
103
|
|
|
* @throws SignatoryException |
|
104
|
|
|
* @throws SignatureException |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getAppSignatory(bool $generate = false, string $confirmKey = ''): AppService { |
|
107
|
|
|
$app = new AppService($this->configService->getRemotePath()); |
|
108
|
|
|
$this->fillSimpleSignatory($app, $generate); |
|
109
|
|
|
$app->setUidFromKey(); |
|
110
|
|
|
|
|
111
|
|
|
if ($confirmKey !== '') { |
|
112
|
|
|
$app->setAuthSigned($this->signString($confirmKey, $app)); |
|
113
|
|
|
$this->verifyString($confirmKey, base64_decode($app->getAuthSigned()), $app->getPublicKey()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$app->setTest($this->configService->getRemotePath('circles.Remote.test')); |
|
117
|
|
|
$app->setIncoming($this->configService->getRemotePath('circles.Remote.incoming')); |
|
118
|
|
|
$app->setCircles($this->configService->getRemotePath('circles.Remote.circles')); |
|
119
|
|
|
$app->setMembers($this->configService->getRemotePath('circles.Remote.members')); |
|
120
|
|
|
|
|
121
|
|
|
return $app; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @throws SignatureException |
|
127
|
|
|
*/ |
|
128
|
|
|
public function resetAppSignatory(): void { |
|
129
|
|
|
try { |
|
130
|
|
|
$app = $this->getAppSignatory(); |
|
131
|
|
|
|
|
132
|
|
|
$this->removeSimpleSignatory($app); |
|
133
|
|
|
} catch (SignatoryException $e) { |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param string $keyId |
|
140
|
|
|
* @param bool $refresh |
|
141
|
|
|
* @param bool $auth |
|
142
|
|
|
* |
|
143
|
|
|
* @return AppService |
|
144
|
|
|
* @throws SignatoryException |
|
145
|
|
|
* @throws SignatureException |
|
146
|
|
|
*/ |
|
147
|
|
|
public function retrieveSignatory(string $keyId, bool $refresh = false, bool $auth = false): AppService { |
|
148
|
|
|
if (!$refresh) { |
|
149
|
|
|
throw new SignatoryException(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$appService = new AppService($keyId); |
|
153
|
|
|
$confirm = ''; |
|
154
|
|
|
$params = []; |
|
155
|
|
|
if ($auth) { |
|
156
|
|
|
$confirm = $this->uuid(); |
|
157
|
|
|
$params['auth'] = $confirm; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$this->downloadSignatory($appService, $keyId, $params); |
|
161
|
|
|
$appService->setUidFromKey(); |
|
162
|
|
|
|
|
163
|
|
|
$this->confirmAuth($appService, $confirm); |
|
164
|
|
|
|
|
165
|
|
|
return $appService; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param AppService $remote |
|
171
|
|
|
* @param string $auth |
|
172
|
|
|
* |
|
173
|
|
|
* @throws SignatureException |
|
174
|
|
|
*/ |
|
175
|
|
|
private function confirmAuth(AppService $remote, string $auth): void { |
|
176
|
|
|
if ($auth === '') { |
|
177
|
|
|
return; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
list($algo, $signed) = explode(':', $this->get('auth-signed', $remote->getOrigData())); |
|
181
|
|
|
try { |
|
182
|
|
|
if ($signed === null) { |
|
183
|
|
|
throw new SignatureException('invalid auth-signed'); |
|
184
|
|
|
} |
|
185
|
|
|
$this->verifyString($auth, base64_decode($signed), $remote->getPublicKey(), $algo); |
|
186
|
|
|
$remote->setIdentityAuthed(true); |
|
187
|
|
|
} catch (SignatureException $e) { |
|
188
|
|
|
$this->debug( |
|
189
|
|
|
'Auth cannot be confirmed', |
|
190
|
|
|
['auth' => $auth, 'signed' => $signed, 'signatory' => $remote] |
|
191
|
|
|
); |
|
192
|
|
|
throw $e; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param string $remote |
|
199
|
|
|
* @param array $data |
|
200
|
|
|
* |
|
201
|
|
|
* @return NC21SignedRequest |
|
202
|
|
|
* @throws RequestNetworkException |
|
203
|
|
|
* @throws SignatoryException |
|
204
|
|
|
*/ |
|
205
|
|
|
public function test(string $remote, array $data = ['test' => 42]): NC21SignedRequest { |
|
206
|
|
|
$request = new NC21Request(); |
|
207
|
|
|
$request->basedOnUrl($remote); |
|
208
|
|
|
$request->setFollowLocation(true); |
|
209
|
|
|
$request->setLocalAddressAllowed(true); |
|
210
|
|
|
$request->setTimeout(5); |
|
211
|
|
|
$request->setData($data); |
|
212
|
|
|
|
|
213
|
|
|
$app = $this->getAppSignatory(); |
|
214
|
|
|
// $app->setAlgorithm(NC21Signatory::SHA512); |
|
215
|
|
|
$signedRequest = $this->signRequest($request, $app); |
|
216
|
|
|
$this->doRequest($signedRequest->getOutgoingRequest()); |
|
217
|
|
|
|
|
218
|
|
|
return $signedRequest; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @return NC21SignedRequest |
|
224
|
|
|
* @throws InvalidOriginException |
|
225
|
|
|
* @throws MalformedArrayException |
|
226
|
|
|
* @throws SignatoryException |
|
227
|
|
|
* @throws SignatureException |
|
228
|
|
|
*/ |
|
229
|
|
|
public function incomingTest(): NC21SignedRequest { |
|
230
|
|
|
return $this->incomingSignedRequest($this->configService->getLocalInstance()); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @param AppService $remote |
|
236
|
|
|
* @param AppService|null $stored |
|
237
|
|
|
* |
|
238
|
|
|
* @throws RemoteNotFoundException |
|
239
|
|
|
* @throws RemoteUidException |
|
240
|
|
|
*/ |
|
241
|
|
|
public function confirmValidRemote(AppService $remote, ?AppService &$stored = null): void { |
|
242
|
|
|
try { |
|
243
|
|
|
$stored = $this->remoteRequest->getFromHref($remote->getId()); |
|
244
|
|
|
} catch (RowNotFoundException $e) { |
|
245
|
|
|
if ($remote->getInstance() === '') { |
|
246
|
|
|
throw new RemoteNotFoundException(); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
try { |
|
250
|
|
|
$stored = $this->remoteRequest->getFromInstance($remote->getInstance()); |
|
251
|
|
|
} catch (RowNotFoundException $e) { |
|
252
|
|
|
throw new RemoteNotFoundException(); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
if ($stored->getUid() !== $remote->getUid(true)) { |
|
257
|
|
|
throw new RemoteUidException(); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @param AppService $remote |
|
264
|
|
|
*/ |
|
265
|
|
|
public function save(AppService $remote): void { |
|
266
|
|
|
$this->remoteRequest->save($remote); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @param AppService $remote |
|
271
|
|
|
* @param string $update |
|
272
|
|
|
* |
|
273
|
|
|
* @throws RemoteUidException |
|
274
|
|
|
*/ |
|
275
|
|
|
public function update(AppService $remote, string $update = self::UPDATE_DATA): void { |
|
276
|
|
|
switch ($update) { |
|
277
|
|
|
case self::UPDATE_DATA: |
|
278
|
|
|
$this->remoteRequest->update($remote); |
|
279
|
|
|
break; |
|
280
|
|
|
|
|
281
|
|
|
case self::UPDATE_HREF: |
|
282
|
|
|
$remote->mustBeIdentityAuthed(); |
|
283
|
|
|
$this->remoteRequest->updateHref($remote); |
|
284
|
|
|
break; |
|
285
|
|
|
|
|
286
|
|
|
case self::UPDATE_INSTANCE: |
|
287
|
|
|
$remote->mustBeIdentityAuthed(); |
|
288
|
|
|
$this->remoteRequest->updateInstance($remote); |
|
289
|
|
|
break; |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
|