Merge pull request #4029 from nextcloud/federation-fixes-stable11

Federation fixes stable11
This commit is contained in:
Morris Jobke 2017-03-28 18:11:31 -06:00 committed by GitHub
commit ca08ab9bd9
6 changed files with 125 additions and 69 deletions

View File

@ -55,14 +55,21 @@ class Converter {
$image = $this->getAvatarImage($user); $image = $this->getAvatarImage($user);
$vCard = new VCard(); $vCard = new VCard();
$vCard->add(new Text($vCard, 'UID', $uid)); $vCard->VERSION = '3.0';
$vCard->UID = $uid;
$publish = false; $publish = false;
foreach ($userData as $property => $value) { foreach ($userData as $property => $value) {
if ($value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY ||
$value['scope'] === AccountManager::VISIBILITY_PUBLIC $shareWithTrustedServers =
) { $value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY ||
$value['scope'] === AccountManager::VISIBILITY_PUBLIC;
$emptyValue = !isset($value['value']) || $value['value'] === '';
$noImage = $image === null;
if ($shareWithTrustedServers && (!$emptyValue || !$noImage)) {
$publish = true; $publish = true;
switch ($property) { switch ($property) {
case AccountManager::PROPERTY_DISPLAYNAME: case AccountManager::PROPERTY_DISPLAYNAME:
@ -71,7 +78,7 @@ class Converter {
break; break;
case AccountManager::PROPERTY_AVATAR: case AccountManager::PROPERTY_AVATAR:
if ($image !== null) { if ($image !== null) {
$vCard->add('PHOTO', 'data:'.$image->mimeType().';base64,' . base64_encode($image->data())); $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]);
} }
break; break;
case AccountManager::PROPERTY_EMAIL: case AccountManager::PROPERTY_EMAIL:

View File

@ -26,6 +26,7 @@ namespace OCA\DAV\CardDAV;
use OC\Accounts\AccountManager; use OC\Accounts\AccountManager;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\ICertificateManager;
use OCP\ILogger; use OCP\ILogger;
use OCP\IUser; use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
@ -52,6 +53,9 @@ class SyncService {
/** @var AccountManager */ /** @var AccountManager */
private $accountManager; private $accountManager;
/** @var string */
protected $certPath;
/** /**
* SyncService constructor. * SyncService constructor.
* *
@ -65,6 +69,7 @@ class SyncService {
$this->userManager = $userManager; $this->userManager = $userManager;
$this->logger = $logger; $this->logger = $logger;
$this->accountManager = $accountManager; $this->accountManager = $accountManager;
$this->certPath = '';
} }
/** /**
@ -132,6 +137,51 @@ class SyncService {
return $this->backend->getAddressBooksByUri($principal, $id); return $this->backend->getAddressBooksByUri($principal, $id);
} }
/**
* Check if there is a valid certPath we should use
*
* @return string
*/
protected function getCertPath() {
// we already have a valid certPath
if ($this->certPath !== '') {
return $this->certPath;
}
/** @var ICertificateManager $certManager */
$certManager = \OC::$server->getCertificateManager(null);
$certPath = $certManager->getAbsoluteBundlePath();
if (file_exists($certPath)) {
$this->certPath = $certPath;
}
return $this->certPath;
}
/**
* @param string $url
* @param string $userName
* @param string $sharedSecret
* @return Client
*/
protected function getClient($url, $userName, $sharedSecret) {
$settings = [
'baseUri' => $url . '/',
'userName' => $userName,
'password' => $sharedSecret,
];
$client = new Client($settings);
$certPath = $this->getCertPath();
$client->setThrowExceptions(true);
if ($certPath !== '' && strpos($url, 'http://') !== 0) {
$client->addCurlSetting(CURLOPT_CAINFO, $this->certPath);
}
return $client;
}
/** /**
* @param string $url * @param string $url
* @param string $userName * @param string $userName
@ -140,13 +190,7 @@ class SyncService {
* @return array * @return array
*/ */
protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) { protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) {
$settings = [ $client = $this->getClient($url, $userName, $sharedSecret);
'baseUri' => $url . '/',
'userName' => $userName,
'password' => $sharedSecret,
];
$client = new Client($settings);
$client->setThrowExceptions(true);
$addressBookUrl = "remote.php/dav/addressbooks/system/system/system"; $addressBookUrl = "remote.php/dav/addressbooks/system/system/system";
$body = $this->buildSyncCollectionRequestBody($syncToken); $body = $this->buildSyncCollectionRequestBody($syncToken);
@ -155,9 +199,7 @@ class SyncService {
'Content-Type' => 'application/xml' 'Content-Type' => 'application/xml'
]); ]);
$result = $this->parseMultiStatus($response['body']); return $this->parseMultiStatus($response['body']);
return $result;
} }
/** /**
@ -167,16 +209,8 @@ class SyncService {
* @return array * @return array
*/ */
protected function download($url, $sharedSecret, $resourcePath) { protected function download($url, $sharedSecret, $resourcePath) {
$settings = [ $client = $this->getClient($url, 'system', $sharedSecret);
'baseUri' => $url, return $client->request('GET', $resourcePath);
'userName' => 'system',
'password' => $sharedSecret,
];
$client = new Client($settings);
$client->setThrowExceptions(true);
$response = $client->request('GET', $resourcePath);
return $response;
} }
/** /**

View File

@ -146,7 +146,7 @@ class ConverterTest extends TestCase {
[ [
'cloud' => 'foo@cloud.net', 'cloud' => 'foo@cloud.net',
'email' => 'foo@bar.net', 'email' => 'foo@bar.net',
'photo' => 'data:image/jpeg;base64,MTIzNDU2Nzg5', 'photo' => 'MTIzNDU2Nzg5',
], ],
null, null,
'foo@bar.net', 'foo@bar.net',
@ -157,7 +157,7 @@ class ConverterTest extends TestCase {
'cloud' => 'foo@cloud.net', 'cloud' => 'foo@cloud.net',
'email' => 'foo@bar.net', 'email' => 'foo@bar.net',
'fn' => 'Dr. Foo Bar', 'fn' => 'Dr. Foo Bar',
'photo' => 'data:image/jpeg;base64,MTIzNDU2Nzg5', 'photo' => 'MTIzNDU2Nzg5',
], ],
"Dr. Foo Bar", "Dr. Foo Bar",
"foo@bar.net", "foo@bar.net",
@ -167,12 +167,22 @@ class ConverterTest extends TestCase {
[ [
'cloud' => 'foo@cloud.net', 'cloud' => 'foo@cloud.net',
'fn' => 'Dr. Foo Bar', 'fn' => 'Dr. Foo Bar',
'photo' => 'data:image/jpeg;base64,MTIzNDU2Nzg5', 'photo' => 'MTIzNDU2Nzg5',
], ],
"Dr. Foo Bar", "Dr. Foo Bar",
null, null,
"foo@cloud.net" "foo@cloud.net"
], ],
[
[
'cloud' => 'foo@cloud.net',
'fn' => 'Dr. Foo Bar',
'photo' => 'MTIzNDU2Nzg5',
],
'Dr. Foo Bar',
'',
'foo@cloud.net'
],
]; ];
} }

View File

@ -103,50 +103,50 @@ class SyncServiceTest extends TestCase {
$user->method('getBackendClassName')->willReturn('unittest'); $user->method('getBackendClassName')->willReturn('unittest');
$user->method('getUID')->willReturn('test-user'); $user->method('getUID')->willReturn('test-user');
$user->method('getCloudId')->willReturn('cloudId'); $user->method('getCloudId')->willReturn('cloudId');
$user->method('getDisplayName')->willReturn('test-user');
$accountManager = $this->getMockBuilder('OC\Accounts\AccountManager')->disableOriginalConstructor()->getMock(); $accountManager = $this->getMockBuilder('OC\Accounts\AccountManager')->disableOriginalConstructor()->getMock();
$accountManager->expects($this->any())->method('getUser') $accountManager->expects($this->any())->method('getUser')
->willReturn([ ->willReturn([
AccountManager::PROPERTY_DISPLAYNAME => AccountManager::PROPERTY_DISPLAYNAME =>
[ [
'value' => $user->getDisplayName(), 'value' => $user->getDisplayName(),
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY, 'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
], ],
AccountManager::PROPERTY_ADDRESS => AccountManager::PROPERTY_ADDRESS =>
[ [
'value' => '', 'value' => '',
'scope' => AccountManager::VISIBILITY_PRIVATE, 'scope' => AccountManager::VISIBILITY_PRIVATE,
], ],
AccountManager::PROPERTY_WEBSITE => AccountManager::PROPERTY_WEBSITE =>
[ [
'value' => '', 'value' => '',
'scope' => AccountManager::VISIBILITY_PRIVATE, 'scope' => AccountManager::VISIBILITY_PRIVATE,
], ],
AccountManager::PROPERTY_EMAIL => AccountManager::PROPERTY_EMAIL =>
[ [
'value' => $user->getEMailAddress(), 'value' => $user->getEMailAddress(),
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY, 'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
], ],
AccountManager::PROPERTY_AVATAR => AccountManager::PROPERTY_AVATAR =>
[ [
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY 'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY
], ],
AccountManager::PROPERTY_PHONE => AccountManager::PROPERTY_PHONE =>
[ [
'value' => '', 'value' => '',
'scope' => AccountManager::VISIBILITY_PRIVATE, 'scope' => AccountManager::VISIBILITY_PRIVATE,
], ],
AccountManager::PROPERTY_TWITTER => AccountManager::PROPERTY_TWITTER =>
[ [
'value' => '', 'value' => '',
'scope' => AccountManager::VISIBILITY_PRIVATE, 'scope' => AccountManager::VISIBILITY_PRIVATE,
], ],
]); ]
);
$ss = new SyncService($backend, $userManager, $logger, $accountManager); $ss = new SyncService($backend, $userManager, $logger, $accountManager);
$ss->updateUser($user); $ss->updateUser($user);
$user->method('getDisplayName')->willReturn('A test user for unit testing');
$ss->updateUser($user); $ss->updateUser($user);
$ss->deleteUser($user); $ss->deleteUser($user);
@ -179,7 +179,7 @@ class SyncServiceTest extends TestCase {
$accountManager = $this->getMockBuilder('OC\Accounts\AccountManager')->disableOriginalConstructor()->getMock(); $accountManager = $this->getMockBuilder('OC\Accounts\AccountManager')->disableOriginalConstructor()->getMock();
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $ss */ /** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $ss */
$ss = $this->getMockBuilder(SyncService::class) $ss = $this->getMockBuilder(SyncService::class)
->setMethods(['ensureSystemAddressBookExists', 'requestSyncReport', 'download']) ->setMethods(['ensureSystemAddressBookExists', 'requestSyncReport', 'download', 'getCertPath'])
->setConstructorArgs([$backend, $userManager, $logger, $accountManager]) ->setConstructorArgs([$backend, $userManager, $logger, $accountManager])
->getMock(); ->getMock();
$ss->method('requestSyncReport')->withAnyParameters()->willReturn(['response' => $response, 'token' => 'sync-token-1']); $ss->method('requestSyncReport')->withAnyParameters()->willReturn(['response' => $response, 'token' => 'sync-token-1']);
@ -189,6 +189,7 @@ class SyncServiceTest extends TestCase {
'statusCode' => 200, 'statusCode' => 200,
'headers' => [] 'headers' => []
]); ]);
$ss->method('getCertPath')->willReturn('');
return $ss; return $ss;
} }

