vendor/league/oauth2-server/src/Entities/Traits/AccessTokenTrait.php line 41

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author      Alex Bilbie <hello@alexbilbie.com>
  4.  * @copyright   Copyright (c) Alex Bilbie
  5.  * @license     http://mit-license.org/
  6.  *
  7.  * @link        https://github.com/thephpleague/oauth2-server
  8.  */
  9. namespace League\OAuth2\Server\Entities\Traits;
  10. use DateTime;
  11. use Lcobucci\JWT\Builder;
  12. use Lcobucci\JWT\Signer\Key;
  13. use Lcobucci\JWT\Signer\Rsa\Sha256;
  14. use Lcobucci\JWT\Token;
  15. use League\OAuth2\Server\CryptKey;
  16. use League\OAuth2\Server\Entities\ClientEntityInterface;
  17. use League\OAuth2\Server\Entities\ScopeEntityInterface;
  18. trait AccessTokenTrait
  19. {
  20.     /**
  21.      * Generate a JWT from the access token
  22.      *
  23.      * @param CryptKey $privateKey
  24.      *
  25.      * @return Token
  26.      */
  27.     public function convertToJWT(CryptKey $privateKey)
  28.     {
  29.         return (new Builder())
  30.             ->setAudience($this->getClient()->getIdentifier())
  31.             ->setId($this->getIdentifier(), true)
  32.             ->setIssuedAt(time())
  33.             ->setNotBefore(time())
  34.             ->setExpiration($this->getExpiryDateTime()->getTimestamp())
  35.             ->setSubject($this->getUserIdentifier())
  36.             ->set('scopes'$this->getScopes())
  37.             ->sign(new Sha256(), new Key($privateKey->getKeyPath(), $privateKey->getPassPhrase()))
  38.             ->getToken();
  39.     }
  40.     /**
  41.      * @return ClientEntityInterface
  42.      */
  43.     abstract public function getClient();
  44.     /**
  45.      * @return DateTime
  46.      */
  47.     abstract public function getExpiryDateTime();
  48.     /**
  49.      * @return string|int
  50.      */
  51.     abstract public function getUserIdentifier();
  52.     /**
  53.      * @return ScopeEntityInterface[]
  54.      */
  55.     abstract public function getScopes();
  56. }