Skip to content

Commit a4fe69c

Browse files
authored
fix: ImpersonatedCredentials getLastReceivedToken returns correct token (#655)
1 parent 29abd13 commit a4fe69c

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/Credentials/ImpersonatedServiceAccountCredentials.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ class ImpersonatedServiceAccountCredentials extends CredentialsLoader implements
7272

7373
private int $lifetime;
7474

75+
/**
76+
* @var array<mixed>|null
77+
*/
78+
protected array|null $lastReceivedToken = null;
79+
7580
/**
7681
* Instantiate an instance of ImpersonatedServiceAccountCredentials from a credentials file that
7782
* has be created with the --impersonate-service-account flag.
@@ -252,7 +257,7 @@ public function fetchAuthToken(?callable $httpHandler = null)
252257
$response = $httpHandler($request);
253258
$body = json_decode((string) $response->getBody(), true);
254259

255-
return match ($this->isIdTokenRequest()) {
260+
return $this->lastReceivedToken = match ($this->isIdTokenRequest()) {
256261
true => ['id_token' => $body['token']],
257262
false => [
258263
'access_token' => $body['accessToken'],
@@ -279,7 +284,7 @@ public function getCacheKey()
279284
*/
280285
public function getLastReceivedToken()
281286
{
282-
return $this->sourceCredentials->getLastReceivedToken();
287+
return $this->lastReceivedToken;
283288
}
284289

285290
protected function getCredType(): string

tests/Credentials/ImpersonatedServiceAccountCredentialsTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public function testGetAccessTokenWithServiceAccountAndUserRefreshCredentials(ar
186186
$token = $creds->fetchAuthToken($httpHandler);
187187
$this->assertEquals('test-impersonated-access-token', $token['access_token']);
188188
$this->assertEquals(2, $requestCount);
189+
$this->assertEquals($token, $creds->getLastReceivedToken());
189190
}
190191

191192
/**
@@ -225,6 +226,7 @@ public function testGetAccessTokenWithExternalAccountCredentials()
225226
$token = $creds->fetchAuthToken($httpHandler);
226227
$this->assertEquals('test-impersonated-access-token', $token['access_token']);
227228
$this->assertEquals(3, $requestCount);
229+
$this->assertEquals($token, $creds->getLastReceivedToken());
228230
}
229231

230232
/**
@@ -266,6 +268,7 @@ public function testGetIdTokenWithServiceAccountAndUserRefreshCredentials(array
266268
$token = $creds->fetchAuthToken($httpHandler);
267269
$this->assertEquals('test-impersonated-id-token', $token['id_token']);
268270
$this->assertEquals(2, $requestCount);
271+
$this->assertEquals($token, $creds->getLastReceivedToken());
269272
}
270273

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

312316
/**
@@ -378,6 +382,7 @@ public function testGetIdTokenWithExternalAccountCredentials(?string $universeDo
378382
$token = $creds->fetchAuthToken($httpHandler);
379383
$this->assertEquals('test-impersonated-id-token', $token['id_token']);
380384
$this->assertEquals(3, $requestCount);
385+
$this->assertEquals($token, $creds->getLastReceivedToken());
381386
}
382387

383388
/**
@@ -416,6 +421,7 @@ public function testGetIdTokenWithArbitraryCredentials(?string $universeDomain =
416421

417422
$token = $creds->fetchAuthToken($httpHandler);
418423
$this->assertEquals('test-impersonated-id-token', $token['id_token']);
424+
$this->assertEquals($token, $creds->getLastReceivedToken());
419425
}
420426

421427
public function provideUniverseDomain()
@@ -455,6 +461,7 @@ public function testGetAccessTokenWithArbitraryCredentials()
455461

456462
$token = $creds->fetchAuthToken($httpHandler);
457463
$this->assertEquals('test-impersonated-access-token', $token['access_token']);
464+
$this->assertEquals($token, $creds->getLastReceivedToken());
458465
}
459466

460467
public function testIdTokenWithAuthTokenMiddleware()

0 commit comments

Comments
 (0)