Credentials.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Aws\Credentials;
  3. /**
  4. * Basic implementation of the AWS Credentials interface that allows callers to
  5. * pass in the AWS Access Key and AWS Secret Access Key in the constructor.
  6. */
  7. class Credentials implements CredentialsInterface, \Serializable
  8. {
  9. private $key;
  10. private $secret;
  11. private $token;
  12. private $expires;
  13. /**
  14. * Constructs a new BasicAWSCredentials object, with the specified AWS
  15. * access key and AWS secret key
  16. *
  17. * @param string $key AWS access key ID
  18. * @param string $secret AWS secret access key
  19. * @param string $token Security token to use
  20. * @param int $expires UNIX timestamp for when credentials expire
  21. */
  22. public function __construct($key, $secret, $token = null, $expires = null)
  23. {
  24. $this->key = trim($key);
  25. $this->secret = trim($secret);
  26. $this->token = $token;
  27. $this->expires = $expires;
  28. }
  29. public static function __set_state(array $state)
  30. {
  31. return new self(
  32. $state['key'],
  33. $state['secret'],
  34. $state['token'],
  35. $state['expires']
  36. );
  37. }
  38. public function getAccessKeyId()
  39. {
  40. return $this->key;
  41. }
  42. public function getSecretKey()
  43. {
  44. return $this->secret;
  45. }
  46. public function getSecurityToken()
  47. {
  48. return $this->token;
  49. }
  50. public function getExpiration()
  51. {
  52. return $this->expires;
  53. }
  54. public function isExpired()
  55. {
  56. return $this->expires !== null && time() >= $this->expires;
  57. }
  58. public function toArray()
  59. {
  60. return [
  61. 'key' => $this->key,
  62. 'secret' => $this->secret,
  63. 'token' => $this->token,
  64. 'expires' => $this->expires
  65. ];
  66. }
  67. public function serialize()
  68. {
  69. return json_encode($this->toArray());
  70. }
  71. public function unserialize($serialized)
  72. {
  73. $data = json_decode($serialized, true);
  74. $this->key = $data['key'];
  75. $this->secret = $data['secret'];
  76. $this->token = $data['token'];
  77. $this->expires = $data['expires'];
  78. }
  79. }