Skip to content

Commit 3e13831

Browse files
prisisNyholm
authored andcommitted
[WIP] Adding encryption decorator (#107)
* Adding encrypted decorator for psr6 * fixed testItemModifiersReturnsStatic * rename classes | added cs fixes | remove old readme lines * Rename folder | fix phpdoc in EncryptedItemDecorator * fix last test * added cs fixes * update php header * added a Changelog * fix format
1 parent c9d6243 commit 3e13831

14 files changed

Lines changed: 531 additions & 1 deletion

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"league/flysystem": "^1.0"
2929
},
3030
"require-dev": {
31+
"defuse/php-encryption": "^2.0",
3132
"phpunit/phpunit": "^4.0 || ^5.1",
3233
"mockery/mockery": "^0.9",
3334
"cache/integration-tests": "^0.11",

src/Adapter/Common/CacheItem.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,19 @@ public function expiresAfter($time)
161161
return $this;
162162
}
163163

164+
/**
165+
* {@inheritdoc}
166+
*/
164167
public function getTags()
165168
{
166169
$this->initialize();
167170

168171
return $this->tags;
169172
}
170173

174+
/**
175+
* {@inheritdoc}
176+
*/
171177
public function addTag($tag)
172178
{
173179
$this->initialize();
@@ -177,6 +183,9 @@ public function addTag($tag)
177183
return $this;
178184
}
179185

