Access token can now return a JWT from itself

This commit is contained in:
Alex Bilbie 2016-02-22 07:58:25 +00:00
parent e08669d50c
commit a1bdaae9a9
2 changed files with 33 additions and 0 deletions

View File

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

View File

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