Minor code tidy up

This commit is contained in:
Andrew Millington 2018-02-28 20:33:19 +00:00
parent c9b07f386c
commit a56acc8dd0
5 changed files with 25 additions and 17 deletions

View File

@ -9,6 +9,7 @@
namespace League\OAuth2\Server; namespace League\OAuth2\Server;
use Defuse\Crypto\Key;
use League\Event\EmitterAwareInterface; use League\Event\EmitterAwareInterface;
use League\Event\EmitterAwareTrait; use League\Event\EmitterAwareTrait;
use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Exception\OAuthServerException;
@ -68,7 +69,7 @@ class AuthorizationServer implements EmitterAwareInterface
private $scopeRepository; private $scopeRepository;
/** /**
* @var string|\Defuse\Crypto\Key * @var string|Key
*/ */
private $encryptionKey; private $encryptionKey;
@ -84,7 +85,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|\Defuse\Crypto\Key $encryptionKey * @param string|Key $encryptionKey
* @param null|ResponseTypeInterface $responseType * @param null|ResponseTypeInterface $responseType
*/ */
public function __construct( public function __construct(

View File

@ -35,9 +35,9 @@ trait CryptTrait
try { try {
if ($this->encryptionKey instanceof Key) { if ($this->encryptionKey instanceof Key) {
return Crypto::encrypt($unencryptedData, $this->encryptionKey); return Crypto::encrypt($unencryptedData, $this->encryptionKey);
} else {
return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
} }
return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
} catch (\Exception $e) { } catch (\Exception $e) {
throw new \LogicException($e->getMessage()); throw new \LogicException($e->getMessage());
} }
@ -57,9 +57,9 @@ trait CryptTrait
try { try {
if ($this->encryptionKey instanceof Key) { if ($this->encryptionKey instanceof Key) {
return Crypto::decrypt($encryptedData, $this->encryptionKey); return Crypto::decrypt($encryptedData, $this->encryptionKey);
} else {
return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
} }
return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
} catch (\Exception $e) { } catch (\Exception $e) {
throw new \LogicException($e->getMessage()); throw new \LogicException($e->getMessage());
} }

View File

@ -11,6 +11,7 @@
namespace League\OAuth2\Server\Grant; namespace League\OAuth2\Server\Grant;
use Defuse\Crypto\Key;
use League\Event\EmitterAwareInterface; use League\Event\EmitterAwareInterface;
use League\OAuth2\Server\CryptKey; use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
@ -136,7 +137,7 @@ interface GrantTypeInterface extends EmitterAwareInterface
/** /**
* Set the encryption key * Set the encryption key
* *
* @param string|\Defuse\Crypto\Key|null $key * @param string|Key|null $key
*/ */
public function setEncryptionKey($key = null); public function setEncryptionKey($key = null);
} }

View File

@ -11,6 +11,7 @@
namespace League\OAuth2\Server\ResponseTypes; namespace League\OAuth2\Server\ResponseTypes;
use Defuse\Crypto\Key;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface; use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
@ -37,7 +38,7 @@ interface ResponseTypeInterface
/** /**
* Set the encryption key * Set the encryption key
* *
* @param string|\Defuse\Crypto\Key|null $key * @param string|Key|null $key
*/ */
public function setEncryptionKey($key = null); public function setEncryptionKey($key = null);
} }

View File

@ -8,27 +8,32 @@ use PHPUnit\Framework\TestCase;
class CryptTraitTest extends TestCase class CryptTraitTest extends TestCase
{ {
protected $cryptStub;
protected function setUp()
{
$this->cryptStub = new CryptTraitStub();
}
public function testEncryptDecryptWithPassword() public function testEncryptDecryptWithPassword()
{ {
$cryptStub = new CryptTraitStub(); $this->cryptStub->setEncryptionKey(base64_encode(random_bytes(36)));
$cryptStub->setEncryptionKey(base64_encode(random_bytes(36)));
return $this->encryptDecrypt($cryptStub); $this->encryptDecrypt();
} }
public function testEncryptDecryptWithKey() public function testEncryptDecryptWithKey()
{ {
$cryptStub = new CryptTraitStub(); $this->cryptStub->setEncryptionKey(Key::createNewRandomKey());
$cryptStub->setEncryptionKey(Key::createNewRandomKey());
return $this->encryptDecrypt($cryptStub); $this->encryptDecrypt();
} }
protected function encryptDecrypt(CryptTraitStub $cryptStub) private function encryptDecrypt()
{ {
$payload = 'alex loves whisky'; $payload = 'alex loves whisky';
$encrypted = $cryptStub->doEncrypt($payload); $encrypted = $this->cryptStub->doEncrypt($payload);
$plainText = $cryptStub->doDecrypt($encrypted); $plainText = $this->cryptStub->doDecrypt($encrypted);
$this->assertNotEquals($payload, $encrypted); $this->assertNotEquals($payload, $encrypted);
$this->assertEquals($payload, $plainText); $this->assertEquals($payload, $plainText);