Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Generate an ID token using Google service acc credentials & set target audience(for IAP)Typescript

I am trying to implement a service in TypeScript that generates an ID token using a Google service account to authenticate requests to a Google Cloud service, such as an Identity-Aware Proxy (IAP)-protected resource. In Java, I use GoogleCredentials.fromStream() to load the service account credentials from a JSON file and create an IdTokenCredentials object to generate the ID token.

Here's what I want to achieve in TypeScript:

  1. Load the service account credentials from a JSON file.
  2. Use those credentials to create an ID token with a specific target audience (IAP client ID).
  3. Refresh and retrieve the ID token.
  4. Use this token to authenticate requests to a Google Cloud resource.

What is the equivalent way to implement this in TypeScript, using google-auth-library or any other recommended library? A TypeScript code sample would be helpful.

Key Java concepts I want to translate into TypeScript:

  • GoogleCredentials.fromStream(InputStream credentialsStream) for loading service account credentials.
  • IdTokenCredentials.newBuilder().setIdTokenProvider(idTokenProvider).setTargetAudience(iapClientId) for generating the ID token.
  • Token refresh and retrieval logic.

I have already referred to this link  https://github.com/googleapis/google-auth-library-nodejs/tree/main?tab=readme-ov-file#oauth2

0 0 131
0 REPLIES 0
Top Labels in this Space
Top Solution Authors