Making WordPress.org

Changeset 4597


Ignore:
Timestamp:
12/27/2016 08:17:57 PM (9 years ago)
Author:
iandunn
Message:

WordCamp Remote CSS: Add GitHub API credentials to avoid rate limiting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-remote-css/platforms/github.php

    r2124 r4597  
    22
    33namespace WordCamp\RemoteCSS;
     4use WP_Error;
    45
    56defined( 'WPINC' ) or die();
     
    1213add_filter( 'wcrcss_trusted_remote_hostnames', __NAMESPACE__ . '\whitelist_trusted_hostnames' );
    1314add_filter( 'wcrcss_validate_remote_css_url',  __NAMESPACE__ . '\convert_to_api_urls'         );
     15add_filter( 'pre_http_request',                __NAMESPACE__ . '\authenticate_requests', 10, 3 );
    1416add_filter( 'wcrcss_unsafe_remote_css',        __NAMESPACE__ . '\decode_api_response',  10, 2 );
    1517
     
    6567
    6668/**
     69 * Add authentication parameters to GitHub API requests
     70 *
     71 * This allows us to make 5k requests per hour, instead of just 60.
     72 *
     73 * @param false|array|WP_Error $preempt      See `pre_http_request`
     74 * @param array                $request_args
     75 * @param string               $request_url
     76 *
     77 * @return false|array|WP_Error
     78 */
     79function authenticate_requests( $preempt, $request_args, $request_url ) {
     80    $parsed_url = parse_url( $request_url );
     81
     82    /*
     83     * SECURITY: Make sure we're only authorizing the requests we're intending to, to avoid the possibility of
     84     * the keys being used for another purpose. That's not likely, but it's better to err on the side of caution.
     85     */
     86    $is_relevant_request = GITHUB_API_HOSTNAME === $parsed_url['host']                 &&
     87                           'GET'               === $request_args['method']             &&
     88                           '/repos'            === substr( $parsed_url['path'], 0, 6 ) &&
     89                           '.css'              === substr( $parsed_url['path'], strlen( $parsed_url['path'] ) - 4 );
     90
     91    if ( $is_relevant_request ) {
     92        if ( isset( $parsed_url['query'] ) ) {
     93            parse_str( $parsed_url['query'], $request_query_params );
     94        } else {
     95            $request_query_params = array();
     96        }
     97
     98        $has_authentication_params = array_key_exists( 'client_id',     $request_query_params ) &&
     99                                     array_key_exists( 'client_secret', $request_query_params );
     100
     101        if ( ! $has_authentication_params ) {
     102            $request_url = add_query_arg(
     103                array(
     104                    'client_id'     => REMOTE_CSS_GITHUB_ID,
     105                    'client_secret' => REMOTE_CSS_GITHUB_SECRET
     106                ),
     107                $request_url
     108            );
     109
     110            $preempt = wp_remote_get( $request_url, $request_args );
     111        }
     112    }
     113
     114    return $preempt;
     115}
     116
     117/**
    67118 * Decode the file contents from GitHub's API response
    68119 *
Note: See TracChangeset for help on using the changeset viewer.