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