From fd72d79ad3fb2b491e319d0c8aac7e7056b3ffd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Unger?= Date: Thu, 15 Mar 2018 14:27:37 +0100 Subject: [PATCH] Generalized access token format --- src/Entities/AccessTokenEntityInterface.php | 9 +++++++++ src/Entities/Traits/AccessTokenTrait.php | 12 ++++++++++++ src/Grant/ImplicitGrant.php | 2 +- src/ResponseTypes/BearerTokenResponse.php | 4 +--- tests/Middleware/ResourceServerMiddlewareTest.php | 4 ++-- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Entities/AccessTokenEntityInterface.php b/src/Entities/AccessTokenEntityInterface.php index 4da7600e..c30a51f9 100644 --- a/src/Entities/AccessTokenEntityInterface.php +++ b/src/Entities/AccessTokenEntityInterface.php @@ -22,4 +22,13 @@ interface AccessTokenEntityInterface extends TokenInterface * @return Token */ public function convertToJWT(CryptKey $privateKey); + + /** + * Generate a string representation from the access token + * + * @param CryptKey $privateKey + * + * @return string + */ + public function getResponseString(CryptKey $privateKey); } diff --git a/src/Entities/Traits/AccessTokenTrait.php b/src/Entities/Traits/AccessTokenTrait.php index 81fc1bfd..71b87732 100644 --- a/src/Entities/Traits/AccessTokenTrait.php +++ b/src/Entities/Traits/AccessTokenTrait.php @@ -40,6 +40,18 @@ trait AccessTokenTrait ->getToken(); } + /** + * Generate a string representation from the access token + * + * @param CryptKey $privateKey + * + * @return string + */ + public function getResponseString(CryptKey $privateKey) + { + return (string) $this->convertToJWT($privateKey); + } + /** * @return ClientEntityInterface */ diff --git a/src/Grant/ImplicitGrant.php b/src/Grant/ImplicitGrant.php index b4157883..d915f61d 100644 --- a/src/Grant/ImplicitGrant.php +++ b/src/Grant/ImplicitGrant.php @@ -216,7 +216,7 @@ class ImplicitGrant extends AbstractAuthorizeGrant $this->makeRedirectUri( $finalRedirectUri, [ - 'access_token' => (string) $accessToken->convertToJWT($this->privateKey), + 'access_token' => $accessToken->getResponseString($this->privateKey), 'token_type' => 'Bearer', 'expires_in' => $accessToken->getExpiryDateTime()->getTimestamp() - (new \DateTime())->getTimestamp(), 'state' => $authorizationRequest->getState(), diff --git a/src/ResponseTypes/BearerTokenResponse.php b/src/ResponseTypes/BearerTokenResponse.php index a57573a0..980965ed 100644 --- a/src/ResponseTypes/BearerTokenResponse.php +++ b/src/ResponseTypes/BearerTokenResponse.php @@ -24,12 +24,10 @@ class BearerTokenResponse extends AbstractResponseType { $expireDateTime = $this->accessToken->getExpiryDateTime()->getTimestamp(); - $jwtAccessToken = $this->accessToken->convertToJWT($this->privateKey); - $responseParams = [ 'token_type' => 'Bearer', 'expires_in' => $expireDateTime - (new \DateTime())->getTimestamp(), - 'access_token' => (string) $jwtAccessToken, + 'access_token' => $this->accessToken->getResponseString($this->privateKey), ]; if ($this->refreshToken instanceof RefreshTokenEntityInterface) { diff --git a/tests/Middleware/ResourceServerMiddlewareTest.php b/tests/Middleware/ResourceServerMiddlewareTest.php index 2269c45a..dbb507e2 100644 --- a/tests/Middleware/ResourceServerMiddlewareTest.php +++ b/tests/Middleware/ResourceServerMiddlewareTest.php @@ -30,7 +30,7 @@ class ResourceServerMiddlewareTest extends TestCase $accessToken->setExpiryDateTime((new \DateTime())->add(new \DateInterval('PT1H'))); $accessToken->setClient($client); - $token = $accessToken->convertToJWT(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); + $token = $accessToken->getResponseString(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); $request = new ServerRequest(); $request = $request->withHeader('authorization', sprintf('Bearer %s', $token)); @@ -65,7 +65,7 @@ class ResourceServerMiddlewareTest extends TestCase $accessToken->setExpiryDateTime((new \DateTime())->sub(new \DateInterval('PT1H'))); $accessToken->setClient($client); - $token = $accessToken->convertToJWT(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); + $token = $accessToken->getResponseString(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); $request = new ServerRequest(); $request = $request->withHeader('authorization', sprintf('Bearer %s', $token));