Skip to content

Commit 626d22c

Browse files
committed
fix: ImpersonatedCredentials getLastReceivedToken returns correct token
1 parent 29abd13 commit 626d22c

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/Credentials/ImpersonatedServiceAccountCredentials.php

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

7373
private int $lifetime;
7474

75+
protected array|null $lastReceivedToken = null;
76+
7577
/**
7678
* Instantiate an instance of ImpersonatedServiceAccountCredentials from a credentials file that
7779
* has be created with the --impersonate-service-account flag.
@@ -252,7 +254,7 @@ public function fetchAuthToken(?callable $httpHandler = null)
252254
$response = $httpHandler($request);
253255
$body = json_decode((string) $response->getBody(), true);
254256

255-
return match ($this->isIdTokenRequest()) {
257+
return $this->lastReceivedToken = match ($this->isIdTokenRequest()) {
256258
true => ['id_token' => $body['token']],
257259
false => [
258260
'access_token' => $body['accessToken'],
@@ -279,7 +281,7 @@ public function getCacheKey()
279281
*/
280282
public function getLastReceivedToken()
281283
{
282-
return $this->sourceCredentials->getLastReceivedToken();
284+
return $this->lastReceivedToken;
283285
}
284286

285287
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)