| 1 |
<?php |
| 2 |
namespace Aws\Token; |
| 3 |
|
| 4 |
use Aws\Exception\TokenException; |
| 5 |
use Aws\SSOOIDC\SSOOIDCClient; |
| 6 |
use GuzzleHttp\Promise; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class SsoTokenProvider implements RefreshableTokenProviderInterface |
| 12 |
{ |
| 13 |
use ParsesIniTrait; |
| 14 |
|
| 15 |
const ENV_PROFILE = 'AWS_PROFILE'; |
| 16 |
const REFRESH_WINDOW_IN_SECS = 300; |
| 17 |
const REFRESH_ATTEMPT_WINDOW_IN_SECS = 30; |
| 18 |
|
| 19 |
|
| 20 |
private $profileName; |
| 21 |
|
| 22 |
|
| 23 |
private $configFilePath; |
| 24 |
|
| 25 |
|
| 26 |
private $ssoOidcClient; |
| 27 |
|
| 28 |
|
| 29 |
private $ssoSessionName; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function __construct( |
| 38 |
$profileName, |
| 39 |
$configFilePath = null, |
| 40 |
?SSOOIDCClient $ssoOidcClient = null |
| 41 |
) { |
| 42 |
$this->profileName = $this->resolveProfileName($profileName); |
| 43 |
$this->configFilePath = $this->resolveConfigFile($configFilePath); |
| 44 |
$this->ssoOidcClient = $ssoOidcClient; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private function resolveProfileName($argProfileName): string |
| 58 |
{ |
| 59 |
if (empty($argProfileName)) { |
| 60 |
return getenv(self::ENV_PROFILE) ?: 'default'; |
| 61 |
} else { |
| 62 |
return $argProfileName; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private function resolveConfigFile($argConfigFilePath): string |
| 76 |
{ |
| 77 |
if (empty($argConfigFilePath)) { |
| 78 |
return self::getHomeDir() . '/.aws/config'; |
| 79 |
} else{ |
| 80 |
return $argConfigFilePath; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function __invoke() |
| 90 |
{ |
| 91 |
return Promise\Coroutine::of(function () { |
| 92 |
if (empty($this->configFilePath) || !is_readable($this->configFilePath)) { |
| 93 |
throw new TokenException("Cannot read profiles from {$this->configFilePath}"); |
| 94 |
} |
| 95 |
|
| 96 |
$profiles = self::loadProfiles($this->configFilePath); |
| 97 |
if (!isset($profiles[$this->profileName])) { |
| 98 |
throw new TokenException("Profile `{$this->profileName}` does not exist in {$this->configFilePath}."); |
| 99 |
} |
| 100 |
|
| 101 |
$profile = $profiles[$this->profileName]; |
| 102 |
if (empty($profile['sso_session'])) { |
| 103 |
throw new TokenException( |
| 104 |
"Profile `{$this->profileName}` in {$this->configFilePath} must contain an sso_session." |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
$ssoSessionName = $profile['sso_session']; |
| 109 |
$this->ssoSessionName = $ssoSessionName; |
| 110 |
$profileSsoSession = 'sso-session ' . $ssoSessionName; |
| 111 |
if (empty($profiles[$profileSsoSession])) { |
| 112 |
throw new TokenException( |
| 113 |
"Sso session `{$ssoSessionName}` does not exist in {$this->configFilePath}" |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
$sessionProfileData = $profiles[$profileSsoSession]; |
| 118 |
foreach (['sso_start_url', 'sso_region'] as $requiredProp) { |
| 119 |
if (empty($sessionProfileData[$requiredProp])) { |
| 120 |
throw new TokenException( |
| 121 |
"Sso session `{$ssoSessionName}` in {$this->configFilePath} is missing the required property `{$requiredProp}`" |
| 122 |
); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
$tokenData = $this->refresh(); |
| 127 |
$tokenLocation = self::getTokenLocation($ssoSessionName); |
| 128 |
$this->validateTokenData($tokenLocation, $tokenData); |
| 129 |
$ssoToken = SsoToken::fromTokenData($tokenData); |
| 130 |
|
| 131 |
if ($ssoToken->isExpired()) { |
| 132 |
throw new TokenException("Cached SSO token returned an expired token."); |
| 133 |
} |
| 134 |
|
| 135 |
yield $ssoToken; |
| 136 |
}); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function refresh(): array |
| 148 |
{ |
| 149 |
$tokenLocation = self::getTokenLocation($this->ssoSessionName); |
| 150 |
$tokenData = $this->getTokenData($tokenLocation); |
| 151 |
if (!$this->shouldAttemptRefresh()) { |
| 152 |
return $tokenData; |
| 153 |
} |
| 154 |
|
| 155 |
if (null === $this->ssoOidcClient) { |
| 156 |
throw new TokenException( |
| 157 |
"Cannot refresh this token without an 'ssooidcClient' " |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
foreach (['clientId', 'clientSecret', 'refreshToken'] as $requiredProp) { |
| 162 |
if (empty($tokenData[$requiredProp])) { |
| 163 |
throw new TokenException( |
| 164 |
"Cannot refresh this token without `{$requiredProp}` being set" |
| 165 |
); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
$response = $this->ssoOidcClient->createToken([ |
| 170 |
'clientId' => $tokenData['clientId'], |
| 171 |
'clientSecret' => $tokenData['clientSecret'], |
| 172 |
'grantType' => 'refresh_token', |
| 173 |
'refreshToken' => $tokenData['refreshToken'], |
| 174 |
]); |
| 175 |
if ($response['@metadata']['statusCode'] !== 200) { |
| 176 |
throw new TokenException('Unable to create a new sso token'); |
| 177 |
} |
| 178 |
|
| 179 |
$tokenData['accessToken'] = $response['accessToken']; |
| 180 |
$tokenData['expiresAt'] = time () + $response['expiresIn']; |
| 181 |
$tokenData['refreshToken'] = $response['refreshToken']; |
| 182 |
|
| 183 |
return $this->writeNewTokenDataToDisk($tokenData, $tokenLocation); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function shouldAttemptRefresh(): bool |
| 195 |
{ |
| 196 |
$tokenLocation = self::getTokenLocation($this->ssoSessionName); |
| 197 |
$tokenData = $this->getTokenData($tokenLocation); |
| 198 |
if (empty($tokenData['expiresAt'])) { |
| 199 |
throw new TokenException( |
| 200 |
"Token file at $tokenLocation must contain an expiration date" |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
$tokenExpiresAt = strtotime($tokenData['expiresAt']); |
| 205 |
$lastRefreshAt = filemtime($tokenLocation); |
| 206 |
$now = \time(); |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
return ($now - $lastRefreshAt) > self::REFRESH_ATTEMPT_WINDOW_IN_SECS |
| 211 |
&& ($tokenExpiresAt - $now) < self::REFRESH_WINDOW_IN_SECS; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public static function getTokenLocation($sso_session): string |
| 219 |
{ |
| 220 |
return self::getHomeDir() |
| 221 |
. '/.aws/sso/cache/' |
| 222 |
. mb_convert_encoding(sha1($sso_session), "UTF-8") |
| 223 |
. ".json"; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
function getTokenData($tokenLocation): array |
| 231 |
{ |
| 232 |
if (empty($tokenLocation) || !is_readable($tokenLocation)) { |
| 233 |
throw new TokenException("Unable to read token file at {$tokenLocation}"); |
| 234 |
} |
| 235 |
|
| 236 |
return json_decode(file_get_contents($tokenLocation), true); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
private function validateTokenData($tokenLocation, $tokenData) |
| 245 |
{ |
| 246 |
foreach (['accessToken', 'expiresAt'] as $requiredProp) { |
| 247 |
if (empty($tokenData[$requiredProp])) { |
| 248 |
throw new TokenException( |
| 249 |
"Token file at {$tokenLocation} must contain the required property `{$requiredProp}`" |
| 250 |
); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
$expiration = strtotime($tokenData['expiresAt']); |
| 255 |
if ($expiration === false) { |
| 256 |
throw new TokenException("Cached SSO token returned an invalid expiration"); |
| 257 |
} elseif ($expiration < time()) { |
| 258 |
throw new TokenException("Cached SSO token returned an expired token"); |
| 259 |
} |
| 260 |
|
| 261 |
return $tokenData; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
private function writeNewTokenDataToDisk(array $tokenData, $tokenLocation): array |
| 271 |
{ |
| 272 |
$tokenData['expiresAt'] = gmdate( |
| 273 |
'Y-m-d\TH:i:s\Z', |
| 274 |
$tokenData['expiresAt'] |
| 275 |
); |
| 276 |
file_put_contents($tokenLocation, json_encode(array_filter($tokenData))); |
| 277 |
|
| 278 |
return $tokenData; |
| 279 |
} |
| 280 |
} |