diff options
author | Björn Schießle <bjoern@schiessle.org> | 2017-04-12 16:01:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-12 16:01:07 +0200 |
commit | b90e91144bc8d378f6f52025f04383ae2e7c647b (patch) | |
tree | 616619d3778182ac53e77dc605fc9bded595fc63 /apps/federation | |
parent | 3cf2f6e31bca4b704549e428d7fcbf6c4ecd6c37 (diff) | |
parent | 42f40659f664b4cdcdd5f19cf7300ad740aec6a4 (diff) | |
download | nextcloud-server-b90e91144bc8d378f6f52025f04383ae2e7c647b.tar.gz nextcloud-server-b90e91144bc8d378f6f52025f04383ae2e7c647b.zip |
Merge pull request #3614 from nextcloud/discover-federatedsharing-endpoints
Discover federatedsharing endpoints
Diffstat (limited to 'apps/federation')
9 files changed, 125 insertions, 23 deletions
diff --git a/apps/federation/appinfo/routes.php b/apps/federation/appinfo/routes.php index 4c742dd705c..01c37eab584 100644 --- a/apps/federation/appinfo/routes.php +++ b/apps/federation/appinfo/routes.php @@ -45,12 +45,12 @@ $application->registerRoutes( 'ocs' => [ // old endpoints, only used by Nextcloud and ownCloud [ - 'name' => 'OCSAuthAPI#getSharedSecret', + 'name' => 'OCSAuthAPI#getSharedSecretLegacy', 'url' => '/api/v1/shared-secret', 'verb' => 'GET', ], [ - 'name' => 'OCSAuthAPI#requestSharedSecret', + 'name' => 'OCSAuthAPI#requestSharedSecretLegacy', 'url' => '/api/v1/request-shared-secret', 'verb' => 'POST', ], diff --git a/apps/federation/lib/AppInfo/Application.php b/apps/federation/lib/AppInfo/Application.php index e5acab52857..3166316b108 100644 --- a/apps/federation/lib/AppInfo/Application.php +++ b/apps/federation/lib/AppInfo/Application.php @@ -135,7 +135,8 @@ class Application extends \OCP\AppFramework\App { public function getSyncService() { $syncService = \OC::$server->query('CardDAVSyncService'); $dbHandler = $this->getContainer()->query('DbHandler'); - return new SyncFederationAddressBooks($dbHandler, $syncService); + $discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class); + return new SyncFederationAddressBooks($dbHandler, $syncService, $discoveryService); } } diff --git a/apps/federation/lib/BackgroundJob/GetSharedSecret.php b/apps/federation/lib/BackgroundJob/GetSharedSecret.php index c0a4b43db64..4a6e720ae2c 100644 --- a/apps/federation/lib/BackgroundJob/GetSharedSecret.php +++ b/apps/federation/lib/BackgroundJob/GetSharedSecret.php @@ -37,6 +37,7 @@ use OCP\Http\Client\IClient; use OCP\Http\Client\IResponse; use OCP\ILogger; use OCP\IURLGenerator; +use OCP\OCS\IDiscoveryService; /** * Class GetSharedSecret @@ -62,13 +63,18 @@ class GetSharedSecret extends Job{ /** @var DbHandler */ private $dbHandler; + /** @var IDiscoveryService */ + private $ocsDiscoveryService; + /** @var ILogger */ private $logger; /** @var bool */ protected $retainJob = false; - private $endPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret?format=json'; + private $format = '?format=json'; + + private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret'; /** * RequestSharedSecret constructor. @@ -79,6 +85,7 @@ class GetSharedSecret extends Job{ * @param TrustedServers $trustedServers * @param ILogger $logger * @param DbHandler $dbHandler + * @param IDiscoveryService $ocsDiscoveryService */ public function __construct( IClient $httpClient = null, @@ -86,13 +93,15 @@ class GetSharedSecret extends Job{ IJobList $jobList = null, TrustedServers $trustedServers = null, ILogger $logger = null, - DbHandler $dbHandler = null + DbHandler $dbHandler = null, + IDiscoveryService $ocsDiscoveryService = null ) { $this->logger = $logger ? $logger : \OC::$server->getLogger(); $this->httpClient = $httpClient ? $httpClient : \OC::$server->getHTTPClientService()->newClient(); $this->jobList = $jobList ? $jobList : \OC::$server->getJobList(); $this->urlGenerator = $urlGenerator ? $urlGenerator : \OC::$server->getURLGenerator(); $this->dbHandler = $dbHandler ? $dbHandler : new DbHandler(\OC::$server->getDatabaseConnection(), \OC::$server->getL10N('federation')); + $this->ocsDiscoveryService = $ocsDiscoveryService ? $ocsDiscoveryService : \OC::$server->query(\OCP\OCS\IDiscoveryService::class); if ($trustedServers) { $this->trustedServers = $trustedServers; } else { @@ -142,10 +151,16 @@ class GetSharedSecret extends Job{ $source = rtrim($source, '/'); $token = $argument['token']; + $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); + $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; + + // make sure that we have a well formated url + $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; + $result = null; try { $result = $this->httpClient->get( - $target . $this->endPoint, + $url, [ 'query' => [ diff --git a/apps/federation/lib/BackgroundJob/RequestSharedSecret.php b/apps/federation/lib/BackgroundJob/RequestSharedSecret.php index 352995572c9..60b22cd6283 100644 --- a/apps/federation/lib/BackgroundJob/RequestSharedSecret.php +++ b/apps/federation/lib/BackgroundJob/RequestSharedSecret.php @@ -37,6 +37,7 @@ use OCP\BackgroundJob\IJobList; use OCP\Http\Client\IClient; use OCP\ILogger; use OCP\IURLGenerator; +use OCP\OCS\IDiscoveryService; /** * Class RequestSharedSecret @@ -62,7 +63,8 @@ class RequestSharedSecret extends Job { /** @var TrustedServers */ private $trustedServers; - private $endPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret?format=json'; + /** @var IDiscoveryService */ + private $ocsDiscoveryService; /** @var ILogger */ private $logger; @@ -70,6 +72,10 @@ class RequestSharedSecret extends Job { /** @var bool */ protected $retainJob = false; + private $format = '?format=json'; + + private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret'; + /** * RequestSharedSecret constructor. * @@ -78,19 +84,22 @@ class RequestSharedSecret extends Job { * @param IJobList $jobList * @param TrustedServers $trustedServers * @param DbHandler $dbHandler + * @param IDiscoveryService $ocsDiscoveryService */ public function __construct( IClient $httpClient = null, IURLGenerator $urlGenerator = null, IJobList $jobList = null, TrustedServers $trustedServers = null, - DbHandler $dbHandler = null + DbHandler $dbHandler = null, + IDiscoveryService $ocsDiscoveryService = null ) { $this->httpClient = $httpClient ? $httpClient : \OC::$server->getHTTPClientService()->newClient(); $this->jobList = $jobList ? $jobList : \OC::$server->getJobList(); $this->urlGenerator = $urlGenerator ? $urlGenerator : \OC::$server->getURLGenerator(); $this->dbHandler = $dbHandler ? $dbHandler : new DbHandler(\OC::$server->getDatabaseConnection(), \OC::$server->getL10N('federation')); $this->logger = \OC::$server->getLogger(); + $this->ocsDiscoveryService = $ocsDiscoveryService ? $ocsDiscoveryService : \OC::$server->query(\OCP\OCS\IDiscoveryService::class); if ($trustedServers) { $this->trustedServers = $trustedServers; } else { @@ -142,9 +151,15 @@ class RequestSharedSecret extends Job { $source = rtrim($source, '/'); $token = $argument['token']; + $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); + $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; + + // make sure that we have a well formated url + $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; + try { $result = $this->httpClient->post( - $target . $this->endPoint, + $url, [ 'body' => [ 'url' => $source, diff --git a/apps/federation/lib/Controller/OCSAuthAPIController.php b/apps/federation/lib/Controller/OCSAuthAPIController.php index fdca601da63..594299a2d02 100644 --- a/apps/federation/lib/Controller/OCSAuthAPIController.php +++ b/apps/federation/lib/Controller/OCSAuthAPIController.php @@ -94,6 +94,37 @@ class OCSAuthAPIController extends OCSController{ * @NoCSRFRequired * @PublicPage * + * request received to ask remote server for a shared secret, for legacy end-points + * + * @param string $url + * @param string $token + * @return Http\DataResponse + * @throws OCSForbiddenException + */ + public function requestSharedSecretLegacy($url, $token) { + return $this->requestSharedSecret($url, $token); + } + + + /** + * @NoCSRFRequired + * @PublicPage + * + * create shared secret and return it, for legacy end-points + * + * @param string $url + * @param string $token + * @return Http\DataResponse + * @throws OCSForbiddenException + */ + public function getSharedSecretLegacy($url, $token) { + return $this->getSharedSecret($url, $token); + } + + /** + * @NoCSRFRequired + * @PublicPage + * * request received to ask remote server for a shared secret * * @param string $url diff --git a/apps/federation/lib/SyncFederationAddressBooks.php b/apps/federation/lib/SyncFederationAddressBooks.php index 759b59183aa..87419a5ba54 100644 --- a/apps/federation/lib/SyncFederationAddressBooks.php +++ b/apps/federation/lib/SyncFederationAddressBooks.php @@ -23,12 +23,10 @@ */ namespace OCA\Federation; +use OC\OCS\DiscoveryService; use OCA\DAV\CardDAV\SyncService; use OCP\AppFramework\Http; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\ProgressBar; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; +use OCP\OCS\IDiscoveryService; class SyncFederationAddressBooks { @@ -38,13 +36,21 @@ class SyncFederationAddressBooks { /** @var SyncService */ private $syncService; + /** @var DiscoveryService */ + private $ocsDiscoveryService; + /** * @param DbHandler $dbHandler * @param SyncService $syncService + * @param IDiscoveryService $ocsDiscoveryService */ - function __construct(DbHandler $dbHandler, SyncService $syncService) { + public function __construct(DbHandler $dbHandler, + SyncService $syncService, + IDiscoveryService $ocsDiscoveryService + ) { $this->syncService = $syncService; $this->dbHandler = $dbHandler; + $this->ocsDiscoveryService = $ocsDiscoveryService; } /** @@ -59,6 +65,10 @@ class SyncFederationAddressBooks { $sharedSecret = $trustedServer['shared_secret']; $syncToken = $trustedServer['sync_token']; + $endPoints = $this->ocsDiscoveryService->discover($url, 'FEDERATED_SHARING'); + $cardDavUser = isset($endPoints['carddav-user']) ? $endPoints['carddav-user'] : 'system'; + $addressBookUrl = isset($endPoints['system-address-book']) ? trim($endPoints['system-address-book'], '/') : 'remote.php/dav/addressbooks/system/system/system'; + if (is_null($sharedSecret)) { continue; } @@ -68,7 +78,7 @@ class SyncFederationAddressBooks { '{DAV:}displayname' => $url ]; try { - $newToken = $this->syncService->syncRemoteAddressBook($url, 'system', $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties); + $newToken = $this->syncService->syncRemoteAddressBook($url, $cardDavUser, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties); if ($newToken !== $syncToken) { $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken); } diff --git a/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php b/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php index fe7cc5cc337..6364ddaedff 100644 --- a/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php +++ b/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php @@ -36,6 +36,7 @@ use OCP\Http\Client\IClient; use OCP\Http\Client\IResponse; use OCP\ILogger; use OCP\IURLGenerator; +use OCP\OCS\IDiscoveryService; /** * Class GetSharedSecretTest @@ -67,6 +68,9 @@ class GetSharedSecretTest extends TestCase { /** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */ private $response; + /** @var \PHPUnit_Framework_MockObject_MockObject | IDiscoveryService */ + private $discoverService; + /** @var GetSharedSecret */ private $getSharedSecret; @@ -82,6 +86,9 @@ class GetSharedSecretTest extends TestCase { ->disableOriginalConstructor()->getMock(); $this->logger = $this->getMockBuilder(ILogger::class)->getMock(); $this->response = $this->getMockBuilder(IResponse::class)->getMock(); + $this->discoverService = $this->getMockBuilder(IDiscoveryService::class)->getMock(); + + $this->discoverService->expects($this->any())->method('discover')->willReturn([]); $this->getSharedSecret = new GetSharedSecret( $this->httpClient, @@ -89,7 +96,8 @@ class GetSharedSecretTest extends TestCase { $this->jobList, $this->trustedServers, $this->logger, - $this->dbHandler + $this->dbHandler, + $this->discoverService ); } @@ -109,7 +117,8 @@ class GetSharedSecretTest extends TestCase { $this->jobList, $this->trustedServers, $this->logger, - $this->dbHandler + $this->dbHandler, + $this->discoverService ] )->setMethods(['parentExecute'])->getMock(); $this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url']]); diff --git a/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php b/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php index 3fa2ca2973e..06da29d17fc 100644 --- a/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php +++ b/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php @@ -33,6 +33,7 @@ use OCP\BackgroundJob\IJobList; use OCP\Http\Client\IClient; use OCP\Http\Client\IResponse; use OCP\IURLGenerator; +use OCP\OCS\IDiscoveryService; use Test\TestCase; class RequestSharedSecretTest extends TestCase { @@ -55,6 +56,9 @@ class RequestSharedSecretTest extends TestCase { /** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */ private $response; + /** @var \PHPUnit_Framework_MockObject_MockObject | IDiscoveryService */ + private $discoveryService; + /** @var RequestSharedSecret */ private $requestSharedSecret; @@ -69,13 +73,17 @@ class RequestSharedSecretTest extends TestCase { $this->dbHandler = $this->getMockBuilder(DbHandler::class) ->disableOriginalConstructor()->getMock(); $this->response = $this->getMockBuilder(IResponse::class)->getMock(); + $this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock(); + + $this->discoveryService->expects($this->any())->method('discover')->willReturn([]); $this->requestSharedSecret = new RequestSharedSecret( $this->httpClient, $this->urlGenerator, $this->jobList, $this->trustedServers, - $this->dbHandler + $this->dbHandler, + $this->discoveryService ); } @@ -94,7 +102,8 @@ class RequestSharedSecretTest extends TestCase { $this->urlGenerator, $this->jobList, $this->trustedServers, - $this->dbHandler + $this->dbHandler, + $this->discoveryService ] )->setMethods(['parentExecute'])->getMock(); $this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url']]); diff --git a/apps/federation/tests/SyncFederationAddressbooksTest.php b/apps/federation/tests/SyncFederationAddressbooksTest.php index 1a2dbf1bcae..9ce5efeb457 100644 --- a/apps/federation/tests/SyncFederationAddressbooksTest.php +++ b/apps/federation/tests/SyncFederationAddressbooksTest.php @@ -24,6 +24,7 @@ */ namespace OCA\Federation\Tests; +use OC\OCS\DiscoveryService; use OCA\Federation\DbHandler; use OCA\Federation\SyncFederationAddressBooks; @@ -32,7 +33,18 @@ class SyncFederationAddressbooksTest extends \Test\TestCase { /** @var array */ private $callBacks = []; - function testSync() { + /** @var \PHPUnit_Framework_MockObject_MockObject | DiscoveryService */ + private $discoveryService; + + public function setUp() { + parent::setUp(); + + $this->discoveryService = $this->getMockBuilder(DiscoveryService::class) + ->disableOriginalConstructor()->getMock(); + $this->discoveryService->expects($this->any())->method('discover')->willReturn([]); + } + + public function testSync() { /** @var DbHandler | \PHPUnit_Framework_MockObject_MockObject $dbHandler */ $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')-> disableOriginalConstructor()-> @@ -55,14 +67,14 @@ class SyncFederationAddressbooksTest extends \Test\TestCase { ->willReturn(1); /** @var \OCA\DAV\CardDAV\SyncService $syncService */ - $s = new SyncFederationAddressBooks($dbHandler, $syncService); + $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService); $s->syncThemAll(function($url, $ex) { $this->callBacks[] = [$url, $ex]; }); $this->assertEquals(1, count($this->callBacks)); } - function testException() { + public function testException() { /** @var DbHandler | \PHPUnit_Framework_MockObject_MockObject $dbHandler */ $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')-> disableOriginalConstructor()-> @@ -83,7 +95,7 @@ class SyncFederationAddressbooksTest extends \Test\TestCase { ->willThrowException(new \Exception('something did not work out')); /** @var \OCA\DAV\CardDAV\SyncService $syncService */ - $s = new SyncFederationAddressBooks($dbHandler, $syncService); + $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService); $s->syncThemAll(function($url, $ex) { $this->callBacks[] = [$url, $ex]; }); |