diff --git a/src/Entities/AccessTokenEntity.php b/src/Entities/AccessTokenEntity.php index d95eee31..75bcb4c6 100644 --- a/src/Entities/AccessTokenEntity.php +++ b/src/Entities/AccessTokenEntity.php @@ -2,6 +2,8 @@ namespace League\OAuth2\Server\Entities; +use Lcobucci\JWT\Builder; +use Lcobucci\JWT\Signer\Rsa\Sha256; use League\OAuth2\Server\Entities\Interfaces\AccessTokenEntityInterface; use League\OAuth2\Server\Entities\Traits\EntityTrait; use League\OAuth2\Server\Entities\Traits\TokenEntityTrait; @@ -9,4 +11,25 @@ use League\OAuth2\Server\Entities\Traits\TokenEntityTrait; class AccessTokenEntity implements AccessTokenEntityInterface { use EntityTrait, TokenEntityTrait; + + /** + * Generate a JWT from the access token + * + * @param string $pathToPrivateKey + * + * @return string + */ + public function convertToJWT($pathToPrivateKey) + { + return (new Builder()) + ->setAudience($this->getClient()->getIdentifier()) + ->setId($this->getIdentifier(), true) + ->setIssuedAt(time()) + ->setNotBefore(time()) + ->setExpiration($this->getExpiryDateTime()->getTimestamp()) + ->setSubject($this->getUserIdentifier()) + ->set('scopes', $this->getScopes()) + ->sign(new Sha256(), new Key($pathToPrivateKey)) + ->getToken(); + } } diff --git a/src/Entities/Interfaces/AccessTokenEntityInterface.php b/src/Entities/Interfaces/AccessTokenEntityInterface.php index a4252bae..2a7ef985 100644 --- a/src/Entities/Interfaces/AccessTokenEntityInterface.php +++ b/src/Entities/Interfaces/AccessTokenEntityInterface.php @@ -2,6 +2,16 @@ namespace League\OAuth2\Server\Entities\Interfaces; +use Lcobucci\JWT\Builder; + interface AccessTokenEntityInterface extends TokenInterface { + /** + * Generate a JWT from the access token + * + * @param string $pathToPrivateKey + * + * @return string + */ + public function convertToJWT($pathToPrivateKey); }