From e05cf65363fe894e4d154d2905e70918fcaf7775 Mon Sep 17 00:00:00 2001 From: Sujith H Date: Wed, 12 Sep 2018 19:19:12 +0530 Subject: [PATCH] [stable10] Backport of Fix user-keys encryption when user created with email When user tries to set the password without logging in for once and no session available, the user-keys encryption was failing to create the private key for the new password. This change helps to fix the issue. Signed-off-by: Sujith H --- apps/encryption/lib/Hooks/UserHooks.php | 11 ++- apps/encryption/tests/Hooks/UserHooksTest.php | 88 ++++++++++++++++++- 2 files changed, 95 insertions(+), 4 deletions(-) diff --git a/apps/encryption/lib/Hooks/UserHooks.php b/apps/encryption/lib/Hooks/UserHooks.php index 3fbf9c7e42b9..ea92552f2168 100644 --- a/apps/encryption/lib/Hooks/UserHooks.php +++ b/apps/encryption/lib/Hooks/UserHooks.php @@ -246,9 +246,14 @@ public function preSetPassphrase($params) { */ public function setPassphrase($params) { - // Get existing decrypted private key - $privateKey = $this->session->getPrivateKey(); - $user = $this->user->getUser(); + $privateKey = null; + $user = null; + //Check if the session is there or not + if ($this->user->getUser() !== null) { + // Get existing decrypted private key + $privateKey = $this->session->getPrivateKey(); + $user = $this->user->getUser(); + } // current logged in user changes his own password if ($user && $params['uid'] === $user->getUID() && $privateKey) { diff --git a/apps/encryption/tests/Hooks/UserHooksTest.php b/apps/encryption/tests/Hooks/UserHooksTest.php index d424a1958368..6152c3b565b5 100644 --- a/apps/encryption/tests/Hooks/UserHooksTest.php +++ b/apps/encryption/tests/Hooks/UserHooksTest.php @@ -5,6 +5,7 @@ * @author Joas Schilling * @author Thomas Müller * @author Vincent Petry + * @author Sujith Haridasan * * @copyright Copyright (c) 2018, ownCloud GmbH * @license AGPL-3.0 @@ -28,10 +29,17 @@ namespace OCA\Encryption\Tests\Hooks; +use OC\User\Manager; use OCA\Encryption\Crypto\Crypt; use OCA\Encryption\Hooks\UserHooks; +use OCA\Encryption\KeyManager; +use OCA\Encryption\Recovery; +use OCA\Encryption\Session; +use OCA\Encryption\Users\Setup; use OCA\Encryption\Util; use OCP\IConfig; +use OCP\ILogger; +use OCP\IUserSession; use Test\TestCase; /** @@ -262,8 +270,86 @@ public function testSetPassphrase() { $this->assertNull($this->instance->setPassphrase($this->params)); } + /** + * Test setPassphrase without session and no logger error should appear + */ + public function testSetPassphraseWithoutSession() { + $keyManager = $this->createMock(KeyManager::class); + $userManager = $this->createMock(Manager::class); + $logger = $this->createMock(ILogger::class); + $setUp = $this->createMock(Setup::class); + $userSession = $this->createMock(IUserSession::class); + $encryptionUtil = $this->createMock(Util::class); + $encryptionSession = $this->createMock(Session::class); + $recovery = $this->createMock(Recovery::class); + $crypt = $this->createMock(Crypt::class); + $config = $this->createMock(IConfig::class); + + //$userHooks = new UserHooks($keyManager, $userManager, $logger, $setUp, $userSession, $encryptionUtil, $encryptionSession, $crypt, $recovery, $config); + $userHooks = $this->getMockBuilder(UserHooks::class) + ->setConstructorArgs([ + $keyManager, $userManager, $logger, + $setUp, $userSession, $encryptionUtil, + $encryptionSession, $crypt, $recovery, $config + ])->setMethods(['initMountPoints'])->getMock(); + + $userSession->expects($this->any()) + ->method('getUser') + ->willReturn(null); + + $crypt->expects($this->any()) + ->method('encryptPrivateKey') + ->willReturn(true); + + $keyManager->expects($this->any()) + ->method('setPrivateKey') + ->willReturn(true); + + //No logger error should appear + $logger->expects($this->never()) + ->method('error'); + + $userHooks->setPassphrase($this->params); + } + + public function testSetPassphraseWithoutSessionLoggerError() { + $keyManager = $this->createMock(KeyManager::class); + $userManager = $this->createMock(Manager::class); + $logger = $this->createMock(ILogger::class); + $setUp = $this->createMock(Setup::class); + $userSession = $this->createMock(IUserSession::class); + $encryptionUtil = $this->createMock(Util::class); + $encryptionSession = $this->createMock(Session::class); + $recovery = $this->createMock(Recovery::class); + $crypt = $this->createMock(Crypt::class); + $config = $this->createMock(IConfig::class); + + //$userHooks = new UserHooks($keyManager, $userManager, $logger, $setUp, $userSession, $encryptionUtil, $encryptionSession, $crypt, $recovery, $config); + $userHooks = $this->getMockBuilder(UserHooks::class) + ->setConstructorArgs([ + $keyManager, $userManager, $logger, + $setUp, $userSession, $encryptionUtil, + $encryptionSession, $crypt, $recovery, $config + ])->setMethods(['initMountPoints'])->getMock(); + + $userSession->expects($this->any()) + ->method('getUser') + ->willReturn(null); + + $crypt->expects($this->any()) + ->method('encryptPrivateKey') + ->willReturn(false); + + //No logger error should appear + $logger->expects($this->any()) + ->method('error') + ->with('Encryption Could not update users encryption password'); + + $userHooks->setPassphrase($this->params); + } + public function testSetPasswordNoUser() { - $this->sessionMock->expects($this->once()) + $this->sessionMock->expects($this->any()) ->method('getPrivateKey') ->willReturn(true);