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

feat: add methods for fetching and using id tokens (second implementation) #867

Merged
merged 23 commits into from Jan 14, 2020

Conversation

bshaffer
Copy link
Contributor

@bshaffer bshaffer commented Jan 8, 2020

This implementation is similar to #859, but uses an IdTokenClient wrapper class. Additionally, the interface IdTokenProvider was added to allow relevant clients to implement the fetchIdToken method.

Some thoughts:

  • The fetchIdToken method can be bypassed all together if the user just populates additionalClaims in the JWT class with the target audience. Do we care?
  • The method as we have it now bypasses all caching. I did this for simplicity, but can add this presumably by extending OAuth2Client in IdTokenClient and using refreshTokenNoCache to call fetchIdToken
  • There is no way to configure the Compute client directly to use ID Tokens (as you can with JWT using additionalClaims). This can be changed by adding a targetAudience option to the constructor, if it's desired.
  • No tests exist for IdTokenClient yet! I wanted to clear the implementation first before writing them.

Thanks!

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jan 8, 2020
src/auth/jwtclient.ts Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Jan 9, 2020

Codecov Report

Merging #867 into master will increase coverage by 0.1%.
The diff coverage is 93.83%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #867     +/-   ##
=========================================
+ Coverage   90.76%   90.86%   +0.1%     
=========================================
  Files          18       19      +1     
  Lines        4005     4150    +145     
  Branches      427      443     +16     
=========================================
+ Hits         3635     3771    +136     
- Misses        364      372      +8     
- Partials        6        7      +1
Impacted Files Coverage Δ
src/index.ts 100% <100%> (ø) ⬆️
src/auth/googleauth.ts 91.62% <87.5%> (-0.09%) ⬇️
src/auth/computeclient.ts 97.26% <91.3%> (-1.12%) ⬇️
src/auth/jwtclient.ts 94.03% <92%> (-0.17%) ⬇️
src/auth/idtokenclient.ts 96.29% <96.29%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 30f0237...662328b. Read the comment docs.

Copy link
Contributor

@bcoe bcoe left a comment

Choose a reason for hiding this comment

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

this is looking super solid ... as expected I think the refresh logic is going to add a few additional annoyances.

samples/idtokens-cloudrun.js Outdated Show resolved Hide resolved
src/auth/jwtclient.ts Outdated Show resolved Hide resolved
src/auth/idtokenclient.ts Outdated Show resolved Hide resolved
const url =
process.env.IAP_URL || 'https://nodejs-docs-samples-iap.appspot.com';
const targetAudience =
process.env.IAP_CLIENT_ID || '170454875485-fbn7jalc9214bb67lslv1pbvmnijrb20.apps.googleusercontent.com';
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we could get away with dropping this test, alternatively we either need to create a similar IAP resource in our CI/CD project, or add our CI/CD user to whatever project has that IAP resource.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like using CI/CD project would be better, but I don't have access to that so LMK what you'd like to do. I can remove it no problem.

src/auth/idtokenclient.ts Show resolved Hide resolved
test/test.jwt.ts Show resolved Hide resolved
@bshaffer
Copy link
Contributor Author

Header lint checker is failing because of a bug. See googleapis/repo-automation-bots#223

Copy link
Contributor

@feywind feywind left a comment

Choose a reason for hiding this comment

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

Cool, this looks overall pretty good to me. I am not as familiar with the subject matter, but it looks like Ben covered that. @bcoe back to you for final approvals?

// limitations under the License.

'use strict';

Copy link
Contributor

Choose a reason for hiding this comment

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

For new samples, I guess we're adding a metadata block to the top of each sample file, like this:

// sample-metadata:
//   title: Create Topic
//   description: Creates a new topic.
//   usage: node createTopic.js <topic-name>

That might be a good addition here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added! PTAL

}

const args = process.argv.slice(2);
main(...args).catch(console.error);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the problem described in this issue may apply here:

googleapis/nodejs-asset#242

samples/idtokens-iap.js Show resolved Hide resolved
src/auth/idtokenclient.ts Outdated Show resolved Hide resolved
@@ -473,7 +476,10 @@
"path": "samples/headers.js"
},
{
"path": "samples/iap.js"
"path": "samples/idtokens-cloudrun.js"
Copy link
Contributor

Choose a reason for hiding this comment

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

this is auto generated, sorry that you ended up editing this by hand 😆

Copy link
Contributor

@bcoe bcoe left a comment

Choose a reason for hiding this comment

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

This is looking good to me once you address @feywind's feedback; it looks like there's an upstream service causing the docs tests to pass, if you don't mind update linkinator.json to:

{
  "recurse": true,
  "skip": [
    "https://codecov.io/gh/googleapis/",
    "www.googleapis.com",
    "https://david-dm.org.*"
  ]
}

Which should get things passing for you.

const jwt = new JWT({
email: 'foo@serviceaccount.com',
key: PEM_CONTENTS,
subject: 'ignored@subjectaccount.com',
Copy link
Contributor

Choose a reason for hiding this comment

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

thanks for taking the time to add this test 👍

@codecov-io
Copy link

codecov-io commented Jan 14, 2020

Codecov Report

Merging #867 into master will increase coverage by 0.13%.
The diff coverage is 94.55%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #867      +/-   ##
==========================================
+ Coverage   90.76%   90.89%   +0.13%     
==========================================
  Files          18       19       +1     
  Lines        4005     4151     +146     
  Branches      427      445      +18     
==========================================
+ Hits         3635     3773     +138     
- Misses        364      372       +8     
  Partials        6        6
Impacted Files Coverage Δ
src/index.ts 100% <100%> (ø) ⬆️
src/auth/googleauth.ts 91.62% <87.5%> (-0.09%) ⬇️
src/auth/computeclient.ts 97.26% <91.3%> (-1.12%) ⬇️
src/auth/jwtclient.ts 94.03% <92%> (-0.17%) ⬇️
src/auth/idtokenclient.ts 97.56% <97.56%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 30f0237...3bacfdd. Read the comment docs.

@bcoe bcoe merged commit 8036f1a into googleapis:master Jan 14, 2020
@bshaffer bshaffer deleted the feat-id-tokens-2 branch January 14, 2020 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants