Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/GrantType/ClientCredentialsGrantType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,27 @@ class ClientCredentialsGrantType implements GrantTypeInterface

private string $clientSecret;

private string $scope;

/**
* @param HttpClientInterface $client A HTTP client to be used to communicate with the OAuth server.
* @param string $tokenUrl The full URL of the token endpoint of the OAuth server.
* @param string $clientId The OAuth client ID.
* @param string $clientSecret The OAuth client secret.
* @param string $scope The scope of the access request.
*/
public function __construct(
HttpClientInterface $client,
string $tokenUrl,
string $clientId,
string $clientSecret,
string $scope = '',
) {
$this->client = $client;
$this->tokenUrl = $tokenUrl;
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->scope = $scope;
}

/**
Expand All @@ -47,9 +52,15 @@ public function __construct(
*/
public function getTokens(): Tokens
{
$data = ['grant_type' => 'client_credentials'];

if ($this->scope !== '') {
$data['scope'] = $this->scope;
}

$response = $this->client->request('POST', $this->tokenUrl, [
'headers' => ['Authorization' => sprintf('Basic %s', base64_encode("{$this->clientId}:{$this->clientSecret}"))],
'body' => http_build_query(['grant_type' => 'client_credentials']),
'body' => http_build_query($data),
]);

return $this->extractTokens($response);
Expand Down