1 | <?php |
||
66 | class RemoteStreamService extends NC21Signature { |
||
67 | |||
68 | |||
69 | use TNC21Deserialize; |
||
70 | use TNC21LocalSignatory; |
||
71 | use TStringTools; |
||
72 | use TNC21WellKnown; |
||
73 | |||
74 | |||
75 | const UPDATE_DATA = 'data'; |
||
76 | const UPDATE_INSTANCE = 'instance'; |
||
77 | const UPDATE_HREF = 'href'; |
||
78 | |||
79 | |||
80 | /** @var IURLGenerator */ |
||
81 | private $urlGenerator; |
||
82 | |||
83 | /** @var RemoteRequest */ |
||
84 | private $remoteRequest; |
||
85 | |||
86 | /** @var ConfigService */ |
||
87 | private $configService; |
||
88 | |||
89 | |||
90 | /** |
||
91 | * RemoteStreamService constructor. |
||
92 | * |
||
93 | * @param IURLGenerator $urlGenerator |
||
94 | * @param RemoteRequest $remoteRequest |
||
95 | * @param ConfigService $configService |
||
96 | */ |
||
97 | public function __construct( |
||
106 | |||
107 | |||
108 | /** |
||
109 | * Returns the Signatory model for the Circles app. |
||
110 | * Can be signed with a confirmKey. |
||
111 | * |
||
112 | * @param bool $generate |
||
113 | * @param string $confirmKey |
||
114 | * |
||
115 | * @return RemoteInstance |
||
116 | * @throws SignatoryException |
||
117 | */ |
||
118 | public function getAppSignatory(bool $generate = true, string $confirmKey = ''): RemoteInstance { |
||
160 | |||
161 | |||
162 | /** |
||
163 | * Reset the Signatory (and the Identity) for the Circles app. |
||
164 | */ |
||
165 | public function resetAppSignatory(): void { |
||
173 | |||
174 | |||
175 | /** |
||
176 | * shortcut to requestRemoteInstance that return result if available, or exception. |
||
177 | * |
||
178 | * @param string $instance |
||
179 | * @param string $item |
||
180 | * @param int $type |
||
181 | * @param JsonSerializable|null $object |
||
182 | * @param array $params |
||
183 | * |
||
184 | * @return array |
||
185 | * @throws RemoteNotFoundException |
||
186 | * @throws RemoteResourceNotFoundException |
||
187 | * @throws RequestNetworkException |
||
188 | * @throws SignatoryException |
||
189 | * @throws UnknownRemoteException |
||
190 | * @throws RemoteInstanceException |
||
191 | */ |
||
192 | public function resultRequestRemoteInstance( |
||
211 | |||
212 | /** |
||
213 | * Send a request to a remote instance, based on: |
||
214 | * - instance: address as saved in database, |
||
215 | * - item: the item to request (incoming, event, ...) |
||
216 | * - type: GET, POST |
||
217 | * - data: Serializable to be send if needed |
||
218 | * |
||
219 | * @param string $instance |
||
220 | * @param string $item |
||
221 | * @param int $type |
||
222 | * @param JsonSerializable|null $object |
||
223 | * @param array $params |
||
224 | * |
||
225 | * @return NC21SignedRequest |
||
226 | * @throws RemoteNotFoundException |
||
227 | * @throws RemoteResourceNotFoundException |
||
228 | * @throws RequestNetworkException |
||
229 | * @throws SignatoryException |
||
230 | * @throws UnknownRemoteException |
||
231 | */ |
||
232 | public function requestRemoteInstance( |
||
260 | |||
261 | |||
262 | /** |
||
263 | * get the value of an entry from the Signatory of the RemoteInstance. |
||
264 | * |
||
265 | * @param string $instance |
||
266 | * @param string $item |
||
267 | * @param array $params |
||
268 | * |
||
269 | * @return string |
||
270 | * @throws RemoteNotFoundException |
||
271 | * @throws RemoteResourceNotFoundException |
||
272 | * @throws UnknownRemoteException |
||
273 | */ |
||
274 | private function getRemoteInstanceEntry(string $instance, string $item, array $params = []): string { |
||
284 | |||
285 | |||
286 | /** |
||
287 | * get RemoteInstance with confirmed and known identity from database. |
||
288 | * |
||
289 | * @param string $instance |
||
290 | * |
||
291 | * @return RemoteInstance |
||
292 | * @throws RemoteNotFoundException |
||
293 | * @throws UnknownRemoteException |
||
294 | */ |
||
295 | public function getCachedRemoteInstance(string $instance): RemoteInstance { |
||
303 | |||
304 | |||
305 | /** |
||
306 | * Add a remote instance, based on the address |
||
307 | * |
||
308 | * @param string $instance |
||
309 | * |
||
310 | * @return RemoteInstance |
||
311 | * @throws RequestNetworkException |
||
312 | * @throws SignatoryException |
||
313 | * @throws SignatureException |
||
314 | * @throws WellKnownLinkNotFoundException |
||
315 | */ |
||
316 | public function retrieveRemoteInstance(string $instance): RemoteInstance { |
||
324 | |||
325 | |||
326 | /** |
||
327 | * retrieve Signatory. |
||
328 | * |
||
329 | * @param string $keyId |
||
330 | * @param bool $refresh |
||
331 | * |
||
332 | * @return RemoteInstance |
||
333 | * @throws SignatoryException |
||
334 | * @throws SignatureException |
||
335 | */ |
||
336 | public function retrieveSignatory(string $keyId, bool $refresh = true): RemoteInstance { |
||
355 | |||
356 | |||
357 | /** |
||
358 | * Add a remote instance, based on the address |
||
359 | * |
||
360 | * @param string $instance |
||
361 | * @param string $type |
||
362 | * @param bool $overwrite |
||
363 | * |
||
364 | * @throws RequestNetworkException |
||
365 | * @throws SignatoryException |
||
366 | * @throws SignatureException |
||
367 | * @throws WellKnownLinkNotFoundException |
||
368 | * @throws RemoteAlreadyExistsException |
||
369 | * @throws RemoteUidException |
||
370 | */ |
||
371 | public function addRemoteInstance( |
||
393 | |||
394 | |||
395 | /** |
||
396 | * Confirm the Auth of a RemoteInstance, based on the result from a request |
||
397 | * |
||
398 | * @param RemoteInstance $remote |
||
399 | * @param string $auth |
||
400 | * |
||
401 | * @throws SignatureException |
||
402 | */ |
||
403 | private function confirmAuth(RemoteInstance $remote, string $auth): void { |
||
419 | |||
420 | |||
421 | /** |
||
422 | * TODO: confirm if method is really needed |
||
423 | * |
||
424 | * @param RemoteInstance $remote |
||
425 | * @param RemoteInstance|null $stored |
||
426 | * |
||
427 | * @throws RemoteNotFoundException |
||
428 | * @throws RemoteUidException |
||
429 | */ |
||
430 | public function confirmValidRemote(RemoteInstance $remote, ?RemoteInstance &$stored = null): void { |
||
445 | |||
446 | |||
447 | /** |
||
448 | * TODO: check if this method is not useless |
||
449 | * |
||
450 | * @param RemoteInstance $remote |
||
451 | * @param string $update |
||
452 | * |
||
453 | * @throws RemoteUidException |
||
454 | */ |
||
455 | public function update(RemoteInstance $remote, string $update = self::UPDATE_DATA): void { |
||
470 | |||
471 | } |
||
472 | |||
473 |