Skip to content

Add optional scope parameter to ClientCredentialsGrantType #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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