Provider.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace AlibabaCloud\Credentials\Providers;
  3. use AlibabaCloud\Credentials\CredentialsInterface;
  4. use AlibabaCloud\Credentials\EcsRamRoleCredential;
  5. use AlibabaCloud\Credentials\RamRoleArnCredential;
  6. use AlibabaCloud\Credentials\RsaKeyPairCredential;
  7. abstract class Provider
  8. {
  9. /**
  10. * For TSC Duration Seconds
  11. */
  12. const DURATION_SECONDS = 3600;
  13. /**
  14. * @var array
  15. */
  16. protected static $credentialsCache = [];
  17. /**
  18. * Expiration time slot for temporary security credentials.
  19. *
  20. * @var int
  21. */
  22. protected $expirationSlot = 180;
  23. /**
  24. * @var RamRoleArnCredential|RsaKeyPairCredential|EcsRamRoleCredential
  25. */
  26. protected $credential;
  27. /**
  28. * @var string
  29. */
  30. protected $error = 'Result contains no credentials';
  31. /**
  32. * @var array
  33. */
  34. protected $config = [];
  35. /**
  36. * CredentialTrait constructor.
  37. *
  38. * @param CredentialsInterface $credential
  39. * @param array $config
  40. */
  41. public function __construct(CredentialsInterface $credential, $config = [])
  42. {
  43. $this->credential = $credential;
  44. $this->config = $config;
  45. }
  46. /**
  47. * Get the credentials from the cache in the validity period.
  48. *
  49. * @return array|null
  50. */
  51. public function getCredentialsInCache()
  52. {
  53. if (isset(self::$credentialsCache[(string)$this->credential])) {
  54. $result = self::$credentialsCache[(string)$this->credential];
  55. if (\strtotime($result['Expiration']) - \time() >= $this->expirationSlot) {
  56. return $result;
  57. }
  58. }
  59. return null;
  60. }
  61. /**
  62. * Cache credentials.
  63. *
  64. * @param array $credential
  65. */
  66. protected function cache(array $credential)
  67. {
  68. self::$credentialsCache[(string)$this->credential] = $credential;
  69. }
  70. }