Abstract CryptKey public methods to the CryptKeyInterface

This commit is contained in:
ErickSkrauch 2019-08-17 23:51:23 +03:00
parent 0b0b43d433
commit 4673e7de89
11 changed files with 58 additions and 48 deletions

View File

@ -40,12 +40,12 @@ class AuthorizationServer implements EmitterAwareInterface
protected $grantTypeAccessTokenTTL = []; protected $grantTypeAccessTokenTTL = [];
/** /**
* @var CryptKey * @var CryptKeyInterface
*/ */
protected $privateKey; protected $privateKey;
/** /**
* @var CryptKey * @var CryptKeyInterface
*/ */
protected $publicKey; protected $publicKey;
@ -82,12 +82,12 @@ class AuthorizationServer implements EmitterAwareInterface
/** /**
* New server instance. * New server instance.
* *
* @param ClientRepositoryInterface $clientRepository * @param ClientRepositoryInterface $clientRepository
* @param AccessTokenRepositoryInterface $accessTokenRepository * @param AccessTokenRepositoryInterface $accessTokenRepository
* @param ScopeRepositoryInterface $scopeRepository * @param ScopeRepositoryInterface $scopeRepository
* @param CryptKey|string $privateKey * @param CryptKeyInterface|string $privateKey
* @param string|Key $encryptionKey * @param string|Key $encryptionKey
* @param null|ResponseTypeInterface $responseType * @param null|ResponseTypeInterface $responseType
*/ */
public function __construct( public function __construct(
ClientRepositoryInterface $clientRepository, ClientRepositoryInterface $clientRepository,
@ -101,7 +101,7 @@ class AuthorizationServer implements EmitterAwareInterface
$this->accessTokenRepository = $accessTokenRepository; $this->accessTokenRepository = $accessTokenRepository;
$this->scopeRepository = $scopeRepository; $this->scopeRepository = $scopeRepository;
if ($privateKey instanceof CryptKey === false) { if ($privateKey instanceof CryptKeyInterface === false) {
$privateKey = new CryptKey($privateKey); $privateKey = new CryptKey($privateKey);
} }

View File

@ -14,7 +14,7 @@ use InvalidArgumentException;
use Lcobucci\JWT\Parser; use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Rsa\Sha256; use Lcobucci\JWT\Signer\Rsa\Sha256;
use Lcobucci\JWT\ValidationData; use Lcobucci\JWT\ValidationData;
use League\OAuth2\Server\CryptKey; use League\OAuth2\Server\CryptKeyInterface;
use League\OAuth2\Server\CryptTrait; use League\OAuth2\Server\CryptTrait;
use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
@ -31,7 +31,7 @@ class BearerTokenValidator implements AuthorizationValidatorInterface
private $accessTokenRepository; private $accessTokenRepository;
/** /**
* @var CryptKey * @var CryptKeyInterface
*/ */
protected $publicKey; protected $publicKey;
@ -46,9 +46,9 @@ class BearerTokenValidator implements AuthorizationValidatorInterface
/** /**
* Set the public key * Set the public key
* *
* @param CryptKey $key * @param CryptKeyInterface $key
*/ */
public function setPublicKey(CryptKey $key) public function setPublicKey(CryptKeyInterface $key)
{ {
$this->publicKey = $key; $this->publicKey = $key;
} }

View File

@ -14,7 +14,7 @@ namespace League\OAuth2\Server;
use LogicException; use LogicException;
use RuntimeException; use RuntimeException;
class CryptKey class CryptKey implements CryptKeyInterface
{ {
const RSA_KEY_PATTERN = const RSA_KEY_PATTERN =
'/^(-----BEGIN (RSA )?(PUBLIC|PRIVATE) KEY-----)\R.*(-----END (RSA )?(PUBLIC|PRIVATE) KEY-----)\R?$/s'; '/^(-----BEGIN (RSA )?(PUBLIC|PRIVATE) KEY-----)\R.*(-----END (RSA )?(PUBLIC|PRIVATE) KEY-----)\R?$/s';
@ -101,22 +101,12 @@ class CryptKey
return 'file://' . $keyPath; return 'file://' . $keyPath;
} }
/** public function getKeyPath(): string
* Retrieve key path.
*
* @return string
*/
public function getKeyPath()
{ {
return $this->keyPath; return $this->keyPath;
} }
/** public function getPassPhrase(): ?string
* Retrieve key pass phrase.
*
* @return null|string
*/
public function getPassPhrase()
{ {
return $this->passPhrase; return $this->passPhrase;
} }

21
src/CryptKeyInterface.php Normal file
View File

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace League\OAuth2\Server;
interface CryptKeyInterface
{
/**
* Retrieve key path.
*
* @return string
*/
public function getKeyPath(): string;
/**
* Retrieve key pass phrase.
*
* @return null|string
*/
public function getPassPhrase(): ?string;
}

View File

@ -9,14 +9,14 @@
namespace League\OAuth2\Server\Entities; namespace League\OAuth2\Server\Entities;
use League\OAuth2\Server\CryptKey; use League\OAuth2\Server\CryptKeyInterface;
interface AccessTokenEntityInterface extends TokenInterface interface AccessTokenEntityInterface extends TokenInterface
{ {
/** /**
* Set a private key used to encrypt the access token. * Set a private key used to encrypt the access token.
*/ */
public function setPrivateKey(CryptKey $privateKey); public function setPrivateKey(CryptKeyInterface $privateKey);
/** /**
* Generate a string representation of the access token. * Generate a string representation of the access token.

View File

@ -14,21 +14,21 @@ use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Key; use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Rsa\Sha256; use Lcobucci\JWT\Signer\Rsa\Sha256;
use Lcobucci\JWT\Token; use Lcobucci\JWT\Token;
use League\OAuth2\Server\CryptKey; use League\OAuth2\Server\CryptKeyInterface;
use League\OAuth2\Server\Entities\ClientEntityInterface; use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface; use League\OAuth2\Server\Entities\ScopeEntityInterface;
trait AccessTokenTrait trait AccessTokenTrait
{ {
/** /**
* @var CryptKey * @var CryptKeyInterface
*/ */
private $privateKey; private $privateKey;
/** /**
* Set the private key used to encrypt this access token. * Set the private key used to encrypt this access token.
*/ */
public function setPrivateKey(CryptKey $privateKey) public function setPrivateKey(CryptKeyInterface $privateKey)
{ {
$this->privateKey = $privateKey; $this->privateKey = $privateKey;
} }
@ -36,11 +36,11 @@ trait AccessTokenTrait
/** /**
* Generate a JWT from the access token * Generate a JWT from the access token
* *
* @param CryptKey $privateKey * @param CryptKeyInterface $privateKey
* *
* @return Token * @return Token
*/ */
private function convertToJWT(CryptKey $privateKey) private function convertToJWT(CryptKeyInterface $privateKey)
{ {
return (new Builder()) return (new Builder())
->setAudience($this->getClient()->getIdentifier()) ->setAudience($this->getClient()->getIdentifier())

View File

@ -15,7 +15,7 @@ use DateTimeImmutable;
use Error; use Error;
use Exception; use Exception;
use League\Event\EmitterAwareTrait; use League\Event\EmitterAwareTrait;
use League\OAuth2\Server\CryptKey; use League\OAuth2\Server\CryptKeyInterface;
use League\OAuth2\Server\CryptTrait; use League\OAuth2\Server\CryptTrait;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\AuthCodeEntityInterface; use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
@ -83,7 +83,7 @@ abstract class AbstractGrant implements GrantTypeInterface
protected $refreshTokenTTL; protected $refreshTokenTTL;
/** /**
* @var CryptKey * @var CryptKeyInterface
*/ */
protected $privateKey; protected $privateKey;
@ -151,9 +151,9 @@ abstract class AbstractGrant implements GrantTypeInterface
/** /**
* Set the private key * Set the private key
* *
* @param CryptKey $key * @param CryptKeyInterface $key
*/ */
public function setPrivateKey(CryptKey $key) public function setPrivateKey(CryptKeyInterface $key)
{ {
$this->privateKey = $key; $this->privateKey = $key;
} }

View File

@ -14,7 +14,7 @@ namespace League\OAuth2\Server\Grant;
use DateInterval; use DateInterval;
use Defuse\Crypto\Key; use Defuse\Crypto\Key;
use League\Event\EmitterAwareInterface; 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\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface; use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
@ -131,9 +131,9 @@ interface GrantTypeInterface extends EmitterAwareInterface
/** /**
* Set the path to the private key. * 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 * Set the encryption key

View File

@ -23,7 +23,7 @@ class ResourceServer
private $accessTokenRepository; private $accessTokenRepository;
/** /**
* @var CryptKey * @var CryptKeyInterface
*/ */
private $publicKey; private $publicKey;
@ -36,7 +36,7 @@ class ResourceServer
* New server instance. * New server instance.
* *
* @param AccessTokenRepositoryInterface $accessTokenRepository * @param AccessTokenRepositoryInterface $accessTokenRepository
* @param CryptKey|string $publicKey * @param CryptKeyInterface|string $publicKey
* @param null|AuthorizationValidatorInterface $authorizationValidator * @param null|AuthorizationValidatorInterface $authorizationValidator
*/ */
public function __construct( public function __construct(
@ -46,7 +46,7 @@ class ResourceServer
) { ) {
$this->accessTokenRepository = $accessTokenRepository; $this->accessTokenRepository = $accessTokenRepository;
if ($publicKey instanceof CryptKey === false) { if ($publicKey instanceof CryptKeyInterface === false) {
$publicKey = new CryptKey($publicKey); $publicKey = new CryptKey($publicKey);
} }
$this->publicKey = $publicKey; $this->publicKey = $publicKey;

View File

@ -11,7 +11,7 @@
namespace League\OAuth2\Server\ResponseTypes; namespace League\OAuth2\Server\ResponseTypes;
use League\OAuth2\Server\CryptKey; use League\OAuth2\Server\CryptKeyInterface;
use League\OAuth2\Server\CryptTrait; use League\OAuth2\Server\CryptTrait;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface; use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
@ -31,7 +31,7 @@ abstract class AbstractResponseType implements ResponseTypeInterface
protected $refreshToken; protected $refreshToken;
/** /**
* @var CryptKey * @var CryptKeyInterface
*/ */
protected $privateKey; protected $privateKey;
@ -54,9 +54,9 @@ abstract class AbstractResponseType implements ResponseTypeInterface
/** /**
* Set the private key * Set the private key
* *
* @param CryptKey $key * @param CryptKeyInterface $key
*/ */
public function setPrivateKey(CryptKey $key) public function setPrivateKey(CryptKeyInterface $key)
{ {
$this->privateKey = $key; $this->privateKey = $key;
} }

View File

@ -4,7 +4,6 @@ namespace LeagueTests;
use DateInterval; use DateInterval;
use League\OAuth2\Server\AuthorizationServer; use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\AuthCodeGrant; use League\OAuth2\Server\Grant\AuthCodeGrant;
use League\OAuth2\Server\Grant\ClientCredentialsGrant; use League\OAuth2\Server\Grant\ClientCredentialsGrant;
@ -153,7 +152,7 @@ class AuthorizationServerTest extends TestCase
$encryptionKey = 'file://' . __DIR__ . '/Stubs/public.key'; $encryptionKey = 'file://' . __DIR__ . '/Stubs/public.key';
$responseTypePrototype = new class extends BearerTokenResponse { $responseTypePrototype = new class extends BearerTokenResponse {
/* @return null|CryptKey */ /* @return null|\League\OAuth2\Server\CryptKeyInterface */
public function getPrivateKey() public function getPrivateKey()
{ {
return $this->privateKey; return $this->privateKey;