]> source.dussan.org Git - nextcloud-server.git/commitdiff
add session class to handle all session operations
authorBjoern Schiessle <schiessle@owncloud.com>
Tue, 31 Mar 2015 09:15:47 +0000 (11:15 +0200)
committerThomas Müller <thomas.mueller@tmit.eu>
Tue, 7 Apr 2015 11:30:28 +0000 (13:30 +0200)
apps/encryption/appinfo/application.php
apps/encryption/hooks/userhooks.php
apps/encryption/lib/exceptions/privatekeymissingexception.php [new file with mode: 0644]
apps/encryption/lib/keymanager.php
apps/encryption/lib/session.php [new file with mode: 0644]
apps/encryption/settings/settings-personal.php
lib/private/encryption/exceptions/privatekeymissingexception.php [deleted file]

index f9b7a1c60daeb1a6e6700a7497b5f269dfb41046..21d7f3e737f42ebb481f0a5fcee91c79f88b9214 100644 (file)
@@ -76,9 +76,8 @@ class Application extends \OCP\AppFramework\App {
                                        $server->getLogger(),
                                        $container->query('UserSetup'),
                                        $server->getUserSession(),
-                                       new \OCP\Util(),
                                        $container->query('Util'),
-                                       $server->getSession()),
+                                       new \OCA\Encryption\Session($server->getSession())),
                        ]);
 
                        $hookManager->fireHooks();
@@ -126,7 +125,7 @@ class Application extends \OCP\AppFramework\App {
                                        $c->query('Crypt'),
                                        $server->getConfig(),
                                        $server->getUserSession(),
-                                       $server->getSession(),
+                                       new \OCA\Encryption\Session($server->getSession()),
                                        $server->getLogger(),
                                        $c->query('Recovery')
                                );
index 1629aca0f55704f873cf51547673aeec401abb32..27780cccfbf96485e84cdb8e5c500b748a24cf3c 100644 (file)
@@ -22,7 +22,6 @@
 namespace OCA\Encryption\Hooks;
 
 
-use OCP\ISession;
 use OCP\Util as OCUtil;
 use OCA\Encryption\Hooks\Contracts\IHook;
 use OCA\Encryption\KeyManager;
@@ -31,7 +30,7 @@ use OCP\App;
 use OCP\ILogger;
 use OCP\IUserSession;
 use OCA\Encryption\Util;
-use Test\User;
+use OCA\Encryption\Session;
 
 class UserHooks implements IHook {
        /**
@@ -55,7 +54,7 @@ class UserHooks implements IHook {
         */
        private $util;
        /**
-        * @var ISession
+        * @var Session
         */
        private $session;
 
@@ -66,17 +65,15 @@ class UserHooks implements IHook {
         * @param ILogger $logger
         * @param Setup $userSetup
         * @param IUserSession $user
-        * @param OCUtil $ocUtil
         * @param Util $util
-        * @param ISession $session
+        * @param Session $session
         */
        public function __construct(KeyManager $keyManager,
                                                                ILogger $logger,
                                                                Setup $userSetup,
                                                                IUserSession $user,
-                                                               OCUtil $ocUtil,
                                                                Util $util,
-                                                               ISession $session) {
+                                                               Session $session) {
 
                $this->keyManager = $keyManager;
                $this->logger = $logger;
diff --git a/apps/encryption/lib/exceptions/privatekeymissingexception.php b/apps/encryption/lib/exceptions/privatekeymissingexception.php
new file mode 100644 (file)
index 0000000..e06940f
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+  /**
+ * @author Clark Tomlinson  <clark@owncloud.com>
+ * @since 2/25/15, 9:39 AM
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+
+namespace OCA\Encryption\Exceptions;
+
+
+class PrivateKeyMissingException extends GenericEncryptionException{
+
+}
index cd983be17f9e447ae592aad56d7047fcbab7a950..6c5f2348027b21f3f7fdf240f0f66088458b97a1 100644 (file)
@@ -30,14 +30,14 @@ use OCP\Encryption\Keys\IStorage;
 use OCP\IConfig;
 use OCP\ILogger;
 use OCP\IUserSession;
-use \OCP\ISession;
+use \OCA\Encryption\Session;
 
 class KeyManager {
 
        /**
-        * @var ISession
+        * @var Session
         */
-       public static $session;
+       protected $session;
        /**
         * @var IStorage
         */
@@ -84,17 +84,13 @@ class KeyManager {
         * @var ILogger
         */
        private $log;
-       /**
-        * @var Recovery
-        */
-       private $recovery;
 
        /**
         * @param IStorage $keyStorage
         * @param Crypt $crypt
         * @param IConfig $config
         * @param IUserSession $userSession
-        * @param \OCP\ISession $session
+        * @param Session $session
         * @param ILogger $log
         * @param Recovery $recovery
         */
@@ -103,12 +99,12 @@ class KeyManager {
                Crypt $crypt,
                IConfig $config,
                IUserSession $userSession,
-               ISession $session,
+               Session $session,
                ILogger $log,
                Recovery $recovery
        ) {
 
-               self::$session = $session;
+               $this->session = $session;
                $this->keyStorage = $keyStorage;
                $this->crypt = $crypt;
                $this->config = $config;
@@ -271,7 +267,6 @@ class KeyManager {
         *
         * @param string $uid userid
         * @param string $passPhrase users password
-        * @return ISession
         */
        public function init($uid, $passPhrase) {
                try {
@@ -284,11 +279,8 @@ class KeyManager {
                        return false;
                }
 
-
-               self::$session->set('privateKey', $privateKey);
-               self::$session->set('initStatus', true);
-
-               return self::$session;
+               $this->session->setPrivateKey($privateKey);
+               $this->session->setStatus(Session::INIT_SUCCESSFUL);
        }
 
        /**
@@ -316,7 +308,7 @@ class KeyManager {
                $encryptedFileKey = $this->keyStorage->getFileKey($path,
                        $this->fileKeyId);
                $shareKey = $this->getShareKey($path, $uid);
-               $privateKey = self::$session->get('privateKey');
+               $privateKey = $this->session->getPrivateKey();
 
                if ($encryptedFileKey && $shareKey && $privateKey) {
                        $key = $this->crypt->multiKeyDecrypt($encryptedFileKey,
@@ -348,7 +340,7 @@ class KeyManager {
        public function setPassphrase($params, IUserSession $user, Util $util) {
 
                // Get existing decrypted private key
-               $privateKey = self::$session->get('privateKey');
+               $privateKey = $this->session->getPrivateKey();
 
                if ($params['uid'] === $user->getUser()->getUID() && $privateKey) {
 
diff --git a/apps/encryption/lib/session.php b/apps/encryption/lib/session.php
new file mode 100644 (file)
index 0000000..e049a8f
--- /dev/null
@@ -0,0 +1,103 @@
+<?php
+
+/**
+ * ownCloud
+ *
+ * @copyright (C) 2015 ownCloud, Inc.
+ *
+ * @author Bjoern Schiessle <schiessle@owncloud.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\Encryption;
+
+use \OCP\ISession;
+
+class Session {
+
+       /** @var ISession */
+       protected $session;
+
+       const NOT_INITIALIZED = '0';
+       const INIT_EXECUTED = '1';
+       const INIT_SUCCESSFUL = '2';
+
+       public function __construct(ISession $session) {
+               $this->session = $session;
+       }
+
+       /**
+        * Sets status of encryption app
+        *
+        * @param string $status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
+        */
+       public function setStatus($status) {
+               $this->session->set('encryptionInitialized', $status);
+       }
+
+       /**
+        * Gets status if we already tried to initialize the encryption app
+        *
+        * @return string init status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
+        */
+       public function getStatus() {
+               $status = $this->session->get('encryptionInitialized');
+               if (is_null($status)) {
+                       $status = self::NOT_INITIALIZED;
+               }
+
+               return $status;
+       }
+
+       /**
+        * Gets user or public share private key from session
+        *
+        * @return string $privateKey The user's plaintext private key
+        * @throws Exceptions\PrivateKeyMissingException
+        */
+       public function getPrivateKey() {
+               $key = $this->session->get('privateKey');
+               if (is_null($key)) {
+                       throw new Exceptions\PrivateKeyMissingException('no private key stored in session');
+               }
+               return $key;
+       }
+
+       /**
+        * check if private key is set
+        *
+        * @return boolean
+        */
+       public function isPrivateKeySet() {
+               $key = $this->session->get('privateKey');
+               if (is_null($key)) {
+                       return false;
+               }
+
+               return true;
+       }
+
+       /**
+        * Sets user private key to session
+        *
+        * @param string $key users private key
+        *
+        * @note this should only be set on login
+        */
+       public function setPrivateKey($key) {
+               $this->session->set('privateKey', $key);
+       }
+
+}
\ No newline at end of file
index 540897b829d1c01496c36b3b5487e9752ca432c7..83594b8467e52eeaaaec3e9a5d877a998cc62f64 100644 (file)
@@ -9,6 +9,8 @@
 // Add CSS stylesheet
 \OC_Util::addStyle('encryption', 'settings-personal');
 
+$session = new \OCA\Encryption\Session(\OC::$server->getSession());
+
 $tmpl = new OCP\Template('encryption', 'settings-personal');
 $crypt = new \OCA\Encryption\Crypto\Crypt(
        \OC::$server->getLogger(),
@@ -19,8 +21,8 @@ $keymanager = new \OCA\Encryption\KeyManager(
        $crypt,
        \OC::$server->getConfig(),
        \OC::$server->getUserSession(),
-       \OC::$server->getSession(),
-       \OC::$server->getLogger(),);
+       $session,
+       \OC::$server->getLogger());
 
 $user = \OCP\User::getUser();
 
@@ -29,11 +31,9 @@ $view = new \OC\Files\View('/');
 $util = new \OCA\Encryption\Util(
        new \OC\Files\View(), $crypt, $keymanager, \OC::$server->getLogger(), \OC::$server->getUserSession(), \OC::$server->getConfig());
 
-$session = \OC::$server->getSession();
-
-$privateKeySet = $session->get('privateKey') !== false;
+$privateKeySet = $session->isPrivateKeySet();
 // did we tried to initialize the keys for this session?
-$initialized = $session->getInitialized();
+$initialized = $session->getStatus();
 
 $recoveryAdminEnabled = \OC::$server->getConfig()->getAppValue('encryption', 'recoveryAdminEnabled');
 $recoveryEnabledForUser = $util->recoveryEnabledForUser();
diff --git a/lib/private/encryption/exceptions/privatekeymissingexception.php b/lib/private/encryption/exceptions/privatekeymissingexception.php
deleted file mode 100644 (file)
index 878b83d..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-<?php
-  /**
- * @author Clark Tomlinson  <clark@owncloud.com>
- * @since 2/25/15, 9:39 AM
- * @copyright Copyright (c) 2015, ownCloud, Inc.
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program.  If not, see <http://www.gnu.org/licenses/>
- *
- */
-
-
-namespace OC\Encryption\Exceptions;
-
-
-class PrivateKeyMissingException extends GenericEncryptionException{
-
-}