Added entities

This commit is contained in:
Alex Bilbie 2013-12-24 17:01:11 +00:00
parent 337cb088e9
commit bc74aff46d
6 changed files with 591 additions and 0 deletions

View File

@ -0,0 +1,188 @@
<?php
namespace League\OAuth2\Server\Entities;
use League\OAuth2\Server\Storage\SessionStorageInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use League\OAuth2\Server\Util\SecureKey;
abstract class AbstractToken
{
/**
* Access token ID
* @var string
*/
protected $id = null;
/**
* Access token storage
* @var \League\OAuth2\Server\Storage\AccessTokenInterface
*/
protected $storage = null;
/**
* Session storage
* @var \League\OAuth2\Server\Storage\SessionInterface
*/
protected $sessionStorage = null;
/**
* Associated session
* @var \League\OAuth2\Server\Session
*/
protected $session = null;
/**
* Session scopes
* @var \Symfony\Component\HttpFoundation\ParameterBag
*/
protected $scopes = null;
/**
* __construct
* @param mixed $storage
* @return self
*/
public function __construct($storage)
{
$this->storage = $storage;
$this->scopes = new ParameterBag();
return $this;
}
/**
* Get storage
* @return AccessTokenInterface
*/
public function getStorage()
{
return $this->storage;
}
/**
* Set session
* @param \League\OAuth2\Server\Session $session
* @return self
*/
public function setSession(Session $session)
{
$this->session = $session;
return $this;
}
/**
* Get session
* @return \League\OAuth2\Server\Session
*/
public function getSession()
{
return $this->session;
}
/**
* Set token TTL
* @param integer $ttl TTL in seconds
* @return self
*/
public function setTTL($ttl = 0)
{
$this->ttl = $ttl;
return $this;
}
/**
* Get token TTL
* @return integer
*/
public function getTTL()
{
return $this->ttl;
}
/**
* Set the creation timestamp
* @param integer $timestamp Unix timestamp
* @return self
*/
public function setTimestamp($timestamp = 0)
{
$this->timestamp = $timestamp;
}
/**
* Get access token creation timestamp
* @return integer Unix timestamp
*/
public function getTimestamp()
{
return $this->timestamp;
}
/**
* Return creation timestamp + TTL
* @return int
*/
public function getExpireTime()
{
return $this->getTimestamp() + $this->getTTL();
}
/**
* Set access token ID
* @param string $id Token ID
* @return self
*/
public function setId($id = null)
{
$this->id = ($id !== null) ? $id : SecureKey::make();
return $this;
}
/**
* Get the token ID
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Associate a scope
* @param \League\OAuth2\Server\Entities\Scope $scope
* @return self
*/
public function associateScope($scope)
{
if (!$this->scopes->has($scope->getId())) {
$this->scopes->set($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)
{
return $this->scopes->has($scope);
}
/**
* Return all associated scopes
* @return ParameterBag
*/
public function getScopes()
{
return $this->scopes;
}
/**
* Save the token to the database
* @return self
*/
abstract function save();
}

View File

@ -0,0 +1,38 @@
<?php
namespace League\OAuth2\Server\Entities;
use League\OAuth2\Server\Storage\SessionStorageInterface;
use League\OAuth2\Server\Storage\AccessTokenInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use League\OAuth2\Server\Util\SecureKey;
use League\OAuth2\Server\Exception\InvalidAccessTokenException;
class AccessToken extends AbstractToken
{
/**
* __construct
* @param AccessTokenInterface $storage
* @return self
*/
public function __construct(AccessTokenInterface $storage)
{
parent::__construct($storage);
}
public function save()
{
$this->getStorage()->createAccessToken(
$this->getId(),
$this->getExpireTime(),
$this->getSession()->getId()
);
// Associate the scope with the token
foreach ($this->getScopes() as $scope) {
$this->getStorage()->associateScope($this->getId(), $scope->getId());
}
return $this;
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace League\OAuth2\Server\Entities;
class Client
{
protected $id = null;
protected $secret = null;
protected $name = null;
protected $redirectUri = null;
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getId()
{
return $this->id;
}
public function setSecret($secret)
{
$this->secret = $secret;
return $this;
}
public function getSecret()
{
return $this->secret;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setRedirectUri($redirectUri)
{
$this->redirectUri = $redirectUri;
return $this;
}
public function getRedirectUri()
{
return $this->redirectUri;
}
}

View File

@ -0,0 +1,61 @@
<?php
namespace League\OAuth2\Server\Entities;
use League\OAuth2\Server\Storage\SessionStorageInterface;
use League\OAuth2\Server\Storage\RefreshTokenInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use League\OAuth2\Server\Util\SecureKey;
use League\OAuth2\Server\Exception\InvalidAccessTokenException;
class RefreshToken extends AbstractToken
{
protected $accessToken;
/**
* __construct
* @param RefreshTokenInterface $storage
* @return self
*/
public function __construct(RefreshTokenInterface $storage)
{
parent::__construct($storage);
}
/**
* Associate an access token
* @param AccessToken $accessToken
* @return self
*/
public function setAccessToken(AccessToken $accessToken)
{
$this->accessToken = $accessToken;
return $this;
}
/**
* Return access token
* @return AccessToken
*/
public function getAccessToken()
{
return $this->accessToken;
}
/**
* (@inheritdoc)
*/
public function save()
{
/*$this->getStorage()->createAccessToken(
$this->getId(),
$this->getExpireTime(),
$this->getAccessToken()->getId()
);
// Associate the scope with the token
foreach ($this->getScopes() as $scope) {
$this->getStorage()->associateScope($this->getId(), $scope->getId());
}*/
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace League\OAuth2\Server\Entities;
class Scope
{
protected $id = null;
protected $scope = null;
protected $name = null;
protected $description = null;
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getId()
{
return $this->id;
}
public function setScope($scope)
{
$this->scope = $scope;
return $this;
}
public function getScope()
{
return $this->scope;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setDescription($description)
{
$this->description = $description;
return $this;
}
public function getDescription()
{
return $this->description;
}
}

View File

@ -0,0 +1,188 @@
<?php
namespace League\OAuth2\Server\Entities;
use OutOfBoundsException;
use League\OAuth2\Server\Exception\OAuth2Exception;
use League\OAuth2\Server\Storage\SessionInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
class Session
{
/**
* Session ID
* @var string
*/
protected $id = null;
protected $clientId = null;
protected $ownerId = null;
protected $ownerType = null;
protected $authCode = null;
protected $accessToken = null;
protected $refreshToken = null;
/**
* Session storage
* @var \League\OAuth2\Server\Storage\SessionInterface
*/
protected $storage = null;
/**
* Session scopes
* @var \Symfony\Component\HttpFoundation\ParameterBag
*/
protected $scopes = null;
/**
* Constuctor
* @param SessionInterface $storage
* @return self
*/
public function __construct(SessionInterface $storage)
{
$this->storage = $storage;
$this->scopes = new ParameterBag();
return $this;
}
/**
* Get storage
* @return SessionInterface
*/
public function getStorage()
{
return $this->storage;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getId()
{
return $this->id;
}
/**
* Associate a scope
* @param \League\OAuth2\Server\Entities\Scope $scope
* @return self
*/
public function associateScope($scope)
{
if (!$this->scopes->has($scope->getId())) {
$this->scopes->set($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)
{
return $this->scopes->has($scope);
}
public function getScopes()
{
return $this->scopes;
}
public function associateAccessToken(AccessToken $accessToken)
{
$this->accessToken = $accessToken;
}
public function associateRefreshToken(RefreshToken $refreshToken)
{
$this->refreshToken = $refreshToken;
}
public function associateAuthCode(AuthCode $authCode)
{
$this->authCode = $authCode;
}
/**
* Associate a client
* @param League\OAuth2\Server\Client $client The client
* @return self
*/
public function associateClient(Client $client)
{
$this->client = $client;
return $this;
}
/**
* Return client
* @return League\OAuth2\Server\Client
*/
public function getClient()
{
return $this->client;
}
/**
* Set the session owner
* @param string $type The type of the owner (e.g. user, app)
* @param string $id The ID of the owner
* @return self
*/
public function setOwner($type, $id)
{
$this->ownerType = $type;
$this->ownerId = $id;
return $this;
}
/**
* Return session owner ID
* @return string
*/
public function getOwnerId()
{
return $this->ownerId;
}
/**
* Return session owner type
* @return string
*/
public function getOwnerType()
{
return $this->ownerType;
}
public function save()
{
// Save the session and get an ID
$id = $this->getStorage()->createSession(
$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->getStorage()->associateScope($this->getId(), $scope->getId());
}
}
}