Skip to content

Commit b99c621

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
0 parents  commit b99c621

File tree

11 files changed

+520
-0
lines changed

11 files changed

+520
-0
lines changed

.github/PULL_REQUEST_TEMPLATE.md

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.

.gitignore

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

.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

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+

EncryptedCachePool.php

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 <[email protected]>, Tobias Nyholm <[email protected]>
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 <[email protected]>
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+
}

EncryptedItemDecorator.php

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 <[email protected]>, Tobias Nyholm <[email protected]>
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 <[email protected]>
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+
}

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 - 2016 Aaron Scherer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

0 commit comments

Comments
 (0)