Skip to content

Commit 1ca1b68

Browse files
authored
Merge pull request #1044 from elyby/abstract_crypt_key
Abstract CryptKey public methods to the CryptKeyInterface
2 parents 0b0b43d + 4ab302a commit 1ca1b68

12 files changed

+57
-38
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
### Added (v9)
10+
- A CryptKeyInterface to allow developers to change the CryptKey implementation with greater ease (PR #1044)
11+
912
### Fixed
1013
- Clients are now explicitly prevented from using the Client Credentials grant unless they are confidential to conform
1114
with the OAuth2 spec (PR #1035)

src/AuthorizationServer.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ class AuthorizationServer implements EmitterAwareInterface
4040
protected $grantTypeAccessTokenTTL = [];
4141

4242
/**
43-
* @var CryptKey
43+
* @var CryptKeyInterface
4444
*/
4545
protected $privateKey;
4646

4747
/**
48-
* @var CryptKey
48+
* @var CryptKeyInterface
4949
*/
5050
protected $publicKey;
5151

@@ -85,7 +85,7 @@ class AuthorizationServer implements EmitterAwareInterface
8585
* @param ClientRepositoryInterface $clientRepository
8686
* @param AccessTokenRepositoryInterface $accessTokenRepository
8787
* @param ScopeRepositoryInterface $scopeRepository
88-
* @param CryptKey|string $privateKey
88+
* @param CryptKeyInterface|string $privateKey
8989
* @param string|Key $encryptionKey
9090
* @param null|ResponseTypeInterface $responseType
9191
*/
@@ -101,7 +101,7 @@ public function __construct(
101101
$this->accessTokenRepository = $accessTokenRepository;
102102
$this->scopeRepository = $scopeRepository;
103103

104-
if ($privateKey instanceof CryptKey === false) {
104+
if ($privateKey instanceof CryptKeyInterface === false) {
105105
$privateKey = new CryptKey($privateKey);
106106
}
107107

src/AuthorizationValidators/BearerTokenValidator.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Lcobucci\JWT\Parser;
1515
use Lcobucci\JWT\Signer\Rsa\Sha256;
1616
use Lcobucci\JWT\ValidationData;
17-
use League\OAuth2\Server\CryptKey;
17+
use League\OAuth2\Server\CryptKeyInterface;
1818
use League\OAuth2\Server\CryptTrait;
1919
use League\OAuth2\Server\Exception\OAuthServerException;
2020
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
@@ -31,7 +31,7 @@ class BearerTokenValidator implements AuthorizationValidatorInterface
3131
private $accessTokenRepository;
3232

3333
/**
34-
* @var CryptKey
34+
* @var CryptKeyInterface
3535
*/
3636
protected $publicKey;
3737

@@ -46,9 +46,9 @@ public function __construct(AccessTokenRepositoryInterface $accessTokenRepositor
4646
/**
4747
* Set the public key
4848
*
49-
* @param CryptKey $key
49+
* @param CryptKeyInterface $key
5050
*/
51-
public function setPublicKey(CryptKey $key)
51+
public function setPublicKey(CryptKeyInterface $key)
5252
{
5353
$this->publicKey = $key;
5454
}

src/CryptKey.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use LogicException;
1515
use RuntimeException;
1616

17-
class CryptKey
17+
class CryptKey implements CryptKeyInterface
1818
{
1919
const RSA_KEY_PATTERN =
2020
'/^(-----BEGIN (RSA )?(PUBLIC|PRIVATE) KEY-----)\R.*(-----END (RSA )?(PUBLIC|PRIVATE) KEY-----)\R?$/s';
@@ -102,19 +102,15 @@ private function saveKeyToFile($key)
102102
}
103103

104104
/**
105-
* Retrieve key path.
106-
*
107-
* @return string
105+
* {@inheritdoc}
108106
*/
109107
public function getKeyPath()
110108
{
111109
return $this->keyPath;
112110
}
113111

114112
/**
115-
* Retrieve key pass phrase.
116-
*
117-
* @return null|string
113+
* {@inheritdoc}
118114
*/
119115
public function getPassPhrase()
120116
{

src/CryptKeyInterface.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace League\OAuth2\Server;
4+
5+
interface CryptKeyInterface
6+
{
7+
/**
8+
* Retrieve key path.
9+
*
10+
* @return string
11+
*/
12+
public function getKeyPath();
13+
14+
/**
15+
* Retrieve key pass phrase.
16+
*
17+
* @return null|string
18+
*/
19+
public function getPassPhrase();
20+
}

src/Entities/AccessTokenEntityInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
namespace League\OAuth2\Server\Entities;
1111

12-
use League\OAuth2\Server\CryptKey;
12+
use League\OAuth2\Server\CryptKeyInterface;
1313

1414
interface AccessTokenEntityInterface extends TokenInterface
1515
{
1616
/**
1717
* Set a private key used to encrypt the access token.
1818
*/
19-
public function setPrivateKey(CryptKey $privateKey);
19+
public function setPrivateKey(CryptKeyInterface $privateKey);
2020

2121
/**
2222
* Generate a string representation of the access token.

src/Entities/Traits/AccessTokenTrait.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,33 @@
1414
use Lcobucci\JWT\Signer\Key;
1515
use Lcobucci\JWT\Signer\Rsa\Sha256;
1616
use Lcobucci\JWT\Token;
17-
use League\OAuth2\Server\CryptKey;
17+
use League\OAuth2\Server\CryptKeyInterface;
1818
use League\OAuth2\Server\Entities\ClientEntityInterface;
1919
use League\OAuth2\Server\Entities\ScopeEntityInterface;
2020

2121
trait AccessTokenTrait
2222
{
2323
/**
24-
* @var CryptKey
24+
* @var CryptKeyInterface
2525
*/
2626
private $privateKey;
2727

2828
/**
2929
* Set the private key used to encrypt this access token.
3030
*/
31-
public function setPrivateKey(CryptKey $privateKey)
31+
public function setPrivateKey(CryptKeyInterface $privateKey)
3232
{
3333
$this->privateKey = $privateKey;
3434
}
3535

3636
/**
3737
* Generate a JWT from the access token
3838
*
39-
* @param CryptKey $privateKey
39+
* @param CryptKeyInterface $privateKey
4040
*
4141
* @return Token
4242
*/
43-
private function convertToJWT(CryptKey $privateKey)
43+
private function convertToJWT(CryptKeyInterface $privateKey)
4444
{
4545
return (new Builder())
4646
->setAudience($this->getClient()->getIdentifier())

src/Grant/AbstractGrant.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Error;
1616
use Exception;
1717
use League\Event\EmitterAwareTrait;
18-
use League\OAuth2\Server\CryptKey;
18+
use League\OAuth2\Server\CryptKeyInterface;
1919
use League\OAuth2\Server\CryptTrait;
2020
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
2121
use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
@@ -83,7 +83,7 @@ abstract class AbstractGrant implements GrantTypeInterface
8383
protected $refreshTokenTTL;
8484

8585
/**
86-
* @var CryptKey
86+
* @var CryptKeyInterface
8787
*/
8888
protected $privateKey;
8989

@@ -151,9 +151,9 @@ public function setRefreshTokenTTL(DateInterval $refreshTokenTTL)
151151
/**
152152
* Set the private key
153153
*
154-
* @param CryptKey $key
154+
* @param CryptKeyInterface $key
155155
*/
156-
public function setPrivateKey(CryptKey $key)
156+
public function setPrivateKey(CryptKeyInterface $key)
157157
{
158158
$this->privateKey = $key;
159159
}

src/Grant/GrantTypeInterface.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use DateInterval;
1515
use Defuse\Crypto\Key;
1616
use League\Event\EmitterAwareInterface;
17-
use League\OAuth2\Server\CryptKey;
17+
use League\OAuth2\Server\CryptKeyInterface;
1818
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
1919
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
2020
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
@@ -131,9 +131,9 @@ public function setDefaultScope($scope);
131131
/**
132132
* Set the path to the private key.
133133
*
134-
* @param CryptKey $privateKey
134+
* @param CryptKeyInterface $privateKey
135135
*/
136-
public function setPrivateKey(CryptKey $privateKey);
136+
public function setPrivateKey(CryptKeyInterface $privateKey);
137137

138138
/**
139139
* Set the encryption key

src/ResourceServer.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ResourceServer
2323
private $accessTokenRepository;
2424

2525
/**
26-
* @var CryptKey
26+
* @var CryptKeyInterface
2727
*/
2828
private $publicKey;
2929

@@ -36,7 +36,7 @@ class ResourceServer
3636
* New server instance.
3737
*
3838
* @param AccessTokenRepositoryInterface $accessTokenRepository
39-
* @param CryptKey|string $publicKey
39+
* @param CryptKeyInterface|string $publicKey
4040
* @param null|AuthorizationValidatorInterface $authorizationValidator
4141
*/
4242
public function __construct(
@@ -46,7 +46,7 @@ public function __construct(
4646
) {
4747
$this->accessTokenRepository = $accessTokenRepository;
4848

49-
if ($publicKey instanceof CryptKey === false) {
49+
if ($publicKey instanceof CryptKeyInterface === false) {
5050
$publicKey = new CryptKey($publicKey);
5151
}
5252
$this->publicKey = $publicKey;

src/ResponseTypes/AbstractResponseType.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace League\OAuth2\Server\ResponseTypes;
1313

14-
use League\OAuth2\Server\CryptKey;
14+
use League\OAuth2\Server\CryptKeyInterface;
1515
use League\OAuth2\Server\CryptTrait;
1616
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
1717
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
@@ -31,7 +31,7 @@ abstract class AbstractResponseType implements ResponseTypeInterface
3131
protected $refreshToken;
3232

3333
/**
34-
* @var CryptKey
34+
* @var CryptKeyInterface
3535
*/
3636
protected $privateKey;
3737

@@ -54,9 +54,9 @@ public function setRefreshToken(RefreshTokenEntityInterface $refreshToken)
5454
/**
5555
* Set the private key
5656
*
57-
* @param CryptKey $key
57+
* @param CryptKeyInterface $key
5858
*/
59-
public function setPrivateKey(CryptKey $key)
59+
public function setPrivateKey(CryptKeyInterface $key)
6060
{
6161
$this->privateKey = $key;
6262
}

tests/AuthorizationServerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use DateInterval;
66
use League\OAuth2\Server\AuthorizationServer;
7-
use League\OAuth2\Server\CryptKey;
7+
use League\OAuth2\Server\CryptKeyInterface;
88
use League\OAuth2\Server\Exception\OAuthServerException;
99
use League\OAuth2\Server\Grant\AuthCodeGrant;
1010
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
@@ -153,7 +153,7 @@ public function testMultipleRequestsGetDifferentResponseTypeInstances()
153153
$encryptionKey = 'file://' . __DIR__ . '/Stubs/public.key';
154154

155155
$responseTypePrototype = new class extends BearerTokenResponse {
156-
/* @return null|CryptKey */
156+
/* @return null|CryptKeyInterface */
157157
public function getPrivateKey()
158158
{
159159
return $this->privateKey;

0 commit comments

Comments
 (0)