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