Allow CryptTrait to accept a \Defuse\Crypto\Key as encryption key #812

This commit is contained in:
SunMar 2017-11-20 07:42:09 +01:00
parent 57ca83a8ba
commit 292272d128
5 changed files with 34 additions and 18 deletions

View File

@ -67,7 +67,7 @@ class AuthorizationServer implements EmitterAwareInterface
private $scopeRepository;
/**
* @var string
* @var string|\Defuse\Crypto\Key
*/
private $encryptionKey;
@ -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(

View File

@ -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;
@ -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());
}
@ -50,7 +55,11 @@ trait CryptTrait
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());
}
@ -59,7 +68,7 @@ trait CryptTrait
/**
* Set the encryption key
*
* @param string $key
* @param string|Key $key
*/
public function setEncryptionKey($key = null)
{

View File

@ -136,7 +136,7 @@ interface GrantTypeInterface extends EmitterAwareInterface
/**
* Set the encryption key
*
* @param string|null $key
* @param string|\Defuse\Crypto\Key|null $key
*/
public function setEncryptionKey($key = null);
}

View File

@ -37,7 +37,7 @@ interface ResponseTypeInterface
/**
* Set the encryption key
*
* @param string|null $key
* @param string|\Defuse\Crypto\Key|null $key
*/
public function setEncryptionKey($key = null);
}

View File

@ -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);