Following the authentication guide for Cloud Run here, I wanted to make a service only accessible by authenticated users.
I created a base service, called helloworld (yes, no originality here), and applied the following policy:
gcloud run services add-iam-policy-binding helloworld \
--member="allAuthenticatedUsers" \
--role="roles/run.invoker"
I then tried to invoke it using the gcloud auth print-identity-token
ID Token like this:
curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" https://helloworld-*****.a.run.app
All good, the service responds with bearer tokens coming from a @gmail.com account and a GSuite one using gcloud
.
As a next step, I followed the end-user authentication guide here and:
- created web application credentials in the same project as the Cloud Run service above
- redeployed the service to absorb the new Client ID configuration
- created a simple HTML page with that Client ID and Google Sign-In logic (like this one)
- run a local webserver to serve that page and got a new ID Token
- attempted to do the same
curl
with that very token
This time though, the response was a 401 with the following value in the WWW-Authenticate
header:
Bearer error="invalid_token" error_description="The access token could not be verified"
This is the response of the https://oauth2.googleapis.com/tokeninfo API:
{
"iss": "accounts.google.com",
"azp": "*****************.apps.googleusercontent.com",
"aud": "*****************.apps.googleusercontent.com",
"sub": "*******",
"email": "[email protected]",
"email_verified": "true",
"at_hash": "********",
"name": "********",
"picture": "********",
"given_name": "********",
"family_name": "********",
"locale": "en",
"iat": "1581617530",
"exp": "1581621130",
"jti": "6a0dd7561c0d95f7c92fc29a885039fe3f23db16",
"alg": "RS256",
"kid": "d8efea1f66e87bb36c2ea09d837338bdd810353b",
"typ": "JWT"
}
and the azp
and aud
values are the same Client ID that is defined as a Web Application credential in the project.
I also tried to add the specific user to the service like this:
gcloud run services add-iam-policy-binding helloworld \
--member="user:[email protected]" \
--role="roles/run.invoker"
Still no luck... What am I doing wrong?
363397985288-7a7e83ssimq8t0tvolqaha161u9tdckh.apps.googleusercontent.com
https://helloworld-r4v55jcbxa-ew.a.run.app
. I also created another project, client ID and service just in case...788273985948-min8mv0jk4sldbaha096avgv034jr32o.apps.googleusercontent.com
https://helloworld-2pr4qhpiyq-ew.a.run.app
– Gherynos