Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/AuthHandler/Guzzle6AuthHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Google\AuthHandler;

use Google\Auth\CredentialsLoader;
use Google\Auth\FetchAuthTokenCache;
use Google\Auth\FetchAuthTokenInterface;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use Google\Auth\Middleware\AuthTokenMiddleware;
use Google\Auth\Middleware\ScopedAccessTokenMiddleware;
Expand All @@ -28,7 +28,7 @@ public function __construct(?CacheItemPoolInterface $cache = null, array $cacheC

public function attachCredentials(
ClientInterface $http,
CredentialsLoader $credentials,
FetchAuthTokenInterface $credentials,
?callable $tokenCallback = null
) {
// use the provided cache
Expand All @@ -40,13 +40,21 @@ public function attachCredentials(
);
}

return $this->attachCredentialsCache($http, $credentials, $tokenCallback);
return $this->attachToHttp($http, $credentials, $tokenCallback);
}

public function attachCredentialsCache(
ClientInterface $http,
FetchAuthTokenCache $credentials,
?callable $tokenCallback = null
) {
return $this->attachToHttp($http, $credentials, $tokenCallback);
}

private function attachToHttp(
ClientInterface $http,
FetchAuthTokenInterface $credentials,
?callable $tokenCallback = null
) {
// if we end up needing to make an HTTP request to retrieve credentials, we
// can use our existing one, but we need to throw exceptions so the error
Expand All @@ -63,9 +71,7 @@ public function attachCredentialsCache(
$config['handler']->remove('google_auth');
$config['handler']->push($middleware, 'google_auth');
$config['auth'] = 'google_auth';
$http = new Client($config);

return $http;
return new Client($config);
}

public function attachToken(ClientInterface $http, array $token, array $scopes)
Expand Down
12 changes: 7 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Google\Auth\Credentials\UserRefreshCredentials;
use Google\Auth\CredentialsLoader;
use Google\Auth\FetchAuthTokenCache;
use Google\Auth\FetchAuthTokenInterface;
use Google\Auth\GetUniverseDomainInterface;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use Google\Auth\OAuth2;
Expand Down Expand Up @@ -90,7 +91,7 @@ class Client
private $logger;

/**
* @var ?CredentialsLoader $credentials
* @var ?FetchAuthTokenInterface $credentials
*/
private $credentials;

Expand Down Expand Up @@ -118,10 +119,10 @@ class Client
* Your Google Cloud client ID found in https://developers.google.com/console
* @type string $client_secret
* Your Google Cloud client secret found in https://developers.google.com/console
* @type string|array|CredentialsLoader $credentials
* @type string|array|FetchAuthTokenInterface $credentials
* Can be a path to JSON credentials or an array representing those
* credentials (@see Google\Client::setAuthConfig), or an instance of
* {@see CredentialsLoader}.
* {@see FetchAuthTokenInterface}.
* @type string|array $scopes
* {@see Google\Client::setScopes}
* @type string $quota_project
Expand Down Expand Up @@ -213,7 +214,7 @@ public function __construct(array $config = [])
], $config);

if (!is_null($this->config['credentials'])) {
if ($this->config['credentials'] instanceof CredentialsLoader) {
if ($this->config['credentials'] instanceof FetchAuthTokenInterface) {
$this->credentials = $this->config['credentials'];
} else {
$this->setAuthConfig($this->config['credentials']);
Expand Down Expand Up @@ -460,7 +461,8 @@ public function authorize(?ClientInterface $http = null)
$authHandler = $this->getAuthHandler();

// These conditionals represent the decision tree for authentication
// 1. Check if a Google\Auth\CredentialsLoader instance has been supplied via the "credentials" option
// 1. Check if an instance of Google\Auth\FetchAuthTokenInterface has
// been supplied via the "credentials" option
// 2. Check for Application Default Credentials
// 3a. Check for an Access Token
// 3b. If access token exists but is expired, try to refresh it
Expand Down
13 changes: 9 additions & 4 deletions tests/Google/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Google\Service\Drive;
use Google\AuthHandler\AuthHandlerFactory;
use Google\Auth\FetchAuthTokenCache;
use Google\Auth\CredentialsLoader;
use Google\Auth\FetchAuthTokenInterface;
use Google\Auth\GCECache;
use Google\Auth\Credentials\GCECredentials;
use GuzzleHttp\Client as GuzzleClient;
Expand All @@ -40,6 +40,8 @@
use InvalidArgumentException;
use Exception;
use DomainException;
use Google\Auth\GetUniverseDomainInterface;
use Google\Auth\UpdateMetadataInterface;

class ClientTest extends BaseTest
{
Expand Down Expand Up @@ -868,10 +870,12 @@ public function testClientOptions()
$this->assertEquals('some-quota-project', $credentials->getQuotaProject());
}

public function testCredentialsOptionWithCredentialsLoader()
public function testCredentialsOptionWithFetchAuthTokenInterface()
{
$request = null;
$credentials = $this->prophesize('Google\Auth\CredentialsLoader');
$credentials = $this->prophesize(FetchAuthTokenInterface::class)
->willImplement(GetUniverseDomainInterface::class)
->willImplement(UpdateMetadataInterface::class);
$credentials->getCacheKey()
->willReturn('cache-key');
$credentials->getUniverseDomain()
Expand Down Expand Up @@ -947,7 +951,8 @@ public function testUniverseDomainMismatch()
'The configured universe domain (example.com) does not match the credential universe domain (foo.com)'
);

$credentials = $this->prophesize(CredentialsLoader::class);
$credentials = $this->prophesize(FetchAuthTokenInterface::class)
->willImplement(GetUniverseDomainInterface::class);
$credentials->getUniverseDomain()
->shouldBeCalledOnce()
->willReturn('foo.com');
Expand Down