mirror of
https://github.com/elyby/oauth2-server.git
synced 2025-05-31 14:12:07 +05:30
63 lines
1.6 KiB
PHP
63 lines
1.6 KiB
PHP
<?php
|
|
/**
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
|
* @copyright Copyright (c) Alex Bilbie
|
|
* @license http://mit-license.org/
|
|
*
|
|
* @link https://github.com/thephpleague/oauth2-server
|
|
*/
|
|
|
|
namespace League\OAuth2\Server\Entities\Traits;
|
|
|
|
use Lcobucci\JWT\Builder;
|
|
use Lcobucci\JWT\Signer\Key;
|
|
use Lcobucci\JWT\Signer\Rsa\Sha256;
|
|
use Lcobucci\JWT\Token;
|
|
use League\OAuth2\Server\CryptKey;
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface;
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface;
|
|
|
|
trait AccessTokenTrait
|
|
{
|
|
/**
|
|
* Generate a JWT from the access token
|
|
*
|
|
* @param CryptKey $privateKey
|
|
*
|
|
* @return Token
|
|
*/
|
|
public function convertToJWT(CryptKey $privateKey)
|
|
{
|
|
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($privateKey->getKeyPath(), $privateKey->getPassPhrase()))
|
|
->getToken();
|
|
}
|
|
|
|
/**
|
|
* @return ClientEntityInterface
|
|
*/
|
|
abstract public function getClient();
|
|
|
|
/**
|
|
* @return \DateTime
|
|
*/
|
|
abstract public function getExpiryDateTime();
|
|
|
|
/**
|
|
* @return string|int
|
|
*/
|
|
abstract public function getUserIdentifier();
|
|
|
|
/**
|
|
* @return ScopeEntityInterface[]
|
|
*/
|
|
abstract public function getScopes();
|
|
}
|