如果您已升級至 Firebase Authentication with Identity Platform,可以使用您選擇的 SAML 識別資訊提供者,透過 Firebase 驗證使用者。這樣一來,您就能使用 SAML 型 SSO 解決方案,讓使用者登入 Firebase 應用程式。
Firebase Authentication 僅支援由服務供應商啟動的 SAML 流程。
事前準備
如要使用 SAML 識別資訊提供者登入使用者,您必須先從提供者收集一些資訊:
- 提供者的實體 ID:用於識別識別資訊提供者的 URI。
- 供應商的 SAML SSO 網址:識別資訊提供者登入頁面的網址。
- 提供者的公用金鑰憑證:用於驗證由識別資訊提供者簽署的權杖。
- 應用程式的實體 ID:用於識別應用程式 (即「服務供應商」) 的 URI。
取得上述資訊後,請為 Firebase 專案啟用 SAML 做為登入供應商:
如果尚未升級,請務必將專案升級至 Firebase Authentication with Identity Platform。SAML 驗證僅適用於升級後的專案。
在 Firebase 控制台中,依序前往「Security」(安全性) >「Authentication」(驗證)。
在「登入方式」分頁中,依序點選「新增供應商」和「SAML」。
為這個供應商命名。記下系統產生的提供者 ID,例如
saml.example-provider。在應用程式中加入登入程式碼時,您需要這個 ID。指定識別資訊提供者的實體 ID、SSO 網址和公用金鑰憑證。並指定應用程式 (服務供應商) 的實體 ID。這些值必須與供應商指派給你的值完全一致。
儲存變更。
如果您尚未授權應用程式的網域,請按照下列步驟操作:
在 Firebase 控制台中,依序前往「Security」(安全性) >「Authentication」(驗證) >「Settings」(設定) 分頁標籤。
在「Authorized domains」(授權網域) 專區中,按一下「Add domain」(新增網域),然後新增網域。
使用 Firebase SDK 處理登入流程
如要使用 Firebase JavaScript SDK 處理登入流程,請按照下列步驟操作:
使用在 Firebase 控制台中取得的供應商 ID,建立
SAMLAuthProvider的執行個體。Web
import { SAMLAuthProvider } from "firebase/auth"; const provider = new SAMLAuthProvider('saml.example-provider');Web
var provider = new firebase.auth.SAMLAuthProvider('saml.example-provider'); ``
使用 SAML 供應商物件向 Firebase 驗證。
您可以將使用者重新導向至供應商的登入頁面,或在彈出式瀏覽器視窗中開啟登入頁面。
重新導向流程
呼叫
signInWithRedirect(),重新導向至供應商登入頁面:Web
import { getAuth, signInWithRedirect } from "firebase/auth"; const auth = getAuth(); signInWithRedirect(auth, provider);Web
firebase.auth().signInWithRedirect(provider);使用者完成登入程序並返回應用程式後,您可以呼叫
getRedirectResult()取得登入結果。Web
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
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. });彈出式視窗流程
Web
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
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. });只有在識別資訊提供者的 SAML 聲明
NameID屬性中提供使用者電子郵件地址時,ID 權杖和 UserInfo 物件才會包含該地址:<Subject> <NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">test@email.com</NameID> </Subject>雖然上述範例著重於登入流程,但您可以使用相同的模式,透過
linkWithRedirect()和linkWithPopup()將 SAML 供應商連結至現有使用者,並使用reauthenticateWithRedirect()和reauthenticateWithPopup()重新驗證使用者,這可用於擷取需要近期登入的敏感作業的新憑證。