Sign.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace AlibabaCloud\Client\Support;
  3. use GuzzleHttp\Psr7\Request;
  4. use Psr\Http\Message\UriInterface;
  5. /**
  6. * Class Sign
  7. *
  8. * @package AlibabaCloud\Client\Support
  9. */
  10. class Sign
  11. {
  12. /**
  13. * @var string
  14. */
  15. private static $headerSeparator = "\n";
  16. /**
  17. * Construct standard Header for Alibaba Cloud.
  18. *
  19. * @param array $headers
  20. *
  21. * @return string
  22. */
  23. private static function acsHeaderString(array $headers)
  24. {
  25. $array = [];
  26. foreach ($headers as $headerKey => $headerValue) {
  27. $key = strtolower($headerKey);
  28. if (strncmp($key, 'x-acs-', 6) === 0) {
  29. $array[$key] = $headerValue;
  30. }
  31. }
  32. ksort($array);
  33. $string = '';
  34. foreach ($array as $sortMapKey => $sortMapValue) {
  35. $string .= $sortMapKey . ':' . $sortMapValue[0] . self::$headerSeparator;
  36. }
  37. return $string;
  38. }
  39. /**
  40. * @param UriInterface $uri
  41. *
  42. * @return string
  43. */
  44. private static function resourceString(UriInterface $uri)
  45. {
  46. return $uri->getPath() . '?' . rawurldecode($uri->getQuery());
  47. }
  48. /**
  49. * @param string $method
  50. * @param array $headers
  51. *
  52. * @return string
  53. */
  54. private static function headerString($method, array $headers)
  55. {
  56. $string = $method . self::$headerSeparator;
  57. if (isset($headers['Accept'][0])) {
  58. $string .= $headers['Accept'][0];
  59. }
  60. $string .= self::$headerSeparator;
  61. if (isset($headers['Content-MD5'][0])) {
  62. $string .= $headers['Content-MD5'][0];
  63. }
  64. $string .= self::$headerSeparator;
  65. if (isset($headers['Content-Type'][0])) {
  66. $string .= $headers['Content-Type'][0];
  67. }
  68. $string .= self::$headerSeparator;
  69. if (isset($headers['Date'][0])) {
  70. $string .= $headers['Date'][0];
  71. }
  72. $string .= self::$headerSeparator;
  73. $string .= self::acsHeaderString($headers);
  74. return $string;
  75. }
  76. /**
  77. * @param string $string
  78. *
  79. * @return null|string|string[]
  80. */
  81. private static function percentEncode($string)
  82. {
  83. $result = urlencode($string);
  84. $result = str_replace(['+', '*'], ['%20', '%2A'], $result);
  85. $result = preg_replace('/%7E/', '~', $result);
  86. return $result;
  87. }
  88. /**
  89. * @param string $method
  90. * @param array $parameters
  91. *
  92. * @return string
  93. */
  94. public static function rpcString($method, array $parameters)
  95. {
  96. ksort($parameters);
  97. $canonicalized = '';
  98. foreach ($parameters as $key => $value) {
  99. if ($value === null || $value === '') {
  100. continue;
  101. }
  102. $canonicalized .= '&' . self::percentEncode($key) . '=' . self::percentEncode($value);
  103. }
  104. return $method . '&%2F&' . self::percentEncode(substr($canonicalized, 1));
  105. }
  106. /**
  107. * @param Request $request
  108. *
  109. * @return string
  110. */
  111. public static function roaString(Request $request)
  112. {
  113. return self::headerString($request->getMethod(), $request->getHeaders()) .
  114. self::resourceString($request->getUri());
  115. }
  116. /**
  117. * @param string $salt
  118. *
  119. * @return string
  120. */
  121. public static function uuid($salt)
  122. {
  123. return md5($salt . uniqid(md5(microtime(true)), true)) . microtime();
  124. }
  125. }