Use __toString() for access token

This commit is contained in:
Andrew Millington
2018-05-23 16:34:39 +01:00
parent 4b0383b16c
commit 61156ef8c7
11 changed files with 50 additions and 10 deletions

View File

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

View File

@@ -19,6 +19,19 @@ use League\OAuth2\Server\Entities\ScopeEntityInterface;
trait AccessTokenTrait
{
/**
* @var CryptKey $privateKey
*/
private $privateKey;
/**
* Set the private key used to encrypt this access token.
*/
public function setPrivateKey(CryptKey $privateKey)
{
$this->privateKey = $privateKey;
}
/**
* Generate a JWT from the access token
*
@@ -26,7 +39,7 @@ trait AccessTokenTrait
*
* @return Token
*/
public function convertToJWT(CryptKey $privateKey)
private function convertToJWT(CryptKey $privateKey)
{
return (new Builder())
->setAudience($this->getClient()->getIdentifier())
@@ -43,9 +56,9 @@ trait AccessTokenTrait
/**
* Generate a string representation from the access token
*/
public function convertToAccessToken(CryptKey $privateKey)
public function __toString()
{
return (string) $this->convertToJWT($privateKey);
return (string) $this->convertToJWT($this->privateKey);
}
/**

View File

@@ -361,6 +361,7 @@ abstract class AbstractGrant implements GrantTypeInterface
$accessToken->setClient($client);
$accessToken->setUserIdentifier($userIdentifier);
$accessToken->setExpiryDateTime((new \DateTime())->add($accessTokenTTL));
$accessToken->setPrivateKey($this->privateKey);
foreach ($scopes as $scope) {
$accessToken->addScope($scope);

View File

@@ -216,7 +216,7 @@ class ImplicitGrant extends AbstractAuthorizeGrant
$this->makeRedirectUri(
$finalRedirectUri,
[
'access_token' => $accessToken->convertToAccessToken($this->privateKey),
'access_token' => (string) $accessToken,
'token_type' => 'Bearer',
'expires_in' => $accessToken->getExpiryDateTime()->getTimestamp() - (new \DateTime())->getTimestamp(),
'state' => $authorizationRequest->getState(),

View File

@@ -27,7 +27,7 @@ class BearerTokenResponse extends AbstractResponseType
$responseParams = [
'token_type' => 'Bearer',
'expires_in' => $expireDateTime - (new \DateTime())->getTimestamp(),
'access_token' => $this->accessToken->convertToAccessToken($this->privateKey),
'access_token' => (string) $this->accessToken,
];
if ($this->refreshToken instanceof RefreshTokenEntityInterface) {