Skip to content

Commit

Permalink
add metric test for user refresh id tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Oct 5, 2024
1 parent 9983e88 commit d0cbb7c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/Credentials/UserRefreshCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class UserRefreshCredentials extends CredentialsLoader implements GetQuotaProjec
*/
protected $quotaProject;

/**
* Whether this is an ID token request or an access token request. Used when
* building the metric header.
*/
private bool $isIdTokenRequest = false;

/**
* Create a new UserRefreshCredentials.
*
Expand Down Expand Up @@ -103,6 +109,7 @@ public function __construct(
$additionalClaims = [];
if ($targetAudience) {
$additionalClaims = ['target_audience' => $targetAudience];
$this->isIdTokenRequest = true;
}
$this->auth = new OAuth2([
'clientId' => $jsonKey['client_id'],
Expand Down Expand Up @@ -136,10 +143,9 @@ public function __construct(
*/
public function fetchAuthToken(callable $httpHandler = null, array $metricsHeader = [])
{
// We don't support id token endpoint requests as of now for User Cred
return $this->auth->fetchAuthToken(
$httpHandler,
$this->applyTokenEndpointMetrics($metricsHeader, 'at')
$this->applyTokenEndpointMetrics($metricsHeader, $this->isIdTokenRequest ? 'it' : 'at')
);
}

Expand Down
14 changes: 10 additions & 4 deletions tests/ObservabilityMetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ public function testImpersonatedServiceAccountCredentials()
$this->assertUpdateMetadata($impersonatedCred, $handler, 'imp', $handlerCalled);
}

/**
* UserRefreshCredentials haven't enabled identity token support hence
* they don't have 'auth-request-type/it' observability metric header check.
*/
public function testUserRefreshCredentials()
{
$keyFile = __DIR__ . '/fixtures2/gcloud.json';
Expand All @@ -145,6 +141,16 @@ public function testUserRefreshCredentials()
$this->assertUpdateMetadata($userRefreshCred, $handler, 'u', $handlerCalled);
}

public function testUserRefreshCredentialsWithIdTokens()
{
$keyFile = __DIR__ . '/fixtures2/gcloud.json';
$handlerCalled = false;
$handler = $this->getCustomHandler('u', 'auth-request-type/it', $handlerCalled);

$userRefreshCred = new UserRefreshCredentials(null, $keyFile, 'test-target-audience');
$this->assertUpdateMetadata($userRefreshCred, $handler, 'u', $handlerCalled);
}

/**
* Invokes the 'updateMetadata' method of cred fetcher with empty metadata argument
* and asserts for proper service api usage observability metrics header.
Expand Down

0 comments on commit d0cbb7c

Please sign in to comment.