Skip to content

Commit 45197ec

Browse files
committed
Merge pull request WP-API#22 from jtsternberg/master
Create additional methods to handle recursive signature string handling. Closes WP-API#22.
2 parents 9dcded1 + 7d14454 commit 45197ec

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

lib/class-wp-json-authentication-oauth1.php

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function get_parameters( $require_token = true, $extra = array() ) {
115115
_n(
116116
'Missing OAuth parameter %s',
117117
'Missing OAuth parameters %s',
118-
count( $errors )
118+
count( $errors )
119119
),
120120
implode(', ', $errors )
121121
);
@@ -431,7 +431,7 @@ public function get_access_token( $oauth_token ) {
431431
/**
432432
* Generate a new access token
433433
*
434-
* @param string $oauth_consumer_key Consumer key
434+
* @param string $oauth_consumer_key Consumer key
435435
* @param string $oauth_token Request token key
436436
* @return WP_Error|array OAuth token data on success, error otherwise
437437
*/
@@ -528,19 +528,13 @@ protected function check_oauth_signature( $consumer, $oauth_params, $token = nul
528528
unset( $params['oauth_signature'] );
529529

530530
// normalize parameter key/values
531-
array_walk( $params, array( $this, 'normalize_parameters' ) );
531+
array_walk_recursive( $params, array( $this, 'normalize_parameters' ) );
532532

533533
// sort parameters
534534
if ( ! uksort( $params, 'strcmp' ) )
535535
return new WP_Error( 'json_oauth1_failed_parameter_sort', __( 'Invalid Signature - failed to sort parameters' ), array( 'status' => 401 ) );
536536

537-
// form query string
538-
$query_params = array();
539-
540-
foreach ( $params as $param_key => $param_value ) {
541-
$query_params[] = $param_key . '%3D' . $param_value; // join with equals sign
542-
}
543-
$query_string = implode( '%26', $query_params ); // join with ampersand
537+
$query_string = $this->create_signature_string( $params );
544538

545539
$token = (array) $token;
546540
$string_to_sign = $http_method . '&' . $base_request_uri . '&' . $query_string;
@@ -558,7 +552,7 @@ protected function check_oauth_signature( $consumer, $oauth_params, $token = nul
558552
case 'HMAC-SHA256':
559553
$hash_algorithm = 'sha256';
560554
break;
561-
555+
562556
default:
563557
return new WP_Error( 'json_oauth1_invalid_signature_method', __( 'Signature method is invalid' ), array( 'status' => 401 ) );
564558
}
@@ -572,6 +566,41 @@ protected function check_oauth_signature( $consumer, $oauth_params, $token = nul
572566
return true;
573567
}
574568

569+
/**
570+
* Creates a signature string from all query parameters
571+
*
572+
* @since 0.1
573+
* @param array $params Array of query parameters
574+
* @return string Signature string
575+
*/
576+
public function create_signature_string( $params ) {
577+
return implode( '%26', $this->join_with_equals_sign( $params ) ); // join with ampersand
578+
}
579+
580+
/**
581+
* Creates an array of urlencoded strings out of each array key/value pairs
582+
*
583+
* @since 0.1.0
584+
* @param array $params Array of parameters to convert.
585+
* @param array $query_params Array to extend.
586+
* @param string $key Optional Array key to append
587+
* @return string Array of urlencoded strings
588+
*/
589+
public function join_with_equals_sign( $params, $query_params = array(), $key = '' ) {
590+
foreach ( $params as $param_key => $param_value ) {
591+
if ( is_array( $param_value ) ) {
592+
$query_params = $this->join_with_equals_sign( $param_value, $query_params, $param_key );
593+
} else {
594+
if ( $key ) {
595+
$param_key = $key . '[' . $param_key . ']'; // Handle multi-dimensional array
596+
}
597+
$string = $param_key . '=' . $param_value; // join with equals sign
598+
$query_params[] = urlencode( $string );
599+
}
600+
}
601+
return $query_params;
602+
}
603+
575604
/**
576605
* Normalize each parameter by assuming each parameter may have already been encoded, so attempt to decode, and then
577606
* re-encode according to RFC 3986
@@ -588,7 +617,7 @@ protected function normalize_parameters( &$key, &$value ) {
588617

589618
/**
590619
* Verify that the timestamp and nonce provided with the request are valid
591-
*
620+
*
592621
* This prevents replay attacks against the request. A timestamp is only
593622
* valid within 15 minutes of the current time, and a nonce is valid if it
594623
* has not been used within the last 15 minutes.

0 commit comments

Comments
 (0)