Score:2

How do I Generate a Bearer Token for cURL to Get Thru IAP (GCP)?

us flag
sts

I need to cURL a web app hosted behind IAP on GCP.

Normally, users log in through IAP and use the web app, but I need to run some cURL commands (interactive and non-interactive) that hit the web app URLs (for example: https://myapp.com/get_pics/1)

I cannot figure out how to get a Bearer token from GCP that I can use in the authorization header for cURL.

I can set up a service account with "IAP Secured Web App User" role and I have the JSON key for this service account, but I am not sure where to go after that to get a proper Bearer token that IAP will accept.

Nadia Espinosa avatar
in flag
I think that this information about [genarate the authentication](https://cloud.google.com/iap/docs/programmatic-oauth-clients#service_accounts_and_gcloud) can be useful.
John Hanley avatar
cn flag
@NadiaEspinosa - Your link is for creating an Access Token. In this question, an Identity Token is required.
John Hanley avatar
cn flag
In your case, you need an OIDC Identity Token. Is the identity a user or service account? While developing use the CLI to generate this token: **gcloud auth print-identity-token** https://cloud.google.com/sdk/gcloud/reference/auth/print-identity-token Also specify the services that you are using. There is a parameter for **aud** (--audiences) which is the URL for the service. Tip: use Python instead of Curl. You will have a much easier time creating OAuth/OIDC tokens.
sts avatar
us flag
sts
The identity is a service account. The token is for an iOS client hitting a REST API behind IAP. Short lived tokens are a bummer since it's just testing against an IAP protected API. Python isn't much help. It's a use case GCP hasn't considered. The developer is third party so we're not giving gcloud to them ergo the service account idea.
Ismael Clemente Aguirre avatar
ye flag
@sts Since this case has been open for 3 months now, I would like to know if you found a way to solve your issue.
sts avatar
us flag
sts
Nope. We punted.
Score:1
ye flag

I cannot figure out how to get a Bearer token from GCP that I can use in the authorization header for cURL.

According with the Google Public Documentation

If your application occupies the Authorization request header, you can include the ID token in a Proxy-Authorization: Bearer header instead. If a valid ID token is found in a Proxy-Authorization header, IAP authorizes the request with it. After authorizing the request, IAP passes the Authorization header to your application without processing the content.

If no valid ID token is found in the Proxy-Authorization header, IAP continues to process the Authorization header and strips the Proxy-Authorization header before passing the request to your application.

Bearer Token OIDC

This document also includes code samples to:

And in this link you can find more information about Verify Bearer Token in GCP

Bearer Token Auth

After some research I found two pages that describe How to provide your service account authentication as a Bearer token and How to send Curl POST request with Bearer Token Authorization Header.

The first one is part of the Google Developers public documentation, and describes the process to obtain a Bearer token with your service account.

  1. Install the gcloud command line tool.

  2. Authenticate to your service account. In the following command, replace ${KEY_FILE} with the path to your service account key file:

    gcloud auth activate-service-account
    --key-file ${KEY_FILE}
    
  3. Use your service account to obtain an authorization token:

    gcloud auth print-access-token 
    

    The command returns an access token value.

When you use the API, pass the token value as a Bearer token in an Authorization header. See the following example:

    curl -X GET -H "X-Goog-User-Project: ${CLIENT_PROJECT}" \  
    -H "Content-Type: application/json" \  
    -H "Authorization: Bearer ${TOKEN}" \ 
"https://sasportal.googleapis.com/v1alpha1/customers" 

Set ${CLIENT_PROJECT} to the ID of the Google Cloud Project from which you make the requests, and then set ${TOKEN} to the authorization token.

And in this link you will find information and examples about Curl Request With Bearer Token Authorization Header

John Hanley avatar
cn flag
There are two types of tokens: OAuth Access Token and OIDC Identity Tokens. The question is about IAP (Identity Aware Proxy), your answer is for a different service. IAP requires an OIDC Identity Token.
Ismael Clemente Aguirre avatar
ye flag
Thanks for your comment, i already edited my answer.
Score:1
us flag

For IAP you need to provide a OIDC identity token. You can use gcloud to create it for you, or use any other mean as described in Programmatic authentication.

I assume you have gcloud, so I'll show you how to use that:

  1. Login using the service account
gcloud auth activate-service-account --key-file service-account-key.json
  1. Send your request
curl -H "Authorization: Bearer $(gcloud auth print-identity-token --audiences="xxx.apps.googleusercontent.com")" https://myapp.com/get_pics/1

Make sure that you granted the service account the iap.httpsResourceAccessor-permission, or you'll still be denied.

  • Bonus: Don't use service account key files. Impersonate a service account from your current user profile.
curl -H "Authorization: Bearer $(gcloud auth print-identity-token --include-email --audiences="xxx.apps.googleusercontent.com" --impersonate-service-account=service_account@project.iam.gserviceaccount.com)" https://myapp.com/get_pics/1
sts avatar
us flag
sts
Belated thanks. Just got back to this. Yes, we use gcloud. We'll give this a try...
sts avatar
us flag
sts
Follow up. This worked great. Note that if you want to hit Firebase with curl, the service account needs Role: Cloud Datastore User. Google doesn't document this. Thanks again!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.