| @@ -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 | { |
| @@ -44,15 +43,15 @@ | ||
| 44 | 43 | private $eagerRefreshThresholdSeconds = 10; |
| 45 | 44 | |
| 46 | 45 | /** |
| 47 | 46 | * @param FetchAuthTokenInterface $fetcher A credentials fetcher |
| 48 | - * @param array<mixed>|null $cacheConfig Configuration for the cache | |
| 47 | + * @param array<mixed> $cacheConfig Configuration for the cache | |
| 49 | 48 | * @param CacheItemPoolInterface $cache |
| 50 | 49 | */ |
| 51 | 50 | public function __construct( |
| 52 | 51 | FetchAuthTokenInterface $fetcher, |
| 53 | - ?array $cacheConfig = null, | |
| 54 | - ?CacheItemPoolInterface $cache = null | |
| 52 | + array $cacheConfig = null, | |
| 53 | + CacheItemPoolInterface $cache | |
| 55 | 54 | ) { |
| 56 | 55 | $this->fetcher = $fetcher; |
| 57 | 56 | $this->cache = $cache; |
| 58 | 57 | $this->cacheConfig = array_merge([ |
| @@ -57,9 +56,8 @@ | ||
| 57 | 56 | $this->cache = $cache; |
| 58 | 57 | $this->cacheConfig = array_merge([ |
| 59 | 58 | 'lifetime' => 1500, |
| 60 | 59 | 'prefix' => '', |
| 61 | - 'cacheUniverseDomain' => $fetcher instanceof Credentials\GCECredentials, | |
| 62 | 60 | ], (array) $cacheConfig); |
| 63 | 61 | } |
| 64 | 62 | |
| 65 | 63 | /** |
| @@ -75,13 +73,13 @@ | ||
| 75 | 73 | * |
| 76 | 74 | * Checks the cache for a valid auth token and fetches the auth tokens |
| 77 | 75 | * from the supplied fetcher. |
| 78 | 76 | * |
| 79 | - * @param callable|null $httpHandler callback which delivers psr7 request | |
| 77 | + * @param callable $httpHandler callback which delivers psr7 request | |
| 80 | 78 | * @return array<mixed> the response |
| 81 | 79 | * @throws \Exception |
| 82 | 80 | */ |
| 83 | - public function fetchAuthToken(?callable $httpHandler = null) | |
| 81 | + public function fetchAuthToken(callable $httpHandler = null) | |
| 84 | 82 | { |
| 85 | 83 | if ($cached = $this->fetchAuthTokenFromCache()) { |
| 86 | 84 | return $cached; |
| 87 | 85 | } |
| @@ -111,12 +109,12 @@ | ||
| 111 | 109 | |
| 112 | 110 | /** |
| 113 | 111 | * Get the client name from the fetcher. |
| 114 | 112 | * |
| 115 | - * @param callable|null $httpHandler An HTTP handler to deliver PSR7 requests. | |
| 113 | + * @param callable $httpHandler An HTTP handler to deliver PSR7 requests. | |
| 116 | 114 | * @return string |
| 117 | 115 | */ |
| 118 | - public function getClientName(?callable $httpHandler = null) | |
| 116 | + public function getClientName(callable $httpHandler = null) | |
| 119 | 117 | { |
| 120 | 118 | if (!$this->fetcher instanceof SignBlobInterface) { |
| 121 | 119 | throw new \RuntimeException( |
| 122 | 120 | 'Credentials fetcher does not implement ' . |
| @@ -146,16 +144,13 @@ | ||
| 146 | 144 | 'Google\Auth\SignBlobInterface' |
| 147 | 145 | ); |
| 148 | 146 | } |
| 149 | 147 | |
| 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 | - ) { | |
| 148 | + // Pass the access token from cache to GCECredentials for signing a blob. | |
| 149 | + // This saves a call to the metadata server when a cached token exists. | |
| 150 | + if ($this->fetcher instanceof Credentials\GCECredentials) { | |
| 156 | 151 | $cached = $this->fetchAuthTokenFromCache(); |
| 157 | - $accessToken = $cached['access_token'] ?? null; | |
| 152 | + $accessToken = isset($cached['access_token']) ? $cached['access_token'] : null; | |
| 158 | 153 | return $this->fetcher->signBlob($stringToSign, $forceOpenSsl, $accessToken); |
| 159 | 154 | } |
| 160 | 155 | |
| 161 | 156 | return $this->fetcher->signBlob($stringToSign, $forceOpenSsl); |
| @@ -175,17 +170,17 @@ | ||
| 175 | 170 | |
| 176 | 171 | return null; |
| 177 | 172 | } |
| 178 | 173 | |
| 179 | - /** | |
| 174 | + /* | |
| 180 | 175 | * Get the Project ID from the fetcher. |
| 181 | 176 | * |
| 182 | - * @param callable|null $httpHandler Callback which delivers psr7 request | |
| 177 | + * @param callable $httpHandler Callback which delivers psr7 request | |
| 183 | 178 | * @return string|null |
| 184 | 179 | * @throws \RuntimeException If the fetcher does not implement |
| 185 | 180 | * `Google\Auth\ProvidesProjectIdInterface`. |
| 186 | 181 | */ |
| 187 | - public function getProjectId(?callable $httpHandler = null) | |
| 182 | + public function getProjectId(callable $httpHandler = null) | |
| 188 | 183 | { |
| 189 | 184 | if (!$this->fetcher instanceof ProjectIdProviderInterface) { |
| 190 | 185 | throw new \RuntimeException( |
| 191 | 186 | 'Credentials fetcher does not implement ' . |
| @@ -192,43 +187,17 @@ | ||
| 192 | 187 | 'Google\Auth\ProvidesProjectIdInterface' |
| 193 | 188 | ); |
| 194 | 189 | } |
| 195 | 190 | |
| 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 | 191 | return $this->fetcher->getProjectId($httpHandler); |
| 206 | 192 | } |
| 207 | 193 | |
| 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 | 194 | /** |
| 226 | 195 | * Updates metadata with the authorization token. |
| 227 | 196 | * |
| 228 | 197 | * @param array<mixed> $metadata metadata hashmap |
| 229 | 198 | * @param string $authUri optional auth uri |
| 230 | - * @param callable|null $httpHandler callback which delivers psr7 request | |
| 199 | + * @param callable $httpHandler callback which delivers psr7 request | |
| 231 | 200 | * @return array<mixed> updated metadata hashmap |
| 232 | 201 | * @throws \RuntimeException If the fetcher does not implement |
| 233 | 202 | * `Google\Auth\UpdateMetadataInterface`. |
| 234 | 203 | */ |
| @@ -234,9 +203,9 @@ | ||
| 234 | 203 | */ |
| 235 | 204 | public function updateMetadata( |
| 236 | 205 | $metadata, |
| 237 | 206 | $authUri = null, |
| 238 | - ?callable $httpHandler = null | |
| 207 | + callable $httpHandler = null | |
| 239 | 208 | ) { |
| 240 | 209 | if (!$this->fetcher instanceof UpdateMetadataInterface) { |
| 241 | 210 | throw new \RuntimeException( |
| 242 | 211 | 'Credentials fetcher does not implement ' . |
| @@ -252,12 +221,8 @@ | ||
| 252 | 221 | if (isset($cached['access_token'])) { |
| 253 | 222 | $metadata[self::AUTH_METADATA_KEY] = [ |
| 254 | 223 | 'Bearer ' . $cached['access_token'] |
| 255 | 224 | ]; |
| 256 | - } elseif (isset($cached['id_token'])) { | |
| 257 | - $metadata[self::AUTH_METADATA_KEY] = [ | |
| 258 | - 'Bearer ' . $cached['id_token'] | |
| 259 | - ]; | |
| 260 | 225 | } |
| 261 | 226 | } |
| 262 | 227 | |
| 263 | 228 | $newMetadata = $this->fetcher->updateMetadata( |
| @@ -322,18 +287,6 @@ | ||
| 322 | 287 | : $this->fetcher->getCacheKey(); |
| 323 | 288 | |
| 324 | 289 | $this->setCachedValue($cacheKey, $authToken); |
| 325 | 290 | } |
| 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 | 291 | } |
| 339 | 292 | } |