Lotsa bug fixes and updates

This commit is contained in:
Alex Bilbie 2014-07-11 18:27:03 +01:00
parent c6bc1b0cfc
commit 1e78f62823
17 changed files with 61 additions and 121 deletions

View File

@ -34,8 +34,10 @@ class ClientStorage extends Adapter implements ClientInterface
if (count($result) === 1) {
$client = new ClientEntity($this->server);
$client->setId($result[0]['id']);
$client->setName($result[0]['name']);
$client->hydrate([
'id' => $result[0]['id'],
'name' => $result[0]['name']
]);
return $client;
}
@ -56,8 +58,10 @@ class ClientStorage extends Adapter implements ClientInterface
if (count($result) === 1) {
$client = new ClientEntity($this->server);
$client->setId($result[0]['id']);
$client->setName($result[0]['name']);
$client->hydrate([
'id' => $result[0]['id'],
'name' => $result[0]['name']
]);
return $client;
}

View File

@ -23,8 +23,9 @@ class ScopeStorage extends Adapter implements ScopeInterface
return null;
}
return (new ScopeEntity($this->server))
->setId($result[0]['id'])
->setDescription($result[0]['description']);
return (new ScopeEntity($this->server))->hydrate([
'id' => $result[0]['id'],
'description' => $result[0]['description']
]);
}
}

View File

@ -132,7 +132,7 @@ abstract class AbstractServer
* Get the access token type
* @return TokenTypeInterface
*/
public function getIdType()
public function getTokenType()
{
return $this->tokenType;
}

View File

@ -78,7 +78,7 @@ class AuthorizationServer extends AbstractServer
$this->storages = [];
// Set Bearer as the default token type
$this->setTokenType(new Bearer);
$this->setIdType(new Bearer);
parent::__construct();

View File

