WordPress.org

Making WordPress.org


Ignore:
Timestamp:
08/17/2019 12:35:04 AM (8 months ago)
Author:
coreymckrill
Message:

Official WordPress Events: Tweak Meetup client to try to avoid auth token probs

This attempts to avoid the scenario where the client has somehow stored an
oauth token that is expired/invalid, and keeps using it for requests anyway.
Now it will try to detect when the token it has isn't working and refresh it
without breaking the current request cycle.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-client.php

    r9105 r9107  
    7373        );
    7474
     75        $this->debug = $settings['debug'];
     76
     77        if ( $this->debug ) {
     78            self::cli_message( "Meetup Client debug is on. Results will be truncated." );
     79        }
     80
    7581        $this->oauth_client = new Meetup_OAuth2_Client;
    76         $this->debug        = $settings['debug'];
    77 
    78         if ( $this->debug ) {
    79             self::cli_message( "Meetup Client debug is ON. Results will be truncated." );
     82
     83        if ( ! empty( $this->oauth_client->error->get_error_messages() ) ) {
     84            $this->error = $this->merge_errors( $this->error, $this->oauth_client->error );
     85        }
     86
     87        add_action( 'api_client_tenacious_remote_request_attempt', array( $this, 'maybe_reset_oauth_token' ) );
     88    }
     89
     90    /**
     91     * Attempt to fix authorization errors before they permanently fail.
     92     *
     93     * Hooked to `api_client_tenacious_remote_request_attempt` so that a request that has failed due to an invalid
     94     * oauth token can be retried after resetting the token.
     95     *
     96     * @param array $response
     97     *
     98     * @return void
     99     */
     100    public function maybe_reset_oauth_token( $response ) {
     101        $code = wp_remote_retrieve_response_code( $response );
     102        $body = json_decode( wp_remote_retrieve_body( $response ), true );
     103
     104        if ( 400 === $code && ! empty( $body['error'] ) && 'invalid_grant' === $body['error'] ) {
     105            $this->oauth_client->reset_oauth_token();
     106
     107            if ( ! empty( $this->oauth_client->error->get_error_messages() ) ) {
     108                $this->error = $this->merge_errors( $this->error, $this->oauth_client->error );
     109            }
     110
     111            // Reset the request headers, so that they include the new oauth token.
     112            $this->current_request_args = $this->get_request_args();
    80113        }
    81114    }
     
    123156                $request_url = $this->get_next_url( $response );
    124157            } else {
    125                 $this->handle_error_response( $response );
     158                $this->handle_error_response( $response, $request_url );
    126159                break;
    127160            }
     
    168201            }
    169202        } else {
    170             $this->handle_error_response( $response );
     203            $this->handle_error_response( $response, $request_url );
    171204        }
    172205
     
    295328                );
    296329            }
     330        } elseif ( isset( $data['error'], $data['error_description'] ) ) {
     331            $this->error->add(
     332                $data['error'],
     333                $data['error_description']
     334            );
    297335        } elseif ( isset( $data['code'] ) && isset( $data['details'] ) ) {
    298336            $this->error->add(
Note: See TracChangeset for help on using the changeset viewer.