Skip to content

Commit

Permalink
fix phpstan, add another test
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Oct 3, 2024
1 parent a03a82c commit cbcc986
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/Credentials/UserRefreshCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Google\Auth\CredentialsLoader;
use Google\Auth\GetQuotaProjectInterface;
use Google\Auth\OAuth2;
use InvalidArgumentException;
use LogicException;

/**
* Authenticates requests using User Refresh credentials.
Expand Down Expand Up @@ -58,41 +60,46 @@ class UserRefreshCredentials extends CredentialsLoader implements GetQuotaProjec
/**
* Create a new UserRefreshCredentials.
*
* @param string|string[] $scope the scope of the access request, expressed
* @param string|string[]|null $scope the scope of the access request, expressed
* either as an Array or as a space-delimited String.
* @param string|array<mixed> $jsonKey JSON credential file path or JSON credentials
* as an associative array
* @param string $targetAudience The audience for the ID token.
* @param string|null $targetAudience The audience for the ID token.
*/
public function __construct(
$scope,
$jsonKey,
$targetAudience = null
string $targetAudience = null
) {
if (is_string($jsonKey)) {
if (!file_exists($jsonKey)) {
throw new \InvalidArgumentException('file does not exist');
throw new InvalidArgumentException('file does not exist');
}
$json = file_get_contents($jsonKey);
if (!$jsonKey = json_decode((string) $json, true)) {
throw new \LogicException('invalid json for auth config');
throw new LogicException('invalid json for auth config');
}
}
if (!array_key_exists('client_id', $jsonKey)) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
'json key is missing the client_id field'
);
}
if (!array_key_exists('client_secret', $jsonKey)) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
'json key is missing the client_secret field'
);
}
if (!array_key_exists('refresh_token', $jsonKey)) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
'json key is missing the refresh_token field'
);
}
if ($scope && $targetAudience) {
throw new InvalidArgumentException(
'Scope and targetAudience cannot both be supplied'
);
}
$additionalClaims = [];
if ($targetAudience) {
$additionalClaims = ['target_audience' => $targetAudience];
Expand Down
9 changes: 9 additions & 0 deletions tests/Credentials/UserRefreshCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ public function testShouldBeIdTokenWhenTargetAudienceIsSet()
$this->assertEquals(1, $timesCalled);
}

public function testSettingBothScopeAndTargetAudienceThrowsException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Scope and targetAudience cannot both be supplied');

$testJson = $this->createTestJson();
$sa = new UserRefreshCredentials('a-scope', $testJson, 'a-target-audience');
}

public function testGetQuotaProject()
{
$keyFile = __DIR__ . '/../fixtures2' . '/private.json';
Expand Down

0 comments on commit cbcc986

Please sign in to comment.