Skip to content
Merged
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
9 changes: 7 additions & 2 deletions src/Credentials/ImpersonatedServiceAccountCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class ImpersonatedServiceAccountCredentials extends CredentialsLoader implements

private int $lifetime;

/**
* @var array<mixed>|null
*/
protected array|null $lastReceivedToken = null;

/**
* Instantiate an instance of ImpersonatedServiceAccountCredentials from a credentials file that
* has be created with the --impersonate-service-account flag.
Expand Down Expand Up @@ -252,7 +257,7 @@ public function fetchAuthToken(?callable $httpHandler = null)
$response = $httpHandler($request);
$body = json_decode((string) $response->getBody(), true);

return match ($this->isIdTokenRequest()) {
return $this->lastReceivedToken = match ($this->isIdTokenRequest()) {
true => ['id_token' => $body['token']],
false => [
'access_token' => $body['accessToken'],
Expand All @@ -279,7 +284,7 @@ public function getCacheKey()
*/
public function getLastReceivedToken()
{
return $this->sourceCredentials->getLastReceivedToken();
return $this->lastReceivedToken;
}

protected function getCredType(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public function testGetAccessTokenWithServiceAccountAndUserRefreshCredentials(ar
$token = $creds->fetchAuthToken($httpHandler);
$this->assertEquals('test-impersonated-access-token', $token['access_token']);
$this->assertEquals(2, $requestCount);
$this->assertEquals($token, $creds->getLastReceivedToken());
}

/**
Expand Down Expand Up @@ -225,6 +226,7 @@ public function testGetAccessTokenWithExternalAccountCredentials()
$token = $creds->fetchAuthToken($httpHandler);
$this->assertEquals('test-impersonated-access-token', $token['access_token']);
$this->assertEquals(3, $requestCount);
$this->assertEquals($token, $creds->getLastReceivedToken());
}

/**
Expand Down Expand Up @@ -266,6 +268,7 @@ public function testGetIdTokenWithServiceAccountAndUserRefreshCredentials(array
$token = $creds->fetchAuthToken($httpHandler);
$this->assertEquals('test-impersonated-id-token', $token['id_token']);
$this->assertEquals(2, $requestCount);
$this->assertEquals($token, $creds->getLastReceivedToken());
}

public function provideAuthTokenJson()
Expand Down Expand Up @@ -307,6 +310,7 @@ public function testGetIdTokenWithServiceAccountCredentialsAndUniverseDomain()
$creds = new ImpersonatedServiceAccountCredentials(null, $json, self::TARGET_AUDIENCE);
$token = $creds->fetchAuthToken($httpHandler);
$this->assertEquals('test-impersonated-id-token', $token['id_token']);
$this->assertEquals($token, $creds->getLastReceivedToken());
}

/**
Expand Down Expand Up @@ -378,6 +382,7 @@ public function testGetIdTokenWithExternalAccountCredentials(?string $universeDo
$token = $creds->fetchAuthToken($httpHandler);
$this->assertEquals('test-impersonated-id-token', $token['id_token']);
$this->assertEquals(3, $requestCount);
$this->assertEquals($token, $creds->getLastReceivedToken());
}

/**
Expand Down Expand Up @@ -416,6 +421,7 @@ public function testGetIdTokenWithArbitraryCredentials(?string $universeDomain =

$token = $creds->fetchAuthToken($httpHandler);
$this->assertEquals('test-impersonated-id-token', $token['id_token']);
$this->assertEquals($token, $creds->getLastReceivedToken());
}

public function provideUniverseDomain()
Expand Down Expand Up @@ -455,6 +461,7 @@ public function testGetAccessTokenWithArbitraryCredentials()

$token = $creds->fetchAuthToken($httpHandler);
$this->assertEquals('test-impersonated-access-token', $token['access_token']);
$this->assertEquals($token, $creds->getLastReceivedToken());
}

public function testIdTokenWithAuthTokenMiddleware()
Expand Down
Loading