Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split the AccessTokenTrait::convertToJWT() function so that the token can be customized #1382

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions src/Entities/Traits/AccessTokenTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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());
}

Expand Down