-
Notifications
You must be signed in to change notification settings - Fork 82
Adding encryption decorator #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Nyholm
merged 15 commits into
php-cache:master
from
PrisisForks:add-encrypting-decorator
Nov 9, 2016
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4ae764f
Merge pull request #4 from php-cache/master
prisis a7231cf
Merge pull request #5 from php-cache/master
prisis c7c2d7a
Merge pull request #6 from php-cache/master
prisis 5904102
Merge pull request #7 from php-cache/master
prisis 7f0d0fc
Adding encrypted decorator for psr6
prisis ecb4cbc
fixed testItemModifiersReturnsStatic
prisis 96967dd
rename classes | added cs fixes | remove old readme lines
prisis 22e0115
Rename folder | fix phpdoc in EncryptedItemDecorator
prisis 975d328
fix last test
prisis b951e24
added cs fixes
prisis f5ed8f0
Merge pull request #8 from php-cache/master
prisis 588ca24
Merge branch 'master' into add-encrypting-decorator
prisis e966694
update php header
prisis 27bc164
added a Changelog
prisis 74639aa
fix format
prisis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| This is a READ ONLY repository. | ||
|
|
||
| Please make your pull request to https://github.com/php-cache/cache | ||
|
|
||
| Thank you for contributing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| composer.lock | ||
| vendor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| language: php | ||
| sudo: false | ||
|
|
||
| matrix: | ||
| include: | ||
| - php: 7.0 | ||
|
|
||
| cache: | ||
| directories: | ||
| - "$HOME/.composer/cache" | ||
|
|
||
| install: | ||
| - composer update | ||
|
|
||
| script: | ||
| - ./vendor/bin/phpunit --coverage-clover=coverage.xml | ||
|
|
||
| after_success: | ||
| - pip install --user codecov && codecov | ||
|
|
||
| notifications: | ||
| email: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Change Log | ||
|
|
||
| The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release. | ||
|
|
||
| ## UNRELEASED | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of php-cache organization. | ||
| * | ||
| * (c) 2015-2016 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com> | ||
| * | ||
| * This source file is subject to the MIT license that is bundled | ||
| * with this source code in the file LICENSE. | ||
| */ | ||
|
|
||
|
|
||
| namespace Cache\Encryption; | ||
|
|
||
| use Defuse\Crypto\Key; | ||
| use Psr\Cache\CacheItemInterface; | ||
| use Psr\Cache\CacheItemPoolInterface; | ||
|
|
||
| /** | ||
| * Wrapps a CacheItemInterface with EncryptedItemDecorator. | ||
| * | ||
| * @author Daniel Bannert <d.bannert@anolilab.de> | ||
| */ | ||
| class EncryptedCachePool implements CacheItemPoolInterface | ||
| { | ||
| /** | ||
| * @type CacheItemPoolInterface | ||
| */ | ||
| private $cachePool; | ||
|
|
||
| /** | ||
| * @type Key | ||
| */ | ||
| private $key; | ||
|
|
||
| /** | ||
| * @param CacheItemPoolInterface $cachePool | ||
| * @param Key $key | ||
| */ | ||
| public function __construct(CacheItemPoolInterface $cachePool, Key $key) | ||
| { | ||
| $this->cachePool = $cachePool; | ||
| $this->key = $key; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getItem($key) | ||
| { | ||
| $item = $this->cachePool->getItem($key); | ||
|
|
||
| if (!($item instanceof EncryptedItemDecorator)) { | ||
| return new EncryptedItemDecorator($item, $this->key); | ||
| } | ||
|
|
||
| return $item; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getItems(array $keys = []) | ||
| { | ||
| return array_map(function (CacheItemInterface $inner) { | ||
| if (!($inner instanceof EncryptedItemDecorator)) { | ||
| return new EncryptedItemDecorator($inner, $this->key); | ||
| } | ||
|
|
||
| return $inner; | ||
| }, $this->cachePool->getItems($keys)); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function hasItem($key) | ||
| { | ||
| return $this->cachePool->hasItem($key); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function clear() | ||
| { | ||
| return $this->cachePool->clear(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function deleteItem($key) | ||
| { | ||
| return $this->cachePool->deleteItem($key); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function deleteItems(array $keys) | ||
| { | ||
| return $this->cachePool->deleteItems($keys); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function save(CacheItemInterface $item) | ||
| { | ||
| if (!($item instanceof EncryptedItemDecorator)) { | ||
| $item = new EncryptedItemDecorator($item, $this->key); | ||
| } | ||
|
|
||
| return $this->cachePool->save($item); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function saveDeferred(CacheItemInterface $item) | ||
| { | ||
| if (!($item instanceof EncryptedItemDecorator)) { | ||
| $item = new EncryptedItemDecorator($item, $this->key); | ||
| } | ||
|
|
||
| return $this->cachePool->saveDeferred($item); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function commit() | ||
| { | ||
| return $this->cachePool->commit(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of php-cache organization. | ||
| * | ||
| * (c) 2015-2016 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com> | ||
| * | ||
| * This source file is subject to the MIT license that is bundled | ||
| * with this source code in the file LICENSE. | ||
| */ | ||
|
|
||
|
|
||
| namespace Cache\Encryption; | ||
|
|
||
| use Cache\Adapter\Common\HasExpirationDateInterface; | ||
| use Cache\Taggable\TaggableItemInterface; | ||
| use Defuse\Crypto\Crypto; | ||
| use Defuse\Crypto\Key; | ||
| use Psr\Cache\CacheItemInterface; | ||
|
|
||
| /** | ||
| * Encrypt and Decrypt all the stored items. | ||
| * | ||
| * @author Daniel Bannert <d.bannert@anolilab.de> | ||
| */ | ||
| class EncryptedItemDecorator implements CacheItemInterface, HasExpirationDateInterface, TaggableItemInterface | ||
| { | ||
| /** | ||
| * @type CacheItemInterface | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC these should all be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can fix it with styleci, we use it in all packages @type |
||
| */ | ||
| private $cacheItem; | ||
|
|
||
| /** | ||
| * @type Key | ||
| */ | ||
| private $key; | ||
|
|
||
| /** | ||
| * @param CacheItemInterface $cacheItem | ||
| * @param Key $key | ||
| */ | ||
| public function __construct(CacheItemInterface $cacheItem, Key $key) | ||
| { | ||
| $this->cacheItem = $cacheItem; | ||
| $this->key = $key; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getKey() | ||
| { | ||
| return $this->cacheItem->getKey(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function set($value) | ||
| { | ||
| $type = gettype($value); | ||
|
|
||
| if ($type === 'object') { | ||
| $value = serialize($value); | ||
| } | ||
|
|
||
| $json = json_encode(['type' => $type, 'value' => $value]); | ||
|
|
||
| $this->cacheItem->set(Crypto::encrypt($json, $this->key)); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function get() | ||
| { | ||
| if (!$this->isHit()) { | ||
| return; | ||
| } | ||
|
|
||
| $item = json_decode(Crypto::decrypt($this->cacheItem->get(), $this->key), true); | ||
|
|
||
| return $this->transform($item); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function isHit() | ||
| { | ||
| return $this->cacheItem->isHit(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getExpirationDate() | ||
| { | ||
| return $this->cacheItem->getExpirationDate(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function expiresAt($expiration) | ||
| { | ||
| $this->cacheItem->expiresAt($expiration); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function expiresAfter($time) | ||
| { | ||
| $this->cacheItem->expiresAfter($time); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getTags() | ||
| { | ||
| return $this->cacheItem->getTags(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function addTag($tag) | ||
| { | ||
| $this->cacheItem->addTag($tag); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function setTags(array $tags) | ||
| { | ||
| $this->cacheItem->setTags($tags); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Creating a copy of the orginal CacheItemInterface object. | ||
| */ | ||
| public function __clone() | ||
| { | ||
| $this->cacheItem = clone $this->cacheItem; | ||
| } | ||
|
|
||
| /** | ||
| * Transfrom value back to it orginal type. | ||
| * | ||
| * @param array $item | ||
| * | ||
| * @return mixed | ||
| */ | ||
| private function transform(array $item) | ||
| { | ||
| if ($item['type'] === 'object') { | ||
| return unserialize($item['value']); | ||
| } | ||
|
|
||
| $value = $item['value']; | ||
|
|
||
| settype($value, $item['type']); | ||
|
|
||
| return $value; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the best encryption package for us?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the best i know, you know a other one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea. =)
I trust that you have done some research to find a popular and robust package. And also one that you like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you like you can read https://paragonie.com/blog/2016/05/defuse-security-s-php-encryption-library-version-2-0-0-released and if @paragonie-scott recommend it, so it musst be really secure :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from libsodium, you'll be hard-pressed to do better. @defuse really knocked it out of the park. :)