@@ -115,7 +115,7 @@ public function get_parameters( $require_token = true, $extra = array() ) {
115
115
_n (
116
116
'Missing OAuth parameter %s ' ,
117
117
'Missing OAuth parameters %s ' ,
118
- count ( $ errors )
118
+ count ( $ errors )
119
119
),
120
120
implode (', ' , $ errors )
121
121
);
@@ -431,7 +431,7 @@ public function get_access_token( $oauth_token ) {
431
431
/**
432
432
* Generate a new access token
433
433
*
434
- * @param string $oauth_consumer_key Consumer key
434
+ * @param string $oauth_consumer_key Consumer key
435
435
* @param string $oauth_token Request token key
436
436
* @return WP_Error|array OAuth token data on success, error otherwise
437
437
*/
@@ -528,19 +528,13 @@ protected function check_oauth_signature( $consumer, $oauth_params, $token = nul
528
528
unset( $ params ['oauth_signature ' ] );
529
529
530
530
// normalize parameter key/values
531
- array_walk ( $ params , array ( $ this , 'normalize_parameters ' ) );
531
+ array_walk_recursive ( $ params , array ( $ this , 'normalize_parameters ' ) );
532
532
533
533
// sort parameters
534
534
if ( ! uksort ( $ params , 'strcmp ' ) )
535
535
return new WP_Error ( 'json_oauth1_failed_parameter_sort ' , __ ( 'Invalid Signature - failed to sort parameters ' ), array ( 'status ' => 401 ) );
536
536
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 );
544
538
545
539
$ token = (array ) $ token ;
546
540
$ string_to_sign = $ http_method . '& ' . $ base_request_uri . '& ' . $ query_string ;
@@ -558,7 +552,7 @@ protected function check_oauth_signature( $consumer, $oauth_params, $token = nul
558
552
case 'HMAC-SHA256 ' :
559
553
$ hash_algorithm = 'sha256 ' ;
560
554
break ;
561
-
555
+
562
556
default :
563
557
return new WP_Error ( 'json_oauth1_invalid_signature_method ' , __ ( 'Signature method is invalid ' ), array ( 'status ' => 401 ) );
564
558
}
@@ -572,6 +566,41 @@ protected function check_oauth_signature( $consumer, $oauth_params, $token = nul
572
566
return true ;
573
567
}
574
568
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
+
575
604
/**
576
605
* Normalize each parameter by assuming each parameter may have already been encoded, so attempt to decode, and then
577
606
* re-encode according to RFC 3986
@@ -588,7 +617,7 @@ protected function normalize_parameters( &$key, &$value ) {
588
617
589
618
/**
590
619
* Verify that the timestamp and nonce provided with the request are valid
591
- *
620
+ *
592
621
* This prevents replay attacks against the request. A timestamp is only
593
622
* valid within 15 minutes of the current time, and a nonce is valid if it
594
623
* has not been used within the last 15 minutes.
0 commit comments