URLCredentialsProvider.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\Exception\GuzzleException;
  7. use InvalidArgumentException;
  8. use RuntimeException;
  9. /**
  10. * @internal This class is intended for internal use within the package.
  11. * Class URLCredentialsProvider
  12. *
  13. * @package AlibabaCloud\Credentials\Providers
  14. */
  15. class URLCredentialsProvider extends SessionCredentialsProvider
  16. {
  17. /**
  18. * @var string
  19. */
  20. private $credentialsURI;
  21. /**
  22. * @var int
  23. */
  24. private $connectTimeout = 5;
  25. /**
  26. * @var int
  27. */
  28. private $readTimeout = 5;
  29. /**
  30. * URLCredentialsProvider constructor.
  31. *
  32. * @param array $params
  33. * @param array $options
  34. */
  35. public function __construct(array $params = [], array $options = [])
  36. {
  37. $this->filterOptions($options);
  38. $this->filterCredentialsURI($params);
  39. }
  40. private function filterOptions(array $options)
  41. {
  42. if (isset($options['connectTimeout'])) {
  43. $this->connectTimeout = $options['connectTimeout'];
  44. }
  45. if (isset($options['readTimeout'])) {
  46. $this->readTimeout = $options['readTimeout'];
  47. }
  48. Filter::timeout($this->connectTimeout, $this->readTimeout);
  49. }
  50. private function filterCredentialsURI(array $params)
  51. {
  52. if (Helper::envNotEmpty('ALIBABA_CLOUD_CREDENTIALS_URI')) {
  53. $this->credentialsURI = Helper::env('ALIBABA_CLOUD_CREDENTIALS_URI');
  54. }
  55. if (isset($params['credentialsURI'])) {
  56. $this->credentialsURI = $params['credentialsURI'];
  57. }
  58. Filter::credentialsURI($this->credentialsURI);
  59. }
  60. /**
  61. * Get credentials by request.
  62. *
  63. * @return array
  64. * @throws InvalidArgumentException
  65. * @throws RuntimeException
  66. * @throws GuzzleException
  67. */
  68. public function refreshCredentials()
  69. {
  70. $options = Request::commonOptions();
  71. $options['read_timeout'] = $this->readTimeout;
  72. $options['connect_timeout'] = $this->connectTimeout;
  73. $result = Request::createClient()->request('GET', $this->credentialsURI, $options);
  74. if ($result->getStatusCode() !== 200) {
  75. throw new RuntimeException('Error refreshing credentials from credentialsURI, statusCode: ' . $result->getStatusCode() . ', result: ' . (string) $result);
  76. }
  77. $credentials = $result->toArray();
  78. if (!isset($credentials['AccessKeyId']) || !isset($credentials['AccessKeySecret']) || !isset($credentials['SecurityToken']) || !isset($credentials['Expiration'])) {
  79. throw new RuntimeException('Error retrieving credentials from credentialsURI result:' . $result->toJson());
  80. }
  81. return $credentials;
  82. }
  83. /**
  84. * @return string
  85. */
  86. public function key()
  87. {
  88. return 'credential_uri#' . $this->credentialsURI;
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function getProviderName()
  94. {
  95. return 'credential_uri';
  96. }
  97. }