@ -21,10 +21,10 @@ use Symfony\Component\HttpFoundation\ParameterBag;
abstract class AbstractTokenEntity
{
/**
* Access token ID
* Token identifier
* @var string
*/
protected $token;
protected $id;
/**
* Associated session
@ -34,9 +34,9 @@ abstract class AbstractTokenEntity
/**
* Session scopes
* @var \Symfony\Component\HttpFoundation\ParameterBag
* @var array Array of ScopeEntity
*/
protected $scopes;
protected $scopes = [];
/**
* Token expire time
@ -96,13 +96,13 @@ abstract class AbstractTokenEntity
}
/**
* Set access token ID
* Set token ID
* @param string $token Token ID
* @return self
*/
public function setToken($token = null)
public function setId($id = null)
{
$this->token = ($token !== null) ? $token : SecureKey::generate();
$this->id = ($id !== null) ? $id : SecureKey::generate();
return $this;
}
@ -111,9 +111,9 @@ abstract class AbstractTokenEntity
* Get the token ID
* @return string
*/
public function getToken()
public function getId()
{
return $this->token;
return $this->id;
}
/**
@ -153,10 +153,10 @@ abstract class AbstractTokenEntity
*/
public function __toString()
{
if ($this->token === null) {
if ($this->id === null) {
return '';
}
return $this->token;
return $this->id;
}
/**

View File

@ -66,7 +66,7 @@ class AccessTokenEntity extends AbstractTokenEntity
public function save()
{
$this->server->getStorage('access_token')->create(
$this->getToken(),
$this->getId(),
$this->getExpireTime(),
$this->getSession()->getId()
);

View File

@ -55,7 +55,7 @@ class AuthCodeEntity extends AbstractTokenEntity
$uri .= (strstr($this->getRedirectUri(), $queryDelimeter) === false) ? $queryDelimeter : '&';
return $uri.http_build_query([
'code' => $this->getToken(),
'code' => $this->getId(),
'state' => $state
]);
}
@ -94,7 +94,7 @@ class AuthCodeEntity extends AbstractTokenEntity
public function save()
{
$this->server->getStorage('auth_code')->create(
$this->getToken(),
$this->getId(),
$this->getExpireTime(),
$this->getSession()->getId()
);

View File

@ -18,6 +18,8 @@ use League\OAuth2\Server\AbstractServer;
*/
class ClientEntity
{
use EntityTrait;
/**
* Client identifier
* @var string
@ -60,18 +62,6 @@ class ClientEntity
return $this;
}
/**
* Set the client identifier
* @param string $id
* @return self
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Return the client identifier
* @return string
@ -81,18 +71,6 @@ class ClientEntity
return $this->id;
}
/**
* Set the client secret
* @param string $secret
* @return self
*/
public function setSecret($secret)
{
$this->secret = $secret;
return $this;
}
/**
* Return the client secret
* @return string
@ -102,18 +80,6 @@ class ClientEntity
return $this->secret;
}
/**
* Set the client name
* @param string $name
* @return self
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get the client name
* @return string
@ -123,18 +89,6 @@ class ClientEntity
return $this->name;
}
/**
* Set the client redirect URI
* @param string $redirectUri
* @return self
*/
public function setRedirectUri($redirectUri)
{
$this->redirectUri = $redirectUri;
return $this;
}
/**
* Returnt the client redirect URI
* @return string

View File

@ -19,10 +19,12 @@ trait EntityTrait
*/
public function hydrate(array $properties)
{
foreach ($properties as $prop) {
if (isset($this->{$prop})) {
$this->{$prop} = $prop;
foreach ($properties as $prop => $val) {
if (property_exists($this, $prop)) {
$this->{$prop} = $val;
}
}
return $this;
}
}

View File

@ -53,9 +53,9 @@ class RefreshTokenEntity extends AbstractTokenEntity
public function save()
{
$this->server->getStorage('refresh_token')->create(
$this->getToken(),
$this->getId(),
$this->getExpireTime(),
$this->getAccessToken()->getToken()
$this->getAccessToken()->getId()
);
}

View File

@ -18,6 +18,8 @@ use League\OAuth2\Server\AbstractServer;
*/
class ScopeEntity implements \JsonSerializable
{
use EntityTrait;
/**
* Scope identifier
* @var string
@ -48,18 +50,6 @@ class ScopeEntity implements \JsonSerializable
return $this;
}
/**
* Set the scope identifer
* @param string $id The scope identifier
* @return self
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Return the scope identifer
* @return string
@ -69,18 +59,6 @@ class ScopeEntity implements \JsonSerializable
return $this->id;
}
/**
* Set the scope's descripton
* @param string $description
* @return self
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Return the scope's description
* @return string

View File

@ -12,6 +12,7 @@
namespace League\OAuth2\Server\Entity;
use League\OAuth2\Server\AbstractServer;
use League\OAuth2\Server\Event;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
@ -228,7 +229,7 @@ class SessionEntity
$this->ownerType = $type;
$this->ownerId = $id;
$this->server->eventEmitter->emit(new Event\SessionOwnerEvent($this));
$this->server->getEventEmitter()->emit(new Event\SessionOwnerEvent($this));
return $this;
}

View File

@ -141,8 +141,8 @@ class AuthCodeGrant extends AbstractGrant
// Create a new auth code
$authCode = new AuthCodeEntity($this->server);
$authCode->setToken(SecureKey::generate());
$authCode->setRedirectUri();
$authCode->setId(SecureKey::generate());
$authCode->setRedirectUri($authParams['redirect_uri']);
$authCode->setExpireTime(time() + $this->authTokenTTL);
foreach ($authParams['scopes'] as $scope) {
@ -210,23 +210,23 @@ class AuthCodeGrant extends AbstractGrant
// Generate the access token
$accessToken = new AccessTokenEntity($this->server);
$accessToken->setToken(SecureKey::generate());
$accessToken->setId(SecureKey::generate());
$accessToken->setExpireTime($this->server->getAccessTokenTTL() + time());
foreach ($authCodeScopes as $authCodeScope) {
$session->associateScope($authCodeScope);
}
$this->server->getTokenType()->set('access_token', $accessToken->getToken());
$this->server->getTokenType()->set('access_token', $accessToken->getId());
$this->server->getTokenType()->set('expires', $accessToken->getExpireTime());
$this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
// Associate a refresh token if set
if ($this->server->hasGrantType('refresh_token')) {
$refreshToken = new RefreshTokenEntity($this->server);
$refreshToken->setToken(SecureKey::generate());
$refreshToken->setId(SecureKey::generate());
$refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
$this->server->getTokenType()->set('refresh_token', $refreshToken->getToken());
$this->server->getTokenType()->set('refresh_token', $refreshToken->getId());
}
// Expire the auth code

View File

@ -87,7 +87,7 @@ class ClientCredentialsGrant extends AbstractGrant
// Generate an access token
$accessToken = new AccessTokenEntity($this->server);
$accessToken->setToken(SecureKey::generate());
$accessToken->setId(SecureKey::generate());
$accessToken->setExpireTime($this->server->getAccessTokenTTL() + time());
// Associate scopes with the session and access token
@ -101,7 +101,7 @@ class ClientCredentialsGrant extends AbstractGrant
$accessToken->setSession($session);
$accessToken->save($this->server->getStorage('access_token'));
$this->server->getTokenType()->set('access_token', $accessToken->getToken());
$this->server->getTokenType()->set('access_token', $accessToken->getId());
$this->server->getTokenType()->set('expires', $accessToken->getExpireTime());
$this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());

View File

@ -127,7 +127,7 @@ class PasswordGrant extends AbstractGrant
// Generate an access token
$accessToken = new AccessTokenEntity($this->server);
$accessToken->setToken(SecureKey::generate());
$accessToken->setId(SecureKey::generate());
$accessToken->setExpireTime($this->server->getAccessTokenTTL() + time());
// Associate scopes with the session and access token
@ -136,16 +136,16 @@ class PasswordGrant extends AbstractGrant
$session->associateScope($scope);
}
$this->server->getTokenType()->set('access_token', $accessToken->getToken());
$this->server->getTokenType()->set('access_token', $accessToken->getId());
$this->server->getTokenType()->set('expires', $accessToken->getExpireTime());
$this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
// Associate a refresh token if set
if ($this->server->hasGrantType('refresh_token')) {
$refreshToken = new RefreshTokenEntity($this->server);
$refreshToken->setToken(SecureKey::generate());
$refreshToken->setId(SecureKey::generate());
$refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
$this->server->getTokenType()->set('refresh_token', $refreshToken->getToken());
$this->server->getTokenType()->set('refresh_token', $refreshToken->getId());
}
// Save everything

View File

@ -119,7 +119,7 @@ class RefreshTokenGrant extends AbstractGrant
// Generate a new access token and assign it the correct sessions
$newAccessToken = new AccessTokenEntity($this->server);
$newAccessToken->setToken(SecureKey::generate());
$newAccessToken->setId(SecureKey::generate());
$newAccessToken->setExpireTime($this->server->getAccessTokenTTL() + time());
$newAccessToken->setSession($session);
@ -131,7 +131,7 @@ class RefreshTokenGrant extends AbstractGrant
$oldAccessToken->expire($this->server->getStorage('access_token'));
$newAccessToken->save($this->server->getStorage('access_token'));
$this->server->getTokenType()->set('access_token', $newAccessToken->getToken());
$this->server->getTokenType()->set('access_token', $newAccessToken->getId());
$this->server->getTokenType()->set('expires', $newAccessToken->getExpireTime());
$this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
@ -140,12 +140,12 @@ class RefreshTokenGrant extends AbstractGrant
// Generate a new refresh token
$newRefreshToken = new RefreshTokenEntity($this->server);
$newRefreshToken->setToken(SecureKey::generate());
$newRefreshToken->setId(SecureKey::generate());
$newRefreshToken->setExpireTime($this->getRefreshTokenTTL() + time());
$newRefreshToken->setAccessToken($newAccessToken);
$newRefreshToken->save($this->server->getStorage('refresh_token'));
$this->server->getTokenType()->set('refresh_token', $newRefreshToken->getToken());
$this->server->getTokenType()->set('refresh_token', $newRefreshToken->getId());
return $this->server->getTokenType()->generateResponse();
}

View File

@ -64,7 +64,7 @@ class ResourceServer extends AbstractServer
$this->setStorage('scope', $scopeStorage);
// Set Bearer as the default token type
$this->setTokenType(new Bearer);
$this->setIdType(new Bearer);
parent::__construct();
@ -89,7 +89,7 @@ class ResourceServer extends AbstractServer
* Returns the query string key for the access token.
* @return string
*/
public function getTokenKey()
public function getIdKey()
{
return $this->tokenKey;
}
@ -99,7 +99,7 @@ class ResourceServer extends AbstractServer
* @param $key The new query string key
* @return self
*/
public function setTokenKey($key)
public function setIdKey($key)
{
$this->tokenKey = $key;
@ -130,7 +130,7 @@ class ResourceServer extends AbstractServer
*/
public function getAccessToken()
{
return $this->accessToken->getToken();
return $this->accessToken->getId();
}
/**