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