get addressbook url and carddav user from remote server

Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
This commit is contained in:
Bjoern Schiessle 2017-02-24 16:09:52 +01:00 committed by Roeland Jago Douma
parent 930c507d89
commit d5dec527c9
No known key found for this signature in database
GPG Key ID: F941078878347C0C
3 changed files with 35 additions and 21 deletions

View File

@ -75,6 +75,7 @@ class SyncService {
/** /**
* @param string $url * @param string $url
* @param string $userName * @param string $userName
* @param string $addressBookUrl
* @param string $sharedSecret * @param string $sharedSecret
* @param string $syncToken * @param string $syncToken
* @param int $targetBookId * @param int $targetBookId
@ -83,14 +84,14 @@ class SyncService {
* @return string * @return string
* @throws \Exception * @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 // 1. create addressbook
$book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties); $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties);
$addressBookId = $book['id']; $addressBookId = $book['id'];
// 2. query changes // 2. query changes
try { try {
$response = $this->requestSyncReport($url, $userName, $sharedSecret, $syncToken); $response = $this->requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken);
} catch (ClientHttpException $ex) { } catch (ClientHttpException $ex) {
if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
// remote server revoked access to the address book, remove it // remote server revoked access to the address book, remove it
@ -105,7 +106,7 @@ class SyncService {
foreach ($response['response'] as $resource => $status) { foreach ($response['response'] as $resource => $status) {
$cardUri = basename($resource); $cardUri = basename($resource);
if (isset($status[200])) { if (isset($status[200])) {
$vCard = $this->download($url, $sharedSecret, $resource); $vCard = $this->download($url, $userName, $sharedSecret, $resource);
$existingCard = $this->backend->getCard($addressBookId, $cardUri); $existingCard = $this->backend->getCard($addressBookId, $cardUri);
if ($existingCard === false) { if ($existingCard === false) {
$this->backend->createCard($addressBookId, $cardUri, $vCard['body']); $this->backend->createCard($addressBookId, $cardUri, $vCard['body']);
@ -162,6 +163,7 @@ class SyncService {
/** /**
* @param string $url * @param string $url
* @param string $userName * @param string $userName
* @param string $addressBookUrl
* @param string $sharedSecret * @param string $sharedSecret
* @return Client * @return Client
*/ */
@ -185,31 +187,32 @@ class SyncService {
/** /**
* @param string $url * @param string $url
* @param string $userName * @param string $userName
* @param string $addressBookUrl
* @param string $sharedSecret * @param string $sharedSecret
* @param string $syncToken * @param string $syncToken
* @return array * @return array
*/ */
protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) { protected function requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken) {
$client = $this->getClient($url, $userName, $sharedSecret); $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, [ $response = $client->request('REPORT', $addressBookUrl, $body, [
'Content-Type' => 'application/xml' 'Content-Type' => 'application/xml'
]); ]);
return $this->parseMultiStatus($response['body']); return $this->parseMultiStatus($response['body']);
} }
/** /**
* @param string $url * @param string $url
* @param string $userName
* @param string $sharedSecret * @param string $sharedSecret
* @param string $resourcePath * @param string $resourcePath
* @return array * @return array
*/ */
protected function download($url, $sharedSecret, $resourcePath) { protected function download($url, $userName, $sharedSecret, $resourcePath) {
$client = $this->getClient($url, 'system', $sharedSecret); $client = $this->getClient($url, $userName, $sharedSecret);
return $client->request('GET', $resourcePath); return $client->request('GET', $resourcePath);
} }

View File

@ -135,7 +135,8 @@ class Application extends \OCP\AppFramework\App {
public function getSyncService() { public function getSyncService() {
$syncService = \OC::$server->query('CardDAVSyncService'); $syncService = \OC::$server->query('CardDAVSyncService');
$dbHandler = $this->getContainer()->query('DbHandler'); $dbHandler = $this->getContainer()->query('DbHandler');
return new SyncFederationAddressBooks($dbHandler, $syncService); $discoveryService = \OC::$server->getOCSDiscoveryService();
return new SyncFederationAddressBooks($dbHandler, $syncService, $discoveryService);
} }
} }

View File

@ -23,12 +23,10 @@
*/ */
namespace OCA\Federation; namespace OCA\Federation;
use OC\OCS\DiscoveryService;
use OCA\DAV\CardDAV\SyncService; use OCA\DAV\CardDAV\SyncService;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use Symfony\Component\Console\Command\Command; use OCP\OCS\IDiscoveryService;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SyncFederationAddressBooks { class SyncFederationAddressBooks {
@ -38,13 +36,21 @@ class SyncFederationAddressBooks {
/** @var SyncService */ /** @var SyncService */
private $syncService; private $syncService;
/** @var DiscoveryService */
private $ocsDiscoveryService;
/** /**
* @param DbHandler $dbHandler * @param DbHandler $dbHandler
* @param SyncService $syncService * @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->syncService = $syncService;
$this->dbHandler = $dbHandler; $this->dbHandler = $dbHandler;
$this->ocsDiscoveryService = $ocsDiscoveryService;
} }
/** /**
@ -59,6 +65,10 @@ class SyncFederationAddressBooks {
$sharedSecret = $trustedServer['shared_secret']; $sharedSecret = $trustedServer['shared_secret'];
$syncToken = $trustedServer['sync_token']; $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)) { if (is_null($sharedSecret)) {
continue; continue;
} }
@ -68,7 +78,7 @@ class SyncFederationAddressBooks {
'{DAV:}displayname' => $url '{DAV:}displayname' => $url
]; ];
try { 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) { if ($newToken !== $syncToken) {
$this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken); $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken);
} }