| @@ -25,9 +25,8 @@ | ||
| 25 | 25 | */ |
| 26 | 26 | class FetchAuthTokenCache implements |
| 27 | 27 | FetchAuthTokenInterface, |
| 28 | 28 | GetQuotaProjectInterface, |
| 29 | - GetUniverseDomainInterface, | |
| 30 | 29 | SignBlobInterface, |
| 31 | 30 | ProjectIdProviderInterface, |
| 32 | 31 | UpdateMetadataInterface |
| 33 | 32 | { |
| @@ -38,21 +37,16 @@ | ||
| 38 | 37 | */ |
| 39 | 38 | private $fetcher; |
| 40 | 39 | |
| 41 | 40 | /** |
| 42 | - * @var int | |
| 43 | - */ | |
| 44 | - private $eagerRefreshThresholdSeconds = 10; | |
| 45 | - | |
| 46 | - /** | |
| 47 | 41 | * @param FetchAuthTokenInterface $fetcher A credentials fetcher |
| 48 | - * @param array<mixed>|null $cacheConfig Configuration for the cache | |
| 42 | + * @param array<mixed> $cacheConfig Configuration for the cache | |
| 49 | 43 | * @param CacheItemPoolInterface $cache |
| 50 | 44 | */ |
| 51 | 45 | public function __construct( |
| 52 | 46 | FetchAuthTokenInterface $fetcher, |
| 53 | - ?array $cacheConfig = null, | |
| 54 | - ?CacheItemPoolInterface $cache = null | |
| 47 | + array $cacheConfig = null, | |
| 48 | + CacheItemPoolInterface $cache | |
| 55 | 49 | ) { |
| 56 | 50 | $this->fetcher = $fetcher; |
| 57 | 51 | $this->cache = $cache; |
| 58 | 52 | $this->cacheConfig = array_merge([ |
| @@ -57,31 +51,22 @@ | ||
| 57 | 51 | $this->cache = $cache; |
| 58 | 52 | $this->cacheConfig = array_merge([ |
| 59 | 53 | 'lifetime' => 1500, |
| 60 | 54 | 'prefix' => '', |
| 61 | - 'cacheUniverseDomain' => $fetcher instanceof Credentials\GCECredentials, | |
| 62 | 55 | ], (array) $cacheConfig); |
| 63 | 56 | } |
| 64 | 57 | |
| 65 | 58 | /** |
| 66 | - * @return FetchAuthTokenInterface | |
| 67 | - */ | |
| 68 | - public function getFetcher() | |
| 69 | - { | |
| 70 | - return $this->fetcher; | |
| 71 | - } | |
| 72 | - | |
| 73 | - /** | |
| 74 | 59 | * Implements FetchAuthTokenInterface#fetchAuthToken. |
| 75 | 60 | * |
| 76 | 61 | * Checks the cache for a valid auth token and fetches the auth tokens |
| 77 | 62 | * from the supplied fetcher. |
| 78 | 63 | * |
| 79 | - * @param callable|null $httpHandler callback which delivers psr7 request | |
| 64 | + * @param callable $httpHandler callback which delivers psr7 request | |
| 80 | 65 | * @return array<mixed> the response |
| 81 | 66 | * @throws \Exception |
| 82 | 67 | */ |
| 83 | - public function fetchAuthToken(?callable $httpHandler = null) | |
| 68 | + public function fetchAuthToken(callable $httpHandler = null) | |
| 84 | 69 | { |
| 85 | 70 | if ($cached = $this->fetchAuthTokenFromCache()) { |
| 86 | 71 | return $cached; |
| 87 | 72 | } |
| @@ -111,12 +96,12 @@ | ||
| 111 | 96 | |
| 112 | 97 | /** |
| 113 | 98 | * Get the client name from the fetcher. |
| 114 | 99 | * |
| 115 | - * @param callable|null $httpHandler An HTTP handler to deliver PSR7 requests. | |
| 100 | + * @param callable $httpHandler An HTTP handler to deliver PSR7 requests. | |
| 116 | 101 | * @return string |
| 117 | 102 | */ |
| 118 | - public function getClientName(?callable $httpHandler = null) | |
| 103 | + public function getClientName(callable $httpHandler = null) | |
| 119 | 104 | { |
| 120 | 105 | if (!$this->fetcher instanceof SignBlobInterface) { |
| 121 | 106 | throw new \RuntimeException( |
| 122 | 107 | 'Credentials fetcher does not implement ' . |
| @@ -146,16 +131,13 @@ | ||
| 146 | 131 | 'Google\Auth\SignBlobInterface' |
| 147 | 132 | ); |
| 148 | 133 | } |
| 149 | 134 | |
| 150 | - // Pass the access token from cache for credentials that sign blobs | |
| 151 | - // using the IAM API. This saves a call to fetch an access token when a | |
| 152 | - // cached token exists. | |
| 153 | - if ($this->fetcher instanceof Credentials\GCECredentials | |
| 154 | - || $this->fetcher instanceof Credentials\ImpersonatedServiceAccountCredentials | |
| 155 | - ) { | |
| 135 | + // Pass the access token from cache to GCECredentials for signing a blob. | |
| 136 | + // This saves a call to the metadata server when a cached token exists. | |
| 137 | + if ($this->fetcher instanceof Credentials\GCECredentials) { | |
| 156 | 138 | $cached = $this->fetchAuthTokenFromCache(); |
| 157 | - $accessToken = $cached['access_token'] ?? null; | |
| 139 | + $accessToken = isset($cached['access_token']) ? $cached['access_token'] : null; | |
| 158 | 140 | return $this->fetcher->signBlob($stringToSign, $forceOpenSsl, $accessToken); |
| 159 | 141 | } |
| 160 | 142 | |
| 161 | 143 | return $this->fetcher->signBlob($stringToSign, $forceOpenSsl); |
| @@ -175,17 +157,17 @@ | ||
| 175 | 157 | |
| 176 | 158 | return null; |
| 177 | 159 | } |
| 178 | 160 | |
| 179 | - /** | |
| 161 | + /* | |
| 180 | 162 | * Get the Project ID from the fetcher. |
| 181 | 163 | * |
| 182 | - * @param callable|null $httpHandler Callback which delivers psr7 request | |
| 164 | + * @param callable $httpHandler Callback which delivers psr7 request | |
| 183 | 165 | * @return string|null |
| 184 | 166 | * @throws \RuntimeException If the fetcher does not implement |
| 185 | 167 | * `Google\Auth\ProvidesProjectIdInterface`. |
| 186 | 168 | */ |
| 187 | - public function getProjectId(?callable $httpHandler = null) | |
| 169 | + public function getProjectId(callable $httpHandler = null) | |
| 188 | 170 | { |
| 189 | 171 | if (!$this->fetcher instanceof ProjectIdProviderInterface) { |
| 190 | 172 | throw new \RuntimeException( |
| 191 | 173 | 'Credentials fetcher does not implement ' . |
| @@ -192,43 +174,17 @@ | ||
| 192 | 174 | 'Google\Auth\ProvidesProjectIdInterface' |
| 193 | 175 | ); |
| 194 | 176 | } |
| 195 | 177 | |
| 196 | - // Pass the access token from cache for credentials that require an | |
| 197 | - // access token to fetch the project ID. This saves a call to fetch an | |
| 198 | - // access token when a cached token exists. | |
| 199 | - if ($this->fetcher instanceof Credentials\ExternalAccountCredentials) { | |
| 200 | - $cached = $this->fetchAuthTokenFromCache(); | |
| 201 | - $accessToken = $cached['access_token'] ?? null; | |
| 202 | - return $this->fetcher->getProjectId($httpHandler, $accessToken); | |
| 203 | - } | |
| 204 | - | |
| 205 | 178 | return $this->fetcher->getProjectId($httpHandler); |
| 206 | 179 | } |
| 207 | 180 | |
| 208 | - /* | |
| 209 | - * Get the Universe Domain from the fetcher. | |
| 210 | - * | |
| 211 | - * @return string | |
| 212 | - */ | |
| 213 | - public function getUniverseDomain(): string | |
| 214 | - { | |
| 215 | - if ($this->fetcher instanceof GetUniverseDomainInterface) { | |
| 216 | - if ($this->cacheConfig['cacheUniverseDomain']) { | |
| 217 | - return $this->getCachedUniverseDomain($this->fetcher); | |
| 218 | - } | |
| 219 | - return $this->fetcher->getUniverseDomain(); | |
| 220 | - } | |
| 221 | - | |
| 222 | - return GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN; | |
| 223 | - } | |
| 224 | - | |
| 225 | 181 | /** |
| 226 | 182 | * Updates metadata with the authorization token. |
| 227 | 183 | * |
| 228 | 184 | * @param array<mixed> $metadata metadata hashmap |
| 229 | 185 | * @param string $authUri optional auth uri |
| 230 | - * @param callable|null $httpHandler callback which delivers psr7 request | |
| 186 | + * @param callable $httpHandler callback which delivers psr7 request | |
| 231 | 187 | * @return array<mixed> updated metadata hashmap |
| 232 | 188 | * @throws \RuntimeException If the fetcher does not implement |
| 233 | 189 | * `Google\Auth\UpdateMetadataInterface`. |
| 234 | 190 | */ |
| @@ -234,9 +190,9 @@ | ||
| 234 | 190 | */ |
| 235 | 191 | public function updateMetadata( |
| 236 | 192 | $metadata, |
| 237 | 193 | $authUri = null, |
| 238 | - ?callable $httpHandler = null | |
| 194 | + callable $httpHandler = null | |
| 239 | 195 | ) { |
| 240 | 196 | if (!$this->fetcher instanceof UpdateMetadataInterface) { |
| 241 | 197 | throw new \RuntimeException( |
| 242 | 198 | 'Credentials fetcher does not implement ' . |
| @@ -252,12 +208,8 @@ | ||
| 252 | 208 | if (isset($cached['access_token'])) { |
| 253 | 209 | $metadata[self::AUTH_METADATA_KEY] = [ |
| 254 | 210 | 'Bearer ' . $cached['access_token'] |
| 255 | 211 | ]; |
| 256 | - } elseif (isset($cached['id_token'])) { | |
| 257 | - $metadata[self::AUTH_METADATA_KEY] = [ | |
| 258 | - 'Bearer ' . $cached['id_token'] | |
| 259 | - ]; | |
| 260 | 212 | } |
| 261 | 213 | } |
| 262 | 214 | |
| 263 | 215 | $newMetadata = $this->fetcher->updateMetadata( |
| @@ -297,9 +249,9 @@ | ||
| 297 | 249 | // If there is no expiration data, assume token is not expired. |
| 298 | 250 | // (for JwtAccess and ID tokens) |
| 299 | 251 | return $cached; |
| 300 | 252 | } |
| 301 | - if ((time() + $this->eagerRefreshThresholdSeconds) < $cached['expires_at']) { | |
| 253 | + if (time() < $cached['expires_at']) { | |
| 302 | 254 | // access token is not expired |
| 303 | 255 | return $cached; |
| 304 | 256 | } |
| 305 | 257 | } |
| @@ -322,18 +274,6 @@ | ||
| 322 | 274 | : $this->fetcher->getCacheKey(); |
| 323 | 275 | |
| 324 | 276 | $this->setCachedValue($cacheKey, $authToken); |
| 325 | 277 | } |
| 326 | - } | |
| 327 | - | |
| 328 | - private function getCachedUniverseDomain(GetUniverseDomainInterface $fetcher): string | |
| 329 | - { | |
| 330 | - $cacheKey = $this->getFullCacheKey($fetcher->getCacheKey() . 'universe_domain'); // @phpstan-ignore-line | |
| 331 | - if ($universeDomain = $this->getCachedValue($cacheKey)) { | |
| 332 | - return $universeDomain; | |
| 333 | - } | |
| 334 | - | |
| 335 | - $universeDomain = $fetcher->getUniverseDomain(); | |
| 336 | - $this->setCachedValue($cacheKey, $universeDomain); | |
| 337 | - return $universeDomain; | |
| 338 | 278 | } |
| 339 | 279 | } |