This document describes when and how to use custom OAuth clients for Identity-Aware Proxy (IAP).
By default, IAP uses Google-managed OAuth clients to authenticate users. Google-managed OAuth clients can only be used to manage access for internal users that are within an organization.
You must use a custom OAuth configuration to do the following:
Manage access to IAP-enabled applications for external users that are outside of your organization.
Manage access to web applications that are in projects that aren't in a Google Cloud organization.
Display custom brand information in consent screens during authentication.
You can configure custom OAuth clients either on IAP or directly on your platform.
When you use custom OAuth clients, you must configure the OAuth consent screen. For custom branding to appear in the consent screen, you must submit your application for Google verification. For more information about the verification process, see Setting up your OAuth consent screen.
When you configure custom OAuth clients, you are responsible for creating and managing the credentials, which includes securely storing the client secret and sharing it with authorized clients when necessary.
Compare Google-managed OAuth clients and custom OAuth clients
Google-managed OAuth clients cannot programmatically access
IAP-protected applications. However, IAP-protected
applications that use the Google-managed OAuth client can still be accessed
programmatically using a separate OAuth client configured through the
programmatic_clients
setting or a service account JWT.
The following table provides a comparison between the Google-managed OAuth client and a custom OAuth client.
| Google managed OAuth client | Custom OAuth client | |
|---|---|---|
| Users | Internal only | Internal and External |
| Brand | Google Cloud brand | Customer owned brand |
| OAuth configuration | Google configured | Customer configured |
| OAuth credentials | Google managed | Customer managed |
| Application access | Browser flow only | Browser flow and Programmatic access |
Configure the branding page
To configure the custom branding page using the Google Cloud console, do the following:
In the Google Cloud console, go to the OAuth Branding page:
Click Get started.
In App name, enter the name of the application for the consent screen.
In User support email, enter the administrator email address for support.
For Audience, select Internal to restrict access to users within your organization or External to allow access to users that are outside of your organization.
In Contact information, enter the administrator email to contact for the apps that are protected by the OAuth clients. You configure the OAuth clients in a later step.
To create the OAuth configuration, click Create.
Configure custom OAuth clients in IAP
This section describes how to set up custom OAuth clients in IAP.
You can create a custom OAuth client for IAP using the Google Cloud console in the following ways:
Create an OAuth client manually: You must add the redirect URI yourself before configuring IAP.
Create an OAuth client automatically by using IAP auto-generate credentials: IAP creates and configures the OAuth client for you and automatically adds the required redirect URI.
Regardless of which method you use, ensure that you have configured the branding page before creating your OAuth client. The required redirect URI format is shown in both methods.
Create the custom OAuth client
This section describes how to create custom OAuth clients by using the Google Cloud console. You can set up custom IAP OAuth clients at any level in the resource hierarchy.
To create custom OAuth clients for a resource using the Google Cloud console, use one of the following methods:
Method 1: Create an OAuth client using the Google Cloud console
Use this method when you want to create the OAuth client separately, perhaps before configuring IAP for your resources. This requires manually adding the IAP redirect URI.
In the Google Cloud console, go to APIs & Services > Credentials.
Click Create Credentials, and then select OAuth client ID.
For Application type, select Web application.
In the Name field, enter a descriptive name for your OAuth client.
Click Create. Note the generated client ID.
After the OAuth client is created, go to the Authorized redirect URIs section of the OAuth client details page for your newly created OAuth client.
Click Add URI, and enter the following IAP-specific Authorized redirect URI:
https://iap.googleapis.com/v1/oauth/clientIds/YOUR_CLIENT_ID:handleRedirect
Replace
YOUR_CLIENT_IDwith the client ID you obtained in the previous step.Click Save.
Method 2: Create an OAuth client by using IAP auto-generate credentials
Use this method to have IAP create and configure the OAuth client for you and automatically add the required redirect URI.
In the Google Cloud console, go to the IAP page.
In the Applications tab, in the list of resources, find the resource that you want to configure.
For project-level resources, do the following:
Create the OAuth client using the Google Cloud console:
In the Actions column, click More options > Settings.
In the Settings dialog, select Custom OAuth.
If you haven't configured a consent screen, do the following:
Click Configure consent screen.
Follow the instructions to configure the branding page that are provided earlier in this document.
In the IAP settings dialog, click Auto Generate Credentials. IAP generates a new OAuth client and secret to use with this resource. In Google Auth Platform, the Authorized redirect URIs field contains an entry in the following format:
https://iap.googleapis.com/v1/oauth/clientIds/CLIENT_ID:handleRedirect
To access the client ID and secret, click Download credentials. The credentials are saved in a JSON-formatted file. Because the file contains sensitive credentials for accessing resources, make sure that the file is secured or deleted.
To save the IAP OAuth configuration and apply the OAuth client to IAP, click Save.
Apply custom OAuth clients to IAP
This section describes how to apply OAuth clients to IAP. You can use this method instead of applying the clients directly on your platform.
To create the custom OAuth client, follow the Google Cloud console instructions provided earlier in this document.
Apply the custom OAuth client.
gcloud
To apply the custom OAuth client using the gcloud CLI, do the following:
Create a settings YAML file.
cat << EOF > iap-oauth.yaml accessSettings: oauthSettings: clientId: CLIENT_ID clientSecret: CLIENT_SECRET EOF
Replace the following:
CLIENT_ID: The client ID from the OAuth credentials that you generated earlier.CLIENT_SECRET: The client secret from the OAuth credentials that you generated earlier.
To set the OAuth configuration, do one of the following:
- To set the OAuth configuration at the project level, run the following command:
gcloud iap settings set iap-oauth.yaml
To set the configuration at another level of the resource hierarchy, use one of the following flags instead of the
--projectflag. Setting custom OAuth clients at a level of the resource hierarchy provides the same custom branding to all of the services that run within that level.--folder=FOLDER_ID--organization=ORGANIZATION_IDTo set the configuration on a specific service, run the following command:
gcloud iap settings set iap-oauth.yaml \ --project=PROJECT_ID \ --resource-type= RESOURCE_TYPE \ --region=REGION \ --service=SERVICE_NAME
Replace the following:
PROJECT_ID: the ID of the project resource. To set the configuration at another level, use one of the following flags instead of the--projectflag:--folder=FOLDER_ID--organization=ORGANIZATION_ID
RESOURCE_TYPE: replace with one of the following resource types, depending on the resource:app-enginebackend-servicescloud-runcomputefolderforwarding-ruleiap_weborganization
REGION: the region where you run your Cloud Run service.SERVICE_NAME: The name of your service.
Terraform
To apply the custom OAuth client using Terraform, do the following:
resource "google_iap_settings" "iap_settings" { name = IAP_RESOURCE_NAME access_settings { oauth_settings { oauth_client_id = CLIENT_ID oauth_client_secret = CLIENT_SECRET } } }Replace the following:
IAP_RESOURCE_NAME: the resource name of theiap_settingsresource for the service, which has the following format:projects/PROJECT_NUMBER/iap_web/REGION/services/SERVICE_NAMECLIENT_ID: the client ID from the OAuth credentials that you generated earlierCLIENT_SECRET: the client secret from the OAuth credentials that you generated earlier
REST API
Create a settings JSON file.
cat << EOF > iap-oauth.json { "accessSettings": { "oauthSettings": { "clientId": "CLIENT_ID", "clientSecret": "CLIENT_SECRET" } } } EOF
Replace the following:
CLIENT_ID: The client ID from the OAuth credentials that you generated earlier.CLIENT_SECRET: The client secret from the OAuth credentials that you generated earlier.
Apply the settings file.
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/yaml" \ "https://iap.googleapis.com/v1/projects/cb-managed-ingress-demo/iap_web/forwarding_rule-us-central1/services/psc-fr:iapSettings?update_mask=iapSettings.accessSettings.oauthSettings.oauthClientId,iapSettings.accessSettings.oauthSettings.oauthClientSecret" \ -d @iap-oauth.json
To test that your web applications are protected by IAP with the OAuth clients, see Test access.
Legacy configuration of custom OAuth clients on a resource
The following sections describe legacy methods to configure custom OAuth clients in IAP for certain resource types. You can skip this section if you used the method described earlier in this document.
App Engine
This section describes how to enable custom OAuth clients on App Engine.
gcloud
Before you set up your project and IAP, you need an up-to-date version of gcloud CLI. For instructions on how to install the gcloud CLI, see Install the gcloud CLI.
-
To authenticate, use the Google Cloud CLI and run the following command.
gcloud auth login - To sign in, follow the URL that appears.
- After you sign in, copy the verification code that appears and paste it in the command line.
-
Run the following command to specify the project that contains the resource that you want to protect with IAP.
gcloud config set project PROJECT_ID - Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.
- Save the OAuth client ID and secret.
-
To enable IAP, run the following command.
gcloud iap web enable \ --oauth2-client-id=CLIENT_ID \ --oauth2-client-secret=CLIENT_SECRET \ --resource-type=app-engine
After you enable IAP, you can use the gcloud CLI to modify the
IAP access policy using the IAM role
roles/iap.httpsResourceAccessor. Learn more about
managing roles and permissions.
API
按照为 IAP 创建 OAuth 客户端中的说明配置 OAuth 权限请求页面和创建 OAuth 客户端。
保存 OAuth 客户端 ID 和 Secret。
运行以下命令以准备
settings.json文件。cat << EOF > settings.json { "iap": { "enabled": true, "oauth2ClientId": "CLIENT_ID", "oauth2ClientSecret":" CLIENT_SECRET" } } EOF运行以下命令以启用 IAP。
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ -H "Accept: application/json" \ -H "Content-Type: application/json" \ -d @settings.json \ "https://appengine.googleapis.com/v1/apps/PROJECT_ID?updateMask=iap"
启用 IAP 后,您可以使用 Google Cloud CLI 通过 IAM 角色 roles/iap.httpsResourceAccessor 修改 IAP 访问权限政策。详细了解如何管理角色和权限。
GKE
This section describes how to enable custom OAuth clients on GKE.
配置 BackendConfig
如果您运行的是 GKE 集群 1.24 版或更高版本,则可以使用 Kubernetes Gateway API 配置 IAP 和 GKE。如需查看相关说明,请参阅配置 IAP。
按照为 IAP 创建 OAuth 客户端中的说明配置 OAuth 权限请求页面和创建 OAuth 客户端。
创建一个 Kubernetes Secret 来封装 OAuth 客户端。
请替换以下内容:kubectl create secret generic MY_SECRET --from-literal=client_id=CLIENT_ID \ --from-literal=client_secret=CLIENT_SECRET
MY_SECRET:要创建的 Secret 的名称CLIENT_ID:OAuth 客户端 IDCLIENT_SECRET:OAuth 客户端密钥
您应该会收到类似以下输出的确认信息,表明 Secret 已成功创建:
secret "MY_SECRET" created
将 OAuth 凭证添加到 BackendConfig。
apiVersion: cloud.google.com/v1 kind: BackendConfig metadata: name: CONFIG_DEFAULT namespace: my-namespace spec: iap: enabled: true oauthclientCredentials: secretName: MY_SECRET通过将 Service 端口与 BackendConfig 相关联来启用 IAP。请参阅将 BackendConfig 与 Ingress 关联。建立这种关联的一种方法是将服务的所有端口默认设置为 BackendConfig,为此,您可以将以下注解添加到 Service 资源中:
metadata: annotations: cloud.google.com/backend-config: '{"default": "CONFIG_DEFAULT"}'
启用 IAP 后,您可以使用 gcloud CLI 通过 IAM 角色 roles/iap.httpsResourceAccessor 修改 IAP 访问权限政策。详细了解如何管理角色和权限。
问题排查
如果您引用的 secretName 不存在或结构不正确,系统会显示以下出错提示之一:
BackendConfig default/config-default is not valid: error retrieving secret "foo": secrets "foo" not found.如需解决此错误,请确保已按照第 2 步中的说明正确创建了 Kubernetes Secret。BackendConfig default/config-default is not valid: secret "foo" missing client_secret data.如需解决此错误,请确保您正确创建了 OAuth 凭据。此外,请确保您引用了正确的client_id和client_secret密钥。
Load balancer backend service
For users of Compute Engine and Cloud Run, this section describes how to set up OAuth clients in IAP for the load balancer backend service.
gcloud
在设置项目和 IAP 之前,您需要最新版本的 gcloud CLI。如需了解如何安装 gcloud CLI,请参阅安装 gcloud CLI。
-
如需进行身份验证,请使用 Google Cloud CLI 并运行以下命令。
gcloud auth login - 如需登录,请访问显示的网址。
- 登录后,复制显示的验证码并将其粘贴到命令行中。
-
运行以下命令,指定包含要使用 IAP 保护的资源的项目。
gcloud config set project PROJECT_ID
- 按照为 IAP 创建 OAuth 客户端中的说明配置 OAuth 权限请求页面和创建 OAuth 客户端。
- 保存 OAuth 客户端 ID 和密钥。
-
如需启用 IAP,请运行全局范围或区域范围的命令。
全局范围 区域范围gcloud compute backend-services update BACKEND_SERVICE_NAME \ --global \ --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRETgcloud compute backend-services update BACKEND_SERVICE_NAME \ --region REGION_NAME \ --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRET
启用 IAP 后,您可以使用 gcloud CLI 通过 IAM 角色 roles/iap.httpsResourceAccessor 修改 IAP 访问权限政策。详细了解如何管理角色和权限。
API
按照为 IAP 创建 OAuth 客户端中的说明配置 OAuth 权限请求页面和创建 OAuth 客户端。
保存 OAuth 客户端 ID 和 Secret。
运行以下命令以准备
settings.json文件。cat << EOF > settings.json { "iap": { "enabled": true, "oauth2ClientId": "CLIENT_ID", "oauth2ClientSecret": "CLIENT_SECRET" } } EOF运行以下命令以启用 IAP。
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ -H "Accept: application/json" \ -H "Content-Type: application/json" \ -d @settings.json \ "https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/REGION/backendServices/BACKEND_SERVICE_NAME"
启用 IAP 后,您可以使用 gcloud CLI 通过 IAM 角色 roles/iap.httpsResourceAccessor 修改 IAP 访问权限政策。详细了解如何管理角色和权限。
Test access
After you configure your custom OAuth client, you can test that IAP is using it to protect your service by doing the following:
In the IAP page, in the Applications tab, view the applications that IAP manages.
Access the URL of one of the applications. If you're accessing the application for the first time since you configured the consent screen, you see the consent screen that you configured earlier.