Merge pull request #16544 from woocommerce/fix/16537

If auth cannot occur, don't send request.
This commit is contained in:
Claudio Sanches 2017-08-21 12:31:46 -03:00 committed by GitHub
commit 237d5b646a
1 changed files with 13 additions and 6 deletions

View File

@ -28,13 +28,15 @@ class WC_Helper_API {
* @param string $endpoint The endpoint to request. * @param string $endpoint The endpoint to request.
* @param array $args Additional data for the request. Set authenticated to a truthy value to enable auth. * @param array $args Additional data for the request. Set authenticated to a truthy value to enable auth.
* *
* @return array The response from wp_safe_remote_request() * @return array|WP_Error The response from wp_safe_remote_request()
*/ */
public static function request( $endpoint, $args = array() ) { public static function request( $endpoint, $args = array() ) {
$url = self::url( $endpoint ); $url = self::url( $endpoint );
if ( ! empty( $args['authenticated'] ) ) { if ( ! empty( $args['authenticated'] ) ) {
self::_authenticate( $url, $args ); if ( ! self::_authenticate( $url, $args ) ) {
return new WP_Error( 'authentication', 'Authentication failed.' );
}
} }
/** /**
@ -52,23 +54,26 @@ class WC_Helper_API {
* *
* @param string $url The request URI. * @param string $url The request URI.
* @param array $args By-ref, the args that will be passed to wp_remote_request(). * @param array $args By-ref, the args that will be passed to wp_remote_request().
* @return bool Were the headers added?
*/ */
private static function _authenticate( $url, &$args ) { private static function _authenticate( $url, &$args ) {
$auth = WC_Helper_Options::get( 'auth' ); $auth = WC_Helper_Options::get( 'auth' );
if ( empty( $auth['access_token'] ) || empty( $auth['access_token_secret'] ) ) { if ( empty( $auth['access_token'] ) || empty( $auth['access_token_secret'] ) ) {
return; return false;
} }
$request_uri = parse_url( $url, PHP_URL_PATH ); $request_uri = parse_url( $url, PHP_URL_PATH );
$query_string = parse_url( $url, PHP_URL_QUERY ); $query_string = parse_url( $url, PHP_URL_QUERY );
if ( $query_string ) { if ( $query_string ) {
$request_uri .= '?' . $query_string; $request_uri .= '?' . $query_string;
} }
$data = array( $data = array(
'host' => parse_url( $url, PHP_URL_HOST ), 'host' => parse_url( $url, PHP_URL_HOST ),
'request_uri' => $request_uri, 'request_uri' => $request_uri,
'method' => ! empty( $args['method'] ) ? $args['method'] : 'GET', 'method' => ! empty( $args['method'] ) ? $args['method'] : 'GET',
); );
if ( ! empty( $args['body'] ) ) { if ( ! empty( $args['body'] ) ) {
@ -84,6 +89,8 @@ class WC_Helper_API {
'Authorization' => 'Bearer ' . $auth['access_token'], 'Authorization' => 'Bearer ' . $auth['access_token'],
'X-Woo-Signature' => $signature, 'X-Woo-Signature' => $signature,
); );
return true;
} }
/** /**