@@ -16,30 +16,21 @@ class ClientCredentialsGrantType implements GrantTypeInterface
1616{
1717 use TokensExtractor;
1818
19- private HttpClientInterface $ client ;
20-
21- private string $ tokenUrl ;
22-
23- private string $ clientId ;
24-
25- private string $ clientSecret ;
26-
2719 /**
2820 * @param HttpClientInterface $client A HTTP client to be used to communicate with the OAuth server.
2921 * @param string $tokenUrl The full URL of the token endpoint of the OAuth server.
3022 * @param string $clientId The OAuth client ID.
3123 * @param string $clientSecret The OAuth client secret.
24+ * @param string $scope The OAuth scope for the access request.
3225 */
3326 public function __construct (
34- HttpClientInterface $ client ,
35- string $ tokenUrl ,
36- string $ clientId ,
37- string $ clientSecret ,
27+ private HttpClientInterface $ client ,
28+ private string $ tokenUrl ,
29+ private string $ clientId ,
30+ private string $ clientSecret ,
31+ private string $ scope = '' ,
3832 ) {
39- $ this ->client = $ client ;
40- $ this ->tokenUrl = $ tokenUrl ;
41- $ this ->clientId = $ clientId ;
42- $ this ->clientSecret = $ clientSecret ;
33+
4334 }
4435
4536 /**
@@ -50,9 +41,17 @@ public function __construct(
5041 #[\Override]
5142 public function getTokens (): Tokens
5243 {
44+ $ body = [
45+ 'grant_type ' => 'client_credentials '
46+ ];
47+
48+ if ($ this ->scope !== '' ) {
49+ $ body ['scope ' ] = $ this ->scope ;
50+ }
51+
5352 $ response = $ this ->client ->request ('POST ' , $ this ->tokenUrl , [
5453 'headers ' => ['Authorization ' => sprintf ('Basic %s ' , base64_encode ("{$ this ->clientId }: {$ this ->clientSecret }" ))],
55- 'body ' => http_build_query ([ ' grant_type ' => ' client_credentials ' ] ),
54+ 'body ' => http_build_query ($ body ),
5655 ]);
5756
5857 return $ this ->extractTokens ($ response );
0 commit comments