RpcAcsRequest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. abstract class RpcAcsRequest extends AcsRequest
  21. {
  22. /**
  23. * @var string
  24. */
  25. private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
  26. /**
  27. * @var array
  28. */
  29. private $domainParameters = array();
  30. /**
  31. * @var string
  32. */
  33. protected $method = 'GET';
  34. /**
  35. * @var string
  36. */
  37. protected $acceptFormat = 'JSON';
  38. /**
  39. * @param string|bool $value
  40. *
  41. * @return string
  42. */
  43. private function prepareValue($value)
  44. {
  45. if (is_bool($value)) {
  46. if ($value) {
  47. return 'true';
  48. }
  49. return 'false';
  50. }
  51. return $value;
  52. }
  53. /**
  54. * @param $iSigner
  55. * @param $credential
  56. * @param $domain
  57. *
  58. * @return bool|mixed|string
  59. */
  60. public function composeUrl($iSigner, $credential, $domain)
  61. {
  62. $apiParams = parent::getQueryParameters();
  63. foreach ($apiParams as $key => $value) {
  64. $apiParams[$key] = $this->prepareValue($value);
  65. }
  66. $apiParams['RegionId'] = $this->getRegionId();
  67. $apiParams['AccessKeyId'] = $credential->getAccessKeyId();
  68. $apiParams['Format'] = $this->getAcceptFormat();
  69. $apiParams['SignatureMethod'] = $iSigner->getSignatureMethod();
  70. $apiParams['SignatureVersion'] = $iSigner->getSignatureVersion();
  71. if ($iSigner->getSignatureType() != null) {
  72. $apiParams['SignatureType'] = $iSigner->getSignatureType();
  73. }
  74. $apiParams['SignatureNonce'] = md5(uniqid(mt_rand(), true));
  75. $apiParams['Timestamp'] = gmdate($this->dateTimeFormat);
  76. $apiParams['Action'] = $this->getActionName();
  77. $apiParams['Version'] = $this->getVersion();
  78. if ($credential->getSecurityToken() != null) {
  79. $apiParams['SecurityToken'] = $credential->getSecurityToken();
  80. }
  81. if ($credential instanceof BearerTokenCredential) {
  82. $apiParams['BearerToken'] = $credential->getBearerToken();
  83. }
  84. $apiParams['Signature'] = $this->computeSignature($apiParams, $credential->getAccessSecret(), $iSigner);
  85. if (parent::getMethod() === 'POST') {
  86. $requestUrl = $this->getProtocol() . '://' . $domain . '/';
  87. foreach ($apiParams as $apiParamKey => $apiParamValue) {
  88. $this->putDomainParameters($apiParamKey, $apiParamValue);
  89. }
  90. return $requestUrl;
  91. }
  92. $requestUrl = $this->getProtocol() . '://' . $domain . '/?';
  93. foreach ($apiParams as $apiParamKey => $apiParamValue) {
  94. $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . '&';
  95. }
  96. return substr($requestUrl, 0, -1);
  97. }
  98. /**
  99. * @param $parameters
  100. * @param $accessKeySecret
  101. * @param $iSigner
  102. *
  103. * @return mixed
  104. */
  105. private function computeSignature($parameters, $accessKeySecret, $iSigner)
  106. {
  107. ksort($parameters);
  108. $canonicalizedQueryString = '';
  109. foreach ($parameters as $key => $value) {
  110. $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
  111. }
  112. $this->stringToBeSigned =
  113. parent::getMethod() . '&%2F&' . $this->percentEncode(substr($canonicalizedQueryString, 1));
  114. return $iSigner->signString($this->stringToBeSigned, $accessKeySecret . '&');
  115. }
  116. /**
  117. * @param $str
  118. *
  119. * @return string|string[]|null
  120. */
  121. protected function percentEncode($str)
  122. {
  123. $res = urlencode($str);
  124. $res = str_replace(array('+', '*'), array('%20', '%2A'), $res);
  125. $res = preg_replace('/%7E/', '~', $res);
  126. return $res;
  127. }
  128. /**
  129. * @return array
  130. */
  131. public function getDomainParameter()
  132. {
  133. return $this->domainParameters;
  134. }
  135. /**
  136. * @param $name
  137. * @param $value
  138. */
  139. public function putDomainParameters($name, $value)
  140. {
  141. $this->domainParameters[$name] = $value;
  142. }
  143. }