]> source.dussan.org Git - nextcloud-server.git/commitdiff
check if session is initialized 2240/head
authorBjoern Schiessle <bjoern@schiessle.org>
Wed, 23 Nov 2016 11:07:04 +0000 (12:07 +0100)
committerBjoern Schiessle <bjoern@schiessle.org>
Wed, 23 Nov 2016 11:20:43 +0000 (12:20 +0100)
Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
apps/encryption/lib/Crypto/Encryption.php
apps/encryption/lib/KeyManager.php
apps/encryption/lib/Session.php
apps/encryption/tests/Crypto/EncryptionTest.php
apps/encryption/tests/SessionTest.php

index c71e25b64425a30dae8f0bde2939136fc4645fb5..fdcbd41a09c878733d20b76465b43ee1c320b4b7 100644 (file)
@@ -177,6 +177,14 @@ class Encryption implements IEncryptionModule {
                $this->isWriteOperation = false;
                $this->writeCache = '';
 
+               if($this->session->isReady() === false) {
+                       // if the master key is enabled we can initialize encryption
+                       // with a empty password and user name
+                       if ($this->util->isMasterKeyEnabled()) {
+                               $this->keyManager->init('', '');
+                       }
+               }
+
                if ($this->session->decryptAllModeActivated()) {
                        $encryptedFileKey = $this->keyManager->getEncryptedFileKey($this->path);
                        $shareKey = $this->keyManager->getShareKey($this->path, $this->session->getDecryptAllUid());
index 96264d013631e1cf5b08ffd69a4c691e05213156..26f023ed8f9f7f218c72f8b144db67b7c60581bb 100644 (file)
@@ -406,9 +406,6 @@ class KeyManager {
                }
 
                if ($this->util->isMasterKeyEnabled()) {
-                       if ($this->session->getStatus() === Session::NOT_INITIALIZED)
-                               $this->init('', '');
-                       
                        $uid = $this->getMasterKeyId();
                }
 
index 92132d6080c96fd2799926160b0f5ac478c59365..a61ee25fadba3469ee4285a72c10fb59a23dc8ac 100644 (file)
@@ -67,6 +67,16 @@ class Session {
                return $status;
        }
 
+       /**
+        * check if encryption was initialized successfully
+        *
+        * @return bool
+        */
+       public function isReady() {
+               $status = $this->getStatus();
+               return $status === self::INIT_SUCCESSFUL;
+       }
+
        /**
         * Gets user or public share private key from session
         *
index 658f6275bb4048e864af54b1a2f1dfc3bd19da87..3525d2d4aec93f0edba32a231ad48d4c29d012b4 100644 (file)
@@ -279,6 +279,21 @@ class EncryptionTest extends TestCase {
                );
        }
 
+       /**
+        * test begin() if encryption is not initialized but the master key is enabled
+        * in this case we can initialize the encryption without a username/password
+        * and continue
+        */
+       public function testBeginInitMasterKey() {
+
+               $this->sessionMock->expects($this->once())->method('isReady')->willReturn(false);
+               $this->utilMock->expects($this->once())->method('isMasterKeyEnabled')
+                       ->willReturn(true);
+               $this->keyManagerMock->expects($this->once())->method('init')->with('', '');
+
+               $this->instance->begin('/user/files/welcome.txt', 'user', 'r', [], []);
+       }
+
        /**
         * @dataProvider dataTestUpdate
         *
index 099acddbca1f136aa0ef919e9caf4e167a25e61d..3000fedf2c3e963cd642914c6b9be52416615e9f 100644 (file)
@@ -133,6 +133,32 @@ class SessionTest extends TestCase {
                $this->assertEquals(2, $this->instance->getStatus());
        }
 
+       /**
+        * @dataProvider dataTestIsReady
+        *
+        * @param int $status
+        * @param bool $expected
+        */
+       public function testIsReady($status, $expected) {
+               /** @var Session | \PHPUnit_Framework_MockObject_MockObject $instance */
+               $instance = $this->getMockBuilder(Session::class)
+                       ->setConstructorArgs([$this->sessionMock])
+                       ->setMethods(['getStatus'])->getMock();
+
+               $instance->expects($this->once())->method('getStatus')
+                       ->willReturn($status);
+
+               $this->assertSame($expected, $instance->isReady());
+       }
+
+       public function dataTestIsReady() {
+               return [
+                       [Session::INIT_SUCCESSFUL, true],
+                       [Session::INIT_EXECUTED, false],
+                       [Session::NOT_INITIALIZED, false],
+               ];
+       }
+
        /**
         * @param $key
         * @param $value