From 1e78f62823f9fc18dd85ac71f91d70c00bc0f133 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Fri, 11 Jul 2014 18:27:03 +0100 Subject: [PATCH] Lotsa bug fixes and updates --- examples/relational/Storage/ClientStorage.php | 12 +++-- examples/relational/Storage/ScopeStorage.php | 7 +-- src/AbstractServer.php | 2 +- src/AuthorizationServer.php | 2 +- src/Entity/AbstractTokenEntity.php | 22 ++++---- src/Entity/AccessTokenEntity.php | 2 +- src/Entity/AuthCodeEntity.php | 4 +- src/Entity/ClientEntity.php | 50 +------------------ src/Entity/EntityTrait.php | 8 +-- src/Entity/RefreshTokenEntity.php | 4 +- src/Entity/ScopeEntity.php | 26 +--------- src/Entity/SessionEntity.php | 3 +- src/Grant/AuthCodeGrant.php | 12 ++--- src/Grant/ClientCredentialsGrant.php | 4 +- src/Grant/PasswordGrant.php | 8 +-- src/Grant/RefreshTokenGrant.php | 8 +-- src/ResourceServer.php | 8 +-- 17 files changed, 61 insertions(+), 121 deletions(-) diff --git a/examples/relational/Storage/ClientStorage.php b/examples/relational/Storage/ClientStorage.php index 0b61f074..8db0e0c0 100644 --- a/examples/relational/Storage/ClientStorage.php +++ b/examples/relational/Storage/ClientStorage.php @@ -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; } diff --git a/examples/relational/Storage/ScopeStorage.php b/examples/relational/Storage/ScopeStorage.php index ab31bfc3..674fc6d9 100644 --- a/examples/relational/Storage/ScopeStorage.php +++ b/examples/relational/Storage/ScopeStorage.php @@ -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'] + ]); } } diff --git a/src/AbstractServer.php b/src/AbstractServer.php index 329d380f..884e8be7 100644 --- a/src/AbstractServer.php +++ b/src/AbstractServer.php @@ -132,7 +132,7 @@ abstract class AbstractServer * Get the access token type * @return TokenTypeInterface */ - public function getIdType() + public function getTokenType() { return $this->tokenType; } diff --git a/src/AuthorizationServer.php b/src/AuthorizationServer.php index b0b0bc14..502a811d 100644 --- a/src/AuthorizationServer.php +++ b/src/AuthorizationServer.php @@ -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(); diff --git a/src/Entity/AbstractTokenEntity.php b/src/Entity/AbstractTokenEntity.php index 02a24d1b..a8848881 100644 --- a/src/Entity/AbstractTokenEntity.php +++ b/src/Entity/AbstractTokenEntity.php @@ -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; } /** diff --git a/src/Entity/AccessTokenEntity.php b/src/Entity/AccessTokenEntity.php index 0f040816..76e8093a 100644 --- a/src/Entity/AccessTokenEntity.php +++ b/src/Entity/AccessTokenEntity.php @@ -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() ); diff --git a/src/Entity/AuthCodeEntity.php b/src/Entity/AuthCodeEntity.php index e07d35a2..0a5809d3 100644 --- a/src/Entity/AuthCodeEntity.php +++ b/src/Entity/AuthCodeEntity.php @@ -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() ); diff --git a/src/Entity/ClientEntity.php b/src/Entity/ClientEntity.php index 9656a066..34a3320a 100644 --- a/src/Entity/ClientEntity.php +++ b/src/Entity/ClientEntity.php @@ -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 diff --git a/src/Entity/EntityTrait.php b/src/Entity/EntityTrait.php index aaef33b4..80d6138f 100644 --- a/src/Entity/EntityTrait.php +++ b/src/Entity/EntityTrait.php @@ -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; } } diff --git a/src/Entity/RefreshTokenEntity.php b/src/Entity/RefreshTokenEntity.php index a1b1dde4..7ed35a76 100644 --- a/src/Entity/RefreshTokenEntity.php +++ b/src/Entity/RefreshTokenEntity.php @@ -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() ); } diff --git a/src/Entity/ScopeEntity.php b/src/Entity/ScopeEntity.php index 0740384b..4b4ed673 100644 --- a/src/Entity/ScopeEntity.php +++ b/src/Entity/ScopeEntity.php @@ -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 diff --git a/src/Entity/SessionEntity.php b/src/Entity/SessionEntity.php index ef5f8d0e..2d03cc48 100644 --- a/src/Entity/SessionEntity.php +++ b/src/Entity/SessionEntity.php @@ -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; } diff --git a/src/Grant/AuthCodeGrant.php b/src/Grant/AuthCodeGrant.php index 8b8234fc..5a9820d6 100644 --- a/src/Grant/AuthCodeGrant.php +++ b/src/Grant/AuthCodeGrant.php @@ -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 diff --git a/src/Grant/ClientCredentialsGrant.php b/src/Grant/ClientCredentialsGrant.php index 786d5d2c..75c50566 100644 --- a/src/Grant/ClientCredentialsGrant.php +++ b/src/Grant/ClientCredentialsGrant.php @@ -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()); diff --git a/src/Grant/PasswordGrant.php b/src/Grant/PasswordGrant.php index a44c718b..3af3143e 100644 --- a/src/Grant/PasswordGrant.php +++ b/src/Grant/PasswordGrant.php @@ -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 diff --git a/src/Grant/RefreshTokenGrant.php b/src/Grant/RefreshTokenGrant.php index 01f5e695..25b01743 100644 --- a/src/Grant/RefreshTokenGrant.php +++ b/src/Grant/RefreshTokenGrant.php @@ -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(); } diff --git a/src/ResourceServer.php b/src/ResourceServer.php index f2a8da7f..654dbb41 100644 --- a/src/ResourceServer.php +++ b/src/ResourceServer.php @@ -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(); } /**