2014-09-30 22:44:18 +01:00
---
layout: default
2016-03-24 14:56:24 +00:00
title: Refresh token grant
2014-09-30 22:44:18 +01:00
permalink: /authorization-server/refresh-token-grant/
---
2016-03-24 14:56:24 +00:00
# Refresh token grant
Access tokens eventually expire; however some grants respond with a refresh token which enables the client to refresh the access token.
## Flow
The client sends a POST request with following body parameters to the authorization server:
* `grant_type` with the value `refresh_token`
2016-06-01 15:07:16 +02:00
* `refresh_token` with the refresh token
2016-03-24 14:56:24 +00:00
* `client_id` with the the client's ID
* `client_secret` with the client's secret
2016-03-29 10:05:49 +02:00
* `scope` with a space-delimited list of requested scope permissions. This is optional; if not sent the original scopes will be used, otherwise you can request a reduced set of scopes.
2016-03-24 14:56:24 +00:00
The authorization server will respond with a JSON object containing the following properties:
* `token_type` with the value `Bearer`
* `expires_in` with an integer representing the TTL of the access token
* `access_token` a new JWT signed with the authorization server's private key
* `refresh_token` an encrypted payload that can be used to refresh the access token when it expires
2014-09-30 22:44:18 +01:00
## Setup
2016-03-24 14:56:24 +00:00
Wherever you initialize your objects, initialize a new instance of the authorization server and bind the storage interfaces and authorization code grant:
{% highlight php %}
// Init our repositories
$clientRepository = new ClientRepository();
$accessTokenRepository = new AccessTokenRepository();
$scopeRepository = new ScopeRepository();
$refreshTokenRepository = new RefreshTokenRepository();
// Path to public and private keys
2016-03-29 10:05:49 +02:00
$privateKey = 'file://path/to/private.key';
2016-04-17 13:16:40 +01:00
//$privateKey = new CryptKey('file://path/to/private.key', 'passphrase'); // if private key has a pass phrase
2016-03-29 10:05:49 +02:00
$publicKey = 'file://path/to/public.key';
2016-03-24 14:56:24 +00:00
// Setup the authorization server
2016-04-17 13:16:40 +01:00
$server = new \League\OAuth2\Server\AuthorizationServer(
2016-03-24 14:56:24 +00:00
$clientRepository,
$accessTokenRepository,
$scopeRepository,
2016-03-29 10:05:49 +02:00
$privateKey,
$publicKey
2016-03-24 14:56:24 +00:00
);
2016-04-17 13:16:40 +01:00
$grant = new \League\OAuth2\Server\Grant\RefreshTokenGrant($refreshTokenRepository);
$grant->setRefreshTokenTTL(new \DateInterval('P1M')); // new refresh tokens will expire after 1 month
// Enable the refresh token grant on the server
2016-03-24 14:56:24 +00:00
$server->enableGrantType(
2016-04-17 13:16:40 +01:00
$grant,
new \DateInterval('PT1H') // new access tokens will expire after an hour
2016-03-24 14:56:24 +00:00
);
{% endhighlight %}
## Implementation
2014-09-30 22:44:18 +01:00
2016-03-24 14:56:24 +00:00
The client will request an access token so create an `/access_token` endpoint.
2014-09-30 22:44:18 +01:00
2016-03-24 14:56:24 +00:00
{% highlight php %}
$app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
2014-09-30 22:44:18 +01:00
2016-04-17 13:16:40 +01:00
/* @var \League\OAuth2\Server\AuthorizationServer $server */
$server = $app->getContainer()->get(AuthorizationServer::class);
2014-09-30 22:44:18 +01:00
2016-03-29 10:05:49 +02:00
// Try to respond to the request
2016-03-24 14:56:24 +00:00
try {
2016-04-10 17:04:24 +01:00
return $server->respondToAccessTokenRequest($request, $response);
2016-03-29 10:05:49 +02:00
2016-03-24 14:56:24 +00:00
} catch (\League\OAuth2\Server\Exception\OAuthServerException $exception) {
return $exception->generateHttpResponse($response);
2016-03-29 10:05:49 +02:00
2016-03-24 14:56:24 +00:00
} catch (\Exception $exception) {
$body = new Stream('php://temp', 'r+');
$body->write($exception->getMessage());
return $response->withStatus(500)->withBody($body);
}
});
{% endhighlight %}