From 35a75efa3e1caad450f9614184c687044bc85bfd Mon Sep 17 00:00:00 2001 From: Maxime Beaudoin Date: Mon, 12 Feb 2018 15:00:17 +0000 Subject: [PATCH] Add method to facilitate BearerTokenValidator override when you want to append data to the jwt token Signed-off-by: Maxime Beaudoin --- .../BearerTokenValidator.php | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/AuthorizationValidators/BearerTokenValidator.php b/src/AuthorizationValidators/BearerTokenValidator.php index 6f299ce46..5cc33ecba 100644 --- a/src/AuthorizationValidators/BearerTokenValidator.php +++ b/src/AuthorizationValidators/BearerTokenValidator.php @@ -11,6 +11,7 @@ use Lcobucci\JWT\Parser; use Lcobucci\JWT\Signer\Rsa\Sha256; +use Lcobucci\JWT\Token; use Lcobucci\JWT\ValidationData; use League\OAuth2\Server\CryptKey; use League\OAuth2\Server\CryptTrait; @@ -83,11 +84,7 @@ public function validateAuthorization(ServerRequestInterface $request) } // Return the request with additional attributes - return $request - ->withAttribute('oauth_access_token_id', $token->getClaim('jti')) - ->withAttribute('oauth_client_id', $token->getClaim('aud')) - ->withAttribute('oauth_user_id', $token->getClaim('sub')) - ->withAttribute('oauth_scopes', $token->getClaim('scopes')); + return $this->appendAttributesFromToken($request, $token); } catch (\InvalidArgumentException $exception) { // JWT couldn't be parsed so return the request as is throw OAuthServerException::accessDenied($exception->getMessage()); @@ -96,4 +93,18 @@ public function validateAuthorization(ServerRequestInterface $request) throw OAuthServerException::accessDenied('Error while decoding to JSON'); } } + + /** + * @param ServerRequestInterface $request + * @param Token $token + * @return ServerRequestInterface + */ + protected function appendAttributesFromToken(ServerRequestInterface $request, Token $token) + { + return $request + ->withAttribute('oauth_access_token_id', $token->getClaim('jti')) + ->withAttribute('oauth_client_id', $token->getClaim('aud')) + ->withAttribute('oauth_user_id', $token->getClaim('sub')) + ->withAttribute('oauth_scopes', $token->getClaim('scopes')); + } }