diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/encryption/lib/Hooks/UserHooks.php | 30 | ||||
-rw-r--r-- | apps/encryption/lib/Recovery.php | 22 | ||||
-rw-r--r-- | apps/encryption/tests/Controller/SettingsControllerTest.php | 29 | ||||
-rw-r--r-- | apps/encryption/tests/Hooks/UserHooksTest.php | 28 | ||||
-rw-r--r-- | apps/encryption/tests/RecoveryTest.php | 38 | ||||
-rw-r--r-- | apps/encryption/tests/UtilTest.php | 31 | ||||
-rw-r--r-- | apps/files_external/lib/Migration/DummyUserSession.php | 20 |
7 files changed, 92 insertions, 106 deletions
diff --git a/apps/encryption/lib/Hooks/UserHooks.php b/apps/encryption/lib/Hooks/UserHooks.php index 48815892900..bf3f3f42b47 100644 --- a/apps/encryption/lib/Hooks/UserHooks.php +++ b/apps/encryption/lib/Hooks/UserHooks.php @@ -68,7 +68,7 @@ class UserHooks implements IHook { /** * @var IUserSession */ - private $user; + private $userSession; /** * @var Util */ @@ -93,7 +93,7 @@ class UserHooks implements IHook { * @param IUserManager $userManager * @param ILogger $logger * @param Setup $userSetup - * @param IUserSession $user + * @param IUserSession $userSession * @param Util $util * @param Session $session * @param Crypt $crypt @@ -103,7 +103,7 @@ class UserHooks implements IHook { IUserManager $userManager, ILogger $logger, Setup $userSetup, - IUserSession $user, + IUserSession $userSession, Util $util, Session $session, Crypt $crypt, @@ -113,7 +113,7 @@ class UserHooks implements IHook { $this->userManager = $userManager; $this->logger = $logger; $this->userSetup = $userSetup; - $this->user = $user; + $this->userSession = $userSession; $this->util = $util; $this->session = $session; $this->recovery = $recovery; @@ -253,7 +253,7 @@ class UserHooks implements IHook { } // Get existing decrypted private key - $user = $this->user->getUser(); + $user = $this->userSession->getUser(); // current logged in user changes his own password if ($user && $params['uid'] === $user->getUID()) { @@ -265,7 +265,7 @@ class UserHooks implements IHook { // Save private key if ($encryptedPrivateKey) { - $this->keyManager->setPrivateKey($this->user->getUser()->getUID(), + $this->keyManager->setPrivateKey($user->getUID(), $this->crypt->generateHeader() . $encryptedPrivateKey); } else { $this->logger->error('Encryption could not update users encryption password'); @@ -275,8 +275,8 @@ class UserHooks implements IHook { // private key has not changed, only the passphrase // used to decrypt it has changed } else { // admin changed the password for a different user, create new keys and re-encrypt file keys - $user = $params['uid']; - $this->initMountPoints($user); + $userId = $params['uid']; + $this->initMountPoints($userId); $recoveryPassword = isset($params['recoveryPassword']) ? $params['recoveryPassword'] : null; $recoveryKeyId = $this->keyManager->getRecoveryKeyId(); @@ -296,9 +296,9 @@ class UserHooks implements IHook { // ...encryption was activated for the first time (no keys exists) // ...the user doesn't have any files if ( - ($this->recovery->isRecoveryEnabledForUser($user) && $recoveryPassword) - || !$this->keyManager->userHasKeys($user) - || !$this->util->userHasFiles($user) + ($this->recovery->isRecoveryEnabledForUser($userId) && $recoveryPassword) + || !$this->keyManager->userHasKeys($userId) + || !$this->util->userHasFiles($userId) ) { // backup old keys @@ -309,16 +309,16 @@ class UserHooks implements IHook { $keyPair = $this->crypt->createKeyPair(); // Save public key - $this->keyManager->setPublicKey($user, $keyPair['publicKey']); + $this->keyManager->setPublicKey($userId, $keyPair['publicKey']); // Encrypt private key with new password - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $newUserPassword, $user); + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $newUserPassword, $userId); if ($encryptedKey) { - $this->keyManager->setPrivateKey($user, $this->crypt->generateHeader() . $encryptedKey); + $this->keyManager->setPrivateKey($userId, $this->crypt->generateHeader() . $encryptedKey); if ($recoveryPassword) { // if recovery key is set we can re-encrypt the key files - $this->recovery->recoverUsersFiles($recoveryPassword, $user); + $this->recovery->recoverUsersFiles($recoveryPassword, $userId); } } else { $this->logger->error('Encryption Could not update users encryption password'); diff --git a/apps/encryption/lib/Recovery.php b/apps/encryption/lib/Recovery.php index 17533e7b114..f9cdef53f13 100644 --- a/apps/encryption/lib/Recovery.php +++ b/apps/encryption/lib/Recovery.php @@ -47,10 +47,6 @@ class Recovery { */ protected $crypt; /** - * @var ISecureRandom - */ - private $random; - /** * @var KeyManager */ private $keyManager; @@ -59,10 +55,6 @@ class Recovery { */ private $config; /** - * @var IStorage - */ - private $keyStorage; - /** * @var View */ private $view; @@ -72,29 +64,23 @@ class Recovery { private $file; /** - * @param IUserSession $user + * @param IUserSession $userSession * @param Crypt $crypt - * @param ISecureRandom $random * @param KeyManager $keyManager * @param IConfig $config - * @param IStorage $keyStorage * @param IFile $file * @param View $view */ - public function __construct(IUserSession $user, + public function __construct(IUserSession $userSession, Crypt $crypt, - ISecureRandom $random, KeyManager $keyManager, IConfig $config, - IStorage $keyStorage, IFile $file, View $view) { - $this->user = ($user && $user->isLoggedIn()) ? $user->getUser() : false; + $this->user = ($userSession && $userSession->isLoggedIn()) ? $userSession->getUser() : false; $this->crypt = $crypt; - $this->random = $random; $this->keyManager = $keyManager; $this->config = $config; - $this->keyStorage = $keyStorage; $this->view = $view; $this->file = $file; } @@ -169,7 +155,7 @@ class Recovery { * @return bool */ public function isRecoveryEnabledForUser($user = '') { - $uid = empty($user) ? $this->user->getUID() : $user; + $uid = $user === '' ? $this->user->getUID() : $user; $recoveryMode = $this->config->getUserValue($uid, 'encryption', 'recoveryEnabled', diff --git a/apps/encryption/tests/Controller/SettingsControllerTest.php b/apps/encryption/tests/Controller/SettingsControllerTest.php index b12652b51c9..b50f7cd0b61 100644 --- a/apps/encryption/tests/Controller/SettingsControllerTest.php +++ b/apps/encryption/tests/Controller/SettingsControllerTest.php @@ -34,8 +34,10 @@ use OCP\AppFramework\Http; use OCP\IL10N; use OCP\IRequest; use OCP\ISession; +use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class SettingsControllerTest extends TestCase { @@ -63,6 +65,8 @@ class SettingsControllerTest extends TestCase { /** @var \OCA\Encryption\Session|\PHPUnit_Framework_MockObject_MockObject */ private $sessionMock; + /** @var MockObject|IUser */ + private $user; /** @var \OCP\ISession|\PHPUnit_Framework_MockObject_MockObject */ private $ocSessionMock; @@ -94,28 +98,17 @@ class SettingsControllerTest extends TestCase { $this->cryptMock = $this->getMockBuilder(Crypt::class) ->disableOriginalConstructor()->getMock(); - $this->userSessionMock = $this->getMockBuilder(IUserSession::class) - ->disableOriginalConstructor() - ->setMethods([ - 'isLoggedIn', - 'getUID', - 'login', - 'logout', - 'setUser', - 'getUser', - 'canChangePassword', - ]) - ->getMock(); - $this->ocSessionMock = $this->getMockBuilder(ISession::class)->disableOriginalConstructor()->getMock(); - $this->userSessionMock->expects($this->any()) + $this->user = $this->createMock(IUser::class); + $this->user->expects($this->any()) ->method('getUID') ->willReturn('testUserUid'); + $this->userSessionMock = $this->createMock(IUserSession::class); $this->userSessionMock->expects($this->any()) - ->method($this->anything()) - ->will($this->returnSelf()); + ->method('getUser') + ->willReturn($this->user); $this->sessionMock = $this->getMockBuilder(Session::class) ->disableOriginalConstructor()->getMock(); @@ -146,7 +139,9 @@ class SettingsControllerTest extends TestCase { $oldPassword = 'old'; $newPassword = 'new'; - $this->userSessionMock->expects($this->once())->method('getUID')->willReturn('uid'); + $this->user->expects($this->any()) + ->method('getUID') + ->willReturn('uid'); $this->userManagerMock ->expects($this->exactly(2)) diff --git a/apps/encryption/tests/Hooks/UserHooksTest.php b/apps/encryption/tests/Hooks/UserHooksTest.php index b14a1f6a559..2295b90625d 100644 --- a/apps/encryption/tests/Hooks/UserHooksTest.php +++ b/apps/encryption/tests/Hooks/UserHooksTest.php @@ -41,6 +41,7 @@ use OCP\ILogger; use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; /** @@ -80,6 +81,10 @@ class UserHooksTest extends TestCase { */ private $userSessionMock; /** + * @var MockObject|IUser + */ + private $user; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ private $cryptMock; @@ -343,24 +348,15 @@ class UserHooksTest extends TestCase { ->disableOriginalConstructor() ->getMock(); - $this->userSessionMock = $this->getMockBuilder(IUserSession::class) - ->disableOriginalConstructor() - ->setMethods([ - 'isLoggedIn', - 'getUID', - 'login', - 'logout', - 'setUser', - 'getUser', - 'canChangePassword' - ]) - ->getMock(); - - $this->userSessionMock->expects($this->any())->method('getUID')->will($this->returnValue('testUser')); + $this->user = $this->createMock(IUser::class); + $this->user->expects($this->any()) + ->method('getUID') + ->willReturn('testUser'); + $this->userSessionMock = $this->createMock(IUserSession::class); $this->userSessionMock->expects($this->any()) - ->method($this->anything()) - ->will($this->returnSelf()); + ->method('getUser') + ->willReturn($this->user); $utilMock = $this->getMockBuilder(Util::class) ->disableOriginalConstructor() diff --git a/apps/encryption/tests/RecoveryTest.php b/apps/encryption/tests/RecoveryTest.php index 0eb9a777ec3..9b737cdb82f 100644 --- a/apps/encryption/tests/RecoveryTest.php +++ b/apps/encryption/tests/RecoveryTest.php @@ -36,8 +36,10 @@ use OCA\Encryption\Recovery; use OCP\Encryption\IFile; use OCP\Encryption\Keys\IStorage; use OCP\IConfig; +use OCP\IUser; use OCP\IUserSession; use OCP\Security\ISecureRandom; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class RecoveryTest extends TestCase { @@ -55,6 +57,10 @@ class RecoveryTest extends TestCase { */ private $userSessionMock; /** + * @var MockObject|IUser + */ + private $user; + /** * @var \OCA\Encryption\KeyManager|\PHPUnit_Framework_MockObject_MockObject */ private $keyManagerMock; @@ -257,32 +263,22 @@ class RecoveryTest extends TestCase { protected function setUp() { parent::setUp(); + $this->user = $this->createMock(IUser::class); + $this->user->expects($this->any()) + ->method('getUID') + ->willReturn('admin'); - $this->userSessionMock = $this->getMockBuilder(IUserSession::class) - ->disableOriginalConstructor() - ->setMethods([ - 'isLoggedIn', - 'getUID', - 'login', - 'logout', - 'setUser', - 'getUser' - ]) - ->getMock(); - - $this->userSessionMock->expects($this->any())->method('getUID')->will($this->returnValue('admin')); - + $this->userSessionMock = $this->createMock(IUserSession::class); + $this->userSessionMock->expects($this->any()) + ->method('getUser') + ->willReturn($this->user); $this->userSessionMock->expects($this->any()) - ->method($this->anything()) - ->will($this->returnSelf()); + ->method('isLoggedIn') + ->willReturn(true); $this->cryptMock = $this->getMockBuilder(Crypt::class)->disableOriginalConstructor()->getMock(); - /** @var \OCP\Security\ISecureRandom $randomMock */ - $randomMock = $this->createMock(ISecureRandom::class); $this->keyManagerMock = $this->getMockBuilder(KeyManager::class)->disableOriginalConstructor()->getMock(); $this->configMock = $this->createMock(IConfig::class); - /** @var \OCP\Encryption\Keys\IStorage $keyStorageMock */ - $keyStorageMock = $this->createMock(IStorage::class); $this->fileMock = $this->createMock(IFile::class); $this->viewMock = $this->createMock(View::class); @@ -296,10 +292,8 @@ class RecoveryTest extends TestCase { $this->instance = new Recovery($this->userSessionMock, $this->cryptMock, - $randomMock, $this->keyManagerMock, $this->configMock, - $keyStorageMock, $this->fileMock, $this->viewMock); } diff --git a/apps/encryption/tests/UtilTest.php b/apps/encryption/tests/UtilTest.php index 1777d1f8f1f..17721d81eaf 100644 --- a/apps/encryption/tests/UtilTest.php +++ b/apps/encryption/tests/UtilTest.php @@ -36,8 +36,10 @@ use OCP\Files\Mount\IMountPoint; use OCP\Files\Storage; use OCP\IConfig; use OCP\ILogger; +use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class UtilTest extends TestCase { @@ -91,27 +93,20 @@ class UtilTest extends TestCase { ->getMock(); /** @var \OCP\ILogger $loggerMock */ $loggerMock = $this->createMock(ILogger::class); - /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSessionMock */ - $userSessionMock = $this->getMockBuilder(IUserSession::class) - ->disableOriginalConstructor() - ->setMethods([ - 'isLoggedIn', - 'getUID', - 'login', - 'logout', - 'setUser', - 'getUser' - ]) - ->getMock(); - - $userSessionMock->method('isLoggedIn')->will($this->returnValue(true)); - $userSessionMock->method('getUID')->will($this->returnValue('admin')); + $user = $this->createMock(IUser::class); + $user->expects($this->any()) + ->method('getUID') + ->willReturn('admin'); + /** @var IUserSession|MockObject $userSessionMock */ + $userSessionMock = $this->createMock(IUserSession::class); $userSessionMock->expects($this->any()) - ->method($this->anything()) - ->will($this->returnSelf()); - + ->method('getUser') + ->willReturn($user); + $userSessionMock->expects($this->any()) + ->method('isLoggedIn') + ->willReturn(true); $this->configMock = $this->createMock(IConfig::class); diff --git a/apps/files_external/lib/Migration/DummyUserSession.php b/apps/files_external/lib/Migration/DummyUserSession.php index 004ca019eb0..0cc726d35b9 100644 --- a/apps/files_external/lib/Migration/DummyUserSession.php +++ b/apps/files_external/lib/Migration/DummyUserSession.php @@ -50,4 +50,24 @@ class DummyUserSession implements IUserSession { public function isLoggedIn() { return !is_null($this->user); } + + /** + * get getImpersonatingUserID + * + * @return string|null + * @since 17.0.0 + */ + public function getImpersonatingUserID() : ?string { + return null; + } + + /** + * set setImpersonatingUserID + * + * @since 17.0.0 + */ + public function setImpersonatingUserID(bool $useCurrentUser = true): void { + //no OP + } + } |