| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\SEO\Config; |
| 4 | 4 | |
| 5 | +use WPSEO_Utils; | |
| 5 | 6 | use Yoast\WP\SEO\Exceptions\OAuth\Authentication_Failed_Exception; |
| 6 | 7 | use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Property_Exception; |
| 7 | 8 | use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Token_Exception; |
| 8 | 9 | use Yoast\WP\SEO\Helpers\Options_Helper; |
| @@ -18,14 +19,14 @@ | ||
| 18 | 19 | |
| 19 | 20 | /** |
| 20 | 21 | * The option's key. |
| 21 | 22 | */ |
| 22 | - const TOKEN_OPTION = 'wincher_tokens'; | |
| 23 | + public const TOKEN_OPTION = 'wincher_tokens'; | |
| 23 | 24 | |
| 24 | 25 | /** |
| 25 | 26 | * Name of the temporary PKCE cookie. |
| 26 | 27 | */ |
| 27 | - const PKCE_COOKIE_NAME = 'yoast_wincher_pkce'; | |
| 28 | + public const PKCE_TRANSIENT_NAME = 'yoast_wincher_pkce'; | |
| 28 | 29 | |
| 29 | 30 | /** |
| 30 | 31 | * The WP_Remote_Handler instance. |
| 31 | 32 | * |
| @@ -40,13 +41,9 @@ | ||
| 40 | 41 | * @param WP_Remote_Handler $wp_remote_handler The request handler. |
| 41 | 42 | * |
| 42 | 43 | * @throws Empty_Property_Exception Exception thrown if a token property is empty. |
| 43 | 44 | */ |
| 44 | - public function __construct( | |
| 45 | - Options_Helper $options_helper, | |
| 46 | - WP_Remote_Handler $wp_remote_handler | |
| 47 | - ) { | |
| 48 | - | |
| 45 | + public function __construct( Options_Helper $options_helper, WP_Remote_Handler $wp_remote_handler ) { | |
| 49 | 46 | $provider = new Wincher_PKCE_Provider( |
| 50 | 47 | [ |
| 51 | 48 | 'clientId' => 'yoast', |
| 52 | 49 | 'redirectUri' => 'https://auth.wincher.com/yoast/setup', |
| @@ -58,15 +55,15 @@ | ||
| 58 | 55 | 'pkceMethod' => 'S256', |
| 59 | 56 | ], |
| 60 | 57 | [ |
| 61 | 58 | 'httpClient' => new Client( [ 'handler' => $wp_remote_handler ] ), |
| 62 | - ] | |
| 59 | + ], | |
| 63 | 60 | ); |
| 64 | 61 | |
| 65 | 62 | parent::__construct( |
| 66 | 63 | self::TOKEN_OPTION, |
| 67 | 64 | $provider, |
| 68 | - $options_helper | |
| 65 | + $options_helper, | |
| 69 | 66 | ); |
| 70 | 67 | } |
| 71 | 68 | |
| 72 | 69 | /** |
| @@ -78,18 +75,17 @@ | ||
| 78 | 75 | $parsed_site_url = \wp_parse_url( \get_site_url() ); |
| 79 | 76 | |
| 80 | 77 | $url = $this->provider->getAuthorizationUrl( |
| 81 | 78 | [ |
| 82 | - 'state' => \WPSEO_Utils::format_json_encode( [ 'domain' => $parsed_site_url['host'] ] ), | |
| 83 | - ] | |
| 79 | + 'state' => WPSEO_Utils::format_json_encode( [ 'domain' => $parsed_site_url['host'] ] ), | |
| 80 | + ], | |
| 84 | 81 | ); |
| 85 | 82 | |
| 86 | 83 | $pkce_code = $this->provider->getPkceCode(); |
| 87 | 84 | |
| 88 | - // Store a session cookie with the PKCE code that we need in order to | |
| 85 | + // Store a transient value with the PKCE code that we need in order to | |
| 89 | 86 | // exchange the returned code for a token after authorization. |
| 90 | - $secure = ! empty( $_SERVER['HTTPS'] ); | |
| 91 | - setcookie( self::PKCE_COOKIE_NAME, $pkce_code, 0, '/', '', $secure, true ); | |
| 87 | + \set_transient( self::PKCE_TRANSIENT_NAME, $pkce_code, \DAY_IN_SECONDS ); | |
| 92 | 88 | |
| 93 | 89 | return $url; |
| 94 | 90 | } |
| 95 | 91 | |
| @@ -102,9 +98,9 @@ | ||
| 102 | 98 | * |
| 103 | 99 | * @throws Authentication_Failed_Exception Exception thrown if authentication has failed. |
| 104 | 100 | */ |
| 105 | 101 | public function request_tokens( $code ) { |
| 106 | - $pkce_code = ! empty( $_COOKIE[ self::PKCE_COOKIE_NAME ] ) ? \sanitize_text_field( \wp_unslash( $_COOKIE[ self::PKCE_COOKIE_NAME ] ) ) : null; | |
| 102 | + $pkce_code = \get_transient( self::PKCE_TRANSIENT_NAME ); | |
| 107 | 103 | if ( $pkce_code ) { |
| 108 | 104 | $this->provider->setPkceCode( $pkce_code ); |
| 109 | 105 | } |
| 110 | 106 | return parent::request_tokens( $code ); |
| @@ -112,8 +108,10 @@ | ||
| 112 | 108 | |
| 113 | 109 | /** |
| 114 | 110 | * Performs the specified request. |
| 115 | 111 | * |
| 112 | + * @codeCoverageIgnore | |
| 113 | + * | |
| 116 | 114 | * @param string $method The HTTP method to use. |
| 117 | 115 | * @param string $url The URL to send the request to. |
| 118 | 116 | * @param array $options The options to pass along to the request. |
| 119 | 117 | * |
| @@ -121,10 +119,8 @@ | ||
| 121 | 119 | * |
| 122 | 120 | * @throws IdentityProviderException Exception thrown if there's something wrong with the identifying data. |
| 123 | 121 | * @throws Authentication_Failed_Exception Exception thrown if authentication has failed. |
| 124 | 122 | * @throws Empty_Token_Exception Exception thrown if the token is empty. |
| 125 | - * | |
| 126 | - * @codeCoverageIgnore | |
| 127 | 123 | */ |
| 128 | 124 | protected function do_request( $method, $url, array $options ) { |
| 129 | 125 | $options['headers'] = [ 'Content-Type' => 'application/json' ]; |
| 130 | 126 | return parent::do_request( $method, $url, $options ); |