如果您已升級到使用 Identity Platform 進行 Firebase 身份驗證,則可以使用您選擇的 SAML 身份提供商通過 Firebase 對您的用戶進行身份驗證。這樣就可以使用基於 SAML 的 SSO 解決方案讓用戶登錄到您的 Firebase 應用程序。
Firebase 身份驗證僅支持服務提供商發起的 SAML 流。
在你開始之前
要使用 SAML 身份提供者登錄用戶,您必須首先從提供者那裡收集一些信息:
- 提供者的實體 ID :標識身份提供者的 URI。
- 提供商的 SAML SSO URL :身份提供商登錄頁面的 URL。
- 提供者的公鑰證書:用於驗證身份提供者簽名的令牌的證書。
- 您的應用程序的實體 ID :標識您的應用程序的 URI,即“服務提供商”。
獲得上述信息後,啟用 SAML 作為您的 Firebase 項目的登錄提供商:
如果您尚未升級到 Firebase Authentication with Identity Platform,請執行此操作。 SAML 身份驗證僅在升級後的項目中可用。
在 Firebase 控制台的Sign-in providers頁面上,點擊Add new provider ,然後點擊SAML 。
給這個提供者一個名字。請注意生成的提供者 ID:類似
saml.example-provider
東西。當您將登錄代碼添加到您的應用程序時,您將需要此 ID。指定身份提供商的實體 ID、SSO URL 和公鑰證書。還要指定您的應用程序的實體 ID(服務提供商)。這些值必須與您的提供商分配給您的值完全匹配。
保存您的更改。
如果您尚未授權應用的域,請將其添加到 Firebase 控制台的“身份驗證”>“設置”頁面上的允許列表。
使用 Firebase SDK 處理登錄流程
要使用 Firebase JavaScript SDK 處理登錄流程,請執行以下步驟:
使用您在 Firebase 控制台中獲得的提供商 ID 創建
SAMLAuthProvider
的實例。網絡模塊化 API
import { SAMLAuthProvider } from "firebase/auth"; const provider = new SAMLAuthProvider('saml.example-provider');
Web 命名空間 API
var provider = new firebase.auth.SAMLAuthProvider('saml.example-provider'); ``
使用 SAML 提供程序對象通過 Firebase 進行身份驗證。
您可以將用戶重定向到提供商的登錄頁面或在彈出式瀏覽器窗口中打開登錄頁面。
重定向流
通過調用
signInWithRedirect()
重定向到提供商登錄頁面:網絡模塊化 API
import { getAuth, signInWithRedirect } from "firebase/auth"; const auth = getAuth(); signInWithRedirect(auth, provider);
Web 命名空間 API
firebase.auth().signInWithRedirect(provider);
用戶完成登錄返回您的應用後,您可以通過調用
getRedirectResult()
獲取登錄結果。網絡模塊化 API
import { getAuth, getRedirectResult, SAMLAuthProvider } from "firebase/auth"; const auth = getAuth(); getRedirectResult(auth) .then((result) => { // User is signed in. // Provider data available using getAdditionalUserInfo() }) .catch((error) => { // Handle error. });
Web 命名空間 API
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. }) .catch((error) => { // Handle error. });
彈窗流
網絡模塊化 API
import { getAuth, signInWithPopup, OAuthProvider } from "firebase/auth"; const auth = getAuth(); signInWithPopup(auth, provider) .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. }) .catch((error) => { // Handle error. });
Web 命名空間 API
firebase.auth().signInWithPopup(provider) .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. }) .catch((error) => { // Handle error. });
雖然上述示例側重於登錄流程,但您可以使用相同的模式使用
linkWithRedirect()
和linkWithPopup()
將 SAML 提供商鏈接到現有用戶,並使用reauthenticateWithRedirect()
和reauthenticateWithPopup()
重新驗證用戶,可用於為需要最近登錄的敏感操作檢索新憑據。