From d5dec527c964fd08b91f9ab6ffeea228ca214ad6 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 24 Feb 2017 16:09:52 +0100 Subject: [PATCH] get addressbook url and carddav user from remote server Signed-off-by: Bjoern Schiessle --- apps/dav/lib/CardDAV/SyncService.php | 31 ++++++++++--------- apps/federation/lib/AppInfo/Application.php | 3 +- .../lib/SyncFederationAddressBooks.php | 22 +++++++++---- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/apps/dav/lib/CardDAV/SyncService.php b/apps/dav/lib/CardDAV/SyncService.php index 477e912a79..b6f5775343 100644 --- a/apps/dav/lib/CardDAV/SyncService.php +++ b/apps/dav/lib/CardDAV/SyncService.php @@ -75,6 +75,7 @@ class SyncService { /** * @param string $url * @param string $userName + * @param string $addressBookUrl * @param string $sharedSecret * @param string $syncToken * @param int $targetBookId @@ -83,14 +84,14 @@ class SyncService { * @return string * @throws \Exception */ - public function syncRemoteAddressBook($url, $userName, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { + public function syncRemoteAddressBook($url, $userName, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { // 1. create addressbook $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties); $addressBookId = $book['id']; // 2. query changes try { - $response = $this->requestSyncReport($url, $userName, $sharedSecret, $syncToken); + $response = $this->requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken); } catch (ClientHttpException $ex) { if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { // remote server revoked access to the address book, remove it @@ -105,7 +106,7 @@ class SyncService { foreach ($response['response'] as $resource => $status) { $cardUri = basename($resource); if (isset($status[200])) { - $vCard = $this->download($url, $sharedSecret, $resource); + $vCard = $this->download($url, $userName, $sharedSecret, $resource); $existingCard = $this->backend->getCard($addressBookId, $cardUri); if ($existingCard === false) { $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); @@ -162,6 +163,7 @@ class SyncService { /** * @param string $url * @param string $userName + * @param string $addressBookUrl * @param string $sharedSecret * @return Client */ @@ -185,31 +187,32 @@ class SyncService { /** * @param string $url * @param string $userName + * @param string $addressBookUrl * @param string $sharedSecret * @param string $syncToken * @return array */ - protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) { - $client = $this->getClient($url, $userName, $sharedSecret); + protected function requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken) { + $client = $this->getClient($url, $userName, $sharedSecret); - $addressBookUrl = "remote.php/dav/addressbooks/system/system/system"; - $body = $this->buildSyncCollectionRequestBody($syncToken); + $body = $this->buildSyncCollectionRequestBody($syncToken); - $response = $client->request('REPORT', $addressBookUrl, $body, [ - 'Content-Type' => 'application/xml' - ]); + $response = $client->request('REPORT', $addressBookUrl, $body, [ + 'Content-Type' => 'application/xml' + ]); - return $this->parseMultiStatus($response['body']); - } + return $this->parseMultiStatus($response['body']); + } /** * @param string $url + * @param string $userName * @param string $sharedSecret * @param string $resourcePath * @return array */ - protected function download($url, $sharedSecret, $resourcePath) { - $client = $this->getClient($url, 'system', $sharedSecret); + protected function download($url, $userName, $sharedSecret, $resourcePath) { + $client = $this->getClient($url, $userName, $sharedSecret); return $client->request('GET', $resourcePath); } diff --git a/apps/federation/lib/AppInfo/Application.php b/apps/federation/lib/AppInfo/Application.php index e5acab5285..e76a8f850c 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->getOCSDiscoveryService(); + return new SyncFederationAddressBooks($dbHandler, $syncService, $discoveryService); } } diff --git a/apps/federation/lib/SyncFederationAddressBooks.php b/apps/federation/lib/SyncFederationAddressBooks.php index 759b59183a..87419a5ba5 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); }