/blog/wp-content/plugins/jetpack/class.jetpack-signature.php

https://gitlab.com/relacilia/cakra · PHP · 188 lines · 167 code · 20 blank · 1 comment · 40 complexity · 3ad3cee28f75fd005e20efe505201d69 MD5 · raw file

  1. <?php
  2. defined( 'JETPACK_SIGNATURE__HTTP_PORT' ) or define( 'JETPACK_SIGNATURE__HTTP_PORT' , 80 );
  3. defined( 'JETPACK_SIGNATURE__HTTPS_PORT' ) or define( 'JETPACK_SIGNATURE__HTTPS_PORT', 443 );
  4. class Jetpack_Signature {
  5. public $token;
  6. public $secret;
  7. function __construct( $access_token, $time_diff = 0 ) {
  8. $secret = explode( '.', $access_token );
  9. if ( 2 != count( $secret ) )
  10. return;
  11. $this->token = $secret[0];
  12. $this->secret = $secret[1];
  13. $this->time_diff = $time_diff;
  14. }
  15. function sign_current_request( $override = array() ) {
  16. if ( isset( $override['scheme'] ) ) {
  17. $scheme = $override['scheme'];
  18. if ( !in_array( $scheme, array( 'http', 'https' ) ) ) {
  19. return new Jetpack_Error( 'invalid_sheme', 'Invalid URL scheme' );
  20. }
  21. } else {
  22. if ( is_ssl() ) {
  23. $scheme = 'https';
  24. } else {
  25. $scheme = 'http';
  26. }
  27. }
  28. if ( is_ssl() ) {
  29. $port = JETPACK_SIGNATURE__HTTPS_PORT == $_SERVER['SERVER_PORT'] ? '' : $_SERVER['SERVER_PORT'];
  30. } else {
  31. $port = JETPACK_SIGNATURE__HTTP_PORT == $_SERVER['SERVER_PORT'] ? '' : $_SERVER['SERVER_PORT'];
  32. }
  33. $url = "{$scheme}://{$_SERVER['HTTP_HOST']}:{$port}" . stripslashes( $_SERVER['REQUEST_URI'] );
  34. if ( array_key_exists( 'body', $override ) && !is_null( $override['body'] ) ) {
  35. $body = $override['body'];
  36. } else if ( 'POST' == strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
  37. $body = isset( $GLOBALS['HTTP_RAW_POST_DATA'] ) ? $GLOBALS['HTTP_RAW_POST_DATA'] : null;
  38. } else {
  39. $body = null;
  40. }
  41. $a = array();
  42. foreach ( array( 'token', 'timestamp', 'nonce', 'body-hash' ) as $parameter ) {
  43. if ( isset( $override[$parameter] ) ) {
  44. $a[$parameter] = $override[$parameter];
  45. } else {
  46. $a[$parameter] = isset( $_GET[$parameter] ) ? stripslashes( $_GET[$parameter] ) : '';
  47. }
  48. }
  49. $method = isset( $override['method'] ) ? $override['method'] : $_SERVER['REQUEST_METHOD'];
  50. return $this->sign_request( $a['token'], $a['timestamp'], $a['nonce'], $a['body-hash'], $method, $url, $body, true );
  51. }
  52. // body_hash v. body-hash is annoying. Refactor to accept an array?
  53. function sign_request( $token = '', $timestamp = 0, $nonce = '', $body_hash = '', $method = '', $url = '', $body = null, $verify_body_hash = true ) {
  54. if ( !$this->secret ) {
  55. return new Jetpack_Error( 'invalid_secret', 'Invalid secret' );
  56. }
  57. if ( !$this->token ) {
  58. return new Jetpack_Error( 'invalid_token', 'Invalid token' );
  59. }
  60. list( $token ) = explode( '.', $token );
  61. if ( 0 !== strpos( $token, "$this->token:" ) ) {
  62. return new Jetpack_Error( 'token_mismatch', 'Incorrect token' );
  63. }
  64. $required_parameters = array( 'token', 'timestamp', 'nonce', 'method', 'url' );
  65. if ( !is_null( $body ) ) {
  66. $required_parameters[] = 'body_hash';
  67. if ( !is_string( $body ) ) {
  68. return new Jetpack_Error( 'invalid_body', 'Body is malformed.' );
  69. }
  70. }
  71. foreach ( $required_parameters as $required ) {
  72. if ( !is_scalar( $$required ) ) {
  73. return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', str_replace( '_', '-', $required ) ) );
  74. }
  75. if ( !strlen( $$required ) ) {
  76. return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is missing.', str_replace( '_', '-', $required ) ) );
  77. }
  78. }
  79. if ( is_null( $body ) ) {
  80. if ( $body_hash ) {
  81. return new Jetpack_Error( 'invalid_body_hash', 'The body hash does not match.' );
  82. }
  83. } else {
  84. if ( $verify_body_hash && jetpack_sha1_base64( $body ) !== $body_hash ) {
  85. return new Jetpack_Error( 'invalid_body_hash', 'The body hash does not match.' );
  86. }
  87. }
  88. $parsed = parse_url( $url );
  89. if ( !isset( $parsed['host'] ) ) {
  90. return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'url' ) );
  91. }
  92. if ( !empty( $parsed['port'] ) ) {
  93. $port = $parsed['port'];
  94. } else {
  95. if ( 'http' == $parsed['scheme'] ) {
  96. $port = 80;
  97. } else if ( 'https' == $parsed['scheme'] ) {
  98. $port = 443;
  99. } else {
  100. return new Jetpack_Error( 'unknown_scheme_port', "The scheme's port is unknown" );
  101. }
  102. }
  103. if ( !ctype_digit( "$timestamp" ) || 10 < strlen( $timestamp ) ) { // If Jetpack is around in 275 years, you can blame mdawaffe for the bug.
  104. return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'timestamp' ) );
  105. }
  106. $local_time = $timestamp - $this->time_diff;
  107. if ( $local_time < time() - 600 || $local_time > time() + 300 ) {
  108. return new Jetpack_Error( 'invalid_signature', 'The timestamp is too old.' );
  109. }
  110. if ( 12 < strlen( $nonce ) || preg_match( '/[^a-zA-Z0-9]/', $nonce ) ) {
  111. return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'nonce' ) );
  112. }
  113. $normalized_request_pieces = array(
  114. $token,
  115. $timestamp,
  116. $nonce,
  117. $body_hash,
  118. strtoupper( $method ),
  119. strtolower( $parsed['host'] ),
  120. $port,
  121. $parsed['path'],
  122. // Normalized Query String
  123. );
  124. $normalized_request_pieces = array_merge( $normalized_request_pieces, $this->normalized_query_parameters( isset( $parsed['query'] ) ? $parsed['query'] : '' ) );
  125. $normalized_request_string = join( "\n", $normalized_request_pieces ) . "\n";
  126. return base64_encode( hash_hmac( 'sha1', $normalized_request_string, $this->secret, true ) );
  127. }
  128. function normalized_query_parameters( $query_string ) {
  129. parse_str( $query_string, $array );
  130. if ( get_magic_quotes_gpc() )
  131. $array = stripslashes_deep( $array );
  132. unset( $array['signature'] );
  133. $names = array_keys( $array );
  134. $values = array_values( $array );
  135. $names = array_map( array( $this, 'encode_3986' ), $names );
  136. $values = array_map( array( $this, 'encode_3986' ), $values );
  137. $pairs = array_map( array( $this, 'join_with_equal_sign' ), $names, $values );
  138. sort( $pairs );
  139. return $pairs;
  140. }
  141. function encode_3986( $string ) {
  142. $string = rawurlencode( $string );
  143. return str_replace( '%7E', '~', $string ); // prior to PHP 5.3, rawurlencode was RFC 1738
  144. }
  145. function join_with_equal_sign( $name, $value ) {
  146. return "{$name}={$value}";
  147. }
  148. }
  149. function jetpack_sha1_base64( $text ) {
  150. return base64_encode( sha1( $text, true ) );
  151. }