mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-11-01 08:23:03 +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;
|
||||
|
||||
/**
|
||||
* @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(
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user