-
Notifications
You must be signed in to change notification settings - Fork 375
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
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
76ee309
feat: add methods for fetching and using id tokens
bshaffer 45aee9d
small fixes
bshaffer 348729c
Update idtokens.js
bshaffer 46f7cc8
Update computeclient.ts
bshaffer 92902b8
Update googleauth.ts
bshaffer 3469d4b
Merge branch 'feat-id-tokens-2' of github.com:bshaffer/google-auth-li…
bshaffer 0cea3da
adds README
bshaffer 5686d84
README updates
bshaffer 965adf6
chore: update sample
bcoe f6e0a5c
chore: update sample, adding test
bcoe 3797959
refactors IAP sample and uses clearer sample names
bshaffer fda860a
adds caching and updates samples
bshaffer 9238f48
adds checking of JWT exp for refreshing ID tokens and adds tests
bshaffer adbd0ba
fixes tests and lints
bshaffer 2decf63
readme update for idtoken samples
bshaffer c91b43e
more lint fixes
bshaffer 9eeb8c2
fixes lint one more time
bshaffer 7b478d2
adds more tests to increase coverage
bshaffer f294a6b
fixes lint once again
bshaffer 662328b
improves speed of tests
bshaffer 0fbe25c
adds sample metadata and addresses review comments
bshaffer 0e6ffc2
addresses sample changes
bshaffer 3bacfdd
test / lint improvements
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 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 was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2020 Google LLC | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
async function main( | ||
url = 'https://service-1234-uc.a.run.app' | ||
bshaffer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
// [START google_auth_idtoken_cloudrun] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const url = 'https://service-1234-uc.a.run.app'; | ||
|
||
const {GoogleAuth} = require('google-auth-library'); | ||
const auth = new GoogleAuth(); | ||
|
||
async function request() { | ||
console.info(`request Cloud Run ${url}`); | ||
const client = await auth.getIdTokenClient(url); | ||
const res = await client.request({url}); | ||
console.info(res.data); | ||
} | ||
|
||
request(); | ||
// [END google_auth_idtoken_cloudrun] | ||
} | ||
|
||
const args = process.argv.slice(2); | ||
main(...args).catch(console.error); | ||
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. I think the problem described in this issue may apply here: |
This file contains 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2020 Google LLC | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
bshaffer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async function main( | ||
url = 'https://some.iap.url', | ||
targetAudience = 'IAP_CLIENT_ID.apps.googleusercontent.com' | ||
) { | ||
// [START google_auth_idtoken_iap] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const url = 'https://some.iap.url'; | ||
// const targetAudience = 'IAP_CLIENT_ID.apps.googleusercontent.com'; | ||
|
||
const {GoogleAuth} = require('google-auth-library'); | ||
const auth = new GoogleAuth(); | ||
|
||
async function request() { | ||
console.info(`request IAP ${url} with target audience ${targetAudience}`); | ||
const client = await auth.getIdTokenClient(targetAudience); | ||
const res = await client.request({url}); | ||
console.info(res.data); | ||
} | ||
|
||
request(); | ||
// [END google_auth_idtoken_iap] | ||
} | ||
|
||
const args = process.argv.slice(2); | ||
main(...args).catch(console.error); |
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { | ||
GaxiosError, | ||
GaxiosOptions, | ||
GaxiosPromise, | ||
GaxiosResponse, | ||
} from 'gaxios'; | ||
|
||
import {BodyResponseCallback} from '../transporters'; | ||
|
||
import {AuthClient} from './authclient'; | ||
import {OAuth2Client} from './oauth2client'; | ||
|
||
export interface IdTokenOptions { | ||
/** | ||
* The client to make the request to fetch an ID token. | ||
*/ | ||
idTokenProvider: IdTokenProvider; | ||
/** | ||
* The audience to use when requesting an ID token. | ||
*/ | ||
targetAudience: string; | ||
} | ||
|
||
export interface IdTokenProvider { | ||
fetchIdToken: (targetAudience: string) => Promise<string>; | ||
} | ||
|
||
export class IdTokenClient extends AuthClient { | ||
bshaffer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
targetAudience: string; | ||
idTokenProvider: IdTokenProvider; | ||
|
||
/** | ||
* Google ID Token client | ||
* | ||
* Retrieve access token from the metadata server. | ||
* See: https://developers.google.com/compute/docs/authentication | ||
*/ | ||
constructor(options: IdTokenOptions) { | ||
super(); | ||
this.targetAudience = options.targetAudience; | ||
this.idTokenProvider = options.idTokenProvider; | ||
} | ||
|
||
/** | ||
* Provides a request implementation with OAuth 2.0 flow. If credentials have | ||
* a refresh_token, in cases of HTTP 401 and 403 responses, it automatically | ||
* asks for a new access token and replays the unsuccessful request. | ||
* @param opts Request options. | ||
* @param callback callback. | ||
* @return Request object | ||
*/ | ||
request<T>(opts: GaxiosOptions): GaxiosPromise<T>; | ||
request<T>(opts: GaxiosOptions, callback: BodyResponseCallback<T>): void; | ||
request<T>( | ||
opts: GaxiosOptions, | ||
callback?: BodyResponseCallback<T> | ||
): GaxiosPromise<T> | void { | ||
if (callback) { | ||
this.requestAsync<T>(opts).then( | ||
r => callback(null, r), | ||
e => { | ||
return callback(e, e.response); | ||
} | ||
); | ||
} else { | ||
return this.requestAsync<T>(opts); | ||
} | ||
} | ||
|
||
protected async requestAsync<T>( | ||
opts: GaxiosOptions, | ||
retry = false | ||
): Promise<GaxiosResponse<T>> { | ||
let r2: GaxiosResponse; | ||
|
||
const idToken = await this.idTokenProvider.fetchIdToken( | ||
this.targetAudience | ||
); | ||
|
||
opts.headers = opts.headers || {}; | ||
opts.headers.Authorization = `Bearer ${idToken}`; | ||
|
||
r2 = await this.transporter.request<T>(opts); | ||
|
||
return r2; | ||
} | ||
} |
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.
For new samples, I guess we're adding a metadata block to the top of each sample file, like this:
That might be a good addition here.
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.
Added! PTAL