GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@Sita04

@Sita04 Sita04 commented Jun 8, 2022

Copy link
Copy Markdown
Contributor

Adding samples for the authentication docs.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Sita04 added 4 commits June 6, 2022 19:58
@Sita04
docs(samples): added client code for idtoken, adc and metadata server
3bd604d
@Sita04
docs(samples): added authexplicit and copyright
bd1bc55
@Sita04
docs(samples): add auth with metadata server
ece1c56
@Sita04
docs(samples): minor refactoring and added tests
0c26c33
@Sita04
Sita04 requested review from a team June 8, 2022 09:35
@product-auto-label product-auto-label Bot added size: xl Pull request size is extra large. samples Issues that are directly related to samples. labels Jun 8, 2022
@blunderbuss-gcf blunderbuss-gcf Bot assigned kolea2 Jun 8, 2022
@gcf-owl-bot
🦉 Updates from OwlBot post-processor
45449b2
See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md
@Sita04 Sita04 changed the title Added auth samples and tests docs(samples): added auth samples and tests Jun 8, 2022
@Sita04 Sita04 assigned kolea2 and unassigned kolea2 Jun 8, 2022
@Shabirmean
Merge branch 'main' into auth-samples
7ef36df
Chizzy09
Chizzy09 approved these changes Jun 18, 2022
View reviewed changes
@Chizzy09

Chizzy09 commented Jun 18, 2022

Copy link
Copy Markdown

45449b2

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

piaxc
piaxc reviewed Jun 27, 2022
View reviewed changes

@piaxc piaxc left a comment

Copy link
Copy Markdown
Contributor

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.

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 reactions
Comment thread samples/snippets/pom.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/AuthWithCredentialsFromMetadataServer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/AuthenticateExplicit.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/AuthenticateExplicit.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/AuthenticateImplicitWithAdc.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/IdTokenFromImpersonatedCredentialsREST.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/IdTokenFromMetadataServer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/IdTokenFromMetadataServer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/IdTokenFromMetadataServer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/IdTokenFromServiceAccount.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Sita04
Sita04 marked this pull request as draft July 18, 2022 04:33
Sita04 added 2 commits July 18, 2022 15:58
@Sita04
refactored acc to review comments
b2f367e
@Sita04
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
01b57ad
# 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
@product-auto-label product-auto-label Bot added size: l Pull request size is large. and removed size: xl Pull request size is extra large. labels Jul 18, 2022
@Sita04
Merge branch 'main' into auth-samples
b9dee60
piaxc
piaxc suggested changes Jul 19, 2022
View reviewed changes

@piaxc piaxc left a comment

Copy link
Copy Markdown
Contributor

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.

Thanks Sita!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread samples/snippets/src/main/java/AuthenticateExplicit.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/AuthenticateExplicit.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/AuthenticateImplicitWithAdc.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/IdTokenFromImpersonatedCredentials_IAM.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/IdTokenFromImpersonatedCredentials_OAuth.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/AuthenticateImplicitWithAdc.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/IdTokenFromServiceAccount.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/IdTokenFromServiceAccount.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/IdTokenFromServiceAccount.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/IdTokenFromServiceAccount.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Sita04 added 2 commits July 23, 2022 01:47
@Sita04
refactored acc to review comments
3ed6be0
@Sita04
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
dd39fd4
@snippet-bot

snippet-bot Bot commented Jul 22, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 5 region tags.
  • samples/snippets/src/main/java/AuthenticateExplicit.java:17, tag auth_cloud_explicit_adc
  • samples/snippets/src/main/java/AuthenticateImplicitWithAdc.java:17, tag auth_cloud_implicit_adc
  • samples/snippets/src/main/java/IdTokenFromMetadataServer.java:17, tag auth_cloud_idtoken_metadata_server
  • samples/snippets/src/main/java/IdTokenFromServiceAccount.java:17, tag auth_cloud_idtoken_service_account
  • samples/snippets/src/main/java/VerifyGoogleIdToken.java:17, tag auth_cloud_verify_google_idtoken

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Sita04
Sita04 marked this pull request as ready for review July 22, 2022 20:21
@Sita04
Merge branch 'main' into auth-samples
a46ccc4
@Sita04
Sita04 requested a review from a team July 25, 2022 17:30
@Sita04
Sita04 requested review from chingor13 and kurtisvg July 28, 2022 18:52
Sita04 added 2 commits July 29, 2022 00:43
@Sita04
minor comment update
1baeebe
@Sita04
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
9fc3a7a
piaxc
piaxc approved these changes Jul 28, 2022
View reviewed changes

@piaxc piaxc left a comment

Copy link
Copy Markdown
Contributor

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.

