Update OAuth script
Now uses wp_remote_get() and actually provides some error messages if something goes wrong. I even tested 3 of them! The Google app was no longer set up and I couldn't be bothered.
This commit is contained in:
parent
b88f4972f7
commit
e08aeecc02
@ -325,19 +325,30 @@ if ( ! function_exists( 'fictioneer_process_oauth_discord' ) ) {
|
||||
|
||||
function fictioneer_process_oauth_discord( string $url, string $access_token ) {
|
||||
// Retrieve user data from Discord
|
||||
$user = json_decode(
|
||||
fictioneer_do_curl(
|
||||
$url,
|
||||
'GET',
|
||||
array(
|
||||
"Authorization: Bearer $access_token",
|
||||
'Client-ID: ' . OAUTH2_CLIENT_ID
|
||||
$response = wp_remote_get(
|
||||
$url,
|
||||
array(
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $access_token,
|
||||
'Client-ID' => OAUTH2_CLIENT_ID,
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
fictioneer_oauth_die( $response->get_error_message() );
|
||||
} else {
|
||||
$user = json_decode( wp_remote_retrieve_body( $response ) );
|
||||
}
|
||||
|
||||
// User data successfully retrieved?
|
||||
if ( ! $user || ! $user->verified ) fictioneer_oauth2_exit_and_return();
|
||||
if ( ! isset( $user ) ) {
|
||||
fictioneer_oauth_die( wp_remote_retrieve_body( $response ) );
|
||||
}
|
||||
|
||||
if ( ! isset( $user->verified ) || ! $user->verified ) {
|
||||
fictioneer_oauth_die( 'Account not verified.' );
|
||||
}
|
||||
|
||||
// Login or register user; note may be 'new', 'known', or 'error'
|
||||
$note = fictioneer_make_oauth_user(
|
||||
@ -346,7 +357,7 @@ if ( ! function_exists( 'fictioneer_process_oauth_discord' ) ) {
|
||||
'avatar' => esc_url_raw( "https://cdn.discordapp.com/avatars/{$user->id}/{$user->avatar}.png" ),
|
||||
'channel' => 'discord',
|
||||
'email' => $user->email,
|
||||
'username' => $user->username . $user->discriminator,
|
||||
'username' => $user->username . ( $user->discriminator ?? ''),
|
||||
'nickname' => $user->username
|
||||
)
|
||||
);
|
||||
@ -386,25 +397,32 @@ if ( ! function_exists( 'fictioneer_process_oauth_twitch' ) ) {
|
||||
|
||||
function fictioneer_process_oauth_twitch( string $url, string $access_token ) {
|
||||
// Retrieve user data from Twitch
|
||||
$user = json_decode(
|
||||
fictioneer_do_curl(
|
||||
$url,
|
||||
'GET',
|
||||
array(
|
||||
"Authorization: Bearer $access_token",
|
||||
'Client-ID: ' . OAUTH2_CLIENT_ID
|
||||
$response = wp_remote_get(
|
||||
$url,
|
||||
array(
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $access_token,
|
||||
'Client-ID' => OAUTH2_CLIENT_ID,
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
fictioneer_oauth_die( $response->get_error_message() );
|
||||
} else {
|
||||
$user = json_decode( wp_remote_retrieve_body( $response ) );
|
||||
}
|
||||
|
||||
// User data successfully retrieved?
|
||||
if ( ! $user ) fictioneer_oauth2_exit_and_return();
|
||||
if ( empty( $user ) ) {
|
||||
fictioneer_oauth_die( wp_remote_retrieve_body( $response ) );
|
||||
}
|
||||
|
||||
// Login or register user; note may be 'new', 'known', or 'error'
|
||||
$note = fictioneer_make_oauth_user(
|
||||
array(
|
||||
'uid' => $user->data[0]->id,
|
||||
'avatar' => esc_url_raw( $user->data[0]->profile_image_url ),
|
||||
'avatar' => esc_url_raw( $user->data[0]->profile_image_url ?? '' ),
|
||||
'channel' => 'twitch',
|
||||
'email' => $user->data[0]->email,
|
||||
'username' => $user->data[0]->login,
|
||||
@ -446,25 +464,36 @@ if ( ! function_exists( 'fictioneer_process_oauth_google' ) ) {
|
||||
|
||||
function fictioneer_process_oauth_google( string $url, string $access_token ) {
|
||||
// Retrieve user data from Google
|
||||
$user = json_decode(
|
||||
fictioneer_do_curl(
|
||||
$url,
|
||||
'GET',
|
||||
array(
|
||||
"Authorization: Bearer $access_token",
|
||||
'Client-ID: ' . OAUTH2_CLIENT_ID
|
||||
$response = wp_remote_get(
|
||||
$url,
|
||||
array(
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $access_token,
|
||||
'Client-ID' => OAUTH2_CLIENT_ID,
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
fictioneer_oauth_die( $response->get_error_message() );
|
||||
} else {
|
||||
$user = json_decode( wp_remote_retrieve_body( $response ) );
|
||||
}
|
||||
|
||||
// User data successfully retrieved?
|
||||
if ( ! $user || ! $user->verified_email ) fictioneer_oauth2_exit_and_return();
|
||||
if ( ! isset( $user ) ) {
|
||||
fictioneer_oauth_die( wp_remote_retrieve_body( $response ) );
|
||||
}
|
||||
|
||||
if ( ! isset( $user->verified_email ) || ! $user->verified_email ) {
|
||||
fictioneer_oauth_die( 'Email not verified.' );
|
||||
}
|
||||
|
||||
// Login or register user; note may be 'new', 'merged', 'known', or an error code
|
||||
$note = fictioneer_make_oauth_user(
|
||||
array(
|
||||
'uid' => $user->id,
|
||||
'avatar' => esc_url_raw( $user->picture ),
|
||||
'avatar' => esc_url_raw( $user->picture ?? '' ),
|
||||
'channel' => 'google',
|
||||
'email' => $user->email,
|
||||
'username' => $user->name,
|
||||
@ -509,14 +538,38 @@ if ( ! function_exists( 'fictioneer_process_oauth_patreon' ) ) {
|
||||
$params .= '&include=memberships.currently_entitled_tiers';
|
||||
|
||||
// Retrieve user data from Patreon
|
||||
$user = json_decode(
|
||||
fictioneer_do_curl(
|
||||
$url . $params,
|
||||
'GET',
|
||||
array( "Authorization: Bearer $access_token" )
|
||||
$response = wp_remote_get(
|
||||
$url . $params,
|
||||
array(
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $access_token
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
fictioneer_oauth_die( $response->get_error_message() );
|
||||
} else {
|
||||
$user = json_decode( wp_remote_retrieve_body( $response ) );
|
||||
}
|
||||
|
||||
// User data successfully retrieved?
|
||||
if ( ! isset( $user ) ) {
|
||||
fictioneer_oauth_die( wp_remote_retrieve_body( $response ) );
|
||||
}
|
||||
|
||||
if ( ! isset( $user->data ) ) {
|
||||
fictioneer_oauth_die( 'Data node not found.' );
|
||||
}
|
||||
|
||||
if ( ! isset( $user->data->attributes ) ) {
|
||||
fictioneer_oauth_die( 'Attributes node not found.' );
|
||||
}
|
||||
|
||||
if ( ! isset( $user->data->attributes->is_email_verified ) || ! $user->data->attributes->is_email_verified ) {
|
||||
fictioneer_oauth_die( 'Email not verified.' );
|
||||
}
|
||||
|
||||
// Find Patreon tiers if any
|
||||
$tiers = [];
|
||||
|
||||
@ -532,12 +585,9 @@ if ( ! function_exists( 'fictioneer_process_oauth_patreon' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
// User data successfully retrieved?
|
||||
if ( ! $user || ! $user->data->attributes->is_email_verified ) fictioneer_oauth2_exit_and_return();
|
||||
|
||||
$args = array(
|
||||
'uid' => $user->data->id,
|
||||
'avatar' => esc_url_raw( $user->data->attributes->image_url ),
|
||||
'avatar' => esc_url_raw( $user->data->attributes->image_url ?? '' ),
|
||||
'channel' => 'patreon',
|
||||
'email' => $user->data->attributes->email,
|
||||
'username' => $user->data->attributes->first_name,
|
||||
@ -688,6 +738,26 @@ if ( ! function_exists( 'fictioneer_make_oauth_user' ) ) {
|
||||
// HELPERS
|
||||
// =============================================================================
|
||||
|
||||
if ( ! function_exists( 'fictioneer_oauth_die' ) ) {
|
||||
/**
|
||||
* Outputs a formatted error message and stops script execution
|
||||
*
|
||||
* @since Fictioneer 5.5.2
|
||||
*
|
||||
* @param string $message The error message.
|
||||
* @param string $title Optional. Title of the error page. Default 'Error'.
|
||||
*/
|
||||
|
||||
function fictioneer_oauth_die( $message, $title = 'Error' ) {
|
||||
wp_die(
|
||||
'<h1 style="margin-top: 0;">' . $title . '</h1>' .
|
||||
'<p><pre>' . print_r( $message, true ) . '</pre></p>' .
|
||||
'<p>The good news is, nothing has happened to your account. The bad new is, something is not working. Please try again later or contact an administrator for help. <a href="' . RETURN_URL . '">Back to site</a></p>',
|
||||
$title
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'fictioneer_set_oauth_constants' ) ) {
|
||||
/**
|
||||
* Set up all constants
|
||||
|
@ -96,66 +96,6 @@ if ( ! function_exists( 'fictioneer_seo_plugin_active' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// CURL HELPER
|
||||
// =============================================================================
|
||||
|
||||
if ( ! function_exists( 'fictioneer_do_curl' ) ) {
|
||||
/**
|
||||
* Helper to do cURL
|
||||
*
|
||||
* @since Fictioneer 4.0
|
||||
* @link https://gist.github.com/cp6/aec1e58498d44111c4cbc3606d366367
|
||||
* @link https://www.php.net/manual/en/function.curl-setopt.php
|
||||
*
|
||||
* @param string $url URL string to cURL.
|
||||
* @param string $type Whether to do a GET or POST request. Default 'GET'.
|
||||
* @param array $headers CURLOPT_HTTPHEADER
|
||||
* @param array $post_fields CURLOPT_POSTFIELDS
|
||||
* @param string $user_agent CURLOPT_USERAGENT
|
||||
* @param boolean $follow CURLOPT_FOLLOWLOCATION
|
||||
* @param boolean $use_ssl CURLOPT_SSL_VERIFYHOST, CURLOPT_SSL_VERIFYPEER
|
||||
* @param int $con_timeout CURLOPT_CONNECTTIMEOUT
|
||||
* @param int $timeout URL CURLOPT_TIMEOUT
|
||||
*
|
||||
* @return boolean True if successful, false otherwise
|
||||
*/
|
||||
|
||||
function fictioneer_do_curl( string $url, string $type = 'GET', array $headers = [], array $post_fields = [], string $user_agent = '', string $referrer = '', bool $follow = true, bool $use_ssl = false, int $con_timeout = 10, int $timeout = 40 ) {
|
||||
$crl = curl_init( $url );
|
||||
|
||||
curl_setopt( $crl, CURLOPT_CUSTOMREQUEST, $type );
|
||||
curl_setopt( $crl, CURLOPT_USERAGENT, $user_agent );
|
||||
curl_setopt( $crl, CURLOPT_REFERER, $referrer );
|
||||
|
||||
if ( $type == 'POST' ) {
|
||||
curl_setopt( $crl, CURLOPT_POST, true );
|
||||
|
||||
if ( ! empty( $post_fields ) ) {
|
||||
curl_setopt( $crl, CURLOPT_POSTFIELDS, $post_fields );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $headers ) ) {
|
||||
curl_setopt( $crl, CURLOPT_HTTPHEADER, $headers );
|
||||
}
|
||||
|
||||
curl_setopt( $crl, CURLOPT_FOLLOWLOCATION, $follow );
|
||||
curl_setopt( $crl, CURLOPT_CONNECTTIMEOUT, $con_timeout );
|
||||
curl_setopt( $crl, CURLOPT_TIMEOUT, $timeout );
|
||||
curl_setopt( $crl, CURLOPT_SSL_VERIFYHOST, $use_ssl );
|
||||
curl_setopt( $crl, CURLOPT_SSL_VERIFYPEER, $use_ssl );
|
||||
curl_setopt( $crl, CURLOPT_ENCODING, 'gzip,deflate' );
|
||||
curl_setopt( $crl, CURLOPT_RETURNTRANSFER, true );
|
||||
|
||||
$call_response = curl_exec( $crl );
|
||||
|
||||
curl_close( $crl );
|
||||
|
||||
return $call_response;
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// GET USER BY ID OR EMAIL
|
||||
// =============================================================================
|
||||
|
Loading…
x
Reference in New Issue
Block a user