| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\AI\Authorization\User_Interface; |
| 5 |
|
| 6 |
use RuntimeException; |
| 7 |
use WP_REST_Request; |
| 8 |
use WP_REST_Response; |
| 9 |
use Yoast\WP\SEO\AI\Authorization\Infrastructure\Access_Token_User_Meta_Repository_Interface; |
| 10 |
use Yoast\WP\SEO\AI\Authorization\Infrastructure\Code_Verifier_User_Meta_Repository_Interface; |
| 11 |
use Yoast\WP\SEO\AI\Authorization\Infrastructure\Refresh_Token_User_Meta_Repository_Interface; |
| 12 |
use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Unauthorized_Exception; |
| 13 |
use Yoast\WP\SEO\Conditionals\AI_Conditional; |
| 14 |
use Yoast\WP\SEO\Main; |
| 15 |
use Yoast\WP\SEO\Routes\Route_Interface; |
| 16 |
|
| 17 |
/** |
| 18 |
* The base class for the callback routes. |
| 19 |
*/ |
| 20 |
abstract class Abstract_Callback_Route implements Route_Interface { |
| 21 |
|
| 22 |
/** |
| 23 |
* The namespace for this route. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE; |
| 28 |
|
| 29 |
/** |
| 30 |
* The access token repository instance. |
| 31 |
* |
| 32 |
* @var Access_Token_User_Meta_Repository_Interface |
| 33 |
*/ |
| 34 |
protected $access_token_repository; |
| 35 |
|
| 36 |
/** |
| 37 |
* The refresh token repository instance. |
| 38 |
* |
| 39 |
* @var Refresh_Token_User_Meta_Repository_Interface |
| 40 |
*/ |
| 41 |
protected $refresh_token_repository; |
| 42 |
|
| 43 |
/** |
| 44 |
* The code verifier instance. |
| 45 |
* |
| 46 |
* @var Code_Verifier_User_Meta_Repository_Interface |
| 47 |
*/ |
| 48 |
protected $code_verifier_repository; |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the conditionals based in which this loadable should be active. |
| 52 |
* |
| 53 |
* @return array<string> The conditionals. |
| 54 |
*/ |
| 55 |
public static function get_conditionals() { |
| 56 |
return [ AI_Conditional::class ]; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Callback_Route constructor. |
| 61 |
* |
| 62 |
* @param Access_Token_User_Meta_Repository_Interface $access_token_repository The access token repository instance. |
| 63 |
* @param Refresh_Token_User_Meta_Repository_Interface $refresh_token_repository The refresh token repository instance. |
| 64 |
* @param Code_Verifier_User_Meta_Repository_Interface $code_verifier_repository The code verifier instance. |
| 65 |
*/ |
| 66 |
public function __construct( Access_Token_User_Meta_Repository_Interface $access_token_repository, Refresh_Token_User_Meta_Repository_Interface $refresh_token_repository, Code_Verifier_User_Meta_Repository_Interface $code_verifier_repository ) { |
| 67 |
$this->access_token_repository = $access_token_repository; |
| 68 |
$this->refresh_token_repository = $refresh_token_repository; |
| 69 |
$this->code_verifier_repository = $code_verifier_repository; |
| 70 |
} |
| 71 |
|
| 72 |
// phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber -- PHPCS doesn't take into account exceptions thrown in called methods. |
| 73 |
|
| 74 |
/** |
| 75 |
* Runs the callback to store connection credentials and the tokens locally. |
| 76 |
* |
| 77 |
* @param WP_REST_Request $request The request object. |
| 78 |
* |
| 79 |
* @return WP_REST_Response The response of the callback action. |
| 80 |
* |
| 81 |
* @throws Unauthorized_Exception If the code challenge is not valid. |
| 82 |
* @throws RuntimeException If the verification code is not found. |
| 83 |
*/ |
| 84 |
public function callback( WP_REST_Request $request ): WP_REST_Response { |
| 85 |
$user_id = $request->get_param( 'user_id' ); |
| 86 |
try { |
| 87 |
$code_verifier = $this->code_verifier_repository->get_code_verifier( $user_id ); |
| 88 |
|
| 89 |
if ( $request->get_param( 'code_challenge' ) !== \hash( 'sha256', $code_verifier->get_code() ) ) { |
| 90 |
throw new Unauthorized_Exception( 'Unauthorized' ); |
| 91 |
} |
| 92 |
|
| 93 |
$this->access_token_repository->store_token( $user_id, $request->get_param( 'access_jwt' ) ); |
| 94 |
$this->refresh_token_repository->store_token( $user_id, $request->get_param( 'refresh_jwt' ) ); |
| 95 |
$this->code_verifier_repository->delete_code_verifier( $user_id ); |
| 96 |
} catch ( Unauthorized_Exception |RuntimeException $e ) { |
| 97 |
return new WP_REST_Response( 'Unauthorized.', 401 ); |
| 98 |
} |
| 99 |
|
| 100 |
return new WP_REST_Response( |
| 101 |
[ |
| 102 |
'message' => 'Tokens successfully stored.', |
| 103 |
'code_verifier' => $code_verifier->get_code(), |
| 104 |
], |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
// phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber -- PHPCS doesn't take into account exceptions thrown in called methods. |
| 109 |
} |
| 110 |
|