Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Token/SsoTokenProvider.php

raw symbols SCIP
1 <?php
2 namespace Aws\Token;
3
4 use Aws\Exception\TokenException;
5 use Aws\SSOOIDC\SSOOIDCClient;
6 use GuzzleHttp\Promise;
7
8 /**
9 * Token that comes from the SSO provider
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 /** @var string $profileName */
20 private $profileName;
21
22 /** @var string $configFilePath */
23 private $configFilePath;
24
25 /** @var SSOOIDCClient $ssoOidcClient */
26 private $ssoOidcClient;
27
28 /** @var string $ssoSessionName */
29 private $ssoSessionName;
30
31 /**
32 * Constructs a new SsoTokenProvider object, which will fetch a token from an authenticated SSO profile
33 * @param string $profileName The name of the profile that contains the sso_session key
34 * @param string|null $configFilePath Name of the config file to sso profile from
35 * @param SSOOIDCClient|null $ssoOidcClient The sso client for generating a new token
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 * This method resolves the profile name to be used. The
49 * profile provided as instantiation argument takes precedence,
50 * followed by AWS_PROFILE env variable, otherwise `default` is
51 * used.
52 *
53 * @param string|null $argProfileName The profile provided as argument.
54 *
55 * @return string
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 * This method resolves the config file from where the profiles
68 * are going to be loaded from. If $argFileName is not empty then,
69 * it takes precedence over the default config file location.
70 *
71 * @param string|null $argConfigFilePath The config path provided as argument.
72 *
73 * @return string
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 * Loads cached sso credentials.
86 *
87 * @return Promise\PromiseInterface
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 // To make sure the token is not expired
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 * This method attempt to refresh when possible.
141 * If a refresh is not possible then it just returns
142 * the current token data as it is.
143 *
144 * @return array
145 * @throws TokenException
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', // REQUIRED
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 * This method checks for whether a token refresh should happen.
188 * It will return true just if more than 30 seconds has happened
189 * since last refresh, and if the expiration is within a 5-minutes
190 * window from the current time.
191 *
192 * @return bool
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 // If last refresh happened after 30 seconds
209 // and if the token expiration is in the 5 minutes window
210 return ($now - $lastRefreshAt) > self::REFRESH_ATTEMPT_WINDOW_IN_SECS
211 && ($tokenExpiresAt - $now) < self::REFRESH_WINDOW_IN_SECS;
212 }
213
214 /**
215 * @param $sso_session
216 * @return string
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 * @param $tokenLocation
228 * @return array
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 * @param $tokenData
241 * @param $tokenLocation
242 * @return mixed
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 * @param array $tokenData
266 * @param string $tokenLocation
267 *
268 * @return array
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 }