new MemoryCacheItemPool(), 'authCacheOptions' => [], 'credentialsFetcher' => null, 'keyFile' => null, 'requestTimeout' => null, 'retries' => 3, 'scopes' => null, 'quotaProject' => null]; if ($config['credentialsFetcher'] && !$config['credentialsFetcher'] instanceof FetchAuthTokenInterface) { throw new \InvalidArgumentException('credentialsFetcher must implement FetchAuthTokenInterface.'); } if (!$config['authCache'] instanceof CacheItemPoolInterface) { throw new \InvalidArgumentException('authCache must implement CacheItemPoolInterface.'); } $this->authCache = $config['authCache']; $this->authCacheOptions = $config['authCacheOptions']; $this->credentialsFetcher = $config['credentialsFetcher']; $this->retries = $config['retries']; $this->scopes = $config['scopes']; $this->keyFile = $config['keyFile']; $this->requestTimeout = $config['requestTimeout']; $this->quotaProject = $config['quotaProject']; } /** * Get the Keyfile. * * @return array */ public function keyFile() { return $this->keyFile; } /** * Get the scopes * * @return array */ public function scopes() { return $this->scopes; } /** * Gets the credentials fetcher and sets up caching. Precedence is as * follows: * * - A user supplied credentials fetcher instance. * - Credentials created from a keyfile. * - Application default credentials. * - Anonymous credentials. * * @return FetchAuthTokenInterface */ public function getCredentialsFetcher() { $fetcher = null; if ($this->credentialsFetcher) { $fetcher = $this->credentialsFetcher; } else { if ($this->keyFile) { if ($this->quotaProject) { $this->keyFile['quota_project_id'] = $this->quotaProject; } $fetcher = CredentialsLoader::makeCredentials($this->scopes, $this->keyFile); } else { try { $fetcher = $this->getADC(); } catch (\DomainException $ex) { $fetcher = new AnonymousCredentials(); } } // Note: If authCache is set and keyFile is not set, the resulting // credentials instance will be FetchAuthTokenCache, and we will be // unable to enable "useJwtAccessWithScope". This is unlikely, as // keyFile is automatically set in ClientTrait::configureAuthentication, // and so should always exist when ServiceAccountCredentials are in use. if ($fetcher instanceof ServiceAccountCredentials) { $fetcher->useJwtAccessWithScope(); } } if ($fetcher instanceof FetchAuthTokenCache) { // The fetcher has already been wrapped in a cache by `ApplicationDefaultCredentials`; // no need to wrap it another time. return $fetcher; } else { return new FetchAuthTokenCache($fetcher, $this->authCacheOptions, $this->authCache); } } /** * Returns application default credentials. Abstracted out for unit testing. * * @return FetchAuthTokenInterface * @throws \DomainException */ protected function getADC() { return ApplicationDefaultCredentials::getCredentials($this->scopes, $this->authHttpHandler, $this->authCacheOptions, $this->authCache, $this->quotaProject); } }