| 1 |
<?php |
| 2 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 |
|
| 4 |
namespace Yoast\WP\SEO\MyYoast_Client\Application\Grants; |
| 5 |
|
| 6 |
/** |
| 7 |
* Grant strategy for the Client Credentials flow (RFC 6749 Section 4.4). |
| 8 |
* |
| 9 |
* Used for site-level tokens without user context. |
| 10 |
* Includes the site URL for site identity. |
| 11 |
*/ |
| 12 |
class Client_Credentials_Grant implements Grant_Interface { |
| 13 |
|
| 14 |
/** |
| 15 |
* The scopes to request. |
| 16 |
* |
| 17 |
* @var string[] |
| 18 |
*/ |
| 19 |
private $scopes; |
| 20 |
|
| 21 |
/** |
| 22 |
* The site URL for site identity. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $site_url; |
| 27 |
|
| 28 |
/** |
| 29 |
* Client_Credentials_Grant constructor. |
| 30 |
* |
| 31 |
* @param string[] $scopes The scopes to request. |
| 32 |
* @param string $site_url The site URL. |
| 33 |
*/ |
| 34 |
public function __construct( array $scopes, string $site_url ) { |
| 35 |
$this->scopes = $scopes; |
| 36 |
$this->site_url = $site_url; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns the grant type identifier. |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
public function get_grant_type(): string { |
| 45 |
return 'client_credentials'; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Returns the grant-specific parameters. |
| 50 |
* |
| 51 |
* @return array<string, string> |
| 52 |
*/ |
| 53 |
public function get_grant_params(): array { |
| 54 |
$params = [ |
| 55 |
'site_url' => $this->site_url, |
| 56 |
]; |
| 57 |
|
| 58 |
if ( ! empty( $this->scopes ) ) { |
| 59 |
$params['scope'] = \implode( ' ', $this->scopes ); |
| 60 |
} |
| 61 |
|
| 62 |
return $params; |
| 63 |
} |
| 64 |
} |
| 65 |
|