View File

@ -25,6 +25,7 @@
namespace OCA\Federation\Middleware; namespace OCA\Federation\Middleware;
use OC\HintException; use OC\HintException;
use OCA\Federation\Controller\SettingsController;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Middleware; use OCP\AppFramework\Middleware;
@ -57,6 +58,9 @@ class AddServerMiddleware extends Middleware {
* @return JSONResponse * @return JSONResponse
*/ */
public function afterException($controller, $methodName, \Exception $exception) { public function afterException($controller, $methodName, \Exception $exception) {
if (($controller instanceof SettingsController) === false) {
throw $exception;
}
$this->logger->error($exception->getMessage(), ['app' => $this->appName]); $this->logger->error($exception->getMessage(), ['app' => $this->appName]);
if ($exception instanceof HintException) { if ($exception instanceof HintException) {
$message = $exception->getHint(); $message = $exception->getHint();

View File

@ -26,8 +26,8 @@ namespace OCA\Federation\Tests\Middleware;
use OC\HintException; use OC\HintException;
use OCA\Federation\Controller\SettingsController;
use OCA\Federation\Middleware\AddServerMiddleware; use OCA\Federation\Middleware\AddServerMiddleware;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\IL10N; use OCP\IL10N;
use OCP\ILogger; use OCP\ILogger;
@ -44,7 +44,7 @@ class AddServerMiddlewareTest extends TestCase {
/** @var AddServerMiddleware */ /** @var AddServerMiddleware */
private $middleware; private $middleware;
/** @var \PHPUnit_Framework_MockObject_MockObject | Controller */ /** @var \PHPUnit_Framework_MockObject_MockObject | SettingsController */
private $controller; private $controller;
public function setUp() { public function setUp() {
@ -52,7 +52,7 @@ class AddServerMiddlewareTest extends TestCase {
$this->logger = $this->getMockBuilder(ILogger::class)->getMock(); $this->logger = $this->getMockBuilder(ILogger::class)->getMock();
$this->l10n = $this->getMockBuilder(IL10N::class)->getMock(); $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
$this->controller = $this->getMockBuilder(Controller::class) $this->controller = $this->getMockBuilder(SettingsController::class)
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->middleware = new AddServerMiddleware( $this->middleware = new AddServerMiddleware(