Hi,
I am trying to connect my Vercel app to another service running on Google Cloud Run. I have followed the guide here, but I am having trouble getting it to work.
I think I need to pass a token ID to my rest API request, which would identify the service to the backend (see here).
However, how do I do this in practice? The following works locally:
const auth = new GoogleAuth({
scopes: "https://www.googleapis.com/auth/cloud-platform",
// Pass the project ID explicitly to avoid the need to grant `roles/browser` to the service account
// or enable Cloud Resource Manager API on the project.
projectId: GCP_PROJECT_ID,
});
const backend_url = "...";
const client = await auth.getIdTokenClient(backend_url);
const response = await client.request({ url: backend_url });
However, this is relying on the fact that I have run (locally) the command gcloud auth login
, so that the credentials are present in my machine and the call to GoogleAuth
goes through.
How to do the same on a vercel function?
The official docs suggest doing something like
const auth = ExternalAccountClient.fromJSON({
type: "external_account",
audience: `//iam.googleapis.com/projects/${GCP_PROJECT_NUMBER}/locations/global/workloadIdentityPools/${GCP_WORKLOAD_IDENTITY_POOL_ID}/providers/${GCP_WORKLOAD_IDENTITY_POOL_PROVIDER_ID}`,
subject_token_type: "urn:ietf:params:oauth:token-type:jwt",
token_url: "https://sts.googleapis.com/v1/token",
service_account_impersonation_url: `https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/${GCP_SERVICE_ACCOUNT_EMAIL}:generateAccessToken`,
subject_token_supplier: {
// Use the Vercel OIDC token as the subject token.
getSubjectToken: getVercelOidcToken,
},
});
But the resulting auth
object does not have a getIdTokenClient
method, so it’s unclear how to make progress now.
I have also tried calling auth.getServiceAccountEmail()
to see if it would show the vercel@${PROJECT_ID}.iam.gserviceaccount.com
which I expect, but it just returns null
instead.
I have been trying to do this for multiple hours now. Can someone help shed some light on this, or point to an working example?
Thank you!