Removed old traits

This commit is contained in:
Alex Bilbie 2015-04-05 14:03:34 +01:00
parent a48630c837
commit f357602090
8 changed files with 0 additions and 1066 deletions

View File

@ -1,209 +0,0 @@
<?php
/**
* OAuth 2.0 Abstract token
*
* @package league/oauth2-server
* @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\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();
}

View File

@ -1,93 +0,0 @@
<?php
/**
* OAuth 2.0 Access token entity
*
* @package league/oauth2-server
* @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\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);
}
}

View File

@ -1,128 +0,0 @@
<?php
/**
* OAuth 2.0 Auth code entity
*
* @package league/oauth2-server
* @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\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);
}
}

View File

@ -1,111 +0,0 @@
<?php
/**
* OAuth 2.0 Client entity
*
* @package league/oauth2-server
* @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\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;
}
}

View File

@ -1,33 +0,0 @@
<?php
/**
* OAuth 2.0 Entity trait
*
* @package league/oauth2-server
* @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\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;
}
}

View File

@ -1,94 +0,0 @@
<?php
/**
* OAuth 2.0 Refresh token entity
*
* @package league/oauth2-server
* @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\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);
}
}

View File

@ -1,90 +0,0 @@
<?php
/**
* OAuth 2.0 scope entity
*
* @package league/oauth2-server
* @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\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()
];
}
}

View File

@ -1,308 +0,0 @@
<?php
/**
* OAuth 2.0 session entity
*
* @package league/oauth2-server
* @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\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);
}
}
}