设置入站 SAML 单点登录 API

本页面介绍了如何在管理 SAML 配置文件或配置单点登录 (SSO) 分配之前,设置 Cloud Identity Inbound SAML SSO Profiles (inboundSamlSsoProfiles) 和 Inbound SSO Assignments (inboundSsoAssignments) API。

准备工作

在使用入站 SAML SSO API 之前,您必须为自己的网域设置 Cloud Identity 或 Google Workspace。如需查看相关说明,请参阅设置 Cloud Identity。

安装 Python 客户端库

如需安装 Python 客户端库和 Google 身份验证库,请运行以下命令:

pip install --upgrade google-api-python-client google-auth \
  google-auth-oauthlib google-auth-httplib2

如需详细了解如何设置 Python 开发环境,请参阅 Python 开发环境设置指南。

启用 API 并设置凭证

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Identity API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Identity API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

使用入站 SAML 单点登录 API 进行身份验证

入站 SAML 单点登录配置文件和入站单点登录分配 API 用于管理网域级身份验证设置。您可以使用以下任一方法对调用进行身份验证:

  • 具有全网域授权的服务账号(推荐用于自动化工作流和后端服务):服务账号会模拟 Google Workspace 或 Cloud Identity 超级用户来调用 API。
  • 管理员 OAuth 2.0 凭证(建议用于交互式工具和 CLI 脚本):管理员用户直接通过 OAuth 2.0 Web 流程或本地权限请求流程进行身份验证。

OAuth 范围

入站 SAML 单点登录配置文件和入站单点登录分配 API 支持以下 OAuth 范围:

范围 说明
https://www.googleapis.com/auth/cloud-identity.inboundsso 拥有完整读写权限,可创建、更新、删除、获取和列出入站 SSO 配置文件和分配关系。
https://www.googleapis.com/auth/cloud-identity.inboundsso.readonly 拥有获取和列出入站单点登录配置文件和分配的只读权限。
https://www.googleapis.com/auth/cloud-identity 拥有对所有 Cloud Identity API 资源的完整访问权限。
https://www.googleapis.com/auth/cloud-platform 拥有对所有 Google Cloud 资源和 API 的完整访问权限。

以服务账号身份进行身份验证并进行全网域授权

如需从服务账号调用入站 SAML 单点登录 API,您必须在 Google 管理控制台中向该服务账号授予全网域授权。由于全网域授权允许服务账号模拟管理员用户,因此服务账号操作在审计日志中会显示为由被模拟的管理员执行。

第 1 步:向服务账号授予全网域权限

  1. 在 Google Cloud 控制台中,从服务账号页面复制服务账号的 OAuth 2 客户端 ID(客户端 ID 数字字符串)。
  2. 打开 Google 管理控制台。
  3. 依次前往主菜单 > 安全性 > 访问权限和数据控件 > API 控件。
  4. 在全网域授权窗格中,点击管理全网域授权。
  5. 点击新增。
  6. 在客户端 ID 字段中,输入服务账号的客户端 ID。
  7. 在 OAuth 范围字段中,以英文逗号分隔列表的形式输入所需范围:
    • 对于读写权限:https://www.googleapis.com/auth/cloud-identity.inboundsso
    • 对于只读访问权限:https://www.googleapis.com/auth/cloud-identity.inboundsso.readonly
  8. 点击授权。

如需了解详细说明,请参阅使用全网域授权功能控制 API 访问权限。

第 2 步:在应用代码中初始化凭证

在应用中初始化委托凭证时,请使用 subject 参数指定网域管理员的邮箱:

Python

from google.auth import iam
from google.auth.transport import requests
import google.auth
from google.oauth2 import service_account
from googleapiclient.discovery import build

# OAuth scope authorized for domain-wide delegation in the Admin console
SCOPES = ['https://www.googleapis.com/auth/cloud-identity.inboundsso']

# Administrator email address to impersonate
ADMIN_EMAIL = 'admin@example.com'

# Option A: Using a service account JSON key file
# credentials = service_account.Credentials.from_service_account_file(
#     'SERVICE_ACCOUNT_KEY_FILE.json',
#     scopes=SCOPES,
#     subject=ADMIN_EMAIL
# )

# Option B: Using Application Default Credentials (ADC) with IAM Signer
base_credentials, _ = google.auth.default(
    scopes=['https://www.googleapis.com/auth/iam']
)
request = requests.Request()
base_credentials.refresh(request)

signer = iam.Signer(
    request,
    base_credentials,
    base_credentials.service_account_email
)
delegated_credentials = service_account.Credentials(
    signer=signer,
    service_account_email=base_credentials.service_account_email,
    token_uri='https://accounts.google.com/o/oauth2/token',
    scopes=SCOPES,
    subject=ADMIN_EMAIL
)

# Build the Cloud Identity API client
service = build('cloudidentity', 'v1', credentials=delegated_credentials)

第 3 步:将 gcloud CLI 与服务账号模拟搭配使用

如需使用 gcloud CLI、应用默认凭据 (ADC) 和服务账号模拟来测试或调用 Inbound SAML SSO API,请运行以下命令:

gcloud auth application-default login \
  --impersonate-service-account=SERVICE_ACCOUNT_EMAIL \
  --scopes=https://www.googleapis.com/auth/iam,https://www.googleapis.com/auth/cloud-identity.inboundsso

替换以下内容:

  • SERVICE_ACCOUNT_EMAIL:已配置全网域委派的服务账号的邮箱。

使用 OAuth 2.0 以管理员身份进行身份验证

如需在不使用服务账号的情况下直接以超级用户身份对交互式应用或脚本进行身份验证,请使用标准 OAuth 2.0 用户凭证。

如需详细了解如何获取 Web 或桌面应用的 OAuth 2.0 凭证,请参阅使用 OAuth 2.0 访问 Google API。

后续步骤