RamRoleArnService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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('STS_PRODUCT_NAME', 'Sts');
  24. /**
  25. *
  26. */
  27. define('STS_DOMAIN', 'sts.aliyuncs.com');
  28. /**
  29. *
  30. */
  31. define('STS_VERSION', '2015-04-01');
  32. /**
  33. *
  34. */
  35. define('STS_ACTION', 'AssumeRole');
  36. /**
  37. *
  38. */
  39. define('STS_REGION', 'cn-hangzhou');
  40. /**
  41. *
  42. */
  43. define('ROLE_ARN_EXPIRE_TIME', 3600);
  44. class AssumeRoleRequest extends RpcAcsRequest
  45. {
  46. /**
  47. * AssumeRoleRequest constructor.
  48. *
  49. * @param $roleArn
  50. * @param $roleSessionName
  51. */
  52. public function __construct($roleArn, $roleSessionName)
  53. {
  54. parent::__construct(STS_PRODUCT_NAME, STS_VERSION, STS_ACTION);
  55. $this->queryParameters['RoleArn'] = $roleArn;
  56. $this->queryParameters['RoleSessionName'] = $roleSessionName;
  57. $this->queryParameters['DurationSeconds'] = ROLE_ARN_EXPIRE_TIME;
  58. $this->setRegionId(ROLE_ARN_EXPIRE_TIME);
  59. $this->setProtocol('https');
  60. $this->setAcceptFormat('JSON');
  61. }
  62. }
  63. class RamRoleArnService
  64. {
  65. /**
  66. * @var IClientProfile
  67. */
  68. private $clientProfile;
  69. /**
  70. * @var null|string
  71. */
  72. private $lastClearTime = null;
  73. /**
  74. * @var null|string
  75. */
  76. private $sessionCredential = null;
  77. /**
  78. * @var string
  79. */
  80. public static $serviceDomain = STS_DOMAIN;
  81. /**
  82. * RamRoleArnService constructor.
  83. *
  84. * @param $clientProfile
  85. */
  86. public function __construct($clientProfile)
  87. {
  88. $this->clientProfile = $clientProfile;
  89. }
  90. /**
  91. * @return Credential|string|null
  92. * @throws ClientException
  93. */
  94. public function getSessionCredential()
  95. {
  96. if ($this->lastClearTime != null && $this->sessionCredential != null) {
  97. $now = time();
  98. $elapsedTime = $now - $this->lastClearTime;
  99. if ($elapsedTime <= ROLE_ARN_EXPIRE_TIME * 0.8) {
  100. return $this->sessionCredential;
  101. }
  102. }
  103. $credential = $this->assumeRole();
  104. if ($credential == null) {
  105. return null;
  106. }
  107. $this->sessionCredential = $credential;
  108. $this->lastClearTime = time();
  109. return $credential;
  110. }
  111. /**
  112. * @return Credential|null
  113. * @throws ClientException
  114. */
  115. private function assumeRole()
  116. {
  117. $signer = $this->clientProfile->getSigner();
  118. $ramRoleArnCredential = $this->clientProfile->getCredential();
  119. $request =
  120. new AssumeRoleRequest($ramRoleArnCredential->getRoleArn(), $ramRoleArnCredential->getRoleSessionName());
  121. $requestUrl = $request->composeUrl($signer, $ramRoleArnCredential, self::$serviceDomain);
  122. $httpResponse = HttpHelper::curl($requestUrl, $request->getMethod(), null, $request->getHeaders());
  123. if (!$httpResponse->isSuccess()) {
  124. return null;
  125. }
  126. $respObj = json_decode($httpResponse->getBody());
  127. $sessionAccessKeyId = $respObj->Credentials->AccessKeyId;
  128. $sessionAccessKeySecret = $respObj->Credentials->AccessKeySecret;
  129. $securityToken = $respObj->Credentials->SecurityToken;
  130. return new Credential($sessionAccessKeyId, $sessionAccessKeySecret, $securityToken);
  131. }
  132. }