From 1ff3d1addacb028e844b76adc72de45b5759e6ce Mon Sep 17 00:00:00 2001 From: pulkit Date: Thu, 11 Sep 2014 13:39:50 +0100 Subject: [PATCH] support grant specific access token ttl --- src/Grant/AbstractGrant.php | 13 +++++++++++++ src/Grant/AuthCodeGrant.php | 4 ++-- src/Grant/ClientCredentialsGrant.php | 4 ++-- src/Grant/PasswordGrant.php | 4 ++-- src/Grant/RefreshTokenGrant.php | 4 ++-- tests/unit/Stubs/StubAbstractGrant.php | 5 ----- 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/Grant/AbstractGrant.php b/src/Grant/AbstractGrant.php index 93e45020..63b65029 100644 --- a/src/Grant/AbstractGrant.php +++ b/src/Grant/AbstractGrant.php @@ -80,6 +80,19 @@ abstract class AbstractGrant implements GrantTypeInterface return $this->responseType; } + /** + * Get the TTL for an access token + * @return int The TTL + */ + public function getAccessTokenTTL() + { + if ($this->accessTokenTTL) { + return $this->accessTokenTTL; + } + + return $this->server->getAccessTokenTTL(); + } + /** * Override the default access token expire time * @param int $accessTokenTTL diff --git a/src/Grant/AuthCodeGrant.php b/src/Grant/AuthCodeGrant.php index eb49f99f..f17769ca 100644 --- a/src/Grant/AuthCodeGrant.php +++ b/src/Grant/AuthCodeGrant.php @@ -217,7 +217,7 @@ class AuthCodeGrant extends AbstractGrant // Generate the access token $accessToken = new AccessTokenEntity($this->server); $accessToken->setId(SecureKey::generate()); - $accessToken->setExpireTime($this->server->getAccessTokenTTL() + time()); + $accessToken->setExpireTime($this->getAccessTokenTTL() + time()); foreach ($authCodeScopes as $authCodeScope) { $session->associateScope($authCodeScope); @@ -228,7 +228,7 @@ class AuthCodeGrant extends AbstractGrant } $this->server->getTokenType()->set('access_token', $accessToken->getId()); - $this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL()); + $this->server->getTokenType()->set('expires_in', $this->getAccessTokenTTL()); // Associate a refresh token if set if ($this->server->hasGrantType('refresh_token')) { diff --git a/src/Grant/ClientCredentialsGrant.php b/src/Grant/ClientCredentialsGrant.php index 73991f25..b5913e97 100644 --- a/src/Grant/ClientCredentialsGrant.php +++ b/src/Grant/ClientCredentialsGrant.php @@ -94,7 +94,7 @@ class ClientCredentialsGrant extends AbstractGrant // Generate an access token $accessToken = new AccessTokenEntity($this->server); $accessToken->setId(SecureKey::generate()); - $accessToken->setExpireTime($this->server->getAccessTokenTTL() + time()); + $accessToken->setExpireTime($this->getAccessTokenTTL() + time()); // Associate scopes with the session and access token foreach ($scopes as $scope) { @@ -111,7 +111,7 @@ class ClientCredentialsGrant extends AbstractGrant $accessToken->save($this->server->getStorage('access_token')); $this->server->getTokenType()->set('access_token', $accessToken->getId()); - $this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL()); + $this->server->getTokenType()->set('expires_in', $this->getAccessTokenTTL()); return $this->server->getTokenType()->generateResponse(); } diff --git a/src/Grant/PasswordGrant.php b/src/Grant/PasswordGrant.php index 484ab595..78f8ab98 100644 --- a/src/Grant/PasswordGrant.php +++ b/src/Grant/PasswordGrant.php @@ -134,7 +134,7 @@ class PasswordGrant extends AbstractGrant // Generate an access token $accessToken = new AccessTokenEntity($this->server); $accessToken->setId(SecureKey::generate()); - $accessToken->setExpireTime($this->server->getAccessTokenTTL() + time()); + $accessToken->setExpireTime($this->getAccessTokenTTL() + time()); // Associate scopes with the session and access token foreach ($scopes as $scope) { @@ -146,7 +146,7 @@ class PasswordGrant extends AbstractGrant } $this->server->getTokenType()->set('access_token', $accessToken->getId()); - $this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL()); + $this->server->getTokenType()->set('expires_in', $this->getAccessTokenTTL()); // Associate a refresh token if set if ($this->server->hasGrantType('refresh_token')) { diff --git a/src/Grant/RefreshTokenGrant.php b/src/Grant/RefreshTokenGrant.php index 58283cc2..74ac13eb 100644 --- a/src/Grant/RefreshTokenGrant.php +++ b/src/Grant/RefreshTokenGrant.php @@ -126,7 +126,7 @@ class RefreshTokenGrant extends AbstractGrant // Generate a new access token and assign it the correct sessions $newAccessToken = new AccessTokenEntity($this->server); $newAccessToken->setId(SecureKey::generate()); - $newAccessToken->setExpireTime($this->server->getAccessTokenTTL() + time()); + $newAccessToken->setExpireTime($this->getAccessTokenTTL() + time()); $newAccessToken->setSession($session); foreach ($newScopes as $newScope) { @@ -138,7 +138,7 @@ class RefreshTokenGrant extends AbstractGrant $newAccessToken->save($this->server->getStorage('access_token')); $this->server->getTokenType()->set('access_token', $newAccessToken->getId()); - $this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL()); + $this->server->getTokenType()->set('expires_in', $this->getAccessTokenTTL()); // Expire the old refresh token $oldRefreshToken->expire($this->server->getStorage('refresh_token')); diff --git a/tests/unit/Stubs/StubAbstractGrant.php b/tests/unit/Stubs/StubAbstractGrant.php index b409500c..d58fb56e 100644 --- a/tests/unit/Stubs/StubAbstractGrant.php +++ b/tests/unit/Stubs/StubAbstractGrant.php @@ -11,11 +11,6 @@ class StubAbstractGrant extends \League\OAuth2\Server\Grant\AbstractGrant return true; } - public function getAccessTokenTTL() - { - return $this->accessTokenTTL; - } - public function getAuthorizationServer() { return $this->server;