186+
/**
187+
* {@inheritdoc}
188+
*/
180189
public function setTags(array $tags)
181190
{
182191
$this->initialize();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is a READ ONLY repository.
2+
3+
Please make your pull request to https://github.com/php-cache/cache
4+
5+
Thank you for contributing.

src/Encryption/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.lock
2+
vendor

src/Encryption/.travis.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: php
2+
sudo: false
3+
4+
matrix:
5+
include:
6+
- php: 7.0
7+
8+
cache:
9+
directories:
10+
- "$HOME/.composer/cache"
11+
12+
install:
13+
- composer update
14+
15+
script:
16+
- ./vendor/bin/phpunit --coverage-clover=coverage.xml
17+
18+
after_success:
19+
- pip install --user codecov && codecov
20+
21+
notifications:
22+
email: false

src/Encryption/Changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log
2+
3+
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
4+
5+
## UNRELEASED
6+
7+
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015-2016 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
13+
namespace Cache\Encryption;
14+
15+
use Defuse\Crypto\Key;
16+
use Psr\Cache\CacheItemInterface;
17+
use Psr\Cache\CacheItemPoolInterface;
18+
19+
/**
20+
* Wrapps a CacheItemInterface with EncryptedItemDecorator.
21+
*
22+
* @author Daniel Bannert <d.bannert@anolilab.de>
23+
*/
24+
class EncryptedCachePool implements CacheItemPoolInterface
25+
{
26+
/**
27+
* @type CacheItemPoolInterface
28+
*/
29+
private $cachePool;
30+
31+
/**
32+
* @type Key
33+
*/
34+
private $key;
35+
36+
/**
37+
* @param CacheItemPoolInterface $cachePool
38+
* @param Key $key
39+
*/
40+
public function __construct(CacheItemPoolInterface $cachePool, Key $key)
41+
{
42+
$this->cachePool = $cachePool;
43+
$this->key = $key;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function getItem($key)
50+
{
51+
$item = $this->cachePool->getItem($key);
52+
53+
if (!($item instanceof EncryptedItemDecorator)) {
54+
return new EncryptedItemDecorator($item, $this->key);
55+
}
56+
57+
return $item;
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function getItems(array $keys = [])
64+
{
65+
return array_map(function (CacheItemInterface $inner) {
66+
if (!($inner instanceof EncryptedItemDecorator)) {
67+
return new EncryptedItemDecorator($inner, $this->key);
68+
}
69+
70+
return $inner;
71+
}, $this->cachePool->getItems($keys));
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
public function hasItem($key)
78+
{
79+
return $this->cachePool->hasItem($key);
80+
}
81+
82+
/**
83+
* {@inheritdoc}
84+
*/
85+
public function clear()
86+
{
87+
return $this->cachePool->clear();
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
public function deleteItem($key)
94+
{
95+
return $this->cachePool->deleteItem($key);
96+
}
97+
98+
/**
99+
* {@inheritdoc}
100+
*/
101+
public function deleteItems(array $keys)
102+
{
103+
return $this->cachePool->deleteItems($keys);
104+
}
105+
106+
/**
107+
* {@inheritdoc}
108+
*/
109+
public function save(CacheItemInterface $item)
110+
{
111+
if (!($item instanceof EncryptedItemDecorator)) {
112+
$item = new EncryptedItemDecorator($item, $this->key);
113+
}
114+
115+
return $this->cachePool->save($item);
116+
}
117+
118+
/**
119+
* {@inheritdoc}
120+
*/
121+
public function saveDeferred(CacheItemInterface $item)
122+
{
123+
if (!($item instanceof EncryptedItemDecorator)) {
124+
$item = new EncryptedItemDecorator($item, $this->key);
125+
}
126+
127+
return $this->cachePool->saveDeferred($item);
128+
}
129+
130+
/**
131+
* {@inheritdoc}
132+
*/
133+
public function commit()
134+
{
135+
return $this->cachePool->commit();
136+
}
137+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015-2016 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
13+
namespace Cache\Encryption;
14+
15+
use Cache\Adapter\Common\HasExpirationDateInterface;
16+
use Cache\Taggable\TaggableItemInterface;
17+
use Defuse\Crypto\Crypto;
18+
use Defuse\Crypto\Key;
19+
use Psr\Cache\CacheItemInterface;
20+
21+
/**
22+
* Encrypt and Decrypt all the stored items.
23+
*
24+
* @author Daniel Bannert <d.bannert@anolilab.de>
25+
*/
26+
class EncryptedItemDecorator implements CacheItemInterface, HasExpirationDateInterface, TaggableItemInterface
27+
{
28+
/**
29+
* @type CacheItemInterface
30+
*/
31+
private $cacheItem;
32+
33+
/**
34+
* @type Key
35+
*/
36+
private $key;
37+
38+
/**
39+
* @param CacheItemInterface $cacheItem
40+
* @param Key $key
41+
*/
42+
public function __construct(CacheItemInterface $cacheItem, Key $key)
43+
{
44+
$this->cacheItem = $cacheItem;
45+
$this->key = $key;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function getKey()
52+
{
53+
return $this->cacheItem->getKey();
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public function set($value)
60+
{
61+
$type = gettype($value);
62+
63+
if ($type === 'object') {
64+
$value = serialize($value);
65+
}
66+
67+
$json = json_encode(['type' => $type, 'value' => $value]);
68+
69+
$this->cacheItem->set(Crypto::encrypt($json, $this->key));
70+
71+
return $this;
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
public function get()
78+
{
79+
if (!$this->isHit()) {
80+
return;
81+
}
82+
83+
$item = json_decode(Crypto::decrypt($this->cacheItem->get(), $this->key), true);
84+
85+
return $this->transform($item);
86+
}
87+
88+
/**
89+
* {@inheritdoc}
90+
*/
91+
public function isHit()
92+
{
93+
return $this->cacheItem->isHit();
94+
}
95+
96+
/**
97+
* {@inheritdoc}
98+
*/
99+
public function getExpirationDate()
100+
{
101+
return $this->cacheItem->getExpirationDate();
102+
}
103+
104+
/**
105+
* {@inheritdoc}
106+
*/
107+
public function expiresAt($expiration)
108+
{
109+
$this->cacheItem->expiresAt($expiration);
110+
111+
return $this;
112+
}
113+
114+
/**
115+
* {@inheritdoc}
116+
*/
117+
public function expiresAfter($time)
118+
{
119+
$this->cacheItem->expiresAfter($time);
120+
121+
return $this;
122+
}
123+
124+
/**
125+
* {@inheritdoc}
126+
*/
127+
public function getTags()
128+
{
129+
return $this->cacheItem->getTags();
130+
}
131+
132+
/**
133+
* {@inheritdoc}
134+
*/
135+
public function addTag($tag)
136+
{
137+
$this->cacheItem->addTag($tag);
138+
139+
return $this;
140+
}
141+
142+
/**
143+
* {@inheritdoc}
144+
*/
145+
public function setTags(array $tags)
146+
{
147+
$this->cacheItem->setTags($tags);
148+
149+
return $this;
150+
}
151+
152+
/**
153+
* Creating a copy of the orginal CacheItemInterface object.
154+
*/
155+
public function __clone()
156+
{
157+
$this->cacheItem = clone $this->cacheItem;
158+
}
159+
160+
/**
161+
* Transfrom value back to it orginal type.
162+
*
163+
* @param array $item
164+
*
165+
* @return mixed
166+
*/
167+
private function transform(array $item)
168+
{
169+
if ($item['type'] === 'object') {
170+
return unserialize($item['value']);
171+
}
172+
173+
$value = $item['value'];
174+
175+
settype($value, $item['type']);
176+
177+
return $value;
178+
}
179+
}

0 commit comments

Comments
 (0)