| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
namespace AWS\CRT\Auth; |
| 7 |
|
| 8 |
use AWS\CRT\NativeResource as NativeResource; |
| 9 |
use AWS\CRT\Options as Options; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
final class AwsCredentials extends NativeResource { |
| 21 |
|
| 22 |
static function defaults() { |
| 23 |
return [ |
| 24 |
'access_key_id' => '', |
| 25 |
'secret_access_key' => '', |
| 26 |
'session_token' => '', |
| 27 |
'expiration_timepoint_seconds' => 0, |
| 28 |
]; |
| 29 |
} |
| 30 |
|
| 31 |
private $access_key_id; |
| 32 |
private $secret_access_key; |
| 33 |
private $session_token; |
| 34 |
private $expiration_timepoint_seconds = 0; |
| 35 |
|
| 36 |
public function __get($name) { |
| 37 |
return $this->$name; |
| 38 |
} |
| 39 |
|
| 40 |
function __construct(array $options = []) { |
| 41 |
parent::__construct(); |
| 42 |
|
| 43 |
$options = new Options($options, self::defaults()); |
| 44 |
$this->access_key_id = $options->access_key_id->asString(); |
| 45 |
$this->secret_access_key = $options->secret_access_key->asString(); |
| 46 |
$this->session_token = $options->session_token ? $options->session_token->asString() : null; |
| 47 |
$this->expiration_timepoint_seconds = $options->expiration_timepoint_seconds->asInt(); |
| 48 |
|
| 49 |
if (strlen($this->access_key_id) == 0) { |
| 50 |
throw new \InvalidArgumentException("access_key_id must be provided"); |
| 51 |
} |
| 52 |
if (strlen($this->secret_access_key) == 0) { |
| 53 |
throw new \InvalidArgumentException("secret_access_key must be provided"); |
| 54 |
} |
| 55 |
|
| 56 |
$creds_options = self::$crt->aws_credentials_options_new(); |
| 57 |
self::$crt->aws_credentials_options_set_access_key_id($creds_options, $this->access_key_id); |
| 58 |
self::$crt->aws_credentials_options_set_secret_access_key($creds_options, $this->secret_access_key); |
| 59 |
self::$crt->aws_credentials_options_set_session_token($creds_options, $this->session_token); |
| 60 |
self::$crt->aws_credentials_options_set_expiration_timepoint_seconds($creds_options, $this->expiration_timepoint_seconds); |
| 61 |
$this->acquire(self::$crt->aws_credentials_new($creds_options)); |
| 62 |
self::$crt->aws_credentials_options_release($creds_options); |
| 63 |
} |
| 64 |
|
| 65 |
function __destruct() { |
| 66 |
self::$crt->aws_credentials_release($this->release()); |
| 67 |
parent::__destruct(); |
| 68 |
} |
| 69 |
} |