Skip to content

Commit

Permalink
Merge pull request #814 from SunMar/master
Browse files Browse the repository at this point in the history
Allow CryptTrait to accept a \Defuse\Crypto\Key as encryption key #812
  • Loading branch information
Sephster authored Feb 28, 2018
2 parents e3266cb + bec0de1 commit cc19da5
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 13 deletions.
1 change: 0 additions & 1 deletion .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ enabled:
- phpdoc_inline_tag
- phpdoc_no_access
- phpdoc_no_simplified_null_return
- phpdoc_order
- phpdoc_property
- phpdoc_scalar
- phpdoc_separation
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added
- Added event emitters for issued access and refresh tokens (PR #860)
- Can now use Defuse\Crypto\Key for encryption/decryption of keys which is faster than the Cryto class (PR #812)

### Removed
- Remove paragone/random_compat from dependencies
Expand Down
5 changes: 3 additions & 2 deletions src/AuthorizationServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace League\OAuth2\Server;

use Defuse\Crypto\Key;
use League\Event\EmitterAwareInterface;
use League\Event\EmitterAwareTrait;
use League\OAuth2\Server\Exception\OAuthServerException;
Expand Down Expand Up @@ -68,7 +69,7 @@ class AuthorizationServer implements EmitterAwareInterface
private $scopeRepository;

/**
* @var string
* @var string|Key
*/
private $encryptionKey;

Expand All @@ -84,7 +85,7 @@ class AuthorizationServer implements EmitterAwareInterface
* @param AccessTokenRepositoryInterface $accessTokenRepository
* @param ScopeRepositoryInterface $scopeRepository
* @param CryptKey|string $privateKey
* @param string $encryptionKey
* @param string|Key $encryptionKey
* @param null|ResponseTypeInterface $responseType
*/
public function __construct(
Expand Down
13 changes: 11 additions & 2 deletions src/CryptTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
namespace League\OAuth2\Server;

use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;

trait CryptTrait
{
/**
* @var string
* @var string|Key
*/
protected $encryptionKey;

Expand All @@ -32,6 +33,10 @@ trait CryptTrait
protected function encrypt($unencryptedData)
{
try {
if ($this->encryptionKey instanceof Key) {
return Crypto::encrypt($unencryptedData, $this->encryptionKey);
}

return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
} catch (\Exception $e) {
throw new \LogicException($e->getMessage());
Expand All @@ -50,6 +55,10 @@ protected function encrypt($unencryptedData)
protected function decrypt($encryptedData)
{
try {
if ($this->encryptionKey instanceof Key) {
return Crypto::decrypt($encryptedData, $this->encryptionKey);
}

return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
} catch (\Exception $e) {
throw new \LogicException($e->getMessage());
Expand All @@ -59,7 +68,7 @@ protected function decrypt($encryptedData)
/**
* Set the encryption key
*
* @param string $key
* @param string|Key $key
*/
public function setEncryptionKey($key = null)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Grant/GrantTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace League\OAuth2\Server\Grant;

use Defuse\Crypto\Key;
use League\Event\EmitterAwareInterface;
use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
Expand Down Expand Up @@ -136,7 +137,7 @@ public function setPrivateKey(CryptKey $privateKey);
/**
* Set the encryption key
*
* @param string|null $key
* @param string|Key|null $key
*/
public function setEncryptionKey($key = null);
}
3 changes: 2 additions & 1 deletion src/ResponseTypes/ResponseTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace League\OAuth2\Server\ResponseTypes;

use Defuse\Crypto\Key;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -37,7 +38,7 @@ public function generateHttpResponse(ResponseInterface $response);
/**
* Set the encryption key
*
* @param string|null $key
* @param string|Key|null $key
*/
public function setEncryptionKey($key = null);
}
24 changes: 18 additions & 6 deletions tests/Utils/CryptTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,34 @@

namespace LeagueTests\Utils;

use Defuse\Crypto\Key;
use LeagueTests\Stubs\CryptTraitStub;
use PHPUnit\Framework\TestCase;

class CryptTraitTest extends TestCase
{
/**
* @var \LeagueTests\Stubs\CryptTraitStub
*/
protected $cryptStub;

public function setUp()
protected function setUp()
{
$this->cryptStub = new CryptTraitStub;
$this->cryptStub = new CryptTraitStub();
}

public function testEncryptDecrypt()
public function testEncryptDecryptWithPassword()
{
$this->cryptStub->setEncryptionKey(base64_encode(random_bytes(36)));

$this->encryptDecrypt();
}

public function testEncryptDecryptWithKey()
{
$this->cryptStub->setEncryptionKey(Key::createNewRandomKey());

$this->encryptDecrypt();
}

private function encryptDecrypt()
{
$payload = 'alex loves whisky';
$encrypted = $this->cryptStub->doEncrypt($payload);
Expand Down

0 comments on commit cc19da5

Please sign in to comment.