| @@ -1,18 +1,21 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Dudlewebs\WPMCS\s3\Aws\Credentials; |
| 4 | 4 | |
| 5 | +use Dudlewebs\WPMCS\s3\Aws\Identity\AwsCredentialIdentity; | |
| 5 | 6 | /** |
| 6 | 7 | * Basic implementation of the AWS Credentials interface that allows callers to |
| 7 | 8 | * pass in the AWS Access Key and AWS Secret Access Key in the constructor. |
| 8 | 9 | */ |
| 9 | -class Credentials implements CredentialsInterface, \Serializable | |
| 10 | +class Credentials extends AwsCredentialIdentity implements CredentialsInterface, \Serializable | |
| 10 | 11 | { |
| 11 | 12 | private $key; |
| 12 | 13 | private $secret; |
| 13 | 14 | private $token; |
| 14 | 15 | private $expires; |
| 16 | + private $accountId; | |
| 17 | + private $source; | |
| 15 | 18 | /** |
| 16 | 19 | * Constructs a new BasicAWSCredentials object, with the specified AWS |
| 17 | 20 | * access key and AWS secret key |
| 18 | 21 | * |
| @@ -20,18 +23,20 @@ | ||
| 20 | 23 | * @param string $secret AWS secret access key |
| 21 | 24 | * @param string $token Security token to use |
| 22 | 25 | * @param int $expires UNIX timestamp for when credentials expire |
| 23 | 26 | */ |
| 24 | - public function __construct($key, $secret, $token = null, $expires = null) | |
| 27 | + public function __construct($key, $secret, $token = null, $expires = null, $accountId = null, $source = CredentialSources::STATIC) | |
| 25 | 28 | { |
| 26 | - $this->key = \trim($key); | |
| 27 | - $this->secret = \trim($secret); | |
| 29 | + $this->key = \trim((string) $key); | |
| 30 | + $this->secret = \trim((string) $secret); | |
| 28 | 31 | $this->token = $token; |
| 29 | 32 | $this->expires = $expires; |
| 33 | + $this->accountId = $accountId; | |
| 34 | + $this->source = $source ?? CredentialSources::STATIC; | |
| 30 | 35 | } |
| 31 | 36 | public static function __set_state(array $state) |
| 32 | 37 | { |
| 33 | - return new self($state['key'], $state['secret'], $state['token'], $state['expires']); | |
| 38 | + return new self($state['key'], $state['secret'], $state['token'], $state['expires'], $state['accountId'], $state['source'] ?? null); | |
| 34 | 39 | } |
| 35 | 40 | public function getAccessKeyId() |
| 36 | 41 | { |
| 37 | 42 | return $this->key; |
| @@ -51,11 +56,19 @@ | ||
| 51 | 56 | public function isExpired() |
| 52 | 57 | { |
| 53 | 58 | return $this->expires !== null && \time() >= $this->expires; |
| 54 | 59 | } |
| 60 | + public function getAccountId() | |
| 61 | + { | |
| 62 | + return $this->accountId; | |
| 63 | + } | |
| 64 | + public function getSource() | |
| 65 | + { | |
| 66 | + return $this->source; | |
| 67 | + } | |
| 55 | 68 | public function toArray() |
| 56 | 69 | { |
| 57 | - return ['key' => $this->key, 'secret' => $this->secret, 'token' => $this->token, 'expires' => $this->expires]; | |
| 70 | + return ['key' => $this->key, 'secret' => $this->secret, 'token' => $this->token, 'expires' => $this->expires, 'accountId' => $this->accountId, 'source' => $this->source]; | |
| 58 | 71 | } |
| 59 | 72 | public function serialize() |
| 60 | 73 | { |
| 61 | 74 | return \json_encode($this->__serialize()); |
| @@ -74,9 +87,17 @@ | ||
| 74 | 87 | $this->key = $data['key']; |
| 75 | 88 | $this->secret = $data['secret']; |
| 76 | 89 | $this->token = $data['token']; |
| 77 | 90 | $this->expires = $data['expires']; |
| 91 | + $this->accountId = $data['accountId'] ?? null; | |
| 92 | + $this->source = $data['source'] ?? null; | |
| 78 | 93 | } |
| 94 | + /** | |
| 95 | + * Internal-only. Used when IMDS is unreachable | |
| 96 | + * or returns expires credentials. | |
| 97 | + * | |
| 98 | + * @internal | |
| 99 | + */ | |
| 79 | 100 | public function extendExpiration() |
| 80 | 101 | { |
| 81 | 102 | $extension = \mt_rand(5, 10); |
| 82 | 103 | $this->expires = \time() + $extension * 60; |
| @@ -85,7 +106,7 @@ | ||
| 85 | 106 | availability issue. A refresh of these credentials will be attempted again |
| 86 | 107 | after {$extension} minutes. |
| 87 | 108 | |
| 88 | 109 | EOT; |
| 89 | - \error_log($message); | |
| 110 | + \trigger_error($message, \E_USER_WARNING); | |
| 90 | 111 | } |
| 91 | 112 | } |