Skip to content

Commit

Permalink
Allow CryptTrait to accept a \Defuse\Crypto\Key as encryption key the…
Browse files Browse the repository at this point in the history
  • Loading branch information
SunMar authored and EricTendian committed Sep 10, 2018
1 parent a0cabb5 commit d224076
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/AuthorizationServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AuthorizationServer implements EmitterAwareInterface
private $scopeRepository;

/**
* @var string
* @var string|\Defuse\Crypto\Key
*/
private $encryptionKey;

Expand All @@ -83,7 +83,7 @@ class AuthorizationServer implements EmitterAwareInterface
* @param AccessTokenRepositoryInterface $accessTokenRepository
* @param ScopeRepositoryInterface $scopeRepository
* @param CryptKey|string $privateKey
* @param string $encryptionKey
* @param string|\Defuse\Crypto\Key $encryptionKey
* @param null|ResponseTypeInterface $responseType
*/
public function __construct(
Expand Down
17 changes: 13 additions & 4 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,7 +33,11 @@ trait CryptTrait
protected function encrypt($unencryptedData)
{
try {
return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
if($this->encryptionKey instanceof Key) {
return Crypto::encrypt($unencryptedData, $this->encryptionKey);
} else {
return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
}
} catch (\Exception $e) {
throw new \LogicException($e->getMessage());
}
Expand All @@ -50,7 +55,11 @@ protected function encrypt($unencryptedData)
protected function decrypt($encryptedData)
{
try {
return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
if($this->encryptionKey instanceof Key) {
return Crypto::decrypt($encryptedData, $this->encryptionKey);
} else {
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
2 changes: 1 addition & 1 deletion src/Grant/GrantTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function setPrivateKey(CryptKey $privateKey);
/**
* Set the encryption key
*
* @param string|null $key
* @param string|\Defuse\Crypto\Key|null $key
*/
public function setEncryptionKey($key = null);
}
2 changes: 1 addition & 1 deletion src/ResponseTypes/ResponseTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function generateHttpResponse(ResponseInterface $response);
/**
* Set the encryption key
*
* @param string|null $key
* @param string|\Defuse\Crypto\Key|null $key
*/
public function setEncryptionKey($key = null);
}
27 changes: 17 additions & 10 deletions tests/CryptTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@

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()
public function testEncryptDecryptWithPassword()
{
$this->cryptStub = new CryptTraitStub;
$cryptStub = new CryptTraitStub();
$cryptStub->setEncryptionKey(base64_encode(random_bytes(36)));

return $this->encryptDecrypt($cryptStub);
}

public function testEncryptDecrypt()
public function testEncryptDecryptWithKey()
{
$cryptStub = new CryptTraitStub();
$cryptStub->setEncryptionKey(Key::createNewRandomKey());

return $this->encryptDecrypt($cryptStub);
}

protected function encryptDecrypt(CryptTraitStub $cryptStub) {

$payload = 'alex loves whisky';
$encrypted = $this->cryptStub->doEncrypt($payload);
$plainText = $this->cryptStub->doDecrypt($encrypted);
$encrypted = $cryptStub->doEncrypt($payload);
$plainText = $cryptStub->doDecrypt($encrypted);

$this->assertNotEquals($payload, $encrypted);
$this->assertEquals($payload, $plainText);
Expand Down

0 comments on commit d224076

Please sign in to comment.