From 4673e7de89f9a221210b16258b6f73a1bb9017d4 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sat, 17 Aug 2019 23:51:23 +0300 Subject: [PATCH 1/2] Abstract CryptKey public methods to the CryptKeyInterface --- src/AuthorizationServer.php | 16 +++++++------- .../BearerTokenValidator.php | 8 +++---- src/CryptKey.php | 16 +++----------- src/CryptKeyInterface.php | 21 +++++++++++++++++++ src/Entities/AccessTokenEntityInterface.php | 4 ++-- src/Entities/Traits/AccessTokenTrait.php | 10 ++++----- src/Grant/AbstractGrant.php | 8 +++---- src/Grant/GrantTypeInterface.php | 6 +++--- src/ResourceServer.php | 6 +++--- src/ResponseTypes/AbstractResponseType.php | 8 +++---- tests/AuthorizationServerTest.php | 3 +-- 11 files changed, 58 insertions(+), 48 deletions(-) create mode 100644 src/CryptKeyInterface.php diff --git a/src/AuthorizationServer.php b/src/AuthorizationServer.php index 8b0b2815..24a5a272 100644 --- a/src/AuthorizationServer.php +++ b/src/AuthorizationServer.php @@ -40,12 +40,12 @@ class AuthorizationServer implements EmitterAwareInterface protected $grantTypeAccessTokenTTL = []; /** - * @var CryptKey + * @var CryptKeyInterface */ protected $privateKey; /** - * @var CryptKey + * @var CryptKeyInterface */ protected $publicKey; @@ -82,12 +82,12 @@ class AuthorizationServer implements EmitterAwareInterface /** * New server instance. * - * @param ClientRepositoryInterface $clientRepository + * @param ClientRepositoryInterface $clientRepository * @param AccessTokenRepositoryInterface $accessTokenRepository - * @param ScopeRepositoryInterface $scopeRepository - * @param CryptKey|string $privateKey - * @param string|Key $encryptionKey - * @param null|ResponseTypeInterface $responseType + * @param ScopeRepositoryInterface $scopeRepository + * @param CryptKeyInterface|string $privateKey + * @param string|Key $encryptionKey + * @param null|ResponseTypeInterface $responseType */ public function __construct( ClientRepositoryInterface $clientRepository, @@ -101,7 +101,7 @@ class AuthorizationServer implements EmitterAwareInterface $this->accessTokenRepository = $accessTokenRepository; $this->scopeRepository = $scopeRepository; - if ($privateKey instanceof CryptKey === false) { + if ($privateKey instanceof CryptKeyInterface === false) { $privateKey = new CryptKey($privateKey); } diff --git a/src/AuthorizationValidators/BearerTokenValidator.php b/src/AuthorizationValidators/BearerTokenValidator.php index 7218f413..2fd087ac 100644 --- a/src/AuthorizationValidators/BearerTokenValidator.php +++ b/src/AuthorizationValidators/BearerTokenValidator.php @@ -14,7 +14,7 @@ use InvalidArgumentException; use Lcobucci\JWT\Parser; use Lcobucci\JWT\Signer\Rsa\Sha256; use Lcobucci\JWT\ValidationData; -use League\OAuth2\Server\CryptKey; +use League\OAuth2\Server\CryptKeyInterface; use League\OAuth2\Server\CryptTrait; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; @@ -31,7 +31,7 @@ class BearerTokenValidator implements AuthorizationValidatorInterface private $accessTokenRepository; /** - * @var CryptKey + * @var CryptKeyInterface */ protected $publicKey; @@ -46,9 +46,9 @@ class BearerTokenValidator implements AuthorizationValidatorInterface /** * Set the public key * - * @param CryptKey $key + * @param CryptKeyInterface $key */ - public function setPublicKey(CryptKey $key) + public function setPublicKey(CryptKeyInterface $key) { $this->publicKey = $key; } diff --git a/src/CryptKey.php b/src/CryptKey.php index 6fc4dff0..0d7a0355 100644 --- a/src/CryptKey.php +++ b/src/CryptKey.php @@ -14,7 +14,7 @@ namespace League\OAuth2\Server; use LogicException; use RuntimeException; -class CryptKey +class CryptKey implements CryptKeyInterface { const RSA_KEY_PATTERN = '/^(-----BEGIN (RSA )?(PUBLIC|PRIVATE) KEY-----)\R.*(-----END (RSA )?(PUBLIC|PRIVATE) KEY-----)\R?$/s'; @@ -101,22 +101,12 @@ class CryptKey return 'file://' . $keyPath; } - /** - * Retrieve key path. - * - * @return string - */ - public function getKeyPath() + public function getKeyPath(): string { return $this->keyPath; } - /** - * Retrieve key pass phrase. - * - * @return null|string - */ - public function getPassPhrase() + public function getPassPhrase(): ?string { return $this->passPhrase; } diff --git a/src/CryptKeyInterface.php b/src/CryptKeyInterface.php new file mode 100644 index 00000000..115f21b2 --- /dev/null +++ b/src/CryptKeyInterface.php @@ -0,0 +1,21 @@ +privateKey = $privateKey; } @@ -36,11 +36,11 @@ trait AccessTokenTrait /** * Generate a JWT from the access token * - * @param CryptKey $privateKey + * @param CryptKeyInterface $privateKey * * @return Token */ - private function convertToJWT(CryptKey $privateKey) + private function convertToJWT(CryptKeyInterface $privateKey) { return (new Builder()) ->setAudience($this->getClient()->getIdentifier()) diff --git a/src/Grant/AbstractGrant.php b/src/Grant/AbstractGrant.php index 0ac9e395..2c342f63 100644 --- a/src/Grant/AbstractGrant.php +++ b/src/Grant/AbstractGrant.php @@ -15,7 +15,7 @@ use DateTimeImmutable; use Error; use Exception; use League\Event\EmitterAwareTrait; -use League\OAuth2\Server\CryptKey; +use League\OAuth2\Server\CryptKeyInterface; use League\OAuth2\Server\CryptTrait; use League\OAuth2\Server\Entities\AccessTokenEntityInterface; use League\OAuth2\Server\Entities\AuthCodeEntityInterface; @@ -83,7 +83,7 @@ abstract class AbstractGrant implements GrantTypeInterface protected $refreshTokenTTL; /** - * @var CryptKey + * @var CryptKeyInterface */ protected $privateKey; @@ -151,9 +151,9 @@ abstract class AbstractGrant implements GrantTypeInterface /** * Set the private key * - * @param CryptKey $key + * @param CryptKeyInterface $key */ - public function setPrivateKey(CryptKey $key) + public function setPrivateKey(CryptKeyInterface $key) { $this->privateKey = $key; } diff --git a/src/Grant/GrantTypeInterface.php b/src/Grant/GrantTypeInterface.php index 41ebeb5f..6bc5329b 100644 --- a/src/Grant/GrantTypeInterface.php +++ b/src/Grant/GrantTypeInterface.php @@ -14,7 +14,7 @@ namespace League\OAuth2\Server\Grant; use DateInterval; use Defuse\Crypto\Key; use League\Event\EmitterAwareInterface; -use League\OAuth2\Server\CryptKey; +use League\OAuth2\Server\CryptKeyInterface; use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; use League\OAuth2\Server\Repositories\ClientRepositoryInterface; use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; @@ -131,9 +131,9 @@ interface GrantTypeInterface extends EmitterAwareInterface /** * Set the path to the private key. * - * @param CryptKey $privateKey + * @param CryptKeyInterface $privateKey */ - public function setPrivateKey(CryptKey $privateKey); + public function setPrivateKey(CryptKeyInterface $privateKey); /** * Set the encryption key diff --git a/src/ResourceServer.php b/src/ResourceServer.php index e1f98d6d..92a72763 100644 --- a/src/ResourceServer.php +++ b/src/ResourceServer.php @@ -23,7 +23,7 @@ class ResourceServer private $accessTokenRepository; /** - * @var CryptKey + * @var CryptKeyInterface */ private $publicKey; @@ -36,7 +36,7 @@ class ResourceServer * New server instance. * * @param AccessTokenRepositoryInterface $accessTokenRepository - * @param CryptKey|string $publicKey + * @param CryptKeyInterface|string $publicKey * @param null|AuthorizationValidatorInterface $authorizationValidator */ public function __construct( @@ -46,7 +46,7 @@ class ResourceServer ) { $this->accessTokenRepository = $accessTokenRepository; - if ($publicKey instanceof CryptKey === false) { + if ($publicKey instanceof CryptKeyInterface === false) { $publicKey = new CryptKey($publicKey); } $this->publicKey = $publicKey; diff --git a/src/ResponseTypes/AbstractResponseType.php b/src/ResponseTypes/AbstractResponseType.php index 192f52aa..f5f20190 100644 --- a/src/ResponseTypes/AbstractResponseType.php +++ b/src/ResponseTypes/AbstractResponseType.php @@ -11,7 +11,7 @@ namespace League\OAuth2\Server\ResponseTypes; -use League\OAuth2\Server\CryptKey; +use League\OAuth2\Server\CryptKeyInterface; use League\OAuth2\Server\CryptTrait; use League\OAuth2\Server\Entities\AccessTokenEntityInterface; use League\OAuth2\Server\Entities\RefreshTokenEntityInterface; @@ -31,7 +31,7 @@ abstract class AbstractResponseType implements ResponseTypeInterface protected $refreshToken; /** - * @var CryptKey + * @var CryptKeyInterface */ protected $privateKey; @@ -54,9 +54,9 @@ abstract class AbstractResponseType implements ResponseTypeInterface /** * Set the private key * - * @param CryptKey $key + * @param CryptKeyInterface $key */ - public function setPrivateKey(CryptKey $key) + public function setPrivateKey(CryptKeyInterface $key) { $this->privateKey = $key; } diff --git a/tests/AuthorizationServerTest.php b/tests/AuthorizationServerTest.php index 870d546f..76bcdda8 100644 --- a/tests/AuthorizationServerTest.php +++ b/tests/AuthorizationServerTest.php @@ -4,7 +4,6 @@ namespace LeagueTests; use DateInterval; use League\OAuth2\Server\AuthorizationServer; -use League\OAuth2\Server\CryptKey; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Grant\AuthCodeGrant; use League\OAuth2\Server\Grant\ClientCredentialsGrant; @@ -153,7 +152,7 @@ class AuthorizationServerTest extends TestCase $encryptionKey = 'file://' . __DIR__ . '/Stubs/public.key'; $responseTypePrototype = new class extends BearerTokenResponse { - /* @return null|CryptKey */ + /* @return null|\League\OAuth2\Server\CryptKeyInterface */ public function getPrivateKey() { return $this->privateKey; From 7db4cdb875f576b4b589a00eb7f8a26c2f178d90 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sun, 18 Aug 2019 00:04:53 +0300 Subject: [PATCH 2/2] Fix CS --- src/AuthorizationServer.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/AuthorizationServer.php b/src/AuthorizationServer.php index 24a5a272..8f37ffc5 100644 --- a/src/AuthorizationServer.php +++ b/src/AuthorizationServer.php @@ -82,12 +82,12 @@ class AuthorizationServer implements EmitterAwareInterface /** * New server instance. * - * @param ClientRepositoryInterface $clientRepository + * @param ClientRepositoryInterface $clientRepository * @param AccessTokenRepositoryInterface $accessTokenRepository - * @param ScopeRepositoryInterface $scopeRepository - * @param CryptKeyInterface|string $privateKey - * @param string|Key $encryptionKey - * @param null|ResponseTypeInterface $responseType + * @param ScopeRepositoryInterface $scopeRepository + * @param CryptKeyInterface|string $privateKey + * @param string|Key $encryptionKey + * @param null|ResponseTypeInterface $responseType */ public function __construct( ClientRepositoryInterface $clientRepository,