RsaKeyPairCredentialsProvider.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace AlibabaCloud\Credentials\Providers;
  3. use AlibabaCloud\Credentials\Utils\Helper;
  4. use AlibabaCloud\Credentials\Utils\Filter;
  5. use AlibabaCloud\Credentials\Request\Request;
  6. use GuzzleHttp\Psr7\Uri;
  7. use GuzzleHttp\Exception\GuzzleException;
  8. use InvalidArgumentException;
  9. use RuntimeException;
  10. use Exception;
  11. /**
  12. * @internal This class is intended for internal use within the package.
  13. * Class RsaKeyPairCredentialsProvider
  14. *
  15. * @package AlibabaCloud\Credentials\Providers
  16. */
  17. class RsaKeyPairCredentialsProvider extends SessionCredentialsProvider
  18. {
  19. /**
  20. * @var string
  21. */
  22. private $publicKeyId;
  23. /**
  24. * @var string
  25. */
  26. private $privateKey;
  27. /**
  28. * @description role session expiration
  29. * @example 3600
  30. * @var int
  31. */
  32. private $durationSeconds = 3600;
  33. /**
  34. * @var string
  35. */
  36. private $stsEndpoint;
  37. /**
  38. * @var int
  39. */
  40. private $connectTimeout = 5;
  41. /**
  42. * @var int
  43. */
  44. private $readTimeout = 5;
  45. /**
  46. * RsaKeyPairCredentialsProvider constructor.
  47. *
  48. * @param array $params
  49. * @param array $options
  50. */
  51. public function __construct(array $params = [], array $options = [])
  52. {
  53. $this->filterOptions($options);
  54. $this->filterDurationSeconds($params);
  55. $this->filterSTSEndpoint($params);
  56. $this->publicKeyId = isset($params['publicKeyId']) ? $params['publicKeyId'] : null;
  57. $privateKeyFile = isset($params['privateKeyFile']) ? $params['privateKeyFile'] : null;
  58. Filter::publicKeyId($this->publicKeyId);
  59. Filter::privateKeyFile($privateKeyFile);
  60. try {
  61. $this->privateKey = file_get_contents($privateKeyFile);
  62. } catch (Exception $exception) {
  63. throw new InvalidArgumentException($exception->getMessage());
  64. }
  65. }
  66. private function filterOptions(array $options)
  67. {
  68. if (isset($options['connectTimeout'])) {
  69. $this->connectTimeout = $options['connectTimeout'];
  70. }
  71. if (isset($options['readTimeout'])) {
  72. $this->readTimeout = $options['readTimeout'];
  73. }
  74. Filter::timeout($this->connectTimeout, $this->readTimeout);
  75. }
  76. private function filterDurationSeconds(array $params)
  77. {
  78. if (isset($params['durationSeconds'])) {
  79. if (is_int($params['durationSeconds'])) {
  80. $this->durationSeconds = $params['durationSeconds'];
  81. }
  82. }
  83. if ($this->durationSeconds < 900) {
  84. throw new InvalidArgumentException('Role session expiration should be in the range of 900s - max session duration');
  85. }
  86. }
  87. private function filterSTSEndpoint(array $params)
  88. {
  89. if (isset($params['stsEndpoint'])) {
  90. $this->stsEndpoint = $params['stsEndpoint'];
  91. }
  92. if (is_null($this->stsEndpoint) || $this->stsEndpoint === '') {
  93. $this->stsEndpoint = 'sts.ap-northeast-1.aliyuncs.com';
  94. }
  95. }
  96. /**
  97. * Get credentials by request.
  98. *
  99. * @return array
  100. * @throws RuntimeException
  101. * @throws GuzzleException
  102. */
  103. public function refreshCredentials()
  104. {
  105. $options = Request::commonOptions();
  106. $options['read_timeout'] = $this->readTimeout;
  107. $options['connect_timeout'] = $this->connectTimeout;
  108. $options['query']['Action'] = 'GenerateSessionAccessKey';
  109. $options['query']['Version'] = '2015-04-01';
  110. $options['query']['Format'] = 'JSON';
  111. $options['query']['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
  112. $options['query']['SignatureMethod'] = 'SHA256withRSA';
  113. $options['query']['SignatureType'] = 'PRIVATEKEY';
  114. $options['query']['SignatureVersion'] = '1.0';
  115. $options['query']['SignatureNonce'] = Request::uuid(json_encode($options['query']));
  116. $options['query']['DurationSeconds'] = (string) $this->durationSeconds;
  117. $options['query']['AccessKeyId'] = $this->publicKeyId;
  118. $options['query']['Signature'] = Request::shaHmac256WithRsasign(
  119. Request::signString('GET', $options['query']),
  120. $this->privateKey
  121. );
  122. $url = (new Uri())->withScheme('https')->withHost($this->stsEndpoint);
  123. $result = Request::createClient()->request('GET', $url, $options);
  124. if ($result->getStatusCode() !== 200) {
  125. throw new RuntimeException('Error refreshing credentials from RsaKeyPair, statusCode: ' . $result->getStatusCode() . ', result: ' . (string) $result);
  126. }
  127. $json = $result->toArray();
  128. if (!isset($json['SessionAccessKey']['SessionAccessKeyId']) || !isset($json['SessionAccessKey']['SessionAccessKeySecret'])) {
  129. throw new RuntimeException('Error retrieving credentials from RsaKeyPair result:' . $result->toJson());
  130. }
  131. $credentials = [];
  132. $credentials['AccessKeyId'] = $json['SessionAccessKey']['SessionAccessKeyId'];
  133. $credentials['AccessKeySecret'] = $json['SessionAccessKey']['SessionAccessKeySecret'];
  134. $credentials['Expiration'] = $json['SessionAccessKey']['Expiration'];
  135. $credentials['SecurityToken'] = null;
  136. return $credentials;
  137. }
  138. public function key()
  139. {
  140. return 'rsa_key_pair#publicKeyId#' . $this->publicKeyId;
  141. }
  142. public function getProviderName()
  143. {
  144. return 'rsa_key_pair';
  145. }
  146. /**
  147. * @return string
  148. */
  149. public function getPublicKeyId()
  150. {
  151. return $this->publicKeyId;
  152. }
  153. /**
  154. * @return mixed
  155. */
  156. public function getPrivateKey()
  157. {
  158. return $this->privateKey;
  159. }
  160. }