Skip to content

Commit 4a7d14f

Browse files
feat(ocm): support new format of notfication payload
Signed-off-by: Cristian Scheid <cristianscheid@gmail.com>
1 parent 1852770 commit 4a7d14f

12 files changed

Lines changed: 334 additions & 29 deletions

‎apps/cloud_federation_api/lib/Controller/RequestHandlerController.php‎

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace OCA\CloudFederationAPI\Controller;
99

1010
use OC\AppFramework\Http\Attributes\FederationRateLimit;
11+
use OC\Federation\SenderDomainAwareCloudFederationNotification;
1112
use OC\OCM\OCMSignatoryManager;
1213
use OCA\CloudFederationAPI\Config;
1314
use OCA\CloudFederationAPI\ResponseDefinitions;
@@ -25,8 +26,10 @@
2526
use OCP\Federation\Exceptions\ProviderCouldNotAddShareException;
2627
use OCP\Federation\Exceptions\ProviderDoesNotExistsException;
2728
use OCP\Federation\ICloudFederationFactory;
29+
use OCP\Federation\ICloudFederationNotification;
2830
use OCP\Federation\ICloudFederationProviderManager;
2931
use OCP\Federation\ICloudIdManager;
32+
use OCP\Federation\ISenderDomainAwareCloudFederationProvider;
3033
use OCP\Federation\ISignedCloudFederationProvider;
3134
use OCP\Federation\IValidationAwareCloudFederationProvider;
3235
use OCP\IAppConfig;
@@ -283,31 +286,49 @@ public function addShare($shareWith, $name, $description, $providerId, $owner, $
283286
}
284287

