support grant specific access token ttl

This commit is contained in:
pulkit 2014-09-11 13:39:50 +01:00
parent 9e2a6ed238
commit 1ff3d1adda
6 changed files with 21 additions and 13 deletions

View File

@ -80,6 +80,19 @@ abstract class AbstractGrant implements GrantTypeInterface
return $this->responseType; 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 * Override the default access token expire time
* @param int $accessTokenTTL * @param int $accessTokenTTL

View File

@ -217,7 +217,7 @@ class AuthCodeGrant extends AbstractGrant
// Generate the access token // Generate the access token
$accessToken = new AccessTokenEntity($this->server); $accessToken = new AccessTokenEntity($this->server);
$accessToken->setId(SecureKey::generate()); $accessToken->setId(SecureKey::generate());
$accessToken->setExpireTime($this->server->getAccessTokenTTL() + time()); $accessToken->setExpireTime($this->getAccessTokenTTL() + time());
foreach ($authCodeScopes as $authCodeScope) { foreach ($authCodeScopes as $authCodeScope) {
$session->associateScope($authCodeScope); $session->associateScope($authCodeScope);
@ -228,7 +228,7 @@ class AuthCodeGrant extends AbstractGrant
} }
$this->server->getTokenType()->set('access_token', $accessToken->getId()); $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 // Associate a refresh token if set
if ($this->server->hasGrantType('refresh_token')) { if ($this->server->hasGrantType('refresh_token')) {

View File

@ -94,7 +94,7 @@ class ClientCredentialsGrant extends AbstractGrant
// Generate an access token // Generate an access token
$accessToken = new AccessTokenEntity($this->server); $accessToken = new AccessTokenEntity($this->server);
$accessToken->setId(SecureKey::generate()); $accessToken->setId(SecureKey::generate());
$accessToken->setExpireTime($this->server->getAccessTokenTTL() + time()); $accessToken->setExpireTime($this->getAccessTokenTTL() + time());
// Associate scopes with the session and access token // Associate scopes with the session and access token
foreach ($scopes as $scope) { foreach ($scopes as $scope) {
@ -111,7 +111,7 @@ class ClientCredentialsGrant extends AbstractGrant
$accessToken->save($this->server->getStorage('access_token')); $accessToken->save($this->server->getStorage('access_token'));
$this->server->getTokenType()->set('access_token', $accessToken->getId()); $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(); return $this->server->getTokenType()->generateResponse();
} }

View File

@ -134,7 +134,7 @@ class PasswordGrant extends AbstractGrant
// Generate an access token // Generate an access token
$accessToken = new AccessTokenEntity($this->server); $accessToken = new AccessTokenEntity($this->server);
$accessToken->setId(SecureKey::generate()); $accessToken->setId(SecureKey::generate());
$accessToken->setExpireTime($this->server->getAccessTokenTTL() + time()); $accessToken->setExpireTime($this->getAccessTokenTTL() + time());
// Associate scopes with the session and access token // Associate scopes with the session and access token
foreach ($scopes as $scope) { foreach ($scopes as $scope) {
@ -146,7 +146,7 @@ class PasswordGrant extends AbstractGrant
} }
$this->server->getTokenType()->set('access_token', $accessToken->getId()); $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 // Associate a refresh token if set
if ($this->server->hasGrantType('refresh_token')) { if ($this->server->hasGrantType('refresh_token')) {

View File

@ -126,7 +126,7 @@ class RefreshTokenGrant extends AbstractGrant
// Generate a new access token and assign it the correct sessions // Generate a new access token and assign it the correct sessions
$newAccessToken = new AccessTokenEntity($this->server); $newAccessToken = new AccessTokenEntity($this->server);
$newAccessToken->setId(SecureKey::generate()); $newAccessToken->setId(SecureKey::generate());
$newAccessToken->setExpireTime($this->server->getAccessTokenTTL() + time()); $newAccessToken->setExpireTime($this->getAccessTokenTTL() + time());
$newAccessToken->setSession($session); $newAccessToken->setSession($session);
foreach ($newScopes as $newScope) { foreach ($newScopes as $newScope) {
@ -138,7 +138,7 @@ class RefreshTokenGrant extends AbstractGrant
$newAccessToken->save($this->server->getStorage('access_token')); $newAccessToken->save($this->server->getStorage('access_token'));
$this->server->getTokenType()->set('access_token', $newAccessToken->getId()); $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 // Expire the old refresh token
$oldRefreshToken->expire($this->server->getStorage('refresh_token')); $oldRefreshToken->expire($this->server->getStorage('refresh_token'));

View File

@ -11,11 +11,6 @@ class StubAbstractGrant extends \League\OAuth2\Server\Grant\AbstractGrant
return true; return true;
} }
public function getAccessTokenTTL()
{
return $this->accessTokenTTL;
}
public function getAuthorizationServer() public function getAuthorizationServer()
{ {
return $this->server; return $this->server;