88namespace OCA \CloudFederationAPI \Controller ;
99
1010use OC \AppFramework \Http \Attributes \FederationRateLimit ;
11+ use OC \Federation \SenderDomainAwareCloudFederationNotification ;
1112use OC \OCM \OCMSignatoryManager ;
1213use OCA \CloudFederationAPI \Config ;
1314use OCA \CloudFederationAPI \ResponseDefinitions ;
2526use OCP \Federation \Exceptions \ProviderCouldNotAddShareException ;
2627use OCP \Federation \Exceptions \ProviderDoesNotExistsException ;
2728use OCP \Federation \ICloudFederationFactory ;
29+ use OCP \Federation \ICloudFederationNotification ;
2830use OCP \Federation \ICloudFederationProviderManager ;
2931use OCP \Federation \ICloudIdManager ;
32+ use OCP \Federation \ISenderDomainAwareCloudFederationProvider ;
3033use OCP \Federation \ISignedCloudFederationProvider ;
3134use OCP \Federation \IValidationAwareCloudFederationProvider ;
3235use 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 /**
0 commit comments