What is the difference between access-token and identity-token when we use gcloud auth print-access-token
and gcloud auth print-identity-token
?
What are the cases where we can use one and not the other in Google Cloud context.
Why we have print-access-token
for gcloud auth
and gcloud auth application-default
while we have print-identity-token
only for gcloud auth
Definition and and in Google Context :
Access tokens (which aren't always JWTs) are used to inform an API that the bearer of the token has been authorized to access the API and perform a predetermined set of actions (specified by the scopes granted).
Access tokens must never be used for authentication. Access tokens cannot tell if the user has authenticated.
In Google Context, an OAuth 2.0 access token is useful for authenticating access from a service account to Google Cloud APIs.
ID tokens are used in token-based authentication. These tokens are signed JSON Web Tokens JWT used to assert identity and do not necessarily carry any implicit authorization against a resource. These tokens will just declare who the caller is and any service that the token is sent to can verify the token's integrity by verifying the signature payload provided with the JWT. The application receives an ID token after a user successfully authenticates, then consumes the ID token and extracts user information from it, which it can then use to personalize the user's experience.
Consider the following example use case for
To get elevated permissions on a project, a service administrator can impersonate a service account to call Google Cloud APIs by creating an OAuth 2.0 access token belonging to that service account. The token has a short lifetime so that the elevated permissions are temporary. Using short-lived tokens helps you implement the principle of least privilege across your identities and resources. It can also be useful when there is an emergency in a production environment, and a service administrator needs a short-term elevated authorization for debugging.
By creating an OIDC ID token belonging to a service account, a service running on Google Cloud can authenticate itself to another service deployed on a third-party cloud provider, such as a data pipeline job. If the target service is configured with OIDC, the authentication will succeed.
Coming on to the gcloud CLI commands :
- gcloud auth application-default print-access-token
- gcloud auth print-access-token
- gcloud auth print-identity-token
Print an identity token for the specified account. If not specified, the current active account will be used.
Both Google Service Accounts and Users can get id_tokens but with an important distinction: User login oauth flows issue id_tokens statically bound to the web or oauth2 client_id the flow as associated with. That is, if a user logs into a web application involving oauth2, the id_token that the provider issues to the browser will have the aud: field bound to the oauth2 client_id. Service Accounts on the other hand, can participate in a flow where it can receive an id_token from google with an aud: field specified earlier.
You can print identitytoken using gcloud command for service accounts, impersonated service accounts and compute engine instances.