summaryrefslogtreecommitdiff
path: root/vendor/aws/aws-sdk-php/src/Token/TokenProvider.php
blob: 1d7014c88813ae3c35b44b3ff6bb002f7afbb880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
namespace Aws\Token;

use Aws;
use Aws\Api\DateTimeResult;
use Aws\CacheInterface;
use Aws\Exception\TokenException;
use GuzzleHttp\Promise;

/**
 * Token providers are functions that accept no arguments and return a
 * promise that is fulfilled with an {@see \Aws\Token\TokenInterface}
 * or rejected with an {@see \Aws\Exception\TokenException}.
 *
 * <code>
 * use Aws\Token\TokenProvider;
 * $provider = TokenProvider::defaultProvider();
 * // Returns a TokenInterface or throws.
 * $token = $provider()->wait();
 * </code>
 *
 * Token providers can be composed to create a token using conditional
 * logic that can create different tokens in different environments. You
 * can compose multiple providers into a single provider using
 * {@see Aws\Token\TokenProvider::chain}. This function accepts
 * providers as variadic arguments and returns a new function that will invoke
 * each provider until a token is successfully returned.
 */
class TokenProvider
{
    use ParsesIniTrait;
    const ENV_PROFILE = 'AWS_PROFILE';

    /**
     * Create a default token provider tha checks for cached a SSO token from
     * the CLI
     *
     * This provider is automatically wrapped in a memoize function that caches
     * previously provided tokens.
     *
     * @param array $config Optional array of token provider options.
     *
     * @return callable
     */
    public static function defaultProvider(array $config = [])
    {

        $cacheable = [
            'sso',
        ];

        $defaultChain = [];

        if (
            !isset($config['use_aws_shared_config_files'])
            || $config['use_aws_shared_config_files'] !== false
        ) {
            $profileName = getenv(self::ENV_PROFILE) ?: 'default';
            $defaultChain['sso'] = self::sso(
                $profileName,
                self::getHomeDir() . '/.aws/config',
                $config
            );
        }

        if (isset($config['token'])
            && $config['token'] instanceof CacheInterface
        ) {
            foreach ($cacheable as $provider) {
                if (isset($defaultChain[$provider])) {
                    $defaultChain[$provider] = self::cache(
                        $defaultChain[$provider],
                        $config['token'],
                        'aws_cached_' . $provider . '_token'
                    );
                }
            }
        }

        return self::memoize(
            call_user_func_array(
                [TokenProvider::class, 'chain'],
                array_values($defaultChain)
            )
        );
    }

    /**
     * Create a token provider function from a static token.
     *
     * @param TokenInterface $token
     *
     * @return callable
     */
    public static function fromToken(TokenInterface $token)
    {
        $promise = Promise\Create::promiseFor($token);

        return function () use ($promise) {
            return $promise;
        };
    }

    /**
     * Creates an aggregate token provider that invokes the provided
     * variadic providers one after the other until a provider returns
     * a token.
     *
     * @return callable
     */
    public static function chain()
    {
        $links = func_get_args();
        //Common use case for when aws_shared_config_files is false
        if (empty($links)) {
            return function () {
                return Promise\Create::promiseFor(false);
            };
        }

        return function () use ($links) {
            /** @var callable $parent */
            $parent = array_shift($links);
            $promise = $parent();
            while ($next = array_shift($links)) {
                $promise = $promise->otherwise($next);
            }
            return $promise;
        };
    }

    /**
     * Wraps a token provider and caches a previously provided token.
     * Ensures that cached tokens are refreshed when they expire.
     *
     * @param callable $provider Token provider function to wrap.
     * @return callable
     */
    public static function memoize(callable $provider)
    {
        return function () use ($provider) {
            static $result;
            static $isConstant;

            // Constant tokens will be returned constantly.
            if ($isConstant) {
                return $result;
            }

            // Create the initial promise that will be used as the cached value
            // until it expires.
            if (null === $result) {
                $result = $provider();
            }

            // Return a token that could expire and refresh when needed.
            return $result
                ->then(function (TokenInterface $token) use ($provider, &$isConstant, &$result) {
                    // Determine if the token is constant.
                    if (!$token->getExpiration()) {
                        $isConstant = true;
                        return $token;
                    }

                    if (!$token->isExpired()) {
                        return $token;
                    }
                    return $result = $provider();
                })
                ->otherwise(function($reason) use (&$result) {
                    // Cleanup rejected promise.
                    $result = null;
                    return Promise\Create::promiseFor(null);
                });
        };
    }

    /**
     * Wraps a token provider and saves provided token in an
     * instance of Aws\CacheInterface. Forwards calls when no token found
     * in cache and updates cache with the results.
     *
     * @param callable $provider Token provider function to wrap
     * @param CacheInterface $cache Cache to store the token
     * @param string|null $cacheKey (optional) Cache key to use
     *
     * @return callable
     */
    public static function cache(
        callable $provider,
        CacheInterface $cache,
        $cacheKey = null
    ) {
        $cacheKey = $cacheKey ?: 'aws_cached_token';

        return function () use ($provider, $cache, $cacheKey) {
            $found = $cache->get($cacheKey);
            if (is_array($found) && isset($found['token'])) {
                if (isset($found['token']) && $found['token'] instanceof TokenInterface) {
                    $foundToken = $found['token'];
                    if (!$foundToken->isExpired()) {
                        return Promise\Create::promiseFor($foundToken);
                    }
                    if (isset($found['refreshMethod']) && is_callable($found['refreshMethod'])) {
                        return Promise\Create::promiseFor($found['refreshMethod']());
                    }
                }
            }

            return $provider()
                ->then(function (TokenInterface $token) use (
                    $cache,
                    $cacheKey
                ) {
                    $cache->set(
                        $cacheKey,
                        $token,
                        null === $token->getExpiration() ?
                            0 : $token->getExpiration() - time()
                    );

                    return $token;
                });
        };
    }

    /**
     * Gets profiles from the ~/.aws/config ini file
     */
    private static function loadDefaultProfiles() {
        $profiles = [];
        $configFile = self::getHomeDir() . '/.aws/config';

        if (file_exists($configFile)) {
            $configProfileData = \Aws\parse_ini_file($configFile, true, INI_SCANNER_RAW);
            foreach ($configProfileData as $name => $profile) {
                // standardize config profile names
                $name = str_replace('profile ', '', $name);
                if (!isset($profiles[$name])) {
                    $profiles[$name] = $profile;
                }
            }
        }

        return $profiles;
    }

    private static function reject($msg)
    {
        return new Promise\RejectedPromise(new TokenException($msg));
    }

    /**
     * Token provider that creates a token from cached sso credentials
     *
     * @param string $ssoProfileName the name of the ini profile name
     * @param string $filename the location of the ini file
     * @param array $config configuration options
     *
     * @return SsoToken
     * @see Aws\Token\SsoToken for $config details.
     */
    public static function sso($profileName, $filename, $config = [])
    {
        return new SsoTokenProvider($profileName, $filename, $config);
    }
}