Skip to content

Instantly share code, notes, and snippets.

@justingreerbbi
Created November 13, 2021 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justingreerbbi/e516e4f552ca12de867c7de6ef966bbe to your computer and use it in GitHub Desktop.
Save justingreerbbi/e516e4f552ca12de867c7de6ef966bbe to your computer and use it in GitHub Desktop.
Increase access token after successful use but X minutes. This is for WordPress OAuth Server. Place this snippet into your themes functions.php file. It taps into the authenticated action WP OAuth Server uses when checking authorization of a call.
/**
* UPDATES AN ACCESS TOKEN'S EXPIRE TIME BY X AMOUNT OF TIME WHEN SUCCESSFULLY USED FOR AUTHENTICATION
*
* NOTE: This will override any token settings used within the plugin but does allow for active tokens to
* stay active instead of expiring. This is useful for auto deauthentication tht is traditionally handled on the
* client side.
*/
add_action( 'wo_endpoint_user_authenticated', 'wp_oauth_successful_authentication_action' );
function wp_oauth_successful_authentication_action( $token ) {
//$current_expires = $token[0]['expires'];
$access_token = $token[0]['access_token'];
$minutes = 30;
$new_expires = date( 'Y-m-d H:i:s', current_time( 'timestamp' ) + ( $minutes * 60 ) );
global $wpdb;
$update = $wpdb->update( $wpdb->prefix . 'oauth_access_tokens', array(
'expires' => $new_expires
), array( 'access_token' => $access_token ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment