Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add methods for fetching and using id tokens (#867)
- Loading branch information
Showing
17 changed files
with
554 additions
and
49 deletions.
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 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 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,55 @@ | ||
// 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. | ||
|
||
// sample-metadata: | ||
// title: ID Tokens for Cloud Run | ||
// description: Requests a Cloud Run URL with an ID Token. | ||
// usage: node idtokens-cloudrun.js <url> [<target-audience>] | ||
|
||
'use strict'; | ||
|
||
function main( | ||
url = 'https://service-1234-uc.a.run.app', | ||
targetAudience = null | ||
) { | ||
// [START google_auth_idtoken_cloudrun] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const url = 'https://YOUR_CLOUD_RUN_URL.run.app'; | ||
const {GoogleAuth} = require('google-auth-library'); | ||
const auth = new GoogleAuth(); | ||
|
||
async function request() { | ||
if (!targetAudience) { | ||
// Use the request URL hostname as the target audience for Cloud Run requests | ||
const {URL} = require('url'); | ||
targetAudience = new URL(url).origin; | ||
} | ||
console.info( | ||
`request Cloud Run ${url} with target audience ${targetAudience}` | ||
); | ||
const client = await auth.getIdTokenClient(targetAudience); | ||
const res = await client.request({url}); | ||
console.info(res.data); | ||
} | ||
|
||
request().catch(err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
// [END google_auth_idtoken_cloudrun] | ||
} | ||
|
||
const args = process.argv.slice(2); | ||
main(...args); |
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,50 @@ | ||
// 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. | ||
|
||
// sample-metadata: | ||
// title: ID Tokens for Identity-Aware Proxy (IAP) | ||
// description: Requests an IAP-protected resource with an ID Token. | ||
// usage: node idtokens-iap.js <url> <target-audience> | ||
|
||
'use strict'; | ||
|
||
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().catch(err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
// [END google_auth_idtoken_iap] | ||
} | ||
|
||
const args = process.argv.slice(2); | ||
main(...args); |
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
Oops, something went wrong.