From bc74aff46db173da01c565d4bc9a3d0868e7543d Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Tue, 24 Dec 2013 17:01:11 +0000 Subject: [PATCH] Added entities --- .../OAuth2/Server/Entities/AbstractToken.php | 188 ++++++++++++++++++ .../OAuth2/Server/Entities/AccessToken.php | 38 ++++ src/League/OAuth2/Server/Entities/Client.php | 58 ++++++ .../OAuth2/Server/Entities/RefreshToken.php | 61 ++++++ src/League/OAuth2/Server/Entities/Scope.php | 58 ++++++ src/League/OAuth2/Server/Entities/Session.php | 188 ++++++++++++++++++ 6 files changed, 591 insertions(+) create mode 100644 src/League/OAuth2/Server/Entities/AbstractToken.php create mode 100644 src/League/OAuth2/Server/Entities/AccessToken.php create mode 100644 src/League/OAuth2/Server/Entities/Client.php create mode 100644 src/League/OAuth2/Server/Entities/RefreshToken.php create mode 100644 src/League/OAuth2/Server/Entities/Scope.php create mode 100644 src/League/OAuth2/Server/Entities/Session.php diff --git a/src/League/OAuth2/Server/Entities/AbstractToken.php b/src/League/OAuth2/Server/Entities/AbstractToken.php new file mode 100644 index 00000000..ad3374d5 --- /dev/null +++ b/src/League/OAuth2/Server/Entities/AbstractToken.php @@ -0,0 +1,188 @@ +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(); +} diff --git a/src/League/OAuth2/Server/Entities/AccessToken.php b/src/League/OAuth2/Server/Entities/AccessToken.php new file mode 100644 index 00000000..b0ce4bc0 --- /dev/null +++ b/src/League/OAuth2/Server/Entities/AccessToken.php @@ -0,0 +1,38 @@ +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; + } +} diff --git a/src/League/OAuth2/Server/Entities/Client.php b/src/League/OAuth2/Server/Entities/Client.php new file mode 100644 index 00000000..59781892 --- /dev/null +++ b/src/League/OAuth2/Server/Entities/Client.php @@ -0,0 +1,58 @@ +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; + } +} \ No newline at end of file diff --git a/src/League/OAuth2/Server/Entities/RefreshToken.php b/src/League/OAuth2/Server/Entities/RefreshToken.php new file mode 100644 index 00000000..2900bec2 --- /dev/null +++ b/src/League/OAuth2/Server/Entities/RefreshToken.php @@ -0,0 +1,61 @@ +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()); + }*/ + } +} diff --git a/src/League/OAuth2/Server/Entities/Scope.php b/src/League/OAuth2/Server/Entities/Scope.php new file mode 100644 index 00000000..acbd86c4 --- /dev/null +++ b/src/League/OAuth2/Server/Entities/Scope.php @@ -0,0 +1,58 @@ +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; + } +} \ No newline at end of file diff --git a/src/League/OAuth2/Server/Entities/Session.php b/src/League/OAuth2/Server/Entities/Session.php new file mode 100644 index 00000000..38b89877 --- /dev/null +++ b/src/League/OAuth2/Server/Entities/Session.php @@ -0,0 +1,188 @@ +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()); + } + } +}