285288
/**
286-
* Send a notification about an existing share
289+
* Send a notification about a known share or resource
287290
*
288291
* @param string $notificationType Notification type, e.g. SHARE_ACCEPTED
289-
* @param string $resourceType calendar, file, contact,...
292+
* @param string|null $resourceType calendar, file, contact,...
290293
* @param string|null $providerId ID of the share
291294
* @param array<string, mixed>|null $notification The actual payload of the notification
295+
* @param string|null $senderDomain Domain of the sending server
296+
* @param string|null $shareType Type of the recipient the notification is about, e.g. user
292297
*
293298
* @return JSONResponse<Http::STATUS_CREATED, array<string, mixed>, array{}>|JSONResponse<Http::STATUS_BAD_REQUEST, CloudFederationAPIValidationError, array{}>|JSONResponse<Http::STATUS_FORBIDDEN|Http::STATUS_NOT_IMPLEMENTED, CloudFederationAPIError, array{}>
294299
*
295300
* 201: The notification was successfully received
296301
* 400: Bad request due to invalid parameters, e.g. when `type` is invalid or missing
297302
* 403: Getting resource is not allowed
298-
* 501: The resource type is not supported
303+
* 501: The resource type or the notification type is not supported
299304
*/
300305
#[NoCSRFRequired]
301306
#[PublicPage]
302307
#[BruteForceProtection(action: 'receiveFederatedShareNotification')]
303-
public function receiveNotification($notificationType, $resourceType, $providerId, ?array $notification) {
308+
public function receiveNotification($notificationType, $resourceType, $providerId, ?array $notification, $senderDomain, $shareType) {
304309
// check if all required parameters are set
305-
if (
306-
$notificationType === null
307-
|| $resourceType === null
308-
|| $providerId === null
309-
|| !is_array($notification)
310-
) {
310+
$missingArguments = false;
311+
if ($senderDomain !== null) {
312+
// new format notification (OCM 1.5+)
313+
if (
314+
$notificationType === null
315+
|| $senderDomain === ''
316+
|| ($resourceType === null && $shareType === null)
317+
) {
318+
$missingArguments = true;
319+
}
320+
} else {
321+
// old format notification (before OCM 1.5)
322+
if (
323+
$notificationType === null
324+
|| $resourceType === null
325+
|| $providerId === null
326+
|| !is_array($notification)
327+
) {
328+
$missingArguments = true;
329+
}
330+
}
331+
if ($missingArguments) {
311332
return new JSONResponse(
312333
[
313334
'message' => 'Missing arguments',
@@ -316,11 +337,19 @@ public function receiveNotification($notificationType, $resourceType, $providerI
316337
Http::STATUS_BAD_REQUEST
317338
);
318339
}
340+
$notification ??= [];
319341

342+
$signedRequest = null;
320343
if (!$this->appConfig->getValueBool('core', OCMSignatoryManager::APPCONFIG_SIGN_DISABLED, lazy: true)) {
321344
try {
322-
$identity = $this->resolveNotificationIdentity($resourceType, $notification);
323-
$signedRequest = $this->ocmDiscoveryService->getIncomingSignedRequest($identity !== '' ? $identity : null);
345+
if ($senderDomain !== null) {
346+
$signedRequest = $this->ocmDiscoveryService->getIncomingSignedRequestFromDomain($senderDomain);
347+
// sharedSecret is optional: when present, the host of the federation ID it resolves to must match senderDomain
348+
$identity = $resourceType !== null && ($notification['sharedSecret'] ?? '') !== '' ? $this->resolveNotificationIdentity($resourceType, $notification) : '';
349+
} else {
350+
$identity = $this->resolveNotificationIdentity($resourceType, $notification);
351+
$signedRequest = $this->ocmDiscoveryService->getIncomingSignedRequest($identity !== '' ? $identity : null);
352+
}
324353
if ($identity !== '') {
325354
$this->ocmDiscoveryService->confirmRequestOrigin($signedRequest?->getOrigin(), $identity);
326355
}
@@ -330,16 +359,34 @@ public function receiveNotification($notificationType, $resourceType, $providerI
330359
}
331360
}
332361

362+
if ($senderDomain !== null && $signedRequest !== null) {
363+
$notificationObject = new SenderDomainAwareCloudFederationNotification($signedRequest->getOrigin(), $shareType);
364+
} else {
365+
$notificationObject = $this->factory->getCloudFederationNotification();
366+
}
367+
$notificationObject->setMessage($notificationType, $resourceType, $providerId, $notification);
368+
333369
try {
334370
$provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
335-
$result = $provider->notificationReceived($notificationType, $providerId, $notification);
371+
if ($senderDomain !== null) {
372+
if (!$provider instanceof ISenderDomainAwareCloudFederationProvider || $signedRequest === null) {
373+
throw new ActionNotSupportedException($notificationType);
374+
}
375+
$result = $provider->senderDomainAwareNotificationReceived($notificationType, $providerId, $notification, $signedRequest->getOrigin(), $shareType);
376+
} else {
377+
$result = $provider->notificationReceived($notificationType, $providerId, $notification);
378+
}
336379
} catch (ProviderDoesNotExistsException $e) {
380+
// new format notifications are dispatched even without provider, since resourceType is optional
381+
if ($notificationObject instanceof SenderDomainAwareCloudFederationNotification) {
382+
$this->dispatchNotificationReceived($notificationObject);
383+
}
337384
return new JSONResponse(
338385
[
339386
'message' => $e->getMessage(),
340387
'validationErrors' => [],
341388
],
342-
Http::STATUS_BAD_REQUEST
389+
$senderDomain !== null ? Http::STATUS_NOT_IMPLEMENTED : Http::STATUS_BAD_REQUEST
343390
);
344391
} catch (ShareNotFound $e) {
345392
$response = new JSONResponse(
@@ -384,16 +431,17 @@ public function receiveNotification($notificationType, $resourceType, $providerI
384431
'cloud_federation_api.notification.received',
385432
));
386433

434+
$this->dispatchNotificationReceived($notificationObject);
435+
436+
return new JSONResponse($result, Http::STATUS_CREATED);
437+
}
438+
439+
private function dispatchNotificationReceived(ICloudFederationNotification $notification): void {
387440
try {
388-
$notificationObject = $this->factory->getCloudFederationNotification();
389-
$notificationObject->setMessage($notificationType, $resourceType, $providerId, $notification);
390-
$notificationEvent = new OCMNotificationReceivedEvent($notificationObject);
391-
$this->dispatcher->dispatchTyped($notificationEvent);
441+
$this->dispatcher->dispatchTyped(new OCMNotificationReceivedEvent($notification));
392442
} catch (\Exception $e) {
393443
$this->logger->warning('error while dispatching OCM notification received event', ['exception' => $e]);
394444
}
395-
396-
return new JSONResponse($result, Http::STATUS_CREATED);
397445
}
398446

399447
/**

‎apps/cloud_federation_api/openapi.json‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@
224224
"/index.php/ocm/notifications": {
225225
"post": {
226226
"operationId": "request_handler-receive-notification",
227-
"summary": "Send a notification about an existing share",
227+
"summary": "Send a notification about a known share or resource",
228228
"tags": [
229229
"request_handler"
230230
],
@@ -244,8 +244,7 @@
244244
"schema": {
245245
"type": "object",
246246
"required": [
247-
"notificationType",
248-
"resourceType"
247+
"notificationType"
249248
],
250249
"properties": {
251250
"notificationType": {
@@ -254,6 +253,7 @@
254253
},
255254
"resourceType": {
256255
"type": "string",
256+
"nullable": true,
257257
"description": "calendar, file, contact,..."
258258
},
259259
"providerId": {
@@ -268,6 +268,16 @@
268268
"additionalProperties": {
269269
"type": "object"
270270
}
271+
},
272+
"senderDomain": {
273+
"type": "string",
274+
"nullable": true,
275+
"description": "Domain of the sending server"
276+
},
277+
"shareType": {
278+
"type": "string",
279+
"nullable": true,
280+
"description": "Type of the recipient the notification is about, e.g. user"
271281
}
272282
}
273283
}
@@ -309,7 +319,7 @@
309319
}
310320
},
311321
"501": {
312-
"description": "The resource type is not supported",
322+
"description": "The resource type or the notification type is not supported",
313323
"content": {
314324
"application/json": {
315325
"schema": {

‎lib/composer/composer/autoload_classmap.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@
474474
'OCP\\Federation\\ICloudId' => $baseDir . '/lib/public/Federation/ICloudId.php',
475475
'OCP\\Federation\\ICloudIdManager' => $baseDir . '/lib/public/Federation/ICloudIdManager.php',
476476
'OCP\\Federation\\ICloudIdResolver' => $baseDir . '/lib/public/Federation/ICloudIdResolver.php',
477+
'OCP\\Federation\\ISenderDomainAwareCloudFederationNotification' => $baseDir . '/lib/public/Federation/ISenderDomainAwareCloudFederationNotification.php',
478+
'OCP\\Federation\\ISenderDomainAwareCloudFederationProvider' => $baseDir . '/lib/public/Federation/ISenderDomainAwareCloudFederationProvider.php',
477479
'OCP\\Federation\\ISignedCloudFederationProvider' => $baseDir . '/lib/public/Federation/ISignedCloudFederationProvider.php',
478480
'OCP\\Federation\\IValidationAwareCloudFederationProvider' => $baseDir . '/lib/public/Federation/IValidationAwareCloudFederationProvider.php',
479481
'OCP\\Files' => $baseDir . '/lib/public/Files.php',
@@ -1906,6 +1908,7 @@
19061908
'OC\\Federation\\CloudFederationShare' => $baseDir . '/lib/private/Federation/CloudFederationShare.php',
19071909
'OC\\Federation\\CloudId' => $baseDir . '/lib/private/Federation/CloudId.php',
19081910
'OC\\Federation\\CloudIdManager' => $baseDir . '/lib/private/Federation/CloudIdManager.php',
1911+
'OC\\Federation\\SenderDomainAwareCloudFederationNotification' => $baseDir . '/lib/private/Federation/SenderDomainAwareCloudFederationNotification.php',
19091912
'OC\\FilesMetadata\\FilesMetadataManager' => $baseDir . '/lib/private/FilesMetadata/FilesMetadataManager.php',
19101913
'OC\\FilesMetadata\\Job\\UpdateSingleMetadata' => $baseDir . '/lib/private/FilesMetadata/Job/UpdateSingleMetadata.php',
19111914
'OC\\FilesMetadata\\Listener\\MetadataDelete' => $baseDir . '/lib/private/FilesMetadata/Listener/MetadataDelete.php',

‎lib/composer/composer/autoload_static.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
515515
'OCP\\Federation\\ICloudId' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudId.php',
516516
'OCP\\Federation\\ICloudIdManager' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudIdManager.php',
517517
'OCP\\Federation\\ICloudIdResolver' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudIdResolver.php',
518+
'OCP\\Federation\\ISenderDomainAwareCloudFederationNotification' => __DIR__ . '/../../..' . '/lib/public/Federation/ISenderDomainAwareCloudFederationNotification.php',
519+
'OCP\\Federation\\ISenderDomainAwareCloudFederationProvider' => __DIR__ . '/../../..' . '/lib/public/Federation/ISenderDomainAwareCloudFederationProvider.php',
518520
'OCP\\Federation\\ISignedCloudFederationProvider' => __DIR__ . '/../../..' . '/lib/public/Federation/ISignedCloudFederationProvider.php',
519521
'OCP\\Federation\\IValidationAwareCloudFederationProvider' => __DIR__ . '/../../..' . '/lib/public/Federation/IValidationAwareCloudFederationProvider.php',
520522
'OCP\\Files' => __DIR__ . '/../../..' . '/lib/public/Files.php',
@@ -1947,6 +1949,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
19471949
'OC\\Federation\\CloudFederationShare' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationShare.php',
19481950
'OC\\Federation\\CloudId' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudId.php',
19491951
'OC\\Federation\\CloudIdManager' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudIdManager.php',
1952+
'OC\\Federation\\SenderDomainAwareCloudFederationNotification' => __DIR__ . '/../../..' . '/lib/private/Federation/SenderDomainAwareCloudFederationNotification.php',
19501953
'OC\\FilesMetadata\\FilesMetadataManager' => __DIR__ . '/../../..' . '/lib/private/FilesMetadata/FilesMetadataManager.php',
19511954
'OC\\FilesMetadata\\Job\\UpdateSingleMetadata' => __DIR__ . '/../../..' . '/lib/private/FilesMetadata/Job/UpdateSingleMetadata.php',
19521955
'OC\\FilesMetadata\\Listener\\MetadataDelete' => __DIR__ . '/../../..' . '/lib/private/FilesMetadata/Listener/MetadataDelete.php',
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OC\Federation;
11+
12+
use OCP\Federation\ISenderDomainAwareCloudFederationNotification;
13+
14+
class SenderDomainAwareCloudFederationNotification extends CloudFederationNotification implements ISenderDomainAwareCloudFederationNotification {
15+
public function __construct(
16+
private readonly string $senderDomain,
17+
private readonly ?string $shareType = null,
18+
) {
19+
}
20+
21+
#[\Override]
22+
public function getMessage(): array {
23+
$message = parent::getMessage();
24+
$message['senderDomain'] = $this->senderDomain;
25+
if ($this->shareType !== null) {
26+
$message['shareType'] = $this->shareType;
27+
}
28+
29+
return $message;
30+
}
31+
32+
#[\Override]
33+
public function getNotificationType(): string {
34+
return (string)($this->getMessage()['notificationType'] ?? '');
35+
}
36+
37+
#[\Override]
38+
public function getSenderDomain(): string {
39+
return $this->senderDomain;
40+
}
41+
42+
#[\Override]
43+
public function getResourceType(): ?string {
44+
return $this->getMessage()['resourceType'] ?? null;
45+
}
46+
47+
#[\Override]
48+
public function getShareType(): ?string {
49+
return $this->shareType;
50+
}
51+
52+
#[\Override]
53+
public function getProviderId(): ?string {
54+
$providerId = $this->getMessage()['providerId'] ?? '';
55+
return $providerId === '' ? null : $providerId;
56+
}
57+
58+
#[\Override]
59+
public function getNotification(): array {
60+
return $this->getMessage()['notification'] ?? [];
61+
}
62+
}

‎lib/private/OCM/OCMDiscoveryService.php‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public function getLocalOCMProvider(bool $fullDetails = true): IOCMProvider {
212212
try {
213213
// http-sig advertisement requires a jwksUri
214214
$provider->setJwksUri($this->signatoryManager->getLocalJwksUri());
215-
$provider->setCapabilities(['http-sig']);
215+
$provider->setCapabilities(['http-sig', 'notifications-sender-domain']);
216216
} catch (IdentityNotFoundException $e) {
217217
$this->logger->warning('cannot build local jwksUri, http-sig capability not advertised', ['exception' => $e]);
218218
}
@@ -290,6 +290,37 @@ public function getIncomingSignedRequest(?string $ocmAddress = null): ?IIncoming
290290
return null;
291291
}
292292

293+
/**
294+
* @inheritDoc
295+
*
296+
* @throws IncomingRequestException
297+
* @since 36.0.0
298+
*/
299+
#[\Override]
300+
public function getIncomingSignedRequestFromDomain(string $senderDomain): IIncomingSignedRequest {
301+
try {
302+
$origin = $this->signatureManager->extractIdentityFromUri('https://' . $senderDomain);
303+
$signedRequest = $this->signatureManager->getIncomingSignedRequest($this->signatoryManager, null, $origin);
304+
} catch (IdentityNotFoundException $e) {
305+
throw new IncomingRequestException('invalid sender domain: ' . $senderDomain, previous: $e);
306+
} catch (SignatureNotFoundException|SignatoryNotFoundException $e) {
307+
$this->logger->debug('remote does not support signed request', ['exception' => $e]);
308+
// as per OCM spec, sender should make HTTP request using httpsig
309+
throw new IncomingRequestException('Unsigned request', previous: $e);
310+
} catch (SignatureException $e) {
311+
$this->logger->warning('wrongly signed request', ['exception' => $e]);
312+
throw new IncomingRequestException('Invalid signature');
313+
}
314+
315+
if ($signedRequest->getOrigin() !== $origin) {
316+
throw new IncomingRequestException(
317+
'sender domain ' . $origin . ' does not match signed origin ' . $signedRequest->getOrigin()
318+
);
319+
}
320+
321+
return $signedRequest;
322+
}
323+
293324
/**
294325
* @inheritDoc
295326
*

0 commit comments

Comments
 (0)