Skip to content

Commit bf55133

Browse files
authored
Stop creator token refresh if app connection has been marked as broken (#195)
### Problem The WordPress plugin is generating an excessive number of `POST /api/oauth2/token` requests, many of which return `HTTP 401` responses. #181 introduced logic to detect `401` responses during creator token refresh attempts and mark the app credentials as invalid. It also implemented a 50-second cooldown period to pause further creator token refresh attempts, which helped reduce API traffic. Despite this, clients with invalid credentials are still attempting to refresh creator access tokens, continuing to send unnecessary requests to the Patreon API. To avoid this, apps with invalid credentials should not attempt creator token refreshes at all. ### Solution - Skip creator access token refresh if app credentials are marked invalid. - Move cooldown logic into `refresh_creator_access_token` to consolidate creator token refresh handling.
1 parent df2698f commit bf55133

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

classes/patreon_oauth.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,9 @@ private function __get_or_update_token($params, $disable_app_on_auth_err)
7878
$status_code = wp_remote_retrieve_response_code($response);
7979

8080
if ($disable_app_on_auth_err && 401 == $status_code) {
81-
// Token refresh failed. Mark the app integration credentials as
82-
// bad. This is done for creator access token to prevent spamming
83-
// Patreon's API with token refresh requests using invalid or
84-
// expired credentials. Add a cooldown period when the token refresh
85-
// could be retried.
81+
// Token refresh failed - mark creator credentials invalid to avoid
82+
// spamming Patreon's API with repeated refresh attempts
8683
update_option('patreon-wordpress-app-credentials-failure', true);
87-
set_transient('patreon-wordpress-app-creator-token-refresh-cooldown', true, PATREON_CREATOR_TOKEN_REFRESH_ATTEMPT_COOLDOWN_S);
88-
8984
Patreon_Wordpress::log_connection_error('Failed get/update creator token. HTTP '.$status_code.', Response: '.$response['body']);
9085
} elseif (200 != $status_code) {
9186
Patreon_Wordpress::log_connection_error('Failed get/update token. HTTP '.$status_code.', Response: '.$response['body']);

classes/patreon_wordpress.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,17 +485,23 @@ public static function refresh_creator_access_token()
485485
return false;
486486
}
487487

488-
// Ensure that only one request at a time refreshes the token
488+
if (PatreonApiUtil::is_app_creds_invalid()) {
489+
// Don't attempt creator token refresh if the plugin client
490+
// credentials have been marked as broken
491+
return false;
492+
}
493+
494+
// Ensure that only one request at a time refreshes the token.
495+
// If returning early, make sure that finally block releases the lock.
489496
set_transient($lock_key, true, 120);
490497

491498
try {
492-
if (PatreonApiUtil::is_creator_token_refresh_cooldown()) {
493-
// Don't attempt creator token refresh if the plugin client
494-
// credentials have been marked as broken
499+
// Limit frequency of creator token refreshes
500+
if (PatreonApiUtil::get_creator_token_refresh_cooldown()) {
495501
return false;
496502
}
503+
PatreonApiUtil::set_creator_token_refresh_cooldown();
497504

498-
/* refresh creators token if error 1 */
499505
$refresh_token = get_option('patreon-creators-refresh-token', false);
500506

501507
if (!$refresh_token) {

includes/patreon_api_util.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class PatreonApiUtil
44
{
55
public const CHECK_API_CONNECTION_COOLDOWN_KEY = 'patreon-check-api-connection-cooldown';
6+
public const REFRESH_CREATOR_TOKEN_COOLDOWN_KEY = 'patreon-wordpress-app-creator-token-refresh-cooldown';
67

78
public static function get_default_headers()
89
{
@@ -14,9 +15,14 @@ public static function is_app_creds_invalid()
1415
return get_option('patreon-wordpress-app-credentials-failure', false);
1516
}
1617

17-
public static function is_creator_token_refresh_cooldown()
18+
public static function get_creator_token_refresh_cooldown()
1819
{
19-
return get_transient('patreon-wordpress-app-creator-token-refresh-cooldown');
20+
return get_transient(self::REFRESH_CREATOR_TOKEN_COOLDOWN_KEY);
21+
}
22+
23+
public static function set_creator_token_refresh_cooldown()
24+
{
25+
set_transient(self::REFRESH_CREATOR_TOKEN_COOLDOWN_KEY, true, PATREON_CREATOR_TOKEN_REFRESH_ATTEMPT_COOLDOWN_S);
2026
}
2127

2228
public static function get_check_api_connection_cooldown()

patreon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
define('PATREON_WARNING_IMPORTANT', 'Important: ');
146146
define('PATREON_WARNING_POST_SYNC_SET_WITHOUT_API_V2', 'Important: Post syncing from Patreon is set to on, but your site is using API v1. Post sync wont work without API v2. Follow <a href="https://www.patreondevelopers.com/t/how-to-upgrade-your-patreon-wordpress-to-use-api-v2/3249" target="_blank">this guide</a> to upgrade your site to API v2 or disable post sync <a href="'.admin_url('admin.php?page=patreon-plugin').'">here in settings</a>');
147147
define('PATREON_CHECK_API_CONNECTION_COOLDOWN_S', 10 * 60);
148-
define('PATREON_CREATOR_TOKEN_REFRESH_ATTEMPT_COOLDOWN_S', 5 * 10);
148+
define('PATREON_CREATOR_TOKEN_REFRESH_ATTEMPT_COOLDOWN_S', 60);
149149

150150
require 'classes/patreon_wordpress.php';
151151

0 commit comments

Comments
 (0)