← All changes
|
includes/sdk/google/google/auth/src/FetchAuthTokenCache.php
+23
-23
1.0.0
→
1.4.2
View file →
| @@ -14,11 +14,11 @@ | ||
| 14 | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | 15 | * See the License for the specific language governing permissions and |
| 16 | 16 | * limitations under the License. |
| 17 | 17 | */ |
| 18 | -namespace Dudlewebs\WPMCS\Google\Auth; | |
| 18 | +namespace Dudlewebs\WPMCS\GCP\Google\Auth; | |
| 19 | 19 | |
| 20 | -use Dudlewebs\WPMCS\Psr\Cache\CacheItemPoolInterface; | |
| 20 | +use Dudlewebs\WPMCS\GCP\Psr\Cache\CacheItemPoolInterface; | |
| 21 | 21 | /** |
| 22 | 22 | * A class to implement caching for any object implementing |
| 23 | 23 | * FetchAuthTokenInterface |
| 24 | 24 | */ |
| @@ -34,16 +34,16 @@ | ||
| 34 | 34 | */ |
| 35 | 35 | private $eagerRefreshThresholdSeconds = 10; |
| 36 | 36 | /** |
| 37 | 37 | * @param FetchAuthTokenInterface $fetcher A credentials fetcher |
| 38 | - * @param array<mixed> $cacheConfig Configuration for the cache | |
| 38 | + * @param array<mixed>|null $cacheConfig Configuration for the cache | |
| 39 | 39 | * @param CacheItemPoolInterface $cache |
| 40 | 40 | */ |
| 41 | - public function __construct(FetchAuthTokenInterface $fetcher, array $cacheConfig = null, CacheItemPoolInterface $cache) | |
| 41 | + public function __construct(FetchAuthTokenInterface $fetcher, ?array $cacheConfig = null, ?CacheItemPoolInterface $cache = null) | |
| 42 | 42 | { |
| 43 | 43 | $this->fetcher = $fetcher; |
| 44 | 44 | $this->cache = $cache; |
| 45 | - $this->cacheConfig = array_merge(['lifetime' => 1500, 'prefix' => '', 'cacheUniverseDomain' => $fetcher instanceof Credentials\GCECredentials], (array) $cacheConfig); | |
| 45 | + $this->cacheConfig = \array_merge(['lifetime' => 1500, 'prefix' => '', 'cacheUniverseDomain' => $fetcher instanceof Credentials\GCECredentials], (array) $cacheConfig); | |
| 46 | 46 | } |
| 47 | 47 | /** |
| 48 | 48 | * @return FetchAuthTokenInterface |
| 49 | 49 | */ |
| @@ -56,13 +56,13 @@ | ||
| 56 | 56 | * |
| 57 | 57 | * Checks the cache for a valid auth token and fetches the auth tokens |
| 58 | 58 | * from the supplied fetcher. |
| 59 | 59 | * |
| 60 | - * @param callable $httpHandler callback which delivers psr7 request | |
| 60 | + * @param callable|null $httpHandler callback which delivers psr7 request | |
| 61 | 61 | * @return array<mixed> the response |
| 62 | 62 | * @throws \Exception |
| 63 | 63 | */ |
| 64 | - public function fetchAuthToken(callable $httpHandler = null) | |
| 64 | + public function fetchAuthToken(?callable $httpHandler = null) | |
| 65 | 65 | { |
| 66 | 66 | if ($cached = $this->fetchAuthTokenFromCache()) { |
| 67 | 67 | return $cached; |
| 68 | 68 | } |
| @@ -86,15 +86,15 @@ | ||
| 86 | 86 | } |
| 87 | 87 | /** |
| 88 | 88 | * Get the client name from the fetcher. |
| 89 | 89 | * |
| 90 | - * @param callable $httpHandler An HTTP handler to deliver PSR7 requests. | |
| 90 | + * @param callable|null $httpHandler An HTTP handler to deliver PSR7 requests. | |
| 91 | 91 | * @return string |
| 92 | 92 | */ |
| 93 | - public function getClientName(callable $httpHandler = null) | |
| 93 | + public function getClientName(?callable $httpHandler = null) | |
| 94 | 94 | { |
| 95 | 95 | if (!$this->fetcher instanceof SignBlobInterface) { |
| 96 | - throw new \RuntimeException('Credentials fetcher does not implement ' . 'Google\Auth\SignBlobInterface'); | |
| 96 | + throw new \RuntimeException('Credentials fetcher does not implement ' . 'Dudlewebs\\WPMCS\\GCP\\Google\\Auth\\SignBlobInterface'); | |
| 97 | 97 | } |
| 98 | 98 | return $this->fetcher->getClientName($httpHandler); |
| 99 | 99 | } |
| 100 | 100 | /** |
| @@ -110,9 +110,9 @@ | ||
| 110 | 110 | */ |
| 111 | 111 | public function signBlob($stringToSign, $forceOpenSsl = \false) |
| 112 | 112 | { |
| 113 | 113 | if (!$this->fetcher instanceof SignBlobInterface) { |
| 114 | - throw new \RuntimeException('Credentials fetcher does not implement ' . 'Google\Auth\SignBlobInterface'); | |
| 114 | + throw new \RuntimeException('Credentials fetcher does not implement ' . 'Dudlewebs\\WPMCS\\GCP\\Google\\Auth\\SignBlobInterface'); | |
| 115 | 115 | } |
| 116 | 116 | // Pass the access token from cache for credentials that sign blobs |
| 117 | 117 | // using the IAM API. This saves a call to fetch an access token when a |
| 118 | 118 | // cached token exists. |
| @@ -135,20 +135,20 @@ | ||
| 135 | 135 | return $this->fetcher->getQuotaProject(); |
| 136 | 136 | } |
| 137 | 137 | return null; |
| 138 | 138 | } |
| 139 | - /* | |
| 139 | + /** | |
| 140 | 140 | * Get the Project ID from the fetcher. |
| 141 | 141 | * |
| 142 | - * @param callable $httpHandler Callback which delivers psr7 request | |
| 142 | + * @param callable|null $httpHandler Callback which delivers psr7 request | |
| 143 | 143 | * @return string|null |
| 144 | 144 | * @throws \RuntimeException If the fetcher does not implement |
| 145 | 145 | * `Google\Auth\ProvidesProjectIdInterface`. |
| 146 | 146 | */ |
| 147 | - public function getProjectId(callable $httpHandler = null) | |
| 147 | + public function getProjectId(?callable $httpHandler = null) | |
| 148 | 148 | { |
| 149 | 149 | if (!$this->fetcher instanceof ProjectIdProviderInterface) { |
| 150 | - throw new \RuntimeException('Credentials fetcher does not implement ' . 'Google\Auth\ProvidesProjectIdInterface'); | |
| 150 | + throw new \RuntimeException('Credentials fetcher does not implement ' . 'Dudlewebs\\WPMCS\\GCP\\Google\\Auth\\ProvidesProjectIdInterface'); | |
| 151 | 151 | } |
| 152 | 152 | // Pass the access token from cache for credentials that require an |
| 153 | 153 | // access token to fetch the project ID. This saves a call to fetch an |
| 154 | 154 | // access token when a cached token exists. |
| @@ -163,9 +163,9 @@ | ||
| 163 | 163 | * Get the Universe Domain from the fetcher. |
| 164 | 164 | * |
| 165 | 165 | * @return string |
| 166 | 166 | */ |
| 167 | - public function getUniverseDomain(): string | |
| 167 | + public function getUniverseDomain() : string | |
| 168 | 168 | { |
| 169 | 169 | if ($this->fetcher instanceof GetUniverseDomainInterface) { |
| 170 | 170 | if ($this->cacheConfig['cacheUniverseDomain']) { |
| 171 | 171 | return $this->getCachedUniverseDomain($this->fetcher); |
| @@ -178,17 +178,17 @@ | ||
| 178 | 178 | * Updates metadata with the authorization token. |
| 179 | 179 | * |
| 180 | 180 | * @param array<mixed> $metadata metadata hashmap |
| 181 | 181 | * @param string $authUri optional auth uri |
| 182 | - * @param callable $httpHandler callback which delivers psr7 request | |
| 182 | + * @param callable|null $httpHandler callback which delivers psr7 request | |
| 183 | 183 | * @return array<mixed> updated metadata hashmap |
| 184 | 184 | * @throws \RuntimeException If the fetcher does not implement |
| 185 | 185 | * `Google\Auth\UpdateMetadataInterface`. |
| 186 | 186 | */ |
| 187 | - public function updateMetadata($metadata, $authUri = null, callable $httpHandler = null) | |
| 187 | + public function updateMetadata($metadata, $authUri = null, ?callable $httpHandler = null) | |
| 188 | 188 | { |
| 189 | 189 | if (!$this->fetcher instanceof UpdateMetadataInterface) { |
| 190 | - throw new \RuntimeException('Credentials fetcher does not implement ' . 'Google\Auth\UpdateMetadataInterface'); | |
| 190 | + throw new \RuntimeException('Credentials fetcher does not implement ' . 'Dudlewebs\\WPMCS\\GCP\\Google\\Auth\\UpdateMetadataInterface'); | |
| 191 | 191 | } |
| 192 | 192 | $cached = $this->fetchAuthTokenFromCache($authUri); |
| 193 | 193 | if ($cached) { |
| 194 | 194 | // Set the access token in the `Authorization` metadata header so |
| @@ -200,9 +200,9 @@ | ||
| 200 | 200 | $metadata[self::AUTH_METADATA_KEY] = ['Bearer ' . $cached['id_token']]; |
| 201 | 201 | } |
| 202 | 202 | } |
| 203 | 203 | $newMetadata = $this->fetcher->updateMetadata($metadata, $authUri, $httpHandler); |
| 204 | - if (!$cached && $token = $this->fetcher->getLastReceivedToken()) { | |
| 204 | + if (!$cached && ($token = $this->fetcher->getLastReceivedToken())) { | |
| 205 | 205 | $this->saveAuthTokenInCache($token, $authUri); |
| 206 | 206 | } |
| 207 | 207 | return $newMetadata; |
| 208 | 208 | } |
| @@ -220,15 +220,15 @@ | ||
| 220 | 220 | // TODO: correct caching; enable the cache to be cleared. |
| 221 | 221 | // if $authUri is set, use it as the cache key |
| 222 | 222 | $cacheKey = $authUri ? $this->getFullCacheKey($authUri) : $this->fetcher->getCacheKey(); |
| 223 | 223 | $cached = $this->getCachedValue($cacheKey); |
| 224 | - if (is_array($cached)) { | |
| 224 | + if (\is_array($cached)) { | |
| 225 | 225 | if (empty($cached['expires_at'])) { |
| 226 | 226 | // If there is no expiration data, assume token is not expired. |
| 227 | 227 | // (for JwtAccess and ID tokens) |
| 228 | 228 | return $cached; |
| 229 | 229 | } |
| 230 | - if (time() + $this->eagerRefreshThresholdSeconds < $cached['expires_at']) { | |
| 230 | + if (\time() + $this->eagerRefreshThresholdSeconds < $cached['expires_at']) { | |
| 231 | 231 | // access token is not expired |
| 232 | 232 | return $cached; |
| 233 | 233 | } |
| 234 | 234 | } |
| @@ -246,9 +246,9 @@ | ||
| 246 | 246 | $cacheKey = $authUri ? $this->getFullCacheKey($authUri) : $this->fetcher->getCacheKey(); |
| 247 | 247 | $this->setCachedValue($cacheKey, $authToken); |
| 248 | 248 | } |
| 249 | 249 | } |
| 250 | - private function getCachedUniverseDomain(GetUniverseDomainInterface $fetcher): string | |
| 250 | + private function getCachedUniverseDomain(GetUniverseDomainInterface $fetcher) : string | |
| 251 | 251 | { |
| 252 | 252 | $cacheKey = $this->getFullCacheKey($fetcher->getCacheKey() . 'universe_domain'); |
| 253 | 253 | // @phpstan-ignore-line |
| 254 | 254 | if ($universeDomain = $this->getCachedValue($cacheKey)) { |