V4Signature.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Copyright 2019 Huawei Technologies Co.,Ltd.
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  5. * this file except in compliance with the License. You may obtain a copy of the
  6. * License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed
  11. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. * specific language governing permissions and limitations under the License.
  14. *
  15. */
  16. namespace Obs\Internal\Signature;
  17. use Obs\Internal\Common\Model;
  18. class V4Signature extends AbstractSignature
  19. {
  20. const CONTENT_SHA256 = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
  21. protected $region;
  22. protected $utcTimeZone;
  23. public function __construct($ak, $sk, $pathStyle, $endpoint, $region, $methodName, $signature, $securityToken=false)
  24. {
  25. parent::__construct($ak, $sk, $pathStyle, $endpoint, $methodName, $signature, $securityToken);
  26. $this->region = $region;
  27. $this->utcTimeZone = new \DateTimeZone ('UTC');
  28. }
  29. public function doAuth(array &$requestConfig, array &$params, Model $model)
  30. {
  31. $result = $this -> prepareAuth($requestConfig, $params, $model);
  32. $result['headers']['x-amz-content-sha256'] = self::CONTENT_SHA256;
  33. $bucketName = $result['dnsParam'];
  34. $result['headers']['Host'] = $result['host'];
  35. $time = null;
  36. if(array_key_exists('x-amz-date', $result['headers'])){
  37. $time = $result['headers']['x-amz-date'];
  38. }else if(array_key_exists('X-Amz-Date', $result['headers'])){
  39. $time = $result['headers']['X-Amz-Date'];
  40. }
  41. $timestamp = $time ? date_create_from_format('Ymd\THis\Z', $time, $this->utcTimeZone) -> getTimestamp()
  42. :time();
  43. $result['headers']['Date'] = gmdate('D, d M Y H:i:s \G\M\T', $timestamp);
  44. $longDate = gmdate('Ymd\THis\Z', $timestamp);
  45. $shortDate = substr($longDate, 0, 8);
  46. $credential = $this-> getCredential($shortDate);
  47. $signedHeaders = $this->getSignedHeaders($result['headers']);
  48. $canonicalstring = $this-> makeCanonicalstring($result['method'], $result['headers'], $result['pathArgs'], $bucketName, $result['uriParam'], $signedHeaders);
  49. $result['cannonicalRequest'] = $canonicalstring;
  50. $signature = $this -> getSignature($canonicalstring, $longDate, $shortDate);
  51. $authorization = 'AWS4-HMAC-SHA256 ' . 'Credential=' . $credential. ',' . 'SignedHeaders=' . $signedHeaders . ',' . 'Signature=' . $signature;
  52. $result['headers']['Authorization'] = $authorization;
  53. return $result;
  54. }
  55. public function getSignature($canonicalstring, $longDate, $shortDate)
  56. {
  57. $stringToSign = [];
  58. $stringToSign[] = 'AWS4-HMAC-SHA256';
  59. $stringToSign[] = "\n";
  60. $stringToSign[] = $longDate;
  61. $stringToSign[] = "\n";
  62. $stringToSign[] = $this -> getScope($shortDate);
  63. $stringToSign[] = "\n";
  64. $stringToSign[] = hash('sha256', $canonicalstring);
  65. $dateKey = hash_hmac('sha256', $shortDate, 'AWS4' . $this -> sk, true);
  66. $regionKey = hash_hmac('sha256', $this->region, $dateKey, true);
  67. $serviceKey = hash_hmac('sha256', 's3', $regionKey, true);
  68. $signingKey = hash_hmac('sha256', 'aws4_request', $serviceKey, true);
  69. $signature = hash_hmac('sha256', implode('', $stringToSign), $signingKey);
  70. return $signature;
  71. }
  72. public function getCanonicalQueryString($pathArgs)
  73. {
  74. $queryStr = '';
  75. ksort($pathArgs);
  76. $index = 0;
  77. foreach ($pathArgs as $key => $value){
  78. $queryStr .= $key . '=' . $value;
  79. if($index++ !== count($pathArgs) - 1){
  80. $queryStr .= '&';
  81. }
  82. }
  83. return $queryStr;
  84. }
  85. public function getCanonicalHeaders($headers)
  86. {
  87. $_headers = [];
  88. foreach ($headers as $key => $value) {
  89. $_headers[strtolower($key)] = $value;
  90. }
  91. ksort($_headers);
  92. $canonicalHeaderStr = '';
  93. foreach ($_headers as $key => $value){
  94. $value = is_array($value) ? implode(',', $value) : $value;
  95. $canonicalHeaderStr .= $key . ':' . $value;
  96. $canonicalHeaderStr .= "\n";
  97. }
  98. return $canonicalHeaderStr;
  99. }
  100. public function getCanonicalURI($bucketName, $objectKey)
  101. {
  102. $uri = '';
  103. if($this -> pathStyle && $bucketName){
  104. $uri .= '/' . $bucketName;
  105. }
  106. if($objectKey){
  107. $uri .= '/' . $objectKey;
  108. }
  109. if($uri === ''){
  110. $uri = '/';
  111. }
  112. return $uri;
  113. }
  114. public function makeCanonicalstring($method, $headers, $pathArgs, $bucketName, $objectKey, $signedHeaders=null, $payload=null)
  115. {
  116. $buffer = [];
  117. $buffer[] = $method;
  118. $buffer[] = "\n";
  119. $buffer[] = $this->getCanonicalURI($bucketName, $objectKey);
  120. $buffer[] = "\n";
  121. $buffer[] = $this->getCanonicalQueryString($pathArgs);
  122. $buffer[] = "\n";
  123. $buffer[] = $this->getCanonicalHeaders($headers);
  124. $buffer[] = "\n";
  125. $buffer[] = $signedHeaders ? $signedHeaders : $this->getSignedHeaders($headers);
  126. $buffer[] = "\n";
  127. $buffer[] = $payload ? strval($payload) : self::CONTENT_SHA256;
  128. return implode('', $buffer);
  129. }
  130. public function getSignedHeaders($headers)
  131. {
  132. $_headers = [];
  133. foreach ($headers as $key => $value) {
  134. $_headers[] = strtolower($key);
  135. }
  136. sort($_headers);
  137. $signedHeaders = '';
  138. foreach ($_headers as $key => $value){
  139. $signedHeaders .= $value;
  140. if($key !== count($_headers) - 1){
  141. $signedHeaders .= ';';
  142. }
  143. }
  144. return $signedHeaders;
  145. }
  146. public function getScope($shortDate)
  147. {
  148. return $shortDate . '/' . $this->region . '/s3/aws4_request';
  149. }
  150. public function getCredential($shortDate)
  151. {
  152. return $this->ak . '/' . $this->getScope($shortDate);
  153. }
  154. }