Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Session Encryption: Adding integration tests and fixing a few bugs.

  • Loading branch information...
commit 3e15aedd7ba13b46d6beb822ebdc0fb68e5e4c7d 1 parent 8f20cb5
@daschl daschl authored
View
11 storage/session/strategy/Encrypt.php
@@ -75,15 +75,16 @@ public function read($data, array $options = array()) {
$class = $options['class'];
$encrypted = $class::read(null, array('strategies' => false));
+ $key = isset($options['key']) ? $options['key'] : null;
if (!isset($encrypted['__encrypted']) || !$encrypted['__encrypted']) {
- return isset($encrypted[$data]) ? $encrypted[$data] : null;
+ return isset($encrypted[$key]) ? $encrypted[$key] : null;
}
$current = $this->_decrypt($encrypted['__encrypted']);
- if($data) {
- return isset($current[$data]) ? $current[$data] : null;
+ if($key) {
+ return isset($current[$key]) ? $current[$key] : null;
} else {
return $current;
}
@@ -99,7 +100,7 @@ public function read($data, array $options = array()) {
public function write($data, array $options = array()) {
$class = $options['class'];
- $futureData = $this->read(null, $options) ?: array();
+ $futureData = $this->read(null, array('key' => null) + $options) ?: array();
$futureData = array($options['key'] => $data) + $futureData;
$payload = empty($futureData) ? null : $this->_encrypt($futureData);
@@ -118,7 +119,7 @@ public function write($data, array $options = array()) {
public function delete($data, array $options = array()) {
$class = $options['class'];
- $futureData = $this->read(null, $options) ?: array();
+ $futureData = $this->read(null, array('key' => null) + $options) ?: array();
unset($futureData[$options['key']]);
$payload = empty($futureData) ? null : $this->_encrypt($futureData);
View
4 tests/cases/storage/session/strategy/EncryptTest.php
@@ -68,7 +68,7 @@ public function testRead() {
$this->assertTrue($result);
$cookie = MockCookieSession::data();
- $result = $encrypt->read($key, array('class' => $this->mock));
+ $result = $encrypt->read($key, array('class' => $this->mock, 'key' => $key));
$this->assertEqual($value, $result);
$this->assertNotEqual($cookie['__encrypted'], $result);
@@ -84,7 +84,7 @@ public function testDelete() {
$this->assertTrue($result);
$cookie = MockCookieSession::data();
- $result = $encrypt->read($key, array('class' => $this->mock));
+ $result = $encrypt->read($key, array('class' => $this->mock, 'key' => $key));
$this->assertEqual($value, $result);
View
37 tests/integration/storage/SessionTest.php
@@ -160,6 +160,43 @@ public function testHmacStrategyWithPhpAdapter() {
Session::read($key, $config);
$_SESSION = $cache;
}
+
+ public function testEncryptStrategyWithPhpAdapter() {
+ $config = array('name' => 'encryptInt');
+
+ Session::config(array(
+ $config['name'] => array(
+ 'adapter' => 'Php',
+ 'strategies' => array(
+ 'Encrypt' => array(
+ 'secret' => 's3cr3t'
+ )
+ )
+ )
+ ));
+
+ Session::clear($config);
+
+ $key = 'test';
+ $value = 'value';
+
+ $this->assertTrue(Session::write($key, $value, $config));
+ $this->assertEqual($value, Session::read($key, $config));
+ $this->assertTrue(Session::delete($key, $config));
+ $this->assertNull(Session::read($key, $config));
+
+ Session::clear($config);
+
+ $this->assertTrue(Session::write('foo', 'bar', $config));
+ $this->assertEqual('bar', Session::read('foo', $config));
+ $this->assertTrue(Session::write('foo', 'bar1', $config));
+ $this->assertEqual('bar1', Session::read('foo', $config));
+
+ Session::clear($config);
+
+ $this->assertTrue(Session::write($key, $value, $config));
+ $this->assertEqual($value, Session::read($key, $config));
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.