RoaAcsRequest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace common\services\mall\aliyunsts\StsCode;
  3. /*
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. */
  21. abstract class RoaAcsRequest extends AcsRequest
  22. {
  23. protected $uriPattern;
  24. private $pathParameters = array();
  25. private $domainParameters = array();
  26. private $dateTimeFormat ="D, d M Y H:i:s \G\M\T";
  27. private static $headerSeparator = "\n";
  28. private static $querySeprator = "&";
  29. function __construct($product, $version, $actionName)
  30. {
  31. parent::__construct($product, $version, $actionName);
  32. $this->setVersion($version);
  33. $this->initialize();
  34. }
  35. private function initialize()
  36. {
  37. $this->setMethod("RAW");
  38. }
  39. public function composeUrl($iSigner, $credential, $domain)
  40. {
  41. $this->prepareHeader($iSigner);
  42. $signString = $this->getMethod().self::$headerSeparator;
  43. if(isset($this->headers["Accept"]))
  44. {
  45. $signString = $signString.$this->headers["Accept"];
  46. }
  47. $signString = $signString.self::$headerSeparator;
  48. if(isset($this->headers["Content-MD5"]))
  49. {
  50. $signString = $signString.$this->headers["Content-MD5"];
  51. }
  52. $signString = $signString.self::$headerSeparator;
  53. if(isset($this->headers["Content-Type"]))
  54. {
  55. $signString = $signString.$this->headers["Content-Type"];
  56. }
  57. $signString = $signString.self::$headerSeparator;
  58. if(isset($this->headers["Date"]))
  59. {
  60. $signString = $signString.$this->headers["Date"];
  61. }
  62. $signString = $signString.self::$headerSeparator;
  63. $uri = $this->replaceOccupiedParameters();
  64. $signString = $signString.$this->buildCanonicalHeaders();
  65. $queryString = $this->buildQueryString($uri);
  66. $signString .= $queryString;
  67. $this->headers["Authorization"] = "acs ".$credential->getAccessKeyId().":"
  68. .$iSigner->signString($signString, $credential->getAccessSecret());
  69. $requestUrl = $this->getProtocol()."://".$domain.$queryString;
  70. return $requestUrl;
  71. }
  72. private function prepareHeader($iSigner)
  73. {
  74. date_default_timezone_set("GMT");
  75. $this->headers["Date"] = date($this->dateTimeFormat);
  76. if(null == $this->acceptFormat)
  77. {
  78. $this->acceptFormat = "RAW";
  79. }
  80. $this->headers["Accept"] = $this->formatToAccept($this->getAcceptFormat());
  81. $this->headers["x-acs-signature-method"] = $iSigner->getSignatureMethod();
  82. $this->headers["x-acs-signature-version"] = $iSigner->getSignatureVersion();
  83. }
  84. private function replaceOccupiedParameters()
  85. {
  86. $result = $this->uriPattern;
  87. foreach ($this->pathParameters as $pathParameterKey => $apiParameterValue)
  88. {
  89. $target = "[".$pathParameterKey."]";
  90. $result = str_replace($target,$apiParameterValue,$result);
  91. }
  92. return $result;
  93. }
  94. private function buildCanonicalHeaders()
  95. {
  96. $sortMap = array();
  97. foreach ($this->headers as $headerKey => $headerValue)
  98. {
  99. $key = strtolower($headerKey);
  100. if(strpos($key, "x-acs-") === 0)
  101. {
  102. $sortMap[$key] = $headerValue;
  103. }
  104. }
  105. ksort($sortMap);
  106. $headerString = "";
  107. foreach ($sortMap as $sortMapKey => $sortMapValue)
  108. {
  109. $headerString = $headerString.$sortMapKey.":".$sortMapValue.self::$headerSeparator;
  110. }
  111. return $headerString;
  112. }
  113. private function splitSubResource($uri)
  114. {
  115. $queIndex = strpos($uri, "?");
  116. $uriParts = array();
  117. if(null != $queIndex)
  118. {
  119. array_push($uriParts, substr($uri,0,$queIndex));
  120. array_push($uriParts, substr($uri,$queIndex+1));
  121. }
  122. else
  123. {
  124. array_push($uriParts,$uri);
  125. }
  126. return $uriParts;
  127. }
  128. private function buildQueryString($uri)
  129. {
  130. $uriParts = $this->splitSubResource($uri);
  131. $sortMap = $this->queryParameters;
  132. if(isset($uriParts[1]))
  133. {
  134. $sortMap[$uriParts[1]] = null;
  135. }
  136. $queryString = $uriParts[0];
  137. if(count($uriParts))
  138. {
  139. $queryString = $queryString."?";
  140. }
  141. ksort($sortMap);
  142. foreach ($sortMap as $sortMapKey => $sortMapValue)
  143. {
  144. $queryString = $queryString.$sortMapKey;
  145. if(isset($sortMapValue))
  146. {
  147. $queryString = $queryString."=".$sortMapValue;
  148. }
  149. $queryString = $queryString.$querySeprator;
  150. }
  151. if(null==count($sortMap))
  152. {
  153. $queryString = substr($queryString, 0, strlen($queryString)-1);
  154. }
  155. return $queryString;
  156. }
  157. private function formatToAccept($acceptFormat)
  158. {
  159. if($acceptFormat == "JSON")
  160. {
  161. return "application/json";
  162. }
  163. elseif ($acceptFormat == "XML") {
  164. return "application/xml";
  165. }
  166. return "application/octet-stream";
  167. }
  168. public function getPathParameters()
  169. {
  170. return $this->pathParameters;
  171. }
  172. public function putPathParameter($name, $value)
  173. {
  174. $this->pathParameters[$name] = $value;
  175. }
  176. public function getDomainParameter()
  177. {
  178. return $this->domainParameters;
  179. }
  180. public function putDomainParameters($name, $value)
  181. {
  182. $this->domainParameters[$name] = $value;
  183. }
  184. public function getUriPattern()
  185. {
  186. return $this->uriPattern;
  187. }
  188. public function setUriPattern($uriPattern)
  189. {
  190. return $this->uriPattern = $uriPattern;
  191. }
  192. public function setVersion($version)
  193. {
  194. $this->version = $version;
  195. $this->headers["x-acs-version"] = $version;
  196. }
  197. }