Skip to content

Commit

Permalink
docs: Improve ID Token Documentation (#1651)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-goog authored Oct 2, 2023
1 parent 0bcacaf commit 992397b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
11 changes: 6 additions & 5 deletions samples/idTokenFromMetadataServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@
* Uses the Google Cloud metadata server environment to create an identity token
* and add it to the HTTP request as part of an Authorization header.
*
* @param {string} url - The url or target audience to obtain the ID token for.
* @param {string} targetAudience - The url or target audience to obtain the ID token for.
*/
function main(url) {
function main(targetAudience) {
// [START auth_cloud_idtoken_metadata_server]
/**
* TODO(developer):
* 1. Uncomment and replace these variables before running the sample.
*/
// const url = 'http://www.example.com';
// const targetAudience = 'http://www.example.com';

const {GoogleAuth} = require('google-auth-library');

async function getIdTokenFromMetadataServer() {
const googleAuth = new GoogleAuth();
const client = await googleAuth.getClient();

const client = await googleAuth.getIdTokenClient(targetAudience);

// Get the ID token.
// Once you've obtained the ID token, you can use it to make an authenticated call
// to the target audience.
await client.fetchIdToken(url);
await client.idTokenProvider.fetchIdToken(targetAudience);
console.log('Generated ID token.');
}

Expand Down
3 changes: 3 additions & 0 deletions samples/idtokens-serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ function main(
async function request() {
console.info(`request ${url} with target audience ${targetAudience}`);
const client = await auth.getIdTokenClient(targetAudience);

// Alternatively, one can use `client.idTokenProvider.fetchIdToken`
// to return the ID Token.
const res = await client.request({url});
console.info(res.data);
}
Expand Down

0 comments on commit 992397b

Please sign in to comment.