-
Notifications
You must be signed in to change notification settings - Fork 194
feat: add ID tokens for user refresh credentials #468
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e2fab29
WIP
bshaffer b8ae86f
add phpdoc
bshaffer 51dbb89
Merge branch 'main' into user-refresh-id-tokens
bshaffer 60b2f44
Merge branch 'main' into user-refresh-id-tokens
bshaffer f3a196d
adds tests, cleans up existing tests
bshaffer a03a82c
cs update
bshaffer cbcc986
fix phpstan, add another test
bshaffer 25cf264
Merge branch 'main' into user-refresh-id-tokens
bshaffer 9983e88
fix cs
bshaffer d0cbb7c
add metric test for user refresh id tokens
bshaffer 135b87c
Merge branch 'main' into user-refresh-id-tokens
bshaffer 223aa88
Update src/Credentials/UserRefreshCredentials.php
bshaffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -55,48 +57,67 @@ 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. | ||
| * | ||
| * @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|null $targetAudience The audience for the ID token. | ||
| */ | ||
| public function __construct( | ||
| $scope, | ||
| $jsonKey | ||
| $jsonKey, | ||
| 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 or is unreadable'); | ||
| } | ||
| $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' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ); | ||
| } | ||
| $additionalClaims = []; | ||
| if ($targetAudience) { | ||
| $additionalClaims = ['target_audience' => $targetAudience]; | ||
| $this->isIdTokenRequest = true; | ||
| } | ||
| $this->auth = new OAuth2([ | ||
| 'clientId' => $jsonKey['client_id'], | ||
| 'clientSecret' => $jsonKey['client_secret'], | ||
| 'refresh_token' => $jsonKey['refresh_token'], | ||
| 'scope' => $scope, | ||
| 'tokenCredentialUri' => self::TOKEN_CREDENTIAL_URI, | ||
| 'additionalClaims' => $additionalClaims, | ||
| ]); | ||
| if (array_key_exists('quota_project_id', $jsonKey)) { | ||
| $this->quotaProject = (string) $jsonKey['quota_project_id']; | ||
|
|
@@ -122,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') | ||
| ); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
file_get_contentscan return afalsevalue on error. Should we take that into consideration?There was a problem hiding this comment.
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 unreadableinstead