Signature.php 4.5 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. // bool: host trigger
  10. private $signHost;
  11. public function __construct( $accessKey, $secretKey, $signHost, $token = null ) {
  12. $this->accessKey = $accessKey;
  13. $this->secretKey = $secretKey;
  14. $this->signHost = $signHost;
  15. $this->token = $token;
  16. $this->signHeader = [
  17. 'cache-control',
  18. 'content-disposition',
  19. 'content-encoding',
  20. 'content-length',
  21. 'content-md5',
  22. 'content-type',
  23. 'expect',
  24. 'expires',
  25. 'host',
  26. 'if-match',
  27. 'if-modified-since',
  28. 'if-none-match',
  29. 'if-unmodified-since',
  30. 'origin',
  31. 'range',
  32. 'response-cache-control',
  33. 'response-content-disposition',
  34. 'response-content-encoding',
  35. 'response-content-language',
  36. 'response-content-type',
  37. 'response-expires',
  38. 'transfer-encoding',
  39. 'versionid',
  40. ];
  41. date_default_timezone_set( 'PRC' );
  42. }
  43. public function __destruct() {
  44. }
  45. public function needCheckHeader( $header ) {
  46. if ( startWith( $header, 'x-cos-' ) ) {
  47. return true;
  48. }
  49. if ( in_array( $header, $this->signHeader ) ) {
  50. return true;
  51. }
  52. return false;
  53. }
  54. public function signRequest( RequestInterface $request ) {
  55. $authorization = $this->createAuthorization( $request );
  56. return $request->withHeader( 'Authorization', $authorization );
  57. }
  58. public function createAuthorization( RequestInterface $request, $expires = '+30 minutes' ) {
  59. if ( is_null( $expires ) || !strtotime( $expires )) {
  60. $expires = '+30 minutes';
  61. }
  62. $signTime = ( string )( time() - 60 ) . ';' . ( string )( strtotime( $expires ) );
  63. $urlParamListArray = [];
  64. foreach ( explode( '&', $request->getUri()->getQuery() ) as $query ) {
  65. if (!empty($query)) {
  66. $tmpquery = explode( '=', $query );
  67. $key = strtolower( $tmpquery[0] );
  68. if (count($tmpquery) >= 2) {
  69. $value = $tmpquery[1];
  70. } else {
  71. $value = "";
  72. }
  73. //host开关
  74. if (!$this->signHost && $key == 'host') {
  75. continue;
  76. }
  77. $urlParamListArray[$key] = $key. '='. $value;
  78. }
  79. }
  80. ksort($urlParamListArray);
  81. $urlParamList = join(';', array_keys($urlParamListArray));
  82. $httpParameters = join('&', array_values($urlParamListArray));
  83. $headerListArray = [];
  84. foreach ( $request->getHeaders() as $key => $value ) {
  85. $key = strtolower( urlencode( $key ) );
  86. $value = rawurlencode( $value[0] );
  87. if ( !$this->signHost && $key == 'host' ) {
  88. continue;
  89. }
  90. if ( $this->needCheckHeader( $key ) ) {
  91. $headerListArray[$key] = $key. '='. $value;
  92. }
  93. }
  94. ksort($headerListArray);
  95. $headerList = join(';', array_keys($headerListArray));
  96. $httpHeaders = join('&', array_values($headerListArray));
  97. $httpString = strtolower( $request->getMethod() ) . "\n" . urldecode( $request->getUri()->getPath() ) . "\n" . $httpParameters.
  98. "\n". $httpHeaders. "\n";
  99. $sha1edHttpString = sha1( $httpString );
  100. $stringToSign = "sha1\n$signTime\n$sha1edHttpString\n";
  101. $signKey = hash_hmac( 'sha1', $signTime, trim($this->secretKey) );
  102. $signature = hash_hmac( 'sha1', $stringToSign, $signKey );
  103. $authorization = 'q-sign-algorithm=sha1&q-ak='. trim($this->accessKey) .
  104. "&q-sign-time=$signTime&q-key-time=$signTime&q-header-list=$headerList&q-url-param-list=$urlParamList&" .
  105. "q-signature=$signature";
  106. return $authorization;
  107. }
  108. public function createPresignedUrl( RequestInterface $request, $expires = '+30 minutes' ) {
  109. $authorization = $this->createAuthorization( $request, $expires);
  110. $uri = $request->getUri();
  111. $query = 'sign='.urlencode( $authorization ) . '&' . $uri->getQuery();
  112. if ( $this->token != null ) {
  113. $query = $query.'&x-cos-security-token='.$this->token;
  114. }
  115. $uri = $uri->withQuery( $query );
  116. return $uri;
  117. }
  118. }