diff --git a/src/Entity/AbstractTokenEntity.php b/src/Entity/AbstractTokenEntity.php deleted file mode 100644 index 5f0465c3..00000000 --- a/src/Entity/AbstractTokenEntity.php +++ /dev/null @@ -1,209 +0,0 @@ - - * @copyright Copyright (c) Alex Bilbie - * @license http://mit-license.org/ - * @link https://github.com/thephpleague/oauth2-server - */ - -namespace League\OAuth2\Server\Entity; - -use League\OAuth2\Server\AbstractServer; -use League\OAuth2\Server\Util\SecureKey; - -/** - * Abstract token class - */ -abstract class AbstractTokenEntity -{ - /** - * Token identifier - * - * @var string - */ - protected $id; - - /** - * Associated session - * - * @var \League\OAuth2\Server\Entity\SessionEntity - */ - protected $session; - - /** - * Session scopes - * - * @var \League\OAuth2\Server\Entity\ScopeEntity[] - */ - protected $scopes; - - /** - * Token expire time - * - * @var int - */ - protected $expireTime = 0; - - /** - * Authorization or resource server - * - * @var \League\OAuth2\Server\AbstractServer - */ - protected $server; - - /** - * __construct - * - * @param \League\OAuth2\Server\AbstractServer $server - * - * @return self - */ - public function __construct(AbstractServer $server) - { - $this->server = $server; - - return $this; - } - - /** - * Set session - * - * @param \League\OAuth2\Server\Entity\SessionEntity $session - * - * @return self - */ - public function setSession(SessionEntity $session) - { - $this->session = $session; - - return $this; - } - - /** - * Set the expire time of the token - * - * @param integer $expireTime Unix time stamp - * - * @return self - */ - public function setExpireTime($expireTime) - { - $this->expireTime = $expireTime; - - return $this; - } - - /** - * Return token expire time - * - * @return int - */ - public function getExpireTime() - { - return $this->expireTime; - } - - /** - * Is the token expired? - * - * @return bool - */ - public function isExpired() - { - return ((time() - $this->expireTime) > 0); - } - - /** - * Set token ID - * - * @param string $id Token ID - * - * @return self - */ - public function setId($id = null) - { - $this->id = ($id !== null) ? $id : SecureKey::generate(); - - return $this; - } - - /** - * Get the token ID - * - * @return string - */ - public function getId() - { - return $this->id; - } - - /** - * Associate a scope - * - * @param \League\OAuth2\Server\Entity\ScopeEntity $scope - * - * @return self - */ - public function associateScope(ScopeEntity $scope) - { - if (!isset($this->scopes[$scope->getId()])) { - $this->scopes[$scope->getId()] = $scope; - } - - return $this; - } - - /** - * Format the local scopes array - * - * @param \League\OAuth2\Server\Entity\ScopeEntity[] - * - * @return array - */ - protected function formatScopes($unformatted = []) - { - if (is_null($unformatted)) { - return []; - } - - $scopes = []; - foreach ($unformatted as $scope) { - if ($scope instanceof ScopeEntity) { - $scopes[$scope->getId()] = $scope; - } - } - - return $scopes; - } - - /** - * Returns the token as a string if the object is cast as a string - * - * @return string - */ - public function __toString() - { - if ($this->id === null) { - return ''; - } - - return $this->id; - } - - /** - * Expire the token - * - * @return void - */ - abstract public function expire(); - - /** - * Save the token - * - * @return void - */ - abstract public function save(); -} diff --git a/src/Entity/AccessTokenEntity.php b/src/Entity/AccessTokenEntity.php deleted file mode 100644 index 7342b498..00000000 --- a/src/Entity/AccessTokenEntity.php +++ /dev/null @@ -1,93 +0,0 @@ - - * @copyright Copyright (c) Alex Bilbie - * @license http://mit-license.org/ - * @link https://github.com/thephpleague/oauth2-server - */ - -namespace League\OAuth2\Server\Entity; - -/** - * Access token entity class - */ -class AccessTokenEntity extends AbstractTokenEntity -{ - /** - * Get session - * - * @return \League\OAuth2\Server\Entity\SessionEntity - */ - public function getSession() - { - if ($this->session instanceof SessionEntity) { - return $this->session; - } - - $this->session = $this->server->getSessionStorage()->getByAccessToken($this); - - return $this->session; - } - - /** - * Check if access token has an associated scope - * - * @param string $scope Scope to check - * - * @return bool - */ - public function hasScope($scope) - { - if ($this->scopes === null) { - $this->getScopes(); - } - - return isset($this->scopes[$scope]); - } - - /** - * Return all scopes associated with the access token - * - * @return \League\OAuth2\Server\Entity\ScopeEntity[] - */ - public function getScopes() - { - if ($this->scopes === null) { - $this->scopes = $this->formatScopes( - $this->server->getAccessTokenStorage()->getScopes($this) - ); - } - - return $this->scopes; - } - - /** - * {@inheritdoc} - */ - public function save() - { - $this->server->getAccessTokenStorage()->create( - $this->getId(), - $this->getExpireTime(), - $this->getSession()->getId() - ); - - // Associate the scope with the token - foreach ($this->getScopes() as $scope) { - $this->server->getAccessTokenStorage()->associateScope($this, $scope); - } - - return $this; - } - - /** - * {@inheritdoc} - */ - public function expire() - { - $this->server->getAccessTokenStorage()->delete($this); - } -} diff --git a/src/Entity/AuthCodeEntity.php b/src/Entity/AuthCodeEntity.php deleted file mode 100644 index ae106f7f..00000000 --- a/src/Entity/AuthCodeEntity.php +++ /dev/null @@ -1,128 +0,0 @@ - - * @copyright Copyright (c) Alex Bilbie - * @license http://mit-license.org/ - * @link https://github.com/thephpleague/oauth2-server - */ - -namespace League\OAuth2\Server\Entity; - -/** - * Access token entity class - */ -class AuthCodeEntity extends AbstractTokenEntity -{ - /** - * Redirect URI - * - * @var string - */ - protected $redirectUri = ''; - - /** - * Set the redirect URI for the authorization request - * - * @param string $redirectUri - * - * @return self - */ - public function setRedirectUri($redirectUri) - { - $this->redirectUri = $redirectUri; - - return $this; - } - - /** - * Get the redirect URI - * - * @return string - */ - public function getRedirectUri() - { - return $this->redirectUri; - } - - /** - * Generate a redirect URI - * - * @param string $state The state parameter if set by the client - * @param string $queryDelimeter The query delimiter ('?' for auth code grant, '#' for implicit grant) - * - * @return string - */ - public function generateRedirectUri($state = null, $queryDelimeter = '?') - { - $uri = $this->getRedirectUri(); - $uri .= (strstr($this->getRedirectUri(), $queryDelimeter) === false) ? $queryDelimeter : '&'; - - return $uri.http_build_query([ - 'code' => $this->getId(), - 'state' => $state, - ]); - } - - /** - * Get session - * - * @return \League\OAuth2\Server\Entity\SessionEntity - */ - public function getSession() - { - if ($this->session instanceof SessionEntity) { - return $this->session; - } - - $this->session = $this->server->getSessionStorage()->getByAuthCode($this); - - return $this->session; - } - - /** - * Return all scopes associated with the session - * - * @return \League\OAuth2\Server\Entity\ScopeEntity[] - */ - public function getScopes() - { - if ($this->scopes === null) { - $this->scopes = $this->formatScopes( - $this->server->getAuthCodeStorage()->getScopes($this) - ); - } - - return $this->scopes; - } - - /** - * {@inheritdoc} - */ - public function save() - { - $this->server->getAuthCodeStorage()->create( - $this->getId(), - $this->getExpireTime(), - $this->getSession()->getId(), - $this->getRedirectUri() - ); - - // Associate the scope with the token - foreach ($this->getScopes() as $scope) { - $this->server->getAuthCodeStorage()->associateScope($this, $scope); - } - - return $this; - } - - /** - * {@inheritdoc} - */ - public function expire() - { - $this->server->getAuthCodeStorage()->delete($this); - } -} diff --git a/src/Entity/ClientEntity.php b/src/Entity/ClientEntity.php deleted file mode 100644 index b0e95285..00000000 --- a/src/Entity/ClientEntity.php +++ /dev/null @@ -1,111 +0,0 @@ - - * @copyright Copyright (c) Alex Bilbie - * @license http://mit-license.org/ - * @link https://github.com/thephpleague/oauth2-server - */ - -namespace League\OAuth2\Server\Entity; - -use League\OAuth2\Server\AbstractServer; - -/** - * Client entity class - */ -class ClientEntity -{ - use EntityTrait; - - /** - * Client identifier - * - * @var string - */ - protected $id = null; - - /** - * Client secret - * - * @var string - */ - protected $secret = null; - - /** - * Client name - * - * @var string - */ - protected $name = null; - - /** - * Client redirect URI - * - * @var string - */ - protected $redirectUri = null; - - /** - * Authorization or resource server - * - * @var \League\OAuth2\Server\AbstractServer - */ - protected $server; - - /** - * __construct - * - * @param \League\OAuth2\Server\AbstractServer $server - * - * @return self - */ - public function __construct(AbstractServer $server) - { - $this->server = $server; - - return $this; - } - - /** - * Return the client identifier - * - * @return string - */ - public function getId() - { - return $this->id; - } - - /** - * Return the client secret - * - * @return string - */ - public function getSecret() - { - return $this->secret; - } - - /** - * Get the client name - * - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * Returnt the client redirect URI - * - * @return string - */ - public function getRedirectUri() - { - return $this->redirectUri; - } -} diff --git a/src/Entity/EntityTrait.php b/src/Entity/EntityTrait.php deleted file mode 100644 index 9424fdfd..00000000 --- a/src/Entity/EntityTrait.php +++ /dev/null @@ -1,33 +0,0 @@ - - * @copyright Copyright (c) Alex Bilbie - * @license http://mit-license.org/ - * @link https://github.com/thephpleague/oauth2-server - */ - -namespace League\OAuth2\Server\Entity; - -trait EntityTrait -{ - /** - * Hydrate an entity with properites - * - * @param array $properties - * - * @return self - */ - public function hydrate(array $properties) - { - foreach ($properties as $prop => $val) { - if (property_exists($this, $prop)) { - $this->{$prop} = $val; - } - } - - return $this; - } -} diff --git a/src/Entity/RefreshTokenEntity.php b/src/Entity/RefreshTokenEntity.php deleted file mode 100644 index f1ec89a5..00000000 --- a/src/Entity/RefreshTokenEntity.php +++ /dev/null @@ -1,94 +0,0 @@ - - * @copyright Copyright (c) Alex Bilbie - * @license http://mit-license.org/ - * @link https://github.com/thephpleague/oauth2-server - */ - -namespace League\OAuth2\Server\Entity; - -/** - * Refresh token entity class - */ -class RefreshTokenEntity extends AbstractTokenEntity -{ - /** - * Access token associated to refresh token - * - * @var \League\OAuth2\Server\Entity\AccessTokenEntity - */ - protected $accessTokenEntity; - - /** - * Id of the access token - * - * @var string - */ - protected $accessTokenId; - - /** - * Set the ID of the associated access token - * - * @param string $accessTokenId - * - * @return self - */ - public function setAccessTokenId($accessTokenId) - { - $this->accessTokenId = $accessTokenId; - - return $this; - } - - /** - * Associate an access token - * - * @param \League\OAuth2\Server\Entity\AccessTokenEntity $accessTokenEntity - * - * @return self - */ - public function setAccessToken(AccessTokenEntity $accessTokenEntity) - { - $this->accessTokenEntity = $accessTokenEntity; - - return $this; - } - - /** - * Return access token - * - * @return AccessTokenEntity - */ - public function getAccessToken() - { - if (! $this->accessTokenEntity instanceof AccessTokenEntity) { - $this->accessTokenEntity = $this->server->getAccessTokenStorage()->get($this->accessTokenId); - } - - return $this->accessTokenEntity; - } - - /** - * {@inheritdoc} - */ - public function save() - { - $this->server->getRefreshTokenStorage()->create( - $this->getId(), - $this->getExpireTime(), - $this->getAccessToken()->getId() - ); - } - - /** - * {@inheritdoc} - */ - public function expire() - { - $this->server->getRefreshTokenStorage()->delete($this); - } -} diff --git a/src/Entity/ScopeEntity.php b/src/Entity/ScopeEntity.php deleted file mode 100644 index 1d5c946f..00000000 --- a/src/Entity/ScopeEntity.php +++ /dev/null @@ -1,90 +0,0 @@ - - * @copyright Copyright (c) Alex Bilbie - * @license http://mit-license.org/ - * @link https://github.com/thephpleague/oauth2-server - */ - -namespace League\OAuth2\Server\Entity; - -use League\OAuth2\Server\AbstractServer; - -/** - * Scope entity class - */ -class ScopeEntity implements \JsonSerializable -{ - use EntityTrait; - - /** - * Scope identifier - * - * @var string - */ - protected $id; - - /** - * Scope description - * - * @var string - */ - protected $description; - - /** - * Authorization or resource server - * - * @var \League\OAuth2\Server\AbstractServer - */ - protected $server; - - /** - * __construct - * - * @param \League\OAuth2\Server\AbstractServer $server - * - * @return self - */ - public function __construct(AbstractServer $server) - { - $this->server = $server; - - return $this; - } - - /** - * Return the scope identifer - * - * @return string - */ - public function getId() - { - return $this->id; - } - - /** - * Return the scope's description - * - * @return string - */ - public function getDescription() - { - return $this->description; - } - - /** - * Returns a JSON object when entity is passed into json_encode - * - * @return array - */ - public function jsonSerialize() - { - return [ - 'id' => $this->getId(), - 'description' => $this->getDescription() - ]; - } -} diff --git a/src/Entity/SessionEntity.php b/src/Entity/SessionEntity.php deleted file mode 100644 index c78cead5..00000000 --- a/src/Entity/SessionEntity.php +++ /dev/null @@ -1,308 +0,0 @@ - - * @copyright Copyright (c) Alex Bilbie - * @license http://mit-license.org/ - * @link https://github.com/thephpleague/oauth2-server - */ - -namespace League\OAuth2\Server\Entity; - -use League\OAuth2\Server\AbstractServer; -use League\OAuth2\Server\Event\SessionOwnerEvent; - -/** - * Session entity grant - */ -class SessionEntity -{ - /** - * Session identifier - * - * @var string - */ - protected $id; - - /** - * Client identifier - * - * @var \League\OAuth2\Server\Entity\ClientEntity - */ - protected $client; - - /** - * Session owner identifier - * - * @var string - */ - protected $ownerId; - - /** - * Session owner type (e.g. "user") - * - * @var string - */ - protected $ownerType; - - /** - * Auth code - * - * @var \League\OAuth2\Server\Entity\AuthCodeEntity - */ - protected $authCode; - - /** - * Access token - * - * @var \League\OAuth2\Server\Entity\AccessTokenEntity - */ - protected $accessToken; - - /** - * Refresh token - * - * @var \League\OAuth2\Server\Entity\RefreshTokenEntity - */ - protected $refreshToken; - - /** - * Session scopes - * - * @var \Symfony\Component\HttpFoundation\ParameterBag - */ - protected $scopes; - - /** - * Authorization or resource server - * - * @var \League\OAuth2\Server\AuthorizationServer|\League\OAuth2\Server\ResourceServer - */ - protected $server; - - /** - * __construct - * - * @param \League\OAuth2\Server\AbstractServer $server - * - * @return self - */ - public function __construct(AbstractServer $server) - { - $this->server = $server; - - return $this; - } - - /** - * Set the session identifier - * - * @param string $id - * - * @return self - */ - public function setId($id) - { - $this->id = $id; - - return $this; - } - - /** - * Return the session identifier - * - * @return string - */ - public function getId() - { - return $this->id; - } - - /** - * Associate a scope - * - * @param \League\OAuth2\Server\Entity\ScopeEntity $scope - * - * @return self - */ - public function associateScope(ScopeEntity $scope) - { - if (!isset($this->scopes[$scope->getId()])) { - $this->scopes[$scope->getId()] = $scope; - } - - return $this; - } - - /** - * Check if access token has an associated scope - * - * @param string $scope Scope to check - * - * @return bool - */ - public function hasScope($scope) - { - if ($this->scopes === null) { - $this->getScopes(); - } - - return isset($this->scopes[$scope]); - } - - /** - * Return all scopes associated with the session - * - * @return \League\OAuth2\Server\Entity\ScopeEntity[] - */ - public function getScopes() - { - if ($this->scopes === null) { - $this->scopes = $this->formatScopes($this->server->getSessionStorage()->getScopes($this)); - } - - return $this->scopes; - } - - /** - * Format the local scopes array - * - * @param \League\OAuth2\Server\Entity\Scope[] - * - * @return array - */ - private function formatScopes($unformatted = []) - { - $scopes = []; - if (is_array($unformatted)) { - foreach ($unformatted as $scope) { - if ($scope instanceof ScopeEntity) { - $scopes[$scope->getId()] = $scope; - } - } - } - - return $scopes; - } - - /** - * Associate an access token with the session - * - * @param \League\OAuth2\Server\Entity\AccessTokenEntity $accessToken - * - * @return self - */ - public function associateAccessToken(AccessTokenEntity $accessToken) - { - $this->accessToken = $accessToken; - - return $this; - } - - /** - * Associate a refresh token with the session - * - * @param \League\OAuth2\Server\Entity\RefreshTokenEntity $refreshToken - * - * @return self - */ - public function associateRefreshToken(RefreshTokenEntity $refreshToken) - { - $this->refreshToken = $refreshToken; - - return $this; - } - - /** - * Associate a client with the session - * - * @param \League\OAuth2\Server\Entity\ClientEntity $client The client - * - * @return self - */ - public function associateClient(ClientEntity $client) - { - $this->client = $client; - - return $this; - } - - /** - * Return the session client - * - * @return \League\OAuth2\Server\Entity\ClientEntity - */ - public function getClient() - { - if ($this->client instanceof ClientEntity) { - return $this->client; - } - - $this->client = $this->server->getClientStorage()->getBySession($this); - - return $this->client; - } - - /** - * Set the session owner - * - * @param string $type The type of the owner (e.g. user, app) - * @param string $id The identifier of the owner - * - * @return self - */ - public function setOwner($type, $id) - { - $this->ownerType = $type; - $this->ownerId = $id; - - $this->server->getEventEmitter()->emit(new SessionOwnerEvent($this)); - - return $this; - } - - /** - * Return session owner identifier - * - * @return string - */ - public function getOwnerId() - { - return $this->ownerId; - } - - /** - * Return session owner type - * - * @return string - */ - public function getOwnerType() - { - return $this->ownerType; - } - - /** - * Save the session - * - * @return void - */ - public function save() - { - // Save the session and get an identifier - $id = $this->server->getSessionStorage()->create( - $this->getOwnerType(), - $this->getOwnerId(), - $this->getClient()->getId(), - $this->getClient()->getRedirectUri() - ); - - $this->setId($id); - - // Associate the scope with the session - foreach ($this->getScopes() as $scope) { - $this->server->getSessionStorage()->associateScope($this, $scope); - } - } -}