1212use OCA \CloudFederationAPI \Db \FederatedInviteMapper ;
1313use OCA \CloudFederationAPI \Events \FederatedInviteAcceptedEvent ;
1414use OCA \CloudFederationAPI \ResponseDefinitions ;
15- use OCA \FederatedFileSharing \AddressHandler ;
1615use OCP \AppFramework \Controller ;
1716use OCP \AppFramework \Db \DoesNotExistException ;
1817use OCP \AppFramework \Http ;
3837use OCP \IURLGenerator ;
3938use OCP \IUserManager ;
4039use OCP \OCM \IOCMDiscoveryService ;
41- use OCP \Security \Signature \Exceptions \IdentityNotFoundException ;
4240use OCP \Security \Signature \Exceptions \IncomingRequestException ;
43- use OCP \Security \Signature \Exceptions \SignatoryNotFoundException ;
4441use OCP \Security \Signature \IIncomingSignedRequest ;
45- use OCP \Security \Signature \ISignatureManager ;
4642use OCP \Share \Exceptions \ShareNotFound ;
4743use OCP \Util ;
4844use Psr \Log \LoggerInterface ;
@@ -69,12 +65,10 @@ public function __construct(
6965 private Config $ config ,
7066 private IEventDispatcher $ dispatcher ,
7167 private FederatedInviteMapper $ federatedInviteMapper ,
72- private readonly AddressHandler $ addressHandler ,
7368 private readonly IAppConfig $ appConfig ,
7469 private ICloudFederationFactory $ factory ,
7570 private ICloudIdManager $ cloudIdManager ,
7671 private readonly IOCMDiscoveryService $ ocmDiscoveryService ,
77- private readonly ISignatureManager $ signatureManager ,
7872 private ITimeFactory $ timeFactory ,
7973 ) {
8074 parent ::__construct ($ appName , $ request );
@@ -440,28 +434,22 @@ private function mapUid($uid) {
440434 * If request is not signed, we still verify that the hostname from the extracted value does,
441435 * actually, not support signed request
442436 *
437+ * Delegates to {@see IOCMDiscoveryService::confirmRequestOrigin()}.
438+ *
443439 * @param IIncomingSignedRequest|null $signedRequest
444440 * @param string $key entry from data available in data
445441 * @param string $value value itself used in case request is not signed
446442 *
447443 * @throws IncomingRequestException
448444 */
449445 private function confirmSignedOrigin (?IIncomingSignedRequest $ signedRequest , string $ key , string $ value ): void {
450- if ($ signedRequest === null ) {
451- $ instance = $ this ->getHostFromFederationId ($ value );
452- try {
453- $ this ->signatureManager ->getSignatory ($ instance );
454- throw new IncomingRequestException ('instance is supposed to sign its request ' );
455- } catch (SignatoryNotFoundException ) {
456- return ;
457- }
458- }
459-
460- $ body = json_decode ($ signedRequest ->getBody (), true ) ?? [];
461- $ entry = trim ($ body [$ key ] ?? '' , '@ ' );
462- if ($ this ->getHostFromFederationId ($ entry ) !== $ signedRequest ->getOrigin ()) {
463- throw new IncomingRequestException ('share initiation ( ' . $ signedRequest ->getOrigin () . ') from different instance ( ' . $ entry . ') [key= ' . $ key . '] ' );
446+ if ($ signedRequest !== null ) {
447+ $ body = json_decode ($ signedRequest ->getBody (), true ) ?? [];
448+ $ entry = trim (($ body [$ key ] ?? '' ), '@ ' );
449+ } else {
450+ $ entry = trim ($ value , '@ ' );
464451 }
452+ $ this ->ocmDiscoveryService ->confirmRequestOrigin ($ signedRequest ?->getOrigin(), $ entry );
465453 }
466454
467455 /**
@@ -498,48 +486,6 @@ private function confirmNotificationIdentity(
498486 throw new IncomingRequestException ($ e ->getMessage (), previous: $ e );
499487 }
500488
501- $ this ->confirmNotificationEntry ($ signedRequest , $ identity );
502- }
503-
504-
505- /**
506- * @param IIncomingSignedRequest|null $signedRequest
507- * @param string $entry
508- *
509- * @return void
510- * @throws IncomingRequestException
511- */
512- private function confirmNotificationEntry (?IIncomingSignedRequest $ signedRequest , string $ entry ): void {
513- $ instance = $ this ->getHostFromFederationId ($ entry );
514- if ($ signedRequest === null ) {
515- try {
516- $ this ->signatureManager ->getSignatory ($ instance );
517- throw new IncomingRequestException ('instance is supposed to sign its request ' );
518- } catch (SignatoryNotFoundException ) {
519- return ;
520- }
521- } elseif ($ instance !== $ signedRequest ->getOrigin ()) {
522- throw new IncomingRequestException ('remote instance ' . $ instance . ' not linked to origin ' . $ signedRequest ->getOrigin ());
523- }
524- }
525-
526- /**
527- * @param string $entry
528- * @return string
529- * @throws IncomingRequestException
530- */
531- private function getHostFromFederationId (string $ entry ): string {
532- if (!str_contains ($ entry , '@ ' )) {
533- throw new IncomingRequestException ('entry ' . $ entry . ' does not contain @ ' );
534- }
535- $ rightPart = substr ($ entry , strrpos ($ entry , '@ ' ) + 1 );
536-
537- // in case the full scheme is sent; getting rid of it
538- $ rightPart = $ this ->addressHandler ->removeProtocolFromUrl ($ rightPart );
539- try {
540- return $ this ->signatureManager ->extractIdentityFromUri ('https:// ' . $ rightPart );
541- } catch (IdentityNotFoundException ) {
542- throw new IncomingRequestException ('invalid host within federation id: ' . $ entry );
543- }
489+ $ this ->ocmDiscoveryService ->confirmRequestOrigin ($ signedRequest ?->getOrigin(), $ identity );
544490 }
545491}
0 commit comments