DefaultSignature.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Resource\Constants;
  18. use Obs\Internal\Common\Model;
  19. use Obs\Internal\Resource\V2Constants;
  20. class DefaultSignature extends AbstractSignature
  21. {
  22. const INTEREST_HEADER_KEY_LIST = array('content-type', 'content-md5', 'date');
  23. public function __construct($ak, $sk, $pathStyle, $endpoint, $methodName, $signature, $securityToken=false)
  24. {
  25. parent::__construct($ak, $sk, $pathStyle, $endpoint, $methodName, $signature, $securityToken);
  26. }
  27. public function doAuth(array &$requestConfig, array &$params, Model $model)
  28. {
  29. $result = $this -> prepareAuth($requestConfig, $params, $model);
  30. $result['headers']['Date'] = gmdate('D, d M Y H:i:s \G\M\T');
  31. $canonicalstring = $this-> makeCanonicalstring($result['method'], $result['headers'], $result['pathArgs'], $result['dnsParam'], $result['uriParam']);
  32. $result['cannonicalRequest'] = $canonicalstring;
  33. $signature = base64_encode(hash_hmac('sha1', $canonicalstring, $this->sk, true));
  34. $constants = Constants::selectConstants($this -> signature);
  35. $signatureFlag = $constants::FLAG;
  36. $authorization = $signatureFlag . ' ' . $this->ak . ':' . $signature;
  37. $result['headers']['Authorization'] = $authorization;
  38. return $result;
  39. }
  40. public function makeCanonicalstring($method, $headers, $pathArgs, $bucketName, $objectKey, $expires = null)
  41. {
  42. $buffer = [];
  43. $buffer[] = $method;
  44. $buffer[] = "\n";
  45. $interestHeaders = [];
  46. $constants = Constants::selectConstants($this -> signature);
  47. foreach ($headers as $key => $value){
  48. $key = strtolower($key);
  49. if(in_array($key, self::INTEREST_HEADER_KEY_LIST) || strpos($key, $constants::HEADER_PREFIX) === 0){
  50. $interestHeaders[$key] = $value;
  51. }
  52. }
  53. if(array_key_exists($constants::ALTERNATIVE_DATE_HEADER, $interestHeaders)){
  54. $interestHeaders['date'] = '';
  55. }
  56. if($expires !== null){
  57. $interestHeaders['date'] = strval($expires);
  58. }
  59. if(!array_key_exists('content-type', $interestHeaders)){
  60. $interestHeaders['content-type'] = '';
  61. }
  62. if(!array_key_exists('content-md5', $interestHeaders)){
  63. $interestHeaders['content-md5'] = '';
  64. }
  65. ksort($interestHeaders);
  66. foreach ($interestHeaders as $key => $value){
  67. if(strpos($key, $constants::HEADER_PREFIX) === 0){
  68. $buffer[] = $key . ':' . $value;
  69. }else{
  70. $buffer[] = $value;
  71. }
  72. $buffer[] = "\n";
  73. }
  74. $uri = '';
  75. if($bucketName){
  76. $uri .= '/';
  77. $uri .= $bucketName;
  78. if(!$this->pathStyle){
  79. $uri .= '/';
  80. }
  81. }
  82. if($objectKey){
  83. if(!($pos=strripos($uri, '/')) || strlen($uri)-1 !== $pos){
  84. $uri .= '/';
  85. }
  86. $uri .= $objectKey;
  87. }
  88. $buffer[] = $uri === ''? '/' : $uri;
  89. if(!empty($pathArgs)){
  90. ksort($pathArgs);
  91. $_pathArgs = [];
  92. foreach ($pathArgs as $key => $value){
  93. if(in_array(strtolower($key), $constants::ALLOWED_RESOURCE_PARAMTER_NAMES)){
  94. $_pathArgs[] = $value === null || $value === '' ? $key : $key . '=' . urldecode($value);
  95. }
  96. }
  97. if(!empty($_pathArgs)){
  98. $buffer[] = '?';
  99. $buffer[] = implode('&', $_pathArgs);
  100. }
  101. }
  102. return implode('', $buffer);
  103. }
  104. }