Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract CryptKey public methods to the CryptKeyInterface #1044

Merged
merged 6 commits into from
Aug 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added (v9)
- A CryptKeyInterface to allow developers to change the CryptKey implementation with greater ease (PR #1044)

### Fixed
- Clients are now explicitly prevented from using the Client Credentials grant unless they are confidential to conform
with the OAuth2 spec (PR #1035)
Expand Down
8 changes: 4 additions & 4 deletions src/AuthorizationServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class AuthorizationServer implements EmitterAwareInterface
protected $grantTypeAccessTokenTTL = [];

/**
* @var CryptKey
* @var CryptKeyInterface
*/
protected $privateKey;

/**
* @var CryptKey
* @var CryptKeyInterface
*/
protected $publicKey;

Expand Down Expand Up @@ -85,7 +85,7 @@ class AuthorizationServer implements EmitterAwareInterface
* @param ClientRepositoryInterface $clientRepository
* @param AccessTokenRepositoryInterface $accessTokenRepository
* @param ScopeRepositoryInterface $scopeRepository
* @param CryptKey|string $privateKey
* @param CryptKeyInterface|string $privateKey
* @param string|Key $encryptionKey
* @param null|ResponseTypeInterface $responseType
*/
Expand All @@ -101,7 +101,7 @@ public function __construct(
$this->accessTokenRepository = $accessTokenRepository;
$this->scopeRepository = $scopeRepository;

if ($privateKey instanceof CryptKey === false) {
if ($privateKey instanceof CryptKeyInterface === false) {
$privateKey = new CryptKey($privateKey);
}

Expand Down
8 changes: 4 additions & 4 deletions src/AuthorizationValidators/BearerTokenValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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;
Expand All @@ -31,7 +31,7 @@ class BearerTokenValidator implements AuthorizationValidatorInterface
private $accessTokenRepository;

/**
* @var CryptKey
* @var CryptKeyInterface
*/
protected $publicKey;

Expand All @@ -46,9 +46,9 @@ public function __construct(AccessTokenRepositoryInterface $accessTokenRepositor
/**
* Set the public key
*
* @param CryptKey $key
* @param CryptKeyInterface $key
*/
public function setPublicKey(CryptKey $key)
public function setPublicKey(CryptKeyInterface $key)
{
$this->publicKey = $key;
}
Expand Down
10 changes: 3 additions & 7 deletions src/CryptKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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';
Expand Down Expand Up @@ -102,19 +102,15 @@ private function saveKeyToFile($key)
}

/**
* Retrieve key path.
*
* @return string
* {@inheritdoc}
*/
public function getKeyPath()
{
return $this->keyPath;
}

/**
* Retrieve key pass phrase.
*
* @return null|string
* {@inheritdoc}
*/
public function getPassPhrase()
{
Expand Down
20 changes: 20 additions & 0 deletions src/CryptKeyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace League\OAuth2\Server;

interface CryptKeyInterface
{
/**
* Retrieve key path.
*
* @return string
*/
public function getKeyPath();

/**
* Retrieve key pass phrase.
*
* @return null|string
*/
public function getPassPhrase();
}
4 changes: 2 additions & 2 deletions src/Entities/AccessTokenEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

namespace League\OAuth2\Server\Entities;

use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\CryptKeyInterface;

interface AccessTokenEntityInterface extends TokenInterface
{
/**
* 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.
Expand Down
10 changes: 5 additions & 5 deletions src/Entities/Traits/AccessTokenTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Rsa\Sha256;
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\ScopeEntityInterface;

trait AccessTokenTrait
{
/**
* @var CryptKey
* @var CryptKeyInterface
*/
private $privateKey;

/**
* Set the private key used to encrypt this access token.
*/
public function setPrivateKey(CryptKey $privateKey)
public function setPrivateKey(CryptKeyInterface $privateKey)
{
$this->privateKey = $privateKey;
}

/**
* 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())
Expand Down
8 changes: 4 additions & 4 deletions src/Grant/AbstractGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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;
Expand Down Expand Up @@ -83,7 +83,7 @@ abstract class AbstractGrant implements GrantTypeInterface
protected $refreshTokenTTL;

/**
* @var CryptKey
* @var CryptKeyInterface
*/
protected $privateKey;

Expand Down Expand Up @@ -151,9 +151,9 @@ public function setRefreshTokenTTL(DateInterval $refreshTokenTTL)
/**
* Set the private key
*
* @param CryptKey $key
* @param CryptKeyInterface $key
*/
public function setPrivateKey(CryptKey $key)
public function setPrivateKey(CryptKeyInterface $key)
{
$this->privateKey = $key;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Grant/GrantTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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;
Expand Down Expand Up @@ -131,9 +131,9 @@ public function setDefaultScope($scope);
/**
* 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
Expand Down
6 changes: 3 additions & 3 deletions src/ResourceServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ResourceServer
private $accessTokenRepository;

/**
* @var CryptKey
* @var CryptKeyInterface
*/
private $publicKey;

Expand All @@ -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(
Expand All @@ -46,7 +46,7 @@ public function __construct(
) {
$this->accessTokenRepository = $accessTokenRepository;

if ($publicKey instanceof CryptKey === false) {
if ($publicKey instanceof CryptKeyInterface === false) {
$publicKey = new CryptKey($publicKey);
}
$this->publicKey = $publicKey;
Expand Down
8 changes: 4 additions & 4 deletions src/ResponseTypes/AbstractResponseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +31,7 @@ abstract class AbstractResponseType implements ResponseTypeInterface
protected $refreshToken;

/**
* @var CryptKey
* @var CryptKeyInterface
*/
protected $privateKey;

Expand All @@ -54,9 +54,9 @@ public function setRefreshToken(RefreshTokenEntityInterface $refreshToken)
/**
* Set the private key
*
* @param CryptKey $key
* @param CryptKeyInterface $key
*/
public function setPrivateKey(CryptKey $key)
public function setPrivateKey(CryptKeyInterface $key)
{
$this->privateKey = $key;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/AuthorizationServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use DateInterval;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\CryptKeyInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\AuthCodeGrant;
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
Expand Down Expand Up @@ -153,7 +153,7 @@ public function testMultipleRequestsGetDifferentResponseTypeInstances()
$encryptionKey = 'file://' . __DIR__ . '/Stubs/public.key';

$responseTypePrototype = new class extends BearerTokenResponse {
/* @return null|CryptKey */
/* @return null|CryptKeyInterface */
public function getPrivateKey()
{
return $this->privateKey;
Expand Down