allow refresh token ttl assign

This commit is contained in:
Julián Gutiérrez 2016-01-20 12:21:44 +01:00
parent 1e1043c04f
commit 44155a8efc
6 changed files with 50 additions and 47 deletions

View File

@ -283,15 +283,16 @@ abstract class AbstractGrant implements GrantTypeInterface
}
/**
* @param \DateInterval $tokenTTL
* @param \League\OAuth2\Server\Entities\AccessTokenEntity $accessToken
*
* @return \League\OAuth2\Server\Entities\RefreshTokenEntity
*/
protected function issueRefreshToken(AccessTokenEntity $accessToken)
protected function issueRefreshToken(\DateInterval $tokenTTL, AccessTokenEntity $accessToken)
{
$refreshToken = new RefreshTokenEntity();
$refreshToken->setIdentifier(SecureKey::generate());
$refreshToken->setExpiryDateTime((new \DateTime())->add(new \DateInterval('P1M')));
$refreshToken->setExpiryDateTime((new \DateTime())->add($tokenTTL));
$refreshToken->setAccessToken($accessToken);
return $refreshToken;

View File

@ -32,14 +32,15 @@ class ClientCredentialsGrant extends AbstractGrant
public function respondToRequest(
ServerRequestInterface $request,
ResponseTypeInterface $responseType,
\DateInterval $tokenTTL
\DateInterval $accessTokenTTL,
\DateInterval $refreshTokenTTL
) {
// Validate request
$client = $this->validateClient($request);
$scopes = $this->validateScopes($request, $client);
// Issue and persist access token
$accessToken = $this->issueAccessToken($tokenTTL, $client, $client->getIdentifier(), $scopes);
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $client->getIdentifier(), $scopes);
$this->accessTokenRepository->persistNewAccessToken($accessToken);
// Inject access token into response type

View File

@ -11,7 +11,6 @@
namespace League\OAuth2\Server\Grant;
use DateInterval;
use League\Event\EmitterInterface;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
@ -43,14 +42,16 @@ interface GrantTypeInterface
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface $responseType
* @param \DateInterval $tokenTTL
* @param \DateInterval $accessTokenTTL
* @param \DateInterval $refreshTokenTTL
*
* @return \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface
*/
public function respondToRequest(
ServerRequestInterface $request,
ResponseTypeInterface $responseType,
DateInterval $tokenTTL
\DateInterval $accessTokenTTL,
\DateInterval $refreshTokenTTL
);
/**

View File

@ -59,7 +59,8 @@ class PasswordGrant extends AbstractGrant
public function respondToRequest(
ServerRequestInterface $request,
ResponseTypeInterface $responseType,
\DateInterval $tokenTTL
\DateInterval $accessTokenTTL,
\DateInterval $refreshTokenTTL
) {
// Validate request
$client = $this->validateClient($request);
@ -67,8 +68,8 @@ class PasswordGrant extends AbstractGrant
$scopes = $this->validateScopes($request, $client);
// Issue and persist new tokens
$accessToken = $this->issueAccessToken($tokenTTL, $client, $user->getIdentifier(), $scopes);
$refreshToken = $this->issueRefreshToken($accessToken);
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $scopes);
$refreshToken = $this->issueRefreshToken($refreshTokenTTL, $accessToken);
$this->accessTokenRepository->persistNewAccessToken($accessToken);
$this->refreshTokenRepository->persistNewRefreshToken($refreshToken);

View File

@ -50,8 +50,10 @@ class RefreshTokenGrant extends AbstractGrant
public function respondToRequest(
ServerRequestInterface $request,
ResponseTypeInterface $responseType,
\DateInterval $tokenTTL
\DateInterval $accessTokenTTL,
\DateInterval $refreshTokenTTL
) {
// Validate request
$client = $this->validateClient($request);
$oldRefreshToken = $this->validateOldRefreshToken($request, $client->getIdentifier());
$scopes = $this->validateScopes($request, $client);
@ -75,8 +77,8 @@ class RefreshTokenGrant extends AbstractGrant
$this->accessTokenRepository->revokeAccessToken($oldRefreshToken['access_token_id']);
$this->refreshTokenRepository->revokeRefreshToken($oldRefreshToken['refresh_token_id']);
$accessToken = $this->issueAccessToken($tokenTTL, $client, $oldRefreshToken['user_id'], $scopes);
$refreshToken = $this->issueRefreshToken($accessToken);
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $oldRefreshToken['user_id'], $scopes);
$refreshToken = $this->issueRefreshToken($refreshTokenTTL, $accessToken);
$this->accessTokenRepository->persistNewAccessToken($accessToken);
$this->refreshTokenRepository->persistNewRefreshToken($refreshToken);

View File

@ -26,15 +26,10 @@ class Server implements EmitterAwareInterface
*/
protected $enabledGrantTypes = [];
/**
* @var ResponseTypeInterface[]
*/
protected $grantResponseTypes = [];
/**
* @var DateInterval[]
*/
protected $grantTypeAccessTokenTTL = [];
protected $grantTypeTokensTTL = [];
/**
* @var string
@ -92,48 +87,31 @@ class Server implements EmitterAwareInterface
$this->responseType = $responseType;
}
/**
* Get the token type that grants will return in the HTTP response
*
* @return ResponseTypeInterface
*/
public function getResponseType()
{
if (!$this->responseType instanceof ResponseTypeInterface) {
$this->responseType = new BearerTokenResponse(
$this->privateKeyPath,
$this->publicKeyPath,
$this->accessTokenRepository
);
}
return $this->responseType;
}
/**
* Enable a grant type on the server
*
* @param \League\OAuth2\Server\Grant\GrantTypeInterface $grantType
* @param DateInterval $accessTokenTTL
* @param DateInterval|null $accessTokenTTL
* @param DateInterval|null $refreshTokenTTL
*/
public function enableGrantType(
GrantTypeInterface $grantType,
\DateInterval $accessTokenTTL
\DateInterval $accessTokenTTL,
\DateInterval $refreshTokenTTL = null
) {
$grantType->setAccessTokenRepository($this->accessTokenRepository);
$grantType->setClientRepository($this->clientRepository);
$grantType->setScopeRepository($this->scopeRepository);
$grantType->setPathToPrivateKey($this->privateKeyPath);
$grantType->setPathToPublicKey($this->publicKeyPath);
$grantType->setEmitter($this->getEmitter());
$this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType;
// Set grant response type
$this->grantResponseTypes[$grantType->getIdentifier()] = $this->getResponseType();
// Set grant access token TTL
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL;
$this->grantTypeTokensTTL[$grantType->getIdentifier()] = [
'access' => $accessTokenTTL,
'refresh' => $refreshTokenTTL !== null ? $refreshTokenTTL : new \DateInterval('P1M'),
];
}
/**
@ -160,8 +138,9 @@ class Server implements EmitterAwareInterface
if ($grantType->canRespondToRequest($request)) {
$tokenResponse = $grantType->respondToRequest(
$request,
$this->grantResponseTypes[$grantType->getIdentifier()],
$this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
$this->getResponseType(),
$this->grantTypeTokensTTL[$grantType->getIdentifier()]['access'],
$this->grantTypeTokensTTL[$grantType->getIdentifier()]['refresh']
);
}
}
@ -172,4 +151,22 @@ class Server implements EmitterAwareInterface
return $tokenResponse->generateHttpResponse($response);
}
/**
* Get the token type that grants will return in the HTTP response
*
* @return ResponseTypeInterface
*/
public function getResponseType()
{
if (!$this->responseType instanceof ResponseTypeInterface) {
$this->responseType = new BearerTokenResponse(
$this->privateKeyPath,
$this->publicKeyPath,
$this->accessTokenRepository
);
}
return $this->responseType;
}
}