mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-22 21:19:46 +05:30
Allow CryptTrait to accept a \Defuse\Crypto\Key as encryption key #812
This commit is contained in:
parent
57ca83a8ba
commit
292272d128
@ -67,7 +67,7 @@ class AuthorizationServer implements EmitterAwareInterface
|
|||||||
private $scopeRepository;
|
private $scopeRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string|\Defuse\Crypto\Key
|
||||||
*/
|
*/
|
||||||
private $encryptionKey;
|
private $encryptionKey;
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ class AuthorizationServer implements EmitterAwareInterface
|
|||||||
* @param AccessTokenRepositoryInterface $accessTokenRepository
|
* @param AccessTokenRepositoryInterface $accessTokenRepository
|
||||||
* @param ScopeRepositoryInterface $scopeRepository
|
* @param ScopeRepositoryInterface $scopeRepository
|
||||||
* @param CryptKey|string $privateKey
|
* @param CryptKey|string $privateKey
|
||||||
* @param string $encryptionKey
|
* @param string|\Defuse\Crypto\Key $encryptionKey
|
||||||
* @param null|ResponseTypeInterface $responseType
|
* @param null|ResponseTypeInterface $responseType
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
@ -12,11 +12,12 @@
|
|||||||
namespace League\OAuth2\Server;
|
namespace League\OAuth2\Server;
|
||||||
|
|
||||||
use Defuse\Crypto\Crypto;
|
use Defuse\Crypto\Crypto;
|
||||||
|
use Defuse\Crypto\Key;
|
||||||
|
|
||||||
trait CryptTrait
|
trait CryptTrait
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string|Key
|
||||||
*/
|
*/
|
||||||
protected $encryptionKey;
|
protected $encryptionKey;
|
||||||
|
|
||||||
@ -32,7 +33,11 @@ trait CryptTrait
|
|||||||
protected function encrypt($unencryptedData)
|
protected function encrypt($unencryptedData)
|
||||||
{
|
{
|
||||||
try {
|
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) {
|
} catch (\Exception $e) {
|
||||||
throw new \LogicException($e->getMessage());
|
throw new \LogicException($e->getMessage());
|
||||||
}
|
}
|
||||||
@ -50,7 +55,11 @@ trait CryptTrait
|
|||||||
protected function decrypt($encryptedData)
|
protected function decrypt($encryptedData)
|
||||||
{
|
{
|
||||||
try {
|
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) {
|
} catch (\Exception $e) {
|
||||||
throw new \LogicException($e->getMessage());
|
throw new \LogicException($e->getMessage());
|
||||||
}
|
}
|
||||||
@ -59,7 +68,7 @@ trait CryptTrait
|
|||||||
/**
|
/**
|
||||||
* Set the encryption key
|
* Set the encryption key
|
||||||
*
|
*
|
||||||
* @param string $key
|
* @param string|Key $key
|
||||||
*/
|
*/
|
||||||
public function setEncryptionKey($key = null)
|
public function setEncryptionKey($key = null)
|
||||||
{
|
{
|
||||||
|
@ -136,7 +136,7 @@ interface GrantTypeInterface extends EmitterAwareInterface
|
|||||||
/**
|
/**
|
||||||
* Set the encryption key
|
* Set the encryption key
|
||||||
*
|
*
|
||||||
* @param string|null $key
|
* @param string|\Defuse\Crypto\Key|null $key
|
||||||
*/
|
*/
|
||||||
public function setEncryptionKey($key = null);
|
public function setEncryptionKey($key = null);
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ interface ResponseTypeInterface
|
|||||||
/**
|
/**
|
||||||
* Set the encryption key
|
* Set the encryption key
|
||||||
*
|
*
|
||||||
* @param string|null $key
|
* @param string|\Defuse\Crypto\Key|null $key
|
||||||
*/
|
*/
|
||||||
public function setEncryptionKey($key = null);
|
public function setEncryptionKey($key = null);
|
||||||
}
|
}
|
||||||
|
@ -2,26 +2,33 @@
|
|||||||
|
|
||||||
namespace LeagueTests\Utils;
|
namespace LeagueTests\Utils;
|
||||||
|
|
||||||
|
use Defuse\Crypto\Key;
|
||||||
use LeagueTests\Stubs\CryptTraitStub;
|
use LeagueTests\Stubs\CryptTraitStub;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class CryptTraitTest extends TestCase
|
class CryptTraitTest extends TestCase
|
||||||
{
|
{
|
||||||
/**
|
public function testEncryptDecryptWithPassword()
|
||||||
* @var \LeagueTests\Stubs\CryptTraitStub
|
|
||||||
*/
|
|
||||||
protected $cryptStub;
|
|
||||||
|
|
||||||
public function setUp()
|
|
||||||
{
|
{
|
||||||
$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';
|
$payload = 'alex loves whisky';
|
||||||
$encrypted = $this->cryptStub->doEncrypt($payload);
|
$encrypted = $cryptStub->doEncrypt($payload);
|
||||||
$plainText = $this->cryptStub->doDecrypt($encrypted);
|
$plainText = $cryptStub->doDecrypt($encrypted);
|
||||||
|
|
||||||
$this->assertNotEquals($payload, $encrypted);
|
$this->assertNotEquals($payload, $encrypted);
|
||||||
$this->assertEquals($payload, $plainText);
|
$this->assertEquals($payload, $plainText);
|
||||||
|
Loading…
Reference in New Issue
Block a user