Signature.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Qcloud\Cos;
  3. use Psr\Http\Message\RequestInterface;
  4. class Signature {
  5. // string: access key.
  6. private $accessKey;
  7. // string: secret key.
  8. private $secretKey;
  9. // array: cos config.
  10. private $options;
  11. // string: token.
  12. private $token;
  13. // array: sign header.
  14. private $signHeader;
  15. public function __construct( $accessKey, $secretKey, $options, $token = null ) {
  16. $this->accessKey = $accessKey;
  17. $this->secretKey = $secretKey;
  18. $this->options = $options;
  19. $this->token = $token;
  20. $this->signHeader = [
  21. 'cache-control',
  22. 'content-disposition',
  23. 'content-encoding',
  24. 'content-length',
  25. 'content-md5',
  26. 'content-type',
  27. 'expect',
  28. 'expires',
  29. 'host',
  30. 'if-match',
  31. 'if-modified-since',
  32. 'if-none-match',
  33. 'if-unmodified-since',
  34. 'origin',
  35. 'range',
  36. 'transfer-encoding',
  37. ];
  38. date_default_timezone_set($this->options['timezone']);
  39. }
  40. public function __destruct() {
  41. }
  42. public function needCheckHeader( $header ) {
  43. if ( startWith( $header, 'x-cos-' ) ) {
  44. return true;
  45. }
  46. if ( in_array( $header, $this->signHeader ) ) {
  47. return true;
  48. }
  49. return false;
  50. }
  51. public function signRequest( RequestInterface $request ) {
  52. $authorization = $this->createAuthorization( $request );
  53. return $request->withHeader( 'Authorization', $authorization );
  54. }
  55. public function createAuthorization( RequestInterface $request, $expires = '+30 minutes' ) {
  56. if ( is_null( $expires ) || !strtotime( $expires )) {
  57. $expires = '+30 minutes';
  58. }
  59. $signTime = ( string )( time() - 60 ) . ';' . ( string )( strtotime( $expires ) );
  60. $urlParamListArray = [];
  61. foreach ( explode( '&', $request->getUri()->getQuery() ) as $query ) {
  62. if (!empty($query)) {
  63. $tmpquery = explode( '=', $query );
  64. //为了保证CI的key中有=号的情况也能正常通过,ci在这层之前已经encode了,这里需要拆开重新encode,防止上方explode拆错
  65. $key = strtolower( rawurlencode(urldecode($tmpquery[0])) );
  66. if (count($tmpquery) >= 2) {
  67. $value = $tmpquery[1];
  68. } else {
  69. $value = "";
  70. }
  71. //host开关
  72. if (!$this->options['signHost'] && $key == 'host') {
  73. continue;
  74. }
  75. $urlParamListArray[$key] = $key. '='. $value;
  76. }
  77. }
  78. ksort($urlParamListArray);
  79. $urlParamList = join(';', array_keys($urlParamListArray));
  80. $httpParameters = join('&', array_values($urlParamListArray));
  81. $headerListArray = [];
  82. foreach ( $request->getHeaders() as $key => $value ) {
  83. $key = strtolower( urlencode( $key ) );
  84. $value = rawurlencode( $value[0] );
  85. if ( !$this->options['signHost'] && $key == 'host' ) {
  86. continue;
  87. }
  88. if ( $this->needCheckHeader( $key ) ) {
  89. $headerListArray[$key] = $key. '='. $value;
  90. }
  91. }
  92. ksort($headerListArray);
  93. $headerList = join(';', array_keys($headerListArray));
  94. $httpHeaders = join('&', array_values($headerListArray));
  95. $httpString = strtolower( $request->getMethod() ) . "\n" . urldecode( $request->getUri()->getPath() ) . "\n" . $httpParameters.
  96. "\n". $httpHeaders. "\n";
  97. $sha1edHttpString = sha1( $httpString );
  98. $stringToSign = "sha1\n$signTime\n$sha1edHttpString\n";
  99. $signKey = hash_hmac( 'sha1', $signTime, trim($this->secretKey) );
  100. $signature = hash_hmac( 'sha1', $stringToSign, $signKey );
  101. $authorization = 'q-sign-algorithm=sha1&q-ak='. trim($this->accessKey) .
  102. "&q-sign-time=$signTime&q-key-time=$signTime&q-header-list=$headerList&q-url-param-list=$urlParamList&" .
  103. "q-signature=$signature";
  104. return $authorization;
  105. }
  106. public function createPresignedUrl( RequestInterface $request, $expires = '+30 minutes' ) {
  107. $authorization = $this->createAuthorization( $request, $expires);
  108. $uri = $request->getUri();
  109. $query = 'sign='.urlencode( $authorization ) . '&' . $uri->getQuery();
  110. if ( $this->token != null ) {
  111. $query = $query.'&x-cos-security-token='.$this->token;
  112. }
  113. $uri = $uri->withQuery( $query );
  114. return $uri;
  115. }
  116. }