LGTM, thanks Sita!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
chingor13
chingor13 reviewed Jul 28, 2022
View reviewed changes
Comment thread samples/snippets/src/main/java/VerifyGoogleIdToken.java Outdated
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;

@chingor13 chingor13 Jul 28, 2022

Copy link
Copy Markdown
Contributor

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.

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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Sita04 Sita04 Jul 29, 2022

Copy link
Copy Markdown
Contributor Author

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.

Done. I've made the change you suggested and also removed the third party dependency.
PTAL.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread samples/snippets/src/main/java/VerifyNonGoogleIdToken.java Outdated
Comment on lines +62 to +98
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.");
}

@chingor13 chingor13 Jul 28, 2022

Copy link
Copy Markdown
Contributor

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.

I would recommend that we use our first-party implementation and not require extra third-party dependencies.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
kurtisvg
kurtisvg approved these changes Jul 29, 2022
View reviewed changes
Comment thread samples/snippets/src/main/java/AuthenticateExplicit.java
// see: https://developers.google.com/identity/protocols/oauth2/scopes

// Construct the Storage client.
Storage storage =

@kurtisvg kurtisvg Jul 29, 2022

Copy link
Copy Markdown

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.

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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Sita04 Sita04 Jul 29, 2022

Copy link
Copy Markdown
Contributor Author

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.

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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Sita04 and others added 8 commits July 30, 2022 00:01
@Sita04
Merge branch 'main' into auth-samples
b8c5f3f
@Sita04
modified google id token verification and removed third party dependency
680cfdd
@Sita04
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
1944e5d
@Sita04
removed third party deps from pom
a943290
@gcf-owl-bot
🦉 Updates from OwlBot post-processor
d7d6257
See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md
@gcf-owl-bot
Merge branch 'auth-samples' of https://github.com/googleapis/google-a…
cf11754
…uth-library-java into auth-samples
@gcf-owl-bot
🦉 Updates from OwlBot post-processor
b910be5
See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md
@gcf-owl-bot
Merge branch 'auth-samples' of https://github.com/googleapis/google-a…
e23fe35
…uth-library-java into auth-samples
@Sita04
Sita04 requested a review from chingor13 July 29, 2022 19:19
Sita04 added 2 commits July 30, 2022 02:50
@Sita04
Merge branch 'main' into auth-samples
ada91fb
@Sita04
Merge branch 'main' into auth-samples
8573bb3
chingor13
chingor13 approved these changes Aug 4, 2022
View reviewed changes

@chingor13 chingor13 left a comment

Copy link
Copy Markdown
Contributor

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.

2 non-blocking comments

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sita04 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread samples/snippets/src/main/java/VerifyGoogleIdToken.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread samples/snippets/src/main/java/VerifyGoogleIdToken.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Sita04 added 2 commits August 4, 2022 22:03
@Sita04
included comment about verifying Google ID tokens
591d0e6
@Sita04
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
82d7350
# Conflicts:
#	samples/snippets/src/main/java/VerifyGoogleIdToken.java
@Sita04

Sita04 commented Aug 4, 2022

Copy link
Copy Markdown
Contributor Author

2 non-blocking comments

Thanks Jeff! Included your comments in the sample for further clarification.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gcf-owl-bot
🦉 Updates from OwlBot post-processor
54013b5
See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md
@Sita04
Sita04 merged commit 32c717f into main Aug 4, 2022
@Sita04
Sita04 deleted the auth-samples branch August 4, 2022 16:47
@release-please release-please Bot mentioned this pull request Aug 4, 2022
chore(main): release 1.10.0 #962
Merged
gcf-merge-on-green Bot pushed a commit that referenced this pull request Aug 9, 2022
@release-please
chore(main): release 1.10.0 (#962)
79fffe2
🤖 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).
TimurSadykov pushed a commit that referenced this pull request Aug 10, 2022
@release-please Timur Sadykov
chore(main): release 1.10.0 (#962)
2be9af1
🤖 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).
@release-please release-please Bot mentioned this pull request Aug 11, 2022
chore(main): release 1.11.0 #974
Merged
chingor13 pushed a commit to googleapis/google-cloud-java that referenced this pull request Mar 12, 2026
@release-please
chore(main): release 1.10.0 (#962)
5d7fde7
🤖 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).
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@kolea2 kolea2 Awaiting requested review from kolea2
4 more reviewers
@chingor13 chingor13 chingor13 approved these changes
@piaxc piaxc piaxc approved these changes
@kurtisvg kurtisvg kurtisvg approved these changes
@Chizzy09 Chizzy09 Chizzy09 approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@kolea2 kolea2

Labels

samples Issues that are directly related to samples. size: l Pull request size is large.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@Sita04 @Chizzy09 @chingor13 @piaxc @kurtisvg @kolea2 @Shabirmean
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.