diff --git a/CHANGELOG.md b/CHANGELOG.md index 46b8c78e1..12fe66dfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Changed +- The `getJwtBuilder()` function was added to the `AccessTokenTrait` to allow customization of the access token (PR #1382) ## [8.5.4] - released 2023-08-25 ### Added diff --git a/src/Entities/Traits/AccessTokenTrait.php b/src/Entities/Traits/AccessTokenTrait.php index 81b634397..9ce8ccbb4 100644 --- a/src/Entities/Traits/AccessTokenTrait.php +++ b/src/Entities/Traits/AccessTokenTrait.php @@ -10,6 +10,7 @@ namespace League\OAuth2\Server\Entities\Traits; use DateTimeImmutable; +use Lcobucci\JWT\Builder; use Lcobucci\JWT\Configuration; use Lcobucci\JWT\Signer\Key\InMemory; use Lcobucci\JWT\Signer\Rsa\Sha256; @@ -51,11 +52,11 @@ public function initJwtConfiguration() } /** - * Generate a JWT from the access token + * Get the JWT builder and apply default claims * - * @return Token + * @return Builder */ - private function convertToJWT() + private function getJwtBuilder() { $this->initJwtConfiguration(); @@ -66,7 +67,17 @@ private function convertToJWT() ->canOnlyBeUsedAfter(new DateTimeImmutable()) ->expiresAt($this->getExpiryDateTime()) ->relatedTo((string) $this->getUserIdentifier()) - ->withClaim('scopes', $this->getScopes()) + ->withClaim('scopes', $this->getScopes()); + } + + /** + * Generate a JWT from the access token + * + * @return Token + */ + private function convertToJWT() + { + return $this->getJwtBuilder() ->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey()); }