| 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 |
use SensitiveParameter; |
| 7 |
|
| 8 |
/** |
| 9 |
* Grant strategy for the Authorization Code flow (RFC 6749 Section 4.1). |
| 10 |
* |
| 11 |
* Provides the authorization code, redirect URI, and PKCE code verifier |
| 12 |
* for the token exchange request. |
| 13 |
*/ |
| 14 |
class Authorization_Code_Grant implements Grant_Interface { |
| 15 |
|
| 16 |
/** |
| 17 |
* The authorization code received from the callback. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
private $code; |
| 22 |
|
| 23 |
/** |
| 24 |
* The redirect URI used in the original authorization request. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $redirect_uri; |
| 29 |
|
| 30 |
/** |
| 31 |
* The PKCE code verifier for this authorization flow. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $code_verifier; |
| 36 |
|
| 37 |
/** |
| 38 |
* Authorization_Code_Grant constructor. |
| 39 |
* |
| 40 |
* @param string $code The authorization code. |
| 41 |
* @param string $redirect_uri The redirect URI. |
| 42 |
* @param string $code_verifier The PKCE code verifier. |
| 43 |
*/ |
| 44 |
public function __construct( |
| 45 |
// phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPNativeAttributeFound -- No-op on PHP < 8.2; redacts parameter from stack traces on PHP 8.2+. |
| 46 |
#[SensitiveParameter] |
| 47 |
string $code, |
| 48 |
string $redirect_uri, |
| 49 |
// phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPNativeAttributeFound -- No-op on PHP < 8.2; redacts parameter from stack traces on PHP 8.2+. |
| 50 |
#[SensitiveParameter] |
| 51 |
string $code_verifier |
| 52 |
) { |
| 53 |
$this->code = $code; |
| 54 |
$this->redirect_uri = $redirect_uri; |
| 55 |
$this->code_verifier = $code_verifier; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the grant type identifier. |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public function get_grant_type(): string { |
| 64 |
return 'authorization_code'; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns the grant-specific parameters. |
| 69 |
* |
| 70 |
* @return array<string, string> |
| 71 |
*/ |
| 72 |
public function get_grant_params(): array { |
| 73 |
return [ |
| 74 |
'code' => $this->code, |
| 75 |
'redirect_uri' => $this->redirect_uri, |
| 76 |
'code_verifier' => $this->code_verifier, |
| 77 |
]; |
| 78 |
} |
| 79 |
} |
| 80 |
|