From: Bjoern Schiessle Date: Tue, 31 Mar 2015 09:15:47 +0000 (+0200) Subject: add session class to handle all session operations X-Git-Tag: v8.1.0alpha1~78^2~76 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0f28d538a0af769220bcba120f0a7340dd0aba5f;p=nextcloud-server.git add session class to handle all session operations --- diff --git a/apps/encryption/appinfo/application.php b/apps/encryption/appinfo/application.php index f9b7a1c60da..21d7f3e737f 100644 --- a/apps/encryption/appinfo/application.php +++ b/apps/encryption/appinfo/application.php @@ -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') ); diff --git a/apps/encryption/hooks/userhooks.php b/apps/encryption/hooks/userhooks.php index 1629aca0f55..27780cccfbf 100644 --- a/apps/encryption/hooks/userhooks.php +++ b/apps/encryption/hooks/userhooks.php @@ -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 index 00000000000..e06940f7ac8 --- /dev/null +++ b/apps/encryption/lib/exceptions/privatekeymissingexception.php @@ -0,0 +1,28 @@ + + * @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 + * + */ + + +namespace OCA\Encryption\Exceptions; + + +class PrivateKeyMissingException extends GenericEncryptionException{ + +} diff --git a/apps/encryption/lib/keymanager.php b/apps/encryption/lib/keymanager.php index cd983be17f9..6c5f2348027 100644 --- a/apps/encryption/lib/keymanager.php +++ b/apps/encryption/lib/keymanager.php @@ -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 index 00000000000..e049a8fe403 --- /dev/null +++ b/apps/encryption/lib/session.php @@ -0,0 +1,103 @@ + + * + * 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 . + */ + +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 diff --git a/apps/encryption/settings/settings-personal.php b/apps/encryption/settings/settings-personal.php index 540897b829d..83594b8467e 100644 --- a/apps/encryption/settings/settings-personal.php +++ b/apps/encryption/settings/settings-personal.php @@ -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 index 878b83d240c..00000000000 --- a/lib/private/encryption/exceptions/privatekeymissingexception.php +++ /dev/null @@ -1,28 +0,0 @@ - - * @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 - * - */ - - -namespace OC\Encryption\Exceptions; - - -class PrivateKeyMissingException extends GenericEncryptionException{ - -}