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