| 1 |
<?php |
| 2 |
namespace Aws\Token; |
| 3 |
|
| 4 |
use Aws; |
| 5 |
use Aws\CacheInterface; |
| 6 |
use Aws\Exception\TokenException; |
| 7 |
use GuzzleHttp\Promise; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class TokenProvider |
| 29 |
{ |
| 30 |
const ENV_PROFILE = 'AWS_PROFILE'; |
| 31 |
|
| 32 |
use ParsesIniTrait; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static function defaultProvider(array $config = []) |
| 46 |
{ |
| 47 |
$cacheable = [ |
| 48 |
'sso', |
| 49 |
]; |
| 50 |
|
| 51 |
$defaultChain = []; |
| 52 |
|
| 53 |
if (!isset($config['use_aws_shared_config_files']) |
| 54 |
|| $config['use_aws_shared_config_files'] !== false |
| 55 |
) { |
| 56 |
$profileName = getenv(self::ENV_PROFILE) ?: 'default'; |
| 57 |
$defaultChain['sso'] = self::sso( |
| 58 |
$profileName, |
| 59 |
self::getHomeDir() . '/.aws/config', |
| 60 |
$config |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
if (isset($config['token']) |
| 65 |
&& $config['token'] instanceof CacheInterface |
| 66 |
) { |
| 67 |
foreach ($cacheable as $provider) { |
| 68 |
if (isset($defaultChain[$provider])) { |
| 69 |
$defaultChain[$provider] = self::cache( |
| 70 |
$defaultChain[$provider], |
| 71 |
$config['token'], |
| 72 |
'aws_cached_' . $provider . '_token' |
| 73 |
); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return self::memoize( |
| 79 |
call_user_func_array( |
| 80 |
[__CLASS__, 'chain'], |
| 81 |
array_values($defaultChain) |
| 82 |
) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public static function fromToken(TokenInterface $token) |
| 94 |
{ |
| 95 |
$promise = Promise\Create::promiseFor($token); |
| 96 |
|
| 97 |
return static function () use ($promise) { |
| 98 |
return $promise; |
| 99 |
}; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public static function chain() |
| 110 |
{ |
| 111 |
$links = func_get_args(); |
| 112 |
|
| 113 |
if (empty($links)) { |
| 114 |
return static function () { |
| 115 |
return Promise\Create::promiseFor(false); |
| 116 |
}; |
| 117 |
} |
| 118 |
|
| 119 |
return static function () use ($links) { |
| 120 |
|
| 121 |
$parent = array_shift($links); |
| 122 |
$promise = $parent(); |
| 123 |
while ($next = array_shift($links)) { |
| 124 |
$promise = $promise->otherwise($next); |
| 125 |
} |
| 126 |
return $promise; |
| 127 |
}; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public static function memoize(callable $provider) |
| 138 |
{ |
| 139 |
return static function () use ($provider) { |
| 140 |
static $result; |
| 141 |
static $isConstant; |
| 142 |
|
| 143 |
|
| 144 |
if ($isConstant) { |
| 145 |
return $result; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
if (null === $result) { |
| 151 |
$result = $provider(); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
return $result |
| 156 |
->then(function (TokenInterface $token) use ($provider, &$isConstant, &$result) { |
| 157 |
|
| 158 |
if (!$token->getExpiration()) { |
| 159 |
$isConstant = true; |
| 160 |
return $token; |
| 161 |
} |
| 162 |
|
| 163 |
if (!$token->isExpired()) { |
| 164 |
return $token; |
| 165 |
} |
| 166 |
return $result = $provider(); |
| 167 |
}) |
| 168 |
->otherwise(function($reason) use (&$result) { |
| 169 |
|
| 170 |
$result = null; |
| 171 |
return Promise\Create::promiseFor(null); |
| 172 |
}); |
| 173 |
}; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public static function cache( |
| 188 |
callable $provider, |
| 189 |
CacheInterface $cache, |
| 190 |
$cacheKey = null |
| 191 |
){ |
| 192 |
$cacheKey = $cacheKey ?: 'aws_cached_token'; |
| 193 |
|
| 194 |
return static function () use ($provider, $cache, $cacheKey) { |
| 195 |
$found = $cache->get($cacheKey); |
| 196 |
if (is_array($found) && isset($found['token'])) { |
| 197 |
$foundToken = $found['token']; |
| 198 |
if ($foundToken instanceof TokenInterface) { |
| 199 |
if (!$foundToken->isExpired()) { |
| 200 |
return Promise\Create::promiseFor($foundToken); |
| 201 |
} |
| 202 |
if (isset($found['refreshMethod']) && is_callable($found['refreshMethod'])) { |
| 203 |
return Promise\Create::promiseFor($found['refreshMethod']()); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return $provider() |
| 209 |
->then(function (TokenInterface $token) use ( |
| 210 |
$cache, |
| 211 |
$cacheKey |
| 212 |
) { |
| 213 |
$cache->set( |
| 214 |
$cacheKey, |
| 215 |
['token' => $token], |
| 216 |
null === $token->getExpiration() ? |
| 217 |
0 : $token->getExpiration() - time() |
| 218 |
); |
| 219 |
|
| 220 |
return $token; |
| 221 |
}); |
| 222 |
}; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
private static function loadDefaultProfiles() |
| 229 |
{ |
| 230 |
$profiles = []; |
| 231 |
$configFile = self::getHomeDir() . '/.aws/config'; |
| 232 |
|
| 233 |
if (file_exists($configFile)) { |
| 234 |
$configProfileData = \Aws\parse_ini_file($configFile, true, INI_SCANNER_RAW); |
| 235 |
foreach ($configProfileData as $name => $profile) { |
| 236 |
|
| 237 |
$name = str_replace('profile ', '', $name); |
| 238 |
if (!isset($profiles[$name])) { |
| 239 |
$profiles[$name] = $profile; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return $profiles; |
| 245 |
} |
| 246 |
|
| 247 |
private static function reject($msg) |
| 248 |
{ |
| 249 |
return new Promise\RejectedPromise(new TokenException($msg)); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public static function sso( |
| 263 |
$profileName, |
| 264 |
$filename, |
| 265 |
$config = [] |
| 266 |
){ |
| 267 |
$ssoClient = $config['ssoClient'] ?? null; |
| 268 |
|
| 269 |
return new SsoTokenProvider($profileName, $filename, $ssoClient); |
| 270 |
} |
| 271 |
} |