2014-10-01 03:14:18 +05:30
---
layout: default
2016-03-24 19:25:10 +05:30
title: Client credentials grant
2014-10-01 03:14:18 +05:30
permalink: /authorization-server/client-credentials-grant/
---
2016-03-16 02:03:44 +05:30
# Client credentials grant
This grant is suitable for machine-to-machine authentication, for example for use in a cron job which is performing maintenance tasks over an API. Another example would be a client making requests to an API that don’ t require user’ s permission.
2014-10-01 03:14:18 +05:30
2016-03-23 18:14:47 +05:30
## Flow
The client sends a POST request with following body parameters to the authorization server:
* `grant_type` with the value `client_credentials`
* `client_id` with the the client's ID
* `client_secret` with the client's secret
* `scope` with a space-delimited list of requested scope permissions.
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 JWT signed with the authorization server's private key
2014-10-01 03:14:18 +05:30
## Setup
2016-03-16 02:03:44 +05:30
Wherever you initialize your objects, initialize a new instance of the authorization server and bind the storage interfaces and authorization code grant:
{% highlight php %}
2016-03-23 18:14:47 +05:30
// Init our repositories
2016-03-16 02:03:44 +05:30
$clientRepository = new ClientRepository();
$accessTokenRepository = new AccessTokenRepository();
$scopeRepository = new ScopeRepository();
2014-10-01 03:14:18 +05:30
2016-03-23 18:14:47 +05:30
// Path to public and private keys
2016-03-16 02:03:44 +05:30
$privateKeyPath = 'file://path/to/private.key';
$publicKeyPath = 'file://path/to/public.key';
2016-03-23 18:14:47 +05:30
2016-03-16 02:03:44 +05:30
// Setup the authorization server
2016-03-23 18:14:47 +05:30
$server = new \League\OAuth2\Server\Server(
2016-03-16 02:03:44 +05:30
$clientRepository,
$accessTokenRepository,
$scopeRepository,
$privateKeyPath,
$publicKeyPath
);
2014-10-01 03:14:18 +05:30
2016-03-16 02:03:44 +05:30
// Enable the client credentials grant on the server with a token TTL of 1 hour
$server->enableGrantType(
2016-03-23 18:14:47 +05:30
new \League\OAuth2\Server\Grant\ClientCredentialsGrant(),
2016-03-16 02:03:44 +05:30
new \DateInterval('PT1H')
);
{% endhighlight %}
2014-10-01 03:14:18 +05:30
## Implementation
The client will request an access token so create an `/access_token` endpoint.
2016-03-16 02:03:44 +05:30
{% highlight php %}
$app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
2014-10-01 03:14:18 +05:30
2016-03-23 18:14:47 +05:30
/* @var \League\OAuth2\Server\Server $server */
2016-03-16 02:03:44 +05:30
$server = $app->getContainer()->get(Server::class);
2016-03-23 18:14:47 +05:30
// Try to respond to the request
2014-10-01 03:14:18 +05:30
try {
2016-03-16 02:03:44 +05:30
return $server->respondToRequest($request, $response);
2016-03-23 18:14:47 +05:30
} catch (\League\OAuth2\Server\Exception\OAuthServerException $exception) {
2016-03-16 02:03:44 +05:30
return $exception->generateHttpResponse($response);
} catch (\Exception $exception) {
$body = new Stream('php://temp', 'r+');
$body->write($exception->getMessage());
return $response->withStatus(500)->withBody($body);
2014-10-01 03:14:18 +05:30
}
});
2016-03-16 02:03:44 +05:30
{% endhighlight %}