Skip to content
17 changes: 15 additions & 2 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 @@ -50,6 +51,16 @@ public function initJwtConfiguration()
);
}

/**
* Configure the JWT builder instance.
*
* @return Builder
*/
protected function withBuilder(Builder $builder)
{
return $builder;
}

/**
* Generate a JWT from the access token
*
Expand All @@ -59,14 +70,16 @@ private function convertToJWT()
{
$this->initJwtConfiguration();

return $this->jwtConfiguration->builder()
$builder = $this->jwtConfiguration->builder()
->permittedFor($this->getClient()->getIdentifier())
->identifiedBy($this->getIdentifier())
->issuedAt(new DateTimeImmutable())
->canOnlyBeUsedAfter(new DateTimeImmutable())
->expiresAt($this->getExpiryDateTime())
->relatedTo((string) $this->getUserIdentifier())
->withClaim('scopes', $this->getScopes())
->withClaim('scopes', $this->getScopes());

return $this->withBuilder($builder)
->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
}

Expand Down