Skip to content
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

docs(samples): added auth samples and tests #927

Merged
merged 30 commits into from Aug 4, 2022
Merged

docs(samples): added auth samples and tests #927

merged 30 commits into from Aug 4, 2022

Conversation

Sita04
Copy link
Contributor

@Sita04 Sita04 commented Jun 8, 2022

Adding samples for the authentication docs.

@Sita04 Sita04 requested review from a team as code owners 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
@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
@Chizzy09
Copy link

45449b2

Copy link
Contributor

@piaxc piaxc left a 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!

@Sita04 Sita04 marked this pull request as draft July 18, 2022 04:33
# 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
Copy link
Contributor

@piaxc piaxc left a comment

Choose a reason for hiding this comment

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

Thanks Sita!

@snippet-bot
Copy link

snippet-bot bot commented Jul 22, 2022

Here is the summary of changes.

You are about to add 5 region tags.

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

@Sita04 Sita04 marked this pull request as ready for review July 22, 2022 20:21
@Sita04 Sita04 requested a review from a team as a code owner July 25, 2022 17:30
Copy link
Contributor

@piaxc piaxc left a comment

Choose a reason for hiding this comment

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

LGTM, thanks Sita!

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;
Copy link
Contributor

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)

Copy link
Contributor Author

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.

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.");
}
Copy link
Contributor

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.

// see: https://developers.google.com/identity/protocols/oauth2/scopes

// Construct the Storage client.
Storage storage =

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

Copy link
Contributor Author

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

@Sita04 Sita04 requested a review from chingor13 July 29, 2022 19:19
Copy link
Contributor

@chingor13 chingor13 left a 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

@Sita04
Copy link
Contributor Author

Sita04 commented Aug 4, 2022

2 non-blocking comments

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

@Sita04 Sita04 merged commit 32c717f into main Aug 4, 2022
@Sita04 Sita04 deleted the auth-samples branch August 4, 2022 16:47
gcf-merge-on-green bot pushed a commit that referenced this pull request Aug 9, 2022
🤖 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
🤖 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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
samples Issues that are directly related to samples. size: l Pull request size is large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants