From c703b9157aec6cae311242e3ed8cea028376b8af Mon Sep 17 00:00:00 2001 From: ve3 Date: Tue, 28 Nov 2023 00:33:56 +0700 Subject: [PATCH] Fix use `wp_remote_xxx()` instead of `cURL`. --- .phpcs.xml.dist | 4 --- App/Libraries/MyOauth/Google.php | 57 ++++++++++---------------------- 2 files changed, 18 insertions(+), 43 deletions(-) diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index 0a997b6..66564f2 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -106,10 +106,6 @@ - - - - diff --git a/App/Libraries/MyOauth/Google.php b/App/Libraries/MyOauth/Google.php index 4e5cd43..79489bd 100644 --- a/App/Libraries/MyOauth/Google.php +++ b/App/Libraries/MyOauth/Google.php @@ -24,23 +24,6 @@ class Google use \RundizOauth\App\AppTrait; - /** - * @var resource The cUrl resource. - */ - protected $ch; - - - /** - * Initialize cURL and set common option. - */ - protected function curlInit() - { - $this->ch = curl_init(); - - curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); - }// curlInit - - /** * Verify code and get access token. * @@ -62,25 +45,25 @@ protected function getAccessToken($code, $redirect_uri) array_key_exists('google_client_id', $rundizoauth_options) && array_key_exists('google_client_secret', $rundizoauth_options) ) { - $this->curlInit(); - curl_setopt($this->ch, CURLOPT_URL, 'https://oauth2.googleapis.com/token'); - curl_setopt($this->ch, CURLOPT_HTTPHEADER, ['Content-type: application/x-www-form-urlencoded']); - curl_setopt($this->ch, CURLOPT_POST, true); $postData = 'code=' . rawurlencode($code) . '&client_id=' . rawurlencode($rundizoauth_options['google_client_id']) . '&client_secret=' . rawurlencode($rundizoauth_options['google_client_secret']) . '&redirect_uri=' . rawurlencode($redirect_uri) . '&grant_type=authorization_code'; - curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postData); - unset($postData); - $result = curl_exec($this->ch); + $remoteArgs = [ + 'headers' => 'Content-type: application/x-www-form-urlencoded', + 'body' => $postData, + ]; + unset($postData); + $response = wp_remote_post('https://oauth2.googleapis.com/token', $remoteArgs); + unset($remoteArgs); + $result = wp_remote_retrieve_body($response); + unset($response); \RundizOauth\App\Libraries\Logger::writeLog('Google OAuth token result:' . PHP_EOL . $result); $result = json_decode($result); - curl_close($this->ch); - return $result; } } @@ -156,15 +139,14 @@ public function getAuthUrl($redirect_url) */ protected function getUserProfileInfo($access_token) { - $this->curlInit(); - - curl_setopt($this->ch, CURLOPT_URL, 'https://www.googleapis.com/oauth2/v2/userinfo?access_token=' . rawurlencode($access_token)); - - $result = curl_exec($this->ch); + $response = wp_remote_get('https://www.googleapis.com/oauth2/v2/userinfo?access_token=' . rawurlencode($access_token)); + $result = wp_remote_retrieve_body($response); + unset($response); $result = json_decode($result); - curl_close($this->ch); - + if (!is_object($result)) { + return new \stdClass(); + } return $result; }// getUserProfileInfo @@ -400,15 +382,12 @@ protected function validateTokenAndGetAttributes($access_token, $id_token) array_key_exists('google_client_id', $rundizoauth_options) && array_key_exists('google_client_secret', $rundizoauth_options) ) { - $this->curlInit(); - curl_setopt($this->ch, CURLOPT_URL, 'https://oauth2.googleapis.com/tokeninfo?id_token=' . rawurlencode($id_token)); - - $result = curl_exec($this->ch); + $response = wp_remote_get('https://oauth2.googleapis.com/tokeninfo?id_token=' . rawurlencode($id_token)); + $result = wp_remote_retrieve_body($response); + unset($response); \RundizOauth\App\Libraries\Logger::writeLog('Google OAuth validate token: ' . PHP_EOL . $result); $result = json_decode($result); - curl_close($this->ch); - if (isset($result->error_description)) { $output['result'] = false; } elseif (