Skip to content

Conversation

@bshaffer
Copy link
Contributor

@bshaffer bshaffer commented Aug 15, 2023

Add ID token support to UserRefreshCredentials

  • add tests
  • manual E2E test to verify it works

Manually tested by running gcloud auth application-default login, and then executing the following:

use Google\Auth\ApplicationDefaultCredentials;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Firebase\JWT\JWK;
use Firebase\JWT\JWT;

// Get the UserRefreshCredentials from "~/.config/gcloud/application_default_credentials.json",
// and supply the target audience
$credentials = ApplicationDefaultCredentials::getIdTokenCredentials('test-target-audience');
$idToken = $credentials->fetchAuthToken();

// Verify the ID token using the certs from Google
$certUrl = 'https://www.googleapis.com/oauth2/v3/certs';
$client = new GuzzleHttp\Client();
$certs = json_decode($client->send(new Request('GET', $certUrl))->getBody(), true);

$keys = JWK::parseKeySet($certs);
$decoded = JWT::decode($idToken['id_token'], $keys);
var_dump($decoded);

see also googleapis/google-auth-library-nodejs#1811

@bshaffer bshaffer requested a review from a team as a code owner August 15, 2023 16:33
@bshaffer bshaffer marked this pull request as draft August 15, 2023 16:33
@bshaffer bshaffer marked this pull request as ready for review October 3, 2024 01:01
throw new \InvalidArgumentException('file does not exist');
throw new InvalidArgumentException('file does not exist');
}
$json = file_get_contents($jsonKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file_get_contents can return a false value on error. Should we take that into consideration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that's a good point... maybe it should be file does not exist or is unreadable instead

}
if ($scope && $targetAudience) {
throw new InvalidArgumentException(
'Scope and targetAudience cannot both be supplied'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be my non native brain but this reads weird to me.
Scope and targetAudience cannot be supplied at the same time

But might be just me though.
english

@bshaffer bshaffer enabled auto-merge (squash) October 31, 2024 21:05
@bshaffer bshaffer merged commit 1601efc into main Oct 31, 2024
@bshaffer bshaffer deleted the user-refresh-id-tokens branch October 31, 2024 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants