Skip to content

Commit

Permalink
throw exception for domain-wide delegation outside GDU
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Dec 13, 2023
1 parent 50d81c9 commit 5f8b409
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Credentials/ServiceAccountCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,20 @@ public function getUniverseDomain(): string
*/
private function useSelfSignedJwt()
{
// When a sub is supplied, the user is using domain-wide delegation, which not available
// with self-signed JWTs
if (null !== $this->auth->getSub()) {
// If we are outside the GDU, we can't use domain-wide delegation
if ($this->getUniverseDomain() !== self::DEFAULT_UNIVERSE_DOMAIN) {
throw new \LogicException(sprintf(
'Service Account subject is configured for the credential. Domain-wide ' .
'delegation is not supported in universes other than %s.',
self::DEFAULT_UNIVERSE_DOMAIN
));
}
return false;
}

// If claims are set, this call is for "id_tokens"
if ($this->auth->getAdditionalClaims()) {
return false;
Expand Down
18 changes: 18 additions & 0 deletions tests/Credentials/ServiceAccountCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,24 @@ public function testSettingBothScopeAndTargetAudienceThrowsException()
);
}

public function testDomainWideDelegationOutsideGduThrowsException()
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage(
'Service Account subject is configured for the credential. Domain-wide ' .
'delegation is not supported in universes other than googleapis.com'
);
$testJson = $this->createTestJson() + ['universe_domain' => 'abc.xyz'];
$sub = 'sub123';
$sa = new ServiceAccountCredentials(
null,
$testJson,
$sub
);

$sa->fetchAuthToken();
}

public function testReturnsClientEmail()
{
$testJson = $this->createTestJson();
Expand Down
2 changes: 2 additions & 0 deletions tests/FetchAuthTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ public function testServiceAccountCredentialsGetLastReceivedToken()
->willReturn($this->scopes);
$oauth2Mock->getAdditionalClaims()
->willReturn([]);
$oauth2Mock->getSub()
->willReturn(null);

$credentials = new ServiceAccountCredentials($this->scopes, $jsonPath);
$property->setValue($credentials, $oauth2Mock->reveal());
Expand Down

0 comments on commit 5f8b409

Please sign in to comment.