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

Allow configuring the JWT builder when generating a token #1328

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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