Skip to content

Commit

Permalink
Fix use wp_remote_xxx() instead of cURL.
Browse files Browse the repository at this point in the history
  • Loading branch information
ve3 committed Nov 27, 2023
1 parent ff9af2c commit c703b91
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 43 deletions.
4 changes: 0 additions & 4 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@
<exclude name="WordPress.WhiteSpace.ControlStructureSpacing"></exclude>
<exclude name="WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore"></exclude>
<exclude name="WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter"></exclude>
<exclude name="WordPress.WP.AlternativeFunctions.curl_curl_close"></exclude>
<exclude name="WordPress.WP.AlternativeFunctions.curl_curl_exec"></exclude>
<exclude name="WordPress.WP.AlternativeFunctions.curl_curl_init"></exclude>
<exclude name="WordPress.WP.AlternativeFunctions.curl_curl_setopt"></exclude>
<exclude name="WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents"></exclude>
<exclude name="WordPress.WP.EnqueuedResourceParameters.MissingVersion"></exclude>
<exclude name="WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion"></exclude>
Expand Down
57 changes: 18 additions & 39 deletions App/Libraries/MyOauth/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit c703b91

Please sign in to comment.