Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Backport of Fix user-keys encryption when user created wit… #32685

Merged
merged 1 commit into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions apps/encryption/lib/Hooks/UserHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
88 changes: 87 additions & 1 deletion apps/encryption/tests/Hooks/UserHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @author Joas Schilling <[email protected]>
* @author Thomas Müller <[email protected]>
* @author Vincent Petry <[email protected]>
* @author Sujith Haridasan <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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);

Expand Down