AipSampleSigner.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace common\helpers\ocr\baidu\lib;
  3. class AipSampleSigner
  4. {
  5. const BCE_AUTH_VERSION = "bce-auth-v1";
  6. const BCE_PREFIX = 'x-bce-';
  7. //不指定headersToSign情况下,默认签名http头,包括:
  8. // 1.host
  9. // 2.content-length
  10. // 3.content-type
  11. // 4.content-md5
  12. public static $defaultHeadersToSign;
  13. public static function __init()
  14. {
  15. AipSampleSigner::$defaultHeadersToSign = array(
  16. "host",
  17. "content-length",
  18. "content-type",
  19. "content-md5",
  20. );
  21. }
  22. /**
  23. * 签名
  24. * @param array $credentials
  25. * @param string $httpMethod
  26. * @param string $path
  27. * @param array $headers
  28. * @param string $params
  29. * @param array $options
  30. * @return string
  31. */
  32. public static function sign(
  33. array $credentials,
  34. $httpMethod,
  35. $path,
  36. $headers,
  37. $params,
  38. $options = array()
  39. ) {
  40. //设定签名有效时间
  41. if (!isset($options[AipSignOption::EXPIRATION_IN_SECONDS])) {
  42. //默认值1800秒
  43. $expirationInSeconds = AipSignOption::DEFAULT_EXPIRATION_IN_SECONDS;
  44. } else {
  45. $expirationInSeconds = $options[AipSignOption::EXPIRATION_IN_SECONDS];
  46. }
  47. //解析ak sk
  48. $accessKeyId = $credentials['ak'];
  49. $secretAccessKey = $credentials['sk'];
  50. //设定时间戳,注意:如果自行指定时间戳需要为UTC时间
  51. if (!isset($options[AipSignOption::TIMESTAMP])) {
  52. //默认值当前时间
  53. $timestamp = gmdate('Y-m-d\TH:i:s\Z');
  54. } else {
  55. $timestamp = $options[AipSignOption::TIMESTAMP];
  56. }
  57. //生成authString
  58. $authString = AipSampleSigner::BCE_AUTH_VERSION . '/' . $accessKeyId . '/'
  59. . $timestamp . '/' . $expirationInSeconds;
  60. //使用sk和authString生成signKey
  61. $signingKey = hash_hmac('sha256', $authString, $secretAccessKey);
  62. //生成标准化URI
  63. $canonicalURI = AipHttpUtil::getCanonicalURIPath($path);
  64. //生成标准化QueryString
  65. $canonicalQueryString = AipHttpUtil::getCanonicalQueryString($params);
  66. //填充headersToSign,也就是指明哪些header参与签名
  67. $headersToSign = null;
  68. if (isset($options[AipSignOption::HEADERS_TO_SIGN])) {
  69. $headersToSign = $options[AipSignOption::HEADERS_TO_SIGN];
  70. }
  71. //生成标准化header
  72. $canonicalHeader = AipHttpUtil::getCanonicalHeaders(
  73. AipSampleSigner::getHeadersToSign($headers, $headersToSign)
  74. );
  75. //整理headersToSign,以';'号连接
  76. $signedHeaders = '';
  77. if ($headersToSign !== null) {
  78. $signedHeaders = strtolower(
  79. trim(implode(";", $headersToSign))
  80. );
  81. }
  82. //组成标准请求串
  83. $canonicalRequest = "$httpMethod\n$canonicalURI\n"
  84. . "$canonicalQueryString\n$canonicalHeader";
  85. //使用signKey和标准请求串完成签名
  86. $signature = hash_hmac('sha256', $canonicalRequest, $signingKey);
  87. //组成最终签名串
  88. $authorizationHeader = "$authString/$signedHeaders/$signature";
  89. return $authorizationHeader;
  90. }
  91. /**
  92. * 根据headsToSign过滤应该参与签名的header
  93. * @param array $headers
  94. * @param array $headersToSign
  95. * @return array
  96. */
  97. public static function getHeadersToSign($headers, $headersToSign)
  98. {
  99. //value被trim后为空串的header不参与签名
  100. $filter_empty = function($v) {
  101. return trim((string) $v) !== '';
  102. };
  103. $headers = array_filter($headers, $filter_empty);
  104. //处理headers的key:去掉前后的空白并转化成小写
  105. $trim_and_lower = function($str){
  106. return strtolower(trim($str));
  107. };
  108. $temp = array();
  109. $process_keys = function($k, $v) use(&$temp, $trim_and_lower) {
  110. $temp[$trim_and_lower($k)] = $v;
  111. };
  112. array_map($process_keys, array_keys($headers), $headers);
  113. $headers = $temp;
  114. //取出headers的key以备用
  115. $header_keys = array_keys($headers);
  116. $filtered_keys = null;
  117. if ($headersToSign !== null) {
  118. //如果有headersToSign,则根据headersToSign过滤
  119. //预处理headersToSign:去掉前后的空白并转化成小写
  120. $headersToSign = array_map($trim_and_lower, $headersToSign);
  121. //只选取在headersToSign里面的header
  122. $filtered_keys = array_intersect_key($header_keys, $headersToSign);
  123. } else {
  124. //如果没有headersToSign,则根据默认规则来选取headers
  125. $filter_by_default = function($k) {
  126. return AipSampleSigner::isDefaultHeaderToSign($k);
  127. };
  128. $filtered_keys = array_filter($header_keys, $filter_by_default);
  129. }
  130. //返回需要参与签名的header
  131. return array_intersect_key($headers, array_flip($filtered_keys));
  132. }
  133. /**
  134. * 检查header是不是默认参加签名的:
  135. * 1.是host、content-type、content-md5、content-length之一
  136. * 2.以x-bce开头
  137. * @param $header
  138. * @return boolean
  139. */
  140. public static function isDefaultHeaderToSign($header)
  141. {
  142. $header = strtolower(trim($header));
  143. if (in_array($header, AipSampleSigner::$defaultHeadersToSign)) {
  144. return true;
  145. }
  146. return substr_compare($header, AipSampleSigner::BCE_PREFIX, 0, strlen(AipSampleSigner::BCE_PREFIX)) == 0;
  147. }
  148. }