StsClient.php
120 lines
| 1 | <?php |
| 2 | namespace Aws\Sts; |
| 3 | |
| 4 | use Aws\AwsClient; |
| 5 | use Aws\CacheInterface; |
| 6 | use Aws\Credentials\Credentials; |
| 7 | use Aws\Result; |
| 8 | use Aws\Sts\RegionalEndpoints\ConfigurationProvider; |
| 9 | |
| 10 | /** |
| 11 | * This client is used to interact with the **AWS Security Token Service (AWS STS)**. |
| 12 | * |
| 13 | * @method \Aws\Result assumeRole(array $args = []) |
| 14 | * @method \GuzzleHttp\Promise\Promise assumeRoleAsync(array $args = []) |
| 15 | * @method \Aws\Result assumeRoleWithSAML(array $args = []) |
| 16 | * @method \GuzzleHttp\Promise\Promise assumeRoleWithSAMLAsync(array $args = []) |
| 17 | * @method \Aws\Result assumeRoleWithWebIdentity(array $args = []) |
| 18 | * @method \GuzzleHttp\Promise\Promise assumeRoleWithWebIdentityAsync(array $args = []) |
| 19 | * @method \Aws\Result decodeAuthorizationMessage(array $args = []) |
| 20 | * @method \GuzzleHttp\Promise\Promise decodeAuthorizationMessageAsync(array $args = []) |
| 21 | * @method \Aws\Result getAccessKeyInfo(array $args = []) |
| 22 | * @method \GuzzleHttp\Promise\Promise getAccessKeyInfoAsync(array $args = []) |
| 23 | * @method \Aws\Result getCallerIdentity(array $args = []) |
| 24 | * @method \GuzzleHttp\Promise\Promise getCallerIdentityAsync(array $args = []) |
| 25 | * @method \Aws\Result getFederationToken(array $args = []) |
| 26 | * @method \GuzzleHttp\Promise\Promise getFederationTokenAsync(array $args = []) |
| 27 | * @method \Aws\Result getSessionToken(array $args = []) |
| 28 | * @method \GuzzleHttp\Promise\Promise getSessionTokenAsync(array $args = []) |
| 29 | */ |
| 30 | class StsClient extends AwsClient |
| 31 | { |
| 32 | |
| 33 | /** |
| 34 | * {@inheritdoc} |
| 35 | * |
| 36 | * In addition to the options available to |
| 37 | * {@see \Aws\AwsClient::__construct}, StsClient accepts the following |
| 38 | * options: |
| 39 | * |
| 40 | * - sts_regional_endpoints: |
| 41 | * (Aws\Sts\RegionalEndpoints\ConfigurationInterface|Aws\CacheInterface\|callable|string|array) |
| 42 | * Specifies whether to use regional or legacy endpoints for legacy regions. |
| 43 | * Provide an Aws\Sts\RegionalEndpoints\ConfigurationInterface object, an |
| 44 | * instance of Aws\CacheInterface, a callable configuration provider used |
| 45 | * to create endpoint configuration, a string value of `legacy` or |
| 46 | * `regional`, or an associative array with the following keys: |
| 47 | * endpoint_types (string) Set to `legacy` or `regional`, defaults to |
| 48 | * `legacy` |
| 49 | * |
| 50 | * @param array $args |
| 51 | */ |
| 52 | public function __construct(array $args) |
| 53 | { |
| 54 | if ( |
| 55 | !isset($args['sts_regional_endpoints']) |
| 56 | || $args['sts_regional_endpoints'] instanceof CacheInterface |
| 57 | ) { |
| 58 | $args['sts_regional_endpoints'] = ConfigurationProvider::defaultProvider($args); |
| 59 | } |
| 60 | $this->addBuiltIns($args); |
| 61 | parent::__construct($args); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates credentials from the result of an STS operations |
| 66 | * |
| 67 | * @param Result $result Result of an STS operation |
| 68 | * |
| 69 | * @return Credentials |
| 70 | * @throws \InvalidArgumentException if the result contains no credentials |
| 71 | */ |
| 72 | public function createCredentials(Result $result) |
| 73 | { |
| 74 | if (!$result->hasKey('Credentials')) { |
| 75 | throw new \InvalidArgumentException('Result contains no credentials'); |
| 76 | } |
| 77 | |
| 78 | $c = $result['Credentials']; |
| 79 | |
| 80 | return new Credentials( |
| 81 | $c['AccessKeyId'], |
| 82 | $c['SecretAccessKey'], |
| 83 | isset($c['SessionToken']) ? $c['SessionToken'] : null, |
| 84 | isset($c['Expiration']) && $c['Expiration'] instanceof \DateTimeInterface |
| 85 | ? (int) $c['Expiration']->format('U') |
| 86 | : null |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Adds service-specific client built-in value |
| 92 | * |
| 93 | * @return void |
| 94 | */ |
| 95 | private function addBuiltIns($args) |
| 96 | { |
| 97 | $key = 'AWS::STS::UseGlobalEndpoint'; |
| 98 | $result = $args['sts_regional_endpoints'] instanceof \Closure ? |
| 99 | $args['sts_regional_endpoints']()->wait() : $args['sts_regional_endpoints']; |
| 100 | |
| 101 | if (is_string($result)) { |
| 102 | if ($result === 'regional') { |
| 103 | $value = false; |
| 104 | } else if ($result === 'legacy') { |
| 105 | $value = true; |
| 106 | } else { |
| 107 | return; |
| 108 | } |
| 109 | } else { |
| 110 | if ($result->getEndpointsType() === 'regional') { |
| 111 | $value = false; |
| 112 | } else { |
| 113 | $value = true; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | $this->clientBuiltIns[$key] = $value; |
| 118 | } |
| 119 | } |
| 120 |