DefaultProfile.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. /**
  21. *
  22. */
  23. define('AUTH_TYPE_RAM_AK', 'RAM_AK');
  24. /**
  25. *
  26. */
  27. define('AUTH_TYPE_RAM_ROLE_ARN', 'RAM_ROLE_ARN');
  28. /**
  29. *
  30. */
  31. define('AUTH_TYPE_ECS_RAM_ROLE', 'ECS_RAM_ROLE');
  32. /**
  33. *
  34. */
  35. define('AUTH_TYPE_BEARER_TOKEN', 'BEARER_TOKEN');
  36. /**
  37. * @deprecated See: https://github.com/aliyun/openapi-sdk-php
  38. * Class DefaultProfile
  39. */
  40. class DefaultProfile implements IClientProfile
  41. {
  42. /**
  43. * @var IClientProfile
  44. */
  45. private static $profile;
  46. /**
  47. * @var array
  48. */
  49. private static $endpoints;
  50. /**
  51. * @var AbstractCredential
  52. */
  53. private static $credential;
  54. /**
  55. * @var string
  56. */
  57. private static $regionId;
  58. /**
  59. * @var string
  60. */
  61. private static $acceptFormat;
  62. /**
  63. * @var string
  64. */
  65. private static $authType;
  66. /**
  67. * @var ISigner
  68. */
  69. private static $isigner;
  70. /**
  71. * @var AbstractCredential
  72. */
  73. private static $iCredential;
  74. /**
  75. * DefaultProfile constructor.
  76. *
  77. * @param $regionId
  78. * @param $credential
  79. * @param string $authType
  80. * @param null $isigner
  81. */
  82. private function __construct($regionId, $credential, $authType = AUTH_TYPE_RAM_AK, $isigner = null)
  83. {
  84. self::$regionId = $regionId;
  85. self::$credential = $credential;
  86. self::$authType = $authType;
  87. self::$isigner = $isigner;
  88. }
  89. /**
  90. * @param $regionId
  91. * @param $accessKeyId
  92. * @param $accessSecret
  93. * @param null $securityToken
  94. *
  95. * @return DefaultProfile|IClientProfile
  96. */
  97. public static function getProfile($regionId, $accessKeyId, $accessSecret, $securityToken = null)
  98. {
  99. $credential = new Credential($accessKeyId, $accessSecret, $securityToken);
  100. self::$profile = new DefaultProfile($regionId, $credential);
  101. return self::$profile;
  102. }
  103. /**
  104. * @param $regionId
  105. * @param $accessKeyId
  106. * @param $accessSecret
  107. * @param $roleArn
  108. * @param $roleSessionName
  109. *
  110. * @return DefaultProfile|IClientProfile
  111. */
  112. public static function getRamRoleArnProfile($regionId, $accessKeyId, $accessSecret, $roleArn, $roleSessionName)
  113. {
  114. $credential = new RamRoleArnCredential($accessKeyId, $accessSecret, $roleArn, $roleSessionName);
  115. self::$profile = new DefaultProfile($regionId, $credential, AUTH_TYPE_RAM_ROLE_ARN);
  116. return self::$profile;
  117. }
  118. /**
  119. * @param $regionId
  120. * @param $roleName
  121. *
  122. * @return DefaultProfile|IClientProfile
  123. */
  124. public static function getEcsRamRoleProfile($regionId, $roleName)
  125. {
  126. $credential = new EcsRamRoleCredential($roleName);
  127. self::$profile = new DefaultProfile($regionId, $credential, AUTH_TYPE_ECS_RAM_ROLE);
  128. return self::$profile;
  129. }
  130. /**
  131. * @param $regionId
  132. * @param $bearerToken
  133. *
  134. * @return DefaultProfile|IClientProfile
  135. */
  136. public static function getBearerTokenProfile($regionId, $bearerToken)
  137. {
  138. $credential = new BearerTokenCredential($bearerToken);
  139. self::$profile = new DefaultProfile($regionId, $credential, AUTH_TYPE_BEARER_TOKEN, new BearTokenSigner());
  140. return self::$profile;
  141. }
  142. /**
  143. * @return ISigner|ShaHmac1Signer|null
  144. */
  145. public function getSigner()
  146. {
  147. if (null == self::$isigner) {
  148. self::$isigner = new ShaHmac1Signer();
  149. }
  150. return self::$isigner;
  151. }
  152. /**
  153. * @return string
  154. */
  155. public function getRegionId()
  156. {
  157. return self::$regionId;
  158. }
  159. /**
  160. * @return string
  161. */
  162. public function getFormat()
  163. {
  164. return self::$acceptFormat;
  165. }
  166. /**
  167. * @return AbstractCredential
  168. */
  169. public function getCredential()
  170. {
  171. if (null == self::$credential && null != self::$iCredential) {
  172. self::$credential = self::$iCredential;
  173. }
  174. return self::$credential;
  175. }
  176. /**
  177. * @return bool
  178. */
  179. public function isRamRoleArn()
  180. {
  181. return self::$authType == AUTH_TYPE_RAM_ROLE_ARN;
  182. }
  183. /**
  184. * @return bool
  185. */
  186. public function isEcsRamRole()
  187. {
  188. return self::$authType == AUTH_TYPE_ECS_RAM_ROLE;
  189. }
  190. /**
  191. * @return array
  192. */
  193. public static function getEndpoints()
  194. {
  195. if (null == self::$endpoints) {
  196. self::$endpoints = EndpointProvider::getEndpoints();
  197. }
  198. return self::$endpoints;
  199. }
  200. /**
  201. * @param $endpointName
  202. * @param $regionId
  203. * @param $product
  204. * @param $domain
  205. */
  206. public static function addEndpoint($endpointName, $regionId, $product, $domain)
  207. {
  208. if (null == self::$endpoints) {
  209. self::$endpoints = self::getEndpoints();
  210. }
  211. $endpoint = self::findEndpointByName($endpointName);
  212. if (null == $endpoint) {
  213. self::addEndpoint_($endpointName, $regionId, $product, $domain);
  214. } else {
  215. self::updateEndpoint($regionId, $product, $domain, $endpoint);
  216. }
  217. LocationService::addEndPoint($regionId, $product, $domain);
  218. }
  219. /**
  220. * @param $endpointName
  221. *
  222. * @return mixed
  223. */
  224. public static function findEndpointByName($endpointName)
  225. {
  226. if (self::$endpoints === null) {
  227. return null;
  228. }
  229. foreach (self::$endpoints as $key => $endpoint) {
  230. if ($endpoint->getName() == $endpointName) {
  231. return $endpoint;
  232. }
  233. }
  234. return null;
  235. }
  236. /**
  237. * @param $endpointName
  238. * @param $regionId
  239. * @param $product
  240. * @param $domain
  241. */
  242. private static function addEndpoint_($endpointName, $regionId, $product, $domain)
  243. {
  244. $regionIds = [$regionId];
  245. $productsDomains = [new ProductDomain($product, $domain)];
  246. $endpoint = new Endpoint($endpointName, $regionIds, $productsDomains);
  247. self::$endpoints[] = $endpoint;
  248. }
  249. /**
  250. * @param string $regionId
  251. * @param string $product
  252. * @param string $domain
  253. * @param Endpoint $endpoint
  254. */
  255. private static function updateEndpoint($regionId, $product, $domain, $endpoint)
  256. {
  257. $regionIds = $endpoint->getRegionIds();
  258. if (!in_array($regionId, $regionIds)) {
  259. $regionIds[] = $regionId;
  260. $endpoint->setRegionIds($regionIds);
  261. }
  262. $productDomains = $endpoint->getProductDomains();
  263. if (null == self::findProductDomainAndUpdate($productDomains, $product, $domain)) {
  264. $productDomains[] = new ProductDomain($product, $domain);
  265. }
  266. $endpoint->setProductDomains($productDomains);
  267. }
  268. /**
  269. * @param $productDomains
  270. * @param $product
  271. * @param $domain
  272. *
  273. * @return string|null
  274. */
  275. private static function findProductDomainAndUpdate($productDomains, $product, $domain)
  276. {
  277. foreach ($productDomains as $key => $productDomain) {
  278. if ($productDomain->getProductName() == $product) {
  279. $productDomain->setDomainName($domain);
  280. return $productDomain;
  281. }
  282. }
  283. return null;
  284. }
  285. }