add session class to handle all session operations

This commit is contained in:
Bjoern Schiessle 2015-03-31 11:15:47 +02:00 committed by Thomas Müller
parent e0ab2c34ea
commit 0f28d538a0
6 changed files with 126 additions and 35 deletions

View 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')
);

View 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;

View File

@ -20,7 +20,7 @@
*/
namespace OC\Encryption\Exceptions;
namespace OCA\Encryption\Exceptions;
class PrivateKeyMissingException extends GenericEncryptionException{

View 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) {

View File

@ -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);
}
}

View 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();