diff --git a/apps/encryption/lib/AppInfo/Application.php b/apps/encryption/lib/AppInfo/Application.php index aeee3b71cab9..dd947123c6d2 100644 --- a/apps/encryption/lib/AppInfo/Application.php +++ b/apps/encryption/lib/AppInfo/Application.php @@ -94,7 +94,7 @@ public function registerHooks() { $container->query('Util'), $container->query('Session'), $container->query('Crypt'), - $container->query('Recovery')) + $container->query('Recovery'), $server->getConfig()) ]); $hookManager->fireHooks(); diff --git a/apps/encryption/lib/Hooks/UserHooks.php b/apps/encryption/lib/Hooks/UserHooks.php index 048911af5a51..3fbf9c7e42b9 100644 --- a/apps/encryption/lib/Hooks/UserHooks.php +++ b/apps/encryption/lib/Hooks/UserHooks.php @@ -26,6 +26,7 @@ use OC\Files\Filesystem; +use OCP\IConfig; use OCP\IUserManager; use OCP\Util as OCUtil; use OCA\Encryption\Hooks\Contracts\IHook; @@ -76,6 +77,10 @@ class UserHooks implements IHook { * @var Crypt */ private $crypt; + /** + * @var IConfig + */ + private $config; /** * UserHooks constructor. @@ -89,6 +94,7 @@ class UserHooks implements IHook { * @param Session $session * @param Crypt $crypt * @param Recovery $recovery + * @param IConfig $config */ public function __construct(KeyManager $keyManager, IUserManager $userManager, @@ -98,7 +104,7 @@ public function __construct(KeyManager $keyManager, Util $util, Session $session, Crypt $crypt, - Recovery $recovery) { + Recovery $recovery, IConfig $config) { $this->keyManager = $keyManager; $this->userManager = $userManager; @@ -109,6 +115,7 @@ public function __construct(KeyManager $keyManager, $this->session = $session; $this->recovery = $recovery; $this->crypt = $crypt; + $this->config = $config; } /** @@ -166,6 +173,11 @@ public function login($params) { $this->userSetup->setupUser($params['uid'], $params['password']); } + if (($this->util->isMasterKeyEnabled() === false) && + ($this->config->getAppValue('encryption', 'userSpecificKey', '') === '')) { + $this->config->setAppValue('encryption', 'userSpecificKey', '1'); + } + $this->keyManager->init($params['uid'], $params['password']); } diff --git a/apps/encryption/tests/Hooks/UserHooksTest.php b/apps/encryption/tests/Hooks/UserHooksTest.php index b6da6169006e..d424a1958368 100644 --- a/apps/encryption/tests/Hooks/UserHooksTest.php +++ b/apps/encryption/tests/Hooks/UserHooksTest.php @@ -30,6 +30,8 @@ use OCA\Encryption\Crypto\Crypt; use OCA\Encryption\Hooks\UserHooks; +use OCA\Encryption\Util; +use OCP\IConfig; use Test\TestCase; /** @@ -76,6 +78,7 @@ class UserHooksTest extends TestCase { * @var \PHPUnit_Framework_MockObject_MockObject */ private $loggerMock; + private $config; /** * @var UserHooks */ @@ -92,6 +95,12 @@ public function testLogin() { ->method('init') ->with('testUser', 'password'); + $this->config->expects($this->once()) + ->method('getAppValue') + ->willReturnMap([ + ['encryption', 'userSpecificKey', '', ''], + ]); + $this->assertNull($this->instance->login($this->params)); } @@ -136,7 +145,8 @@ public function testPreSetPassphrase($canChange) { $this->utilMock, $this->sessionMock, $this->cryptMock, - $this->recoveryMock + $this->recoveryMock, + $this->config ] ) ->setMethods(['setPassphrase']) @@ -215,7 +225,8 @@ public function testSetPassphrase() { $this->utilMock, $this->sessionMock, $this->cryptMock, - $this->recoveryMock + $this->recoveryMock, + $this->config ] )->setMethods(['initMountPoints'])->getMock(); @@ -278,7 +289,8 @@ public function testSetPasswordNoUser() { $this->utilMock, $this->sessionMock, $this->cryptMock, - $this->recoveryMock + $this->recoveryMock, + $this->config ] )->setMethods(['initMountPoints'])->getMock(); @@ -331,9 +343,9 @@ protected function setUp() { ->method($this->anything()) ->will($this->returnSelf()); - $utilMock = $this->getMockBuilder('OCA\Encryption\Util') + /*$utilMock = $this->getMockBuilder('OCA\Encryption\Util') ->disableOriginalConstructor() - ->getMock(); + ->getMock();*/ $sessionMock = $this->getMockBuilder('OCA\Encryption\Session') ->disableOriginalConstructor() @@ -345,10 +357,11 @@ protected function setUp() { $recoveryMock = $this->getMockBuilder('OCA\Encryption\Recovery') ->disableOriginalConstructor() ->getMock(); + $this->config = $this->createMock(IConfig::class); $this->sessionMock = $sessionMock; $this->recoveryMock = $recoveryMock; - $this->utilMock = $utilMock; + $this->utilMock = $this->createMock(Util::class); $this->utilMock->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false); $this->instance = $this->getMockBuilder('OCA\Encryption\Hooks\UserHooks') @@ -362,7 +375,8 @@ protected function setUp() { $this->utilMock, $this->sessionMock, $this->cryptMock, - $this->recoveryMock + $this->recoveryMock, + $this->config ] )->setMethods(['setupFS'])->getMock(); diff --git a/core/Command/Encryption/EncryptAll.php b/core/Command/Encryption/EncryptAll.php index 860aa85871b8..1e274fe1b643 100644 --- a/core/Command/Encryption/EncryptAll.php +++ b/core/Command/Encryption/EncryptAll.php @@ -107,6 +107,15 @@ protected function execute(InputInterface $input, OutputInterface $output) { throw new \Exception('Server side encryption is not enabled'); } + $masterKeyEnabled = $this->config->getAppValue('encryption', 'useMasterKey', ''); + $userKeyEnabled = $this->config->getAppValue('encryption', 'userSpecificKey', ''); + if (($masterKeyEnabled === '') && ($userKeyEnabled === '')) { + /** + * Enable user specific encryption if nothing is enabled. + */ + $this->config->setAppValue('encryption', 'userSpecificKey', '1'); + } + $output->writeln("\n"); $output->writeln('You are about to encrypt all files stored in your ownCloud installation.'); $output->writeln('Depending on the number of available files, and their size, this may take quite some time.'); diff --git a/tests/Core/Command/Encryption/EncryptAllTest.php b/tests/Core/Command/Encryption/EncryptAllTest.php index 1d8718e748f0..0a10c06d0106 100644 --- a/tests/Core/Command/Encryption/EncryptAllTest.php +++ b/tests/Core/Command/Encryption/EncryptAllTest.php @@ -99,6 +99,15 @@ public function testExecute($answer, $askResult) { $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(true); $this->questionHelper->expects($this->once())->method('ask')->willReturn($askResult); + $this->config->expects($this->any()) + ->method('getAppValue') + ->willReturnMap([ + ['encryption', 'useMasterKey', '', ''], + ['encryption', 'userSpecificKey', '', ''] + ]); + $this->config->expects($this->once()) + ->method('setAppValue') + ->willReturn(null); if ($answer === 'Y' || $answer === 'y') { $this->encryptionManager->expects($this->once())