Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add universe domain support to external account credentials #524

Closed
wants to merge 4 commits into from
Closed
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
19 changes: 18 additions & 1 deletion src/Credentials/ExternalAccountCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Google\Auth\ExternalAccountCredentialSourceInterface;
use Google\Auth\FetchAuthTokenInterface;
use Google\Auth\GetQuotaProjectInterface;
use Google\Auth\GetUniverseDomainInterface;
use Google\Auth\HttpHandler\HttpClientCache;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use Google\Auth\OAuth2;
Expand All @@ -31,7 +32,11 @@
use GuzzleHttp\Psr7\Request;
use InvalidArgumentException;

class ExternalAccountCredentials implements FetchAuthTokenInterface, UpdateMetadataInterface, GetQuotaProjectInterface
class ExternalAccountCredentials implements
FetchAuthTokenInterface,
UpdateMetadataInterface,
GetQuotaProjectInterface,
GetUniverseDomainInterface
{
use UpdateMetadataTrait;

Expand All @@ -40,6 +45,7 @@ class ExternalAccountCredentials implements FetchAuthTokenInterface, UpdateMetad
private OAuth2 $auth;
private ?string $quotaProject;
private ?string $serviceAccountImpersonationUrl;
private string $universeDomain;

/**
* @param string|string[] $scope The scope of the access request, expressed either as an array
Expand Down Expand Up @@ -90,6 +96,7 @@ public function __construct(
}

$this->quotaProject = $jsonKey['quota_project_id'] ?? null;
$this->universeDomain = $jsonKey['universe_domain'] ?? GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN;

$this->auth = new OAuth2([
'tokenCredentialUri' => $jsonKey['token_url'],
Expand Down Expand Up @@ -238,4 +245,14 @@ public function getQuotaProject()
{
return $this->quotaProject;
}

/**
* Get the universe domain used for this API request
*
* @return string
*/
public function getUniverseDomain(): string
{
return $this->universeDomain;
}
}
32 changes: 32 additions & 0 deletions tests/Credentials/ExternalAccountCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Google\Auth\CredentialSource\AwsNativeSource;
use Google\Auth\CredentialSource\FileSource;
use Google\Auth\CredentialSource\UrlSource;
use Google\Auth\GetUniverseDomainInterface;
use Google\Auth\OAuth2;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -337,4 +338,35 @@ public function testGetQuotaProject()
$creds = new ExternalAccountCredentials('a-scope', $jsonCreds);
$this->assertEquals('test_quota_project', $creds->getQuotaProject());
}

public function testGetUniverseDomain()
{
// no universe domain is the default "googleapis.com"
$jsonCreds = [
'type' => 'external_account',
'token_url' => 'token-url.com',
'audience' => '',
'subject_token_type' => '',
'credential_source' => ['url' => 'sts-url.com'],
];
$creds = new ExternalAccountCredentials('a-scope', $jsonCreds);
$this->assertEquals(
GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN,
$creds->getUniverseDomain()
);

// universe domain in credentials is used if supplied
$universeDomain = 'example-universe.com';
$jsonCreds = [
'type' => 'external_account',
'token_url' => 'token-url.com',
'audience' => '',
'subject_token_type' => '',
'credential_source' => ['url' => 'sts-url.com'],
'universe_domain' => $universeDomain,
];

$creds = new ExternalAccountCredentials('a-scope', $jsonCreds);
$this->assertEquals($universeDomain, $creds->getUniverseDomain());
}
}