This page describes how to authenticate to an Identity-Aware Proxy (IAP)-secured resource from a user account or a service account.
- A user account belongs to an individual user. You authenticate a user account when your application requires access to IAP-secured resources on a user's behalf. Read about user account credentials.
- A service account belongs to an application instead of an individual user. You authenticate a service account when you want to allow an application to access your IAP-secured resources. Learn how to understand service accounts.
Before you begin
Before you begin, you'll need the following:
- A IAP-secured application to which you want to programmatically connect using a developer account, service account, or mobile app credentials.
Authenticating a user account
You can enable user access to your app from a desktop or mobile app to allow a program to interact with a IAP-secured resource.
Authenticating from a mobile app
- Create an OAuth 2.0 client ID for your mobile app in the same project as
the IAP-secured resource:
- Go to the Credentials
page.
Go to the Credentials page - Select the project with the IAP-secured resource.
- Click Create credentials, then select OAuth Client ID.
- Select the Application type for which you want to create credentials.
- Add a Name and Restrictions if appropriate, then click Create.
- Go to the Credentials
page.
- On the OAuth client window that appears, note the Client ID for the IAP-secured resource you want to connect to.
- Get an ID token for the IAP-secured client ID:
- Android: Use the
Google Sign-In API to request
an
OpenID Connect
(OIDC) token. Set the
requestIdToken
client ID to the client ID for the resource you're connecting to. - iOS: Use
Google Sign-In
to get an ID token. Set
serverClientID
to the client ID for the resource you're connecting to.
- Android: Use the
Google Sign-In API to request
an
OpenID Connect
(OIDC) token. Set the
- Include the ID token in an
Authorization: Bearer
header to make the authenticated request to the IAP-secured resource.
Authenticating from a desktop app
This section describes how to authenticate a user account from a desktop command line.
Setting up the client ID
To allow developers to access your application from the command line, you'll first need to create OAuth client ID credentials of type Desktop app:
- Go to the Credentials page.
Go to the Credentials page - Select the project with the IAP-secured resource.
- Click Create credentials, then select OAuth Client ID.
- Under Application type, select Desktop app, add a Name, then click Create.
- On the OAuth client window that appears, note the client ID and client secret. You'll need to use these in a script to manage credentials or otherwise share with your developers.
- On the Credentials window, your new Desktop app credentials appear along with the primary client ID that's used to access your application.
Signing in to the application
Each developer who wants to access a IAP-secured app will need to sign in first. You can package the process into a script, such as by using Cloud SDK. Following is an example using curl to sign in and generate a token that can be used to access the application:
- Sign in to your account that has access to the Google Cloud resource.
-
Go to the following URI where
DESKTOP_CLIENT_ID
is the Desktop app client ID you created above:https://accounts.google.com/o/oauth2/v2/auth?client_id=DESKTOP_CLIENT_ID&response_type=code&scope=openid%20email&access_type=offline&redirect_uri=urn:ietf:wg:oauth:2.0:oob
-
In the window that appears, note the Authorization code to
replace
AUTH_CODE
below along with the Desktop app client ID and secret you created above:curl --verbose \ --data client_id=DESKTOP_CLIENT_ID \ --data client_secret=DESKTOP_CLIENT_SECRET \ --data code=AUTH_CODE \ --data redirect_uri=urn:ietf:wg:oauth:2.0:oob \ --data grant_type=authorization_code \ https://oauth2.googleapis.com/token
This code returns a JSON object with a
refresh_token
field that you can save as a login token to access the application.
Accessing the application
To access the application, you'll exchange the
refresh_token
you generated during the sign-in flow for an ID token. The ID
token is valid for about one hour, during which time you can make multiple
requests to a specific app. Following is an example using curl to use the token
and access the application:
-
Use the code below where
REFRESH_TOKEN
is the token from the sign-in flow,IAP_CLIENT_ID
is the primary client ID used to access your application, andDESKTOP_CLIENT_ID
andDESKTOP_CLIENT_SECRET
are the client ID and secret you created when you set up the client ID above:curl --verbose \ --data client_id=DESKTOP_CLIENT_ID \ --data client_secret=DESKTOP_CLIENT_SECRET \ --data refresh_token=REFRESH_TOKEN \ --data grant_type=refresh_token \ --data audience=IAP_CLIENT_ID \ https://oauth2.googleapis.com/token
This code returns a JSON object with an
id_token
field that you can use to access the app. -
To access the app, use the
id_token
as follows:curl --verbose --header 'Authorization: Bearer ID_TOKEN' URL
Authenticating from a service account
Use an OpenID Connect (OIDC) token to authenticate a service account to a IAP-secured resource. Follow these steps to find your client ID:
- Go to the IAP page.
Find the resource you want to access, then click More > Edit OAuth Client.
On the Credentials page that appears, note the client ID.
You also need to add the service account to the access list
for the IAP-secured project. The following code samples show
how to obtain an OIDC token. No matter which one you choose, you need to include
the token in an Authorization: Bearer
header to make the authenticated request
to the IAP-secured resource.
Obtaining an OIDC token for the default service account
If you want to get an OIDC token for the default service account for Compute Engine, App Engine, or Cloud Run, you can use the following code sample to generate the token to access a IAP-secured resource:
C#
Go
Java
Node.js
PHP
Python
Ruby
Obtaining an OIDC token from a local service account key file
If you have a service account key file, you can adapt the preceding code samples to provide the service account key file.
Obtaining an OIDC token in all other cases
In all other cases, use the IAM credentials API to generate an OIDC token based on an access token for another service account right before accessing a IAP-secured resource:
- Add the account in the access token with the role
service account token creator
to the target account. This ensures it has the required IAM permission to create an OIDC token for the target service account. - Call generateIdToken
on the target service account with the access token.
Pay special attention to set the
audience
field to your client ID.
What's next
- Learn more about Authorization: Bearer Tokens.
- Try Sign-In for Android or Sign-In for iOS.