GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — docs(samples): added auth samples and tests by Sita04 · Pull Request #927 · googleapis/google-auth-library-java
This repository was archived by the owner on May 12, 2026. It is now read-only.
Conversation
Chizzy09
approved these changes
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
piaxc
reviewed
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. Hi Sita, feel free to grab a slot on my calendar if you want to discuss any of my comments!
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsUh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page.
Sita04
marked this pull request as draft
# Conflicts: # samples/snippets/src/main/java/AuthenticateExplicit.java # samples/snippets/src/main/java/AuthenticateImplicitWithAdc.java # samples/snippets/src/main/java/IdTokenFromImpersonatedCredentials.java # samples/snippets/src/main/java/IdTokenFromImpersonatedCredentials_OAuth.java # samples/snippets/src/main/java/IdTokenFromMetadataServer.java # samples/snippets/src/main/java/IdTokenFromServiceAccount.java # samples/snippets/src/main/java/IdTokenFromServiceAccountREST.java # samples/snippets/src/main/java/VerifyNonGoogleIdToken.java # samples/snippets/src/test/java/SnippetsIT.java
piaxc
suggested changes
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. Thanks Sita!
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsUh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page.
|
There was an error while loading. Please reload this page.
|
Here is the summary of changes. You are about to add 5 region tags.
This comment is generated by snippet-bot.
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks Sita!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| import com.auth0.jwk.JwkException; | ||
| import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken; | ||
| import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload; | ||
| import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This verifier is from google-api-java-client and was not being maintained. It also had a CVE until recently where we weren't actually validating the signature.
We should instead use the token verifier from this library (like https://github.com/GoogleCloudPlatform/java-docs-samples/blob/96c4f10bf01683d0e0a5b2a44ba4bee1ac5fc507/iap/src/main/java/com/example/iap/VerifyIapRequestHeader.java#L60-L73)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Done. I've made the change you suggested and also removed the third party dependency.
PTAL.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| public static void verifyNonGoogleIdToken(String idToken, String targetAudience, | ||
| String jwkUrl) | ||
| throws IOException, JwkException { | ||
|
|
||
| // Start verification. | ||
| DecodedJWT jwt = JWT.decode(idToken); | ||
|
|
||
| // Check if the token has expired. | ||
| if (jwt.getExpiresAt().before(Calendar.getInstance().getTime())) { | ||
| System.out.println("Token already expired.."); | ||
| return; | ||
| } | ||
|
|
||
| // Construct the jwkProvider from the provided jwkURL. | ||
| JwkProvider jwkProvider = new UrlJwkProvider(new URL(jwkUrl)); | ||
| // Get the jwk from the provided key id. | ||
| Jwk jwk = jwkProvider.get(jwt.getKeyId()); | ||
| // Retrieve the public key and use that to create an instance of the Algorithm. | ||
| Algorithm algorithm = Algorithm.RSA256((RSAPublicKey) jwk.getPublicKey(), null); | ||
| // Create the verifier with the algorithm and target audience. | ||
| JWTVerifier jwtVerifier = JWT.require(algorithm).withAudience(targetAudience).build(); | ||
|
|
||
| boolean isVerified = true; | ||
| try { | ||
| // Verify the obtained id token. This is done at the receiving end of the OIDC endpoint. | ||
| jwt = jwtVerifier.verify(idToken); | ||
| } catch (JWTVerificationException e) { | ||
| System.out.println("Could not verify Signature: " + e.getMessage()); | ||
| isVerified = false; | ||
| } | ||
|
|
||
| if (isVerified) { | ||
| System.out.println("Id token verified."); | ||
| return; | ||
| } | ||
| System.out.println("Unable to verify ID token."); | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I would recommend that we use our first-party implementation and not require extra third-party dependencies.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| // see: https://developers.google.com/identity/protocols/oauth2/scopes | ||
|
|
||
| // Construct the Storage client. | ||
| Storage storage = |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This should use a try/catch and include the warning about making sure to close: disingenuous: https://googlecloudplatform.github.io/samples-style-guide/#clients
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I am not sure of this as many of the storage samples look like they don't follow the auto close try-catch..
https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/ListBuckets.java
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
2 non-blocking comments
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
# Conflicts: # samples/snippets/src/main/java/VerifyGoogleIdToken.java
Thanks Jeff! Included your comments in the sample for further clarification. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
🤖 I have created a release *beep* *boop* --- ## [1.10.0](v1.9.0...v1.10.0) (2022-08-05) ### Features * workforce identity federation for pluggable auth ([#959](#959)) ([7f2c535](7f2c535)) ### Bug Fixes * updates executable response spec for executable-sourced credentials ([#955](#955)) ([48ff83d](48ff83d)) ### Documentation * **samples:** added auth samples and tests ([#927](#927)) ([32c717f](32c717f)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
🤖 I have created a release *beep* *boop* --- ## [1.10.0](v1.9.0...v1.10.0) (2022-08-05) ### Features * workforce identity federation for pluggable auth ([#959](#959)) ([7f2c535](7f2c535)) ### Bug Fixes * updates executable response spec for executable-sourced credentials ([#955](#955)) ([48ff83d](48ff83d)) ### Documentation * **samples:** added auth samples and tests ([#927](#927)) ([32c717f](32c717f)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
🤖 I have created a release *beep* *boop* --- ## [1.10.0](googleapis/google-auth-library-java@v1.9.0...v1.10.0) (2022-08-05) ### Features * workforce identity federation for pluggable auth ([#959](googleapis/google-auth-library-java#959)) ([51b200c](googleapis/google-auth-library-java@51b200c)) ### Bug Fixes * updates executable response spec for executable-sourced credentials ([#955](googleapis/google-auth-library-java#955)) ([3f5ab60](googleapis/google-auth-library-java@3f5ab60)) ### Documentation * **samples:** added auth samples and tests ([#927](googleapis/google-auth-library-java#927)) ([821202f](googleapis/google-auth-library-java@821202f)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.