設定傳入 SAML SSO API
本頁面說明如何設定 Cloud Identity Inbound SAML SSO Profiles (inboundSamlSsoProfiles) 和 Inbound SSO Assignments (inboundSsoAssignments) API,然後再管理 SAML 設定檔或設定單一登入 (SSO) 指派作業。
事前準備
使用 Inbound 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 並設定憑證
- 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Identity API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Identity API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.
使用傳入 SAML SSO API 進行驗證
Inbound SAML SSO Profiles API 和 Inbound SSO Assignments API 可管理網域層級的驗證設定。您可以使用下列任一方法驗證呼叫:
- 具備全網域委派權限的服務帳戶 (建議用於自動化工作流程和後端服務):服務帳戶會模擬 Google Workspace 或 Cloud Identity 超級管理員,呼叫 API。
- 管理員 OAuth 2.0 憑證 (建議用於互動式工具和 CLI 指令碼):管理員使用者直接透過 OAuth 2.0 網頁流程或本機同意流程進行驗證。
個 OAuth 範圍
傳入 SAML SSO 設定檔和傳入 SSO 指派 API 支援下列 OAuth 範圍:
| 範圍 | 說明 |
|---|---|
https://www.googleapis.com/auth/cloud-identity.inboundsso |
具備完整的讀寫權限,可建立、更新、刪除、取得及列出傳入單一登入 (SSO) 設定檔和指派作業。 |
https://www.googleapis.com/auth/cloud-identity.inboundsso.readonly |
具備唯讀權限,可取得及列出傳入單一登入 (SSO) 設定檔和指派項目。 |
https://www.googleapis.com/auth/cloud-identity |
具備所有 Cloud Identity API 資源的完整存取權。 |
https://www.googleapis.com/auth/cloud-platform |
具備所有 Google Cloud 資源和 API 的完整存取權。 |
以具備全網域委派功能的服務帳戶進行驗證
如要透過服務帳戶呼叫 Inbound SAML SSO API,您必須在 Google 管理控制台中,為服務帳戶授予全網域委派權限。由於全網域委派功能可讓服務帳戶模擬管理員使用者,因此服務帳戶動作在稽核記錄中會顯示為由模擬的管理員執行。
步驟 1:將全網域授權委派給服務帳戶
- 在 Google Cloud 控制台的「服務帳戶」頁面,複製服務帳戶的 OAuth 2 用戶端 ID (用戶端 ID 數字字串)。
- 開啟 Google 管理控制台。
- 依序前往「主選單」>「安全性」>「存取權與資料控管」>「API 控制項」。
- 在「全網域委派」窗格中,按一下「管理全網域委派設定」。
- 按一下「新增」。
- 在「Client ID」(用戶端 ID) 欄位中,輸入服務帳戶的用戶端 ID。
- 在「OAuth scopes」(OAuth 範圍) 欄位中,輸入必要範圍,並以逗號分隔的清單形式呈現:
- 如要授予讀取和寫入權限:
https://www.googleapis.com/auth/cloud-identity.inboundsso - 如要授予唯讀存取權,請執行下列指令:
https://www.googleapis.com/auth/cloud-identity.inboundsso.readonly
- 如要授予讀取和寫入權限:
- 按一下 [授權]。
如需詳細操作說明,請參閱「使用全網域委派功能控管 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 使用者憑證。
如要進一步瞭解如何取得網頁或電腦應用程式的 OAuth 2.0 憑證,請參閱「使用 OAuth 2.0 存取 Google API」。