| 1 |
<?php |
| 2 |
namespace Aws\Credentials; |
| 3 |
|
| 4 |
use Aws\Identity\AwsCredentialIdentity; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class Credentials extends AwsCredentialIdentity implements |
| 11 |
CredentialsInterface, |
| 12 |
\Serializable |
| 13 |
{ |
| 14 |
private $key; |
| 15 |
private $secret; |
| 16 |
private $token; |
| 17 |
private $expires; |
| 18 |
private $accountId; |
| 19 |
private $source; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function __construct( |
| 31 |
$key, |
| 32 |
$secret, |
| 33 |
$token = null, |
| 34 |
$expires = null, |
| 35 |
$accountId = null, |
| 36 |
$source = CredentialSources::STATIC |
| 37 |
) |
| 38 |
{ |
| 39 |
$this->key = trim((string) $key); |
| 40 |
$this->secret = trim((string) $secret); |
| 41 |
$this->token = $token; |
| 42 |
$this->expires = $expires; |
| 43 |
$this->accountId = $accountId; |
| 44 |
$this->source = $source ?? CredentialSources::STATIC; |
| 45 |
} |
| 46 |
|
| 47 |
public static function __set_state(array $state) |
| 48 |
{ |
| 49 |
return new self( |
| 50 |
$state['key'], |
| 51 |
$state['secret'], |
| 52 |
$state['token'], |
| 53 |
$state['expires'], |
| 54 |
$state['accountId'], |
| 55 |
$state['source'] ?? null |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
public function getAccessKeyId() |
| 60 |
{ |
| 61 |
return $this->key; |
| 62 |
} |
| 63 |
|
| 64 |
public function getSecretKey() |
| 65 |
{ |
| 66 |
return $this->secret; |
| 67 |
} |
| 68 |
|
| 69 |
public function getSecurityToken() |
| 70 |
{ |
| 71 |
return $this->token; |
| 72 |
} |
| 73 |
|
| 74 |
public function getExpiration() |
| 75 |
{ |
| 76 |
return $this->expires; |
| 77 |
} |
| 78 |
|
| 79 |
public function isExpired() |
| 80 |
{ |
| 81 |
return $this->expires !== null && time() >= $this->expires; |
| 82 |
} |
| 83 |
|
| 84 |
public function getAccountId() |
| 85 |
{ |
| 86 |
return $this->accountId; |
| 87 |
} |
| 88 |
|
| 89 |
public function getSource() |
| 90 |
{ |
| 91 |
return $this->source; |
| 92 |
} |
| 93 |
|
| 94 |
public function toArray() |
| 95 |
{ |
| 96 |
return [ |
| 97 |
'key' => $this->key, |
| 98 |
'secret' => $this->secret, |
| 99 |
'token' => $this->token, |
| 100 |
'expires' => $this->expires, |
| 101 |
'accountId' => $this->accountId, |
| 102 |
'source' => $this->source |
| 103 |
]; |
| 104 |
} |
| 105 |
|
| 106 |
public function serialize() |
| 107 |
{ |
| 108 |
return json_encode($this->__serialize()); |
| 109 |
} |
| 110 |
|
| 111 |
public function unserialize($serialized) |
| 112 |
{ |
| 113 |
$data = json_decode($serialized, true); |
| 114 |
|
| 115 |
$this->__unserialize($data); |
| 116 |
} |
| 117 |
|
| 118 |
public function __serialize() |
| 119 |
{ |
| 120 |
return $this->toArray(); |
| 121 |
} |
| 122 |
|
| 123 |
public function __unserialize($data) |
| 124 |
{ |
| 125 |
$this->key = $data['key']; |
| 126 |
$this->secret = $data['secret']; |
| 127 |
$this->token = $data['token']; |
| 128 |
$this->expires = $data['expires']; |
| 129 |
$this->accountId = $data['accountId'] ?? null; |
| 130 |
$this->source = $data['source'] ?? null; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function extendExpiration() { |
| 140 |
$extension = mt_rand(5, 10); |
| 141 |
$this->expires = time() + $extension * 60; |
| 142 |
|
| 143 |
$message = <<<EOT |
| 144 |
Attempting credential expiration extension due to a credential service |
| 145 |
availability issue. A refresh of these credentials will be attempted again |
| 146 |
after {$extension} minutes.\n |
| 147 |
EOT; |
| 148 |
trigger_error($message, E_USER_WARNING); |
| 149 |
} |
| 150 |
} |