透過 SAML 登入使用者
本文說明如何使用 Identity Platform,透過安全宣告標記語言 (SAML) 2.0 供應商登入使用者。
事前準備
-
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.
- 啟用 Identity Platform,並將用戶端 SDK 新增至應用程式。如需更多資訊和操作說明,請參閱透過 Identity Platform 使用電子郵件地址登入使用者帳戶快速入門導覽課程。
設定提供者
在 Google Cloud 控制台,依序前往「Identity Platform」>「Identity providers」(識別資訊提供者) 頁面。
前往「識別資訊提供者」按一下「Add a Provider」,然後從清單中選取「SAML」。
輸入下列詳細資訊:
供應商的名稱。可以與提供者 ID 相同,也可以是自訂名稱。如果輸入自訂名稱,請按一下「Provider ID」(供應商 ID) 旁的「Edit」(編輯),指定 ID (必須以
saml開頭)。供應商的實體 ID。
供應商的 SAML SSO 網址。
用於在提供者上簽署權杖的憑證。請務必包含開頭和結尾字串。例如:
-----BEGIN CERTIFICATE----- MIICajCCAdOgAwIBAgIBADANBgkqhkiG9w0BAQ0FADBSMQswCQYDVQQGEwJ1czEL ... LEzc1JwEGQQVDYQCwsQMSBDAF0QAB0w9GikhqkgBNADABIgABIwAgOdACCjaCIIM -----END CERTIFICATE-----
在「服務供應商」部分,輸入應用程式的「實體 ID」。這通常是應用程式的網址。在 SAML 識別資訊提供者中,這稱為目標對象。
在「Project settings」(專案設定) 側窗格中,按一下「Add Domain」(新增網域),然後新增應用程式的網域。舉例來說,如果應用程式的登入網址是
https://example.com/login,請新增example.com。如要完成設定,請執行下列任一操作:
從「Authorization callback (URL)」(授權回呼 (網址)) 欄位複製預設授權回呼網址,並新增至 SAML 應用程式設定。
使用預設授權回呼網址,可降低驗證 SAML 回應的複雜度。
將自訂授權回呼網址新增至 SAML 應用程式設定,例如
https://PROJECT-ID.firebaseapp.com/__/auth/handler。
在「Configure your application」(設定應用程式) 部分,按一下「Setup Details」(設定詳細資料)。將程式碼片段複製到應用程式的程式碼,初始化 Identity Platform 用戶端 SDK。
按一下「儲存」。
供應商必要元素
Identity Platform 預期供應商的回應中會包含 <saml:Subject> 和 <saml:NameID> 元素。設定供應商時,如果未定義這些元素的值,SAML 斷言就會失敗。
經簽署的要求
您可以簽署驗證要求,提高安全性。
如要簽署要求,請先呼叫 inboundSamlConfigs.patch(),並將 idp_config.sign_request 設為 true,為識別資訊提供者啟用經簽署的要求:
REST
使用任何要求資料之前,請先修改下列項目的值:
project-id: Google Cloud 專案的 IDprovider-id:SAML 提供者 ID
HTTP 方法和網址:
PATCH https://identitytoolkit.googleapis.com/admin/v2/projects/project-id/inboundSamlConfigs/provider-id?updateMask=idpConfig.signRequest
JSON 要求主體:
{
"idp_config": {
"sign_request": true
}
}
請展開以下其中一個選項,以傳送要求:
您必須使用 REST API 啟用經簽署的要求,不支援使用Google Cloud 控制台或 Google Cloud CLI。
回應是 InboundSamlConfig 物件,其中包含 SpCertificate 物件陣列。使用 SAML 識別資訊提供者設定 X509 憑證的值,以便驗證要求的簽章。
輪替服務供應商憑證
啟用已簽署的 SAML 要求後,Identity Platform 會產生效期五年的服務供應商 (SP) 憑證。憑證到期後,識別資訊提供者 (IdP) 會拒絕驗證要求,使用者也無法登入。為避免驗證中斷,請監控 SP 憑證的到期日,並在到期前輪換。
Identity Platform 會將服務供應商憑證限定在專案或租戶範圍內,並在該專案或租戶的所有 SAML 設定 (InboundSamlConfig 物件) 之間共用。輪替一個 SAML 設定的服務供應商憑證,會輪替同一專案或租戶中所有其他 SAML 設定的憑證。
如要查看到期日及輪替服務供應商憑證,請按照下列步驟操作:
如要檢查 SP 憑證到期日,請使用
jq擷取憑證到期時間戳記:curl -s -X GET \ "https://identitytoolkit.googleapis.com/v2/projects/PROJECT_ID/inboundSamlConfigs" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "X-Goog-User-Project: PROJECT_ID" \ -H "Content-Type: application/json" | \ jq -r '.inboundSamlConfigs[].spConfig.spCertificates[]?.expiresAt'如要進行租戶層級的 SAML 設定,請在要求網址中加入
/tenants/TENANT_ID:curl -s -X GET \ "https://identitytoolkit.googleapis.com/v2/projects/PROJECT_ID/tenants/TENANT_ID/inboundSamlConfigs" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "X-Goog-User-Project: PROJECT_ID" \ -H "Content-Type: application/json" | \ jq -r '.inboundSamlConfigs[].spConfig.spCertificates[]?.expiresAt'更改下列內容:
PROJECT_ID:您的 Google Cloud專案 ID。TENANT_ID:租戶層級 SAML 設定的租戶 ID。
檢查
spConfig.spCertificates陣列中的expiresAt值。如果到期時間戳記 (
expiresAt) 在 30 天內,或在貴組織要求的時間範圍內,請與 IdP 管理員協調,安排憑證更新的停機時間。在排定的停機時間內,傳送
POST要求至inboundSamlConfigs.rotateCertificate端點,輪替 SP 憑證。目標 SAML 設定必須將idpConfig.signRequest欄位設為true。否則要求會失敗,並顯示INVALID_CONFIG錯誤:curl -s -X POST \ "https://identitytoolkit.googleapis.com/v2/projects/PROJECT_ID/inboundSamlConfigs/PROVIDER_ID:rotateCertificate" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "X-Goog-User-Project: PROJECT_ID" \ -H "Content-Type: application/json" \ -d "{}"如要進行租戶層級的 SAML 設定,請在要求網址中加入
/tenants/TENANT_ID:curl -s -X POST \ "https://identitytoolkit.googleapis.com/v2/projects/PROJECT_ID/tenants/TENANT_ID/inboundSamlConfigs/PROVIDER_ID:rotateCertificate" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "X-Goog-User-Project: PROJECT_ID" \ -H "Content-Type: application/json" \ -d "{}"更改下列內容:
PROJECT_ID:您的 Google Cloud專案 ID。PROVIDER_ID:SAML 提供者 ID,開頭為saml.。這項設定必須將idpConfig.signRequest欄位設為true。TENANT_ID:租戶層級 SAML 設定的租戶 ID。
回應會傳回
inboundSamlConfig物件,其中包含新產生的 X.509 憑證 (x509Certificate) 和inboundSamlConfig.spConfig.spCertificates陣列中的新到期時間戳記 (expiresAt)。如要更新 IdP 中的憑證設定,請從回應中複製新的
x509Certificate值,並提供給 IdP 管理員,用於專案或租戶中所有使用經簽署的要求的 SAML 設定。測試 SSO 是否能與更新後的設定搭配運作。
讓使用者登入
使用者登入時,用戶端 SDK 會處理驗證交握,然後傳回 ID 權杖,其中包含酬載中的 SAML 屬性。如要讓使用者登入並從 SAML 供應商取得屬性,請按照下列步驟操作:
使用您在上一節設定的供應商 ID 建立
SAMLAuthProvider執行個體。提供者 ID 的開頭必須是saml。網頁版 9
import { SAMLAuthProvider } from "firebase/auth"; const provider = new SAMLAuthProvider("saml.myProvider");
網頁版 8
const provider = new firebase.auth.SAMLAuthProvider('saml.myProvider');
啟動登入流程。您可以選擇使用對話方塊或重新導向。
彈出式視窗
網頁版 9
import { getAuth, signInWithPopup, SAMLAuthProvider } from "firebase/auth"; const auth = getAuth(); signInWithPopup(auth, provider) .then((result) => { // User is signed in. // Provider data available from the result.user.getIdToken() // or from result.user.providerData }).catch((error) => { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; // The email of the user's account used. const email = error.customData.email; // The AuthCredential type that was used. const credential = SAMLAuthProvider.credentialFromError(error); // Handle / display error. // ... });
網頁版 8
firebase.auth().signInWithPopup(provider) .then((result) => { // User is signed in. // Identity provider data available in result.additionalUserInfo.profile, // or from the user's ID token obtained from result.user.getIdToken() // as an object in the firebase.sign_in_attributes custom claim // This is also available from result.user.getIdTokenResult() // idTokenResult.claims.firebase.sign_in_attributes. }) .catch((error) => { // Handle / display error. // ... });
重新導向
如要重新導向至登入頁面,請呼叫
signInWithRedirect():網頁版 9
import { getAuth, signInWithRedirect } from "firebase/auth"; const auth = getAuth(); signInWithRedirect(auth, provider);
網頁版 8
firebase.auth().signInWithRedirect(provider);
接著,在使用者返回應用程式時,呼叫
getRedirectResult()取得結果:網頁版 9
import { getAuth, getRedirectResult, SAMLAuthProvider } from "firebase/auth"; const auth = getAuth(); getRedirectResult(auth) .then((result) => { // User is signed in. // Provider data available from the result.user.getIdToken() // or from result.user.providerData }) .catch((error) => { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; // The email of the user's account used. const email = error.customData.email; // The AuthCredential type that was used. const credential = SAMLAuthProvider.credentialFromError(error); // Handle / display error. // ... });
網頁版 8
firebase.auth().getRedirectResult() .then((result) => { // User is signed in. // Provider data available in result.additionalUserInfo.profile, // or from the user's ID token obtained from result.user.getIdToken() // as an object in the firebase.sign_in_attributes custom claim // This is also available from result.user.getIdTokenResult() // idTokenResult.claims.firebase.sign_in_attributes. }).catch((error) => { // Handle / display error. // ... });
使用
firebase.sign_in_attributes宣告,從 ID 權杖擷取與 SAML 供應商相關聯的使用者屬性。將 ID 權杖傳送至伺服器時,請務必使用 Admin SDK 驗證。只有在識別資訊提供者提供的 SAML 宣告
NameID屬性中,提供使用者的電子郵件地址時,ID 權杖才會包含該地址:<Subject> <NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">test@email.com</NameID> </Subject>這項資訊會填入 Firebase 核發的 ID 權杖和 UserInfo 物件。
僅支援由服務供應商啟動的用戶端 SDK SAML 流程。
連結使用者帳戶
如果使用者已透過其他方式 (例如電子郵件/密碼) 登入應用程式,您可以使用 linkWithPopup() 或 linkWithRedirect(),將現有帳戶連結至 SAML 供應商。
下列程式碼片段說明如何將使用者的 Google 帳戶連結至 SAML 供應商:
網頁版 9
import { getAuth, linkWithPopup, GoogleAuthProvider } from "firebase/auth"; const provider = new GoogleAuthProvider(); const auth = getAuth(); linkWithPopup(auth.currentUser, provider).then((result) => { // Accounts successfully linked. const credential = GoogleAuthProvider.credentialFromResult(result); const user = result.user; // ... }).catch((error) => { // Handle Errors here. // ... });
網頁版 8
auth.currentUser.linkWithPopup(provider).then((result) => { // Accounts successfully linked. var credential = result.credential; var user = result.user; // ... }).catch((error) => { // Handle Errors here. // ... });