實作 FedCM 時,身分識別提供者 (IdP) 和依賴方 (RP) 都需要完成幾個核心步驟。請參閱說明文件,瞭解如何在依賴方 (RP) 端導入 FedCM。
IdPs 必須完成下列步驟,才能實作 FedCM:
建立 .well-known 檔案
為防止追蹤器濫用 API,必須從 IdP 的 eTLD+1 的 /.well-known/web-identity
提供 .well-known 檔案。
知名檔案可包含下列屬性:
屬性 | 必填 | 說明 |
---|---|---|
provider_urls
|
必要 | IdP 設定檔路徑的陣列。如果指定 accounts_endpoint 和 login_url ,系統會忽略這個值 (但仍須提供)。 |
accounts_endpoint
|
建議使用,但需要 login_url |
帳戶端點的網址。只要每個設定檔使用相同的 login_url 和 accounts_endpoint URL,即可支援多個設定。注意:Chrome 132 以上版本支援這個參數。 |
login_url
|
建議使用,需要 accounts_endpoint |
使用者登入 IdP 的登入頁面網址。只要每個設定檔都使用相同的 login_url 和 accounts_endpoint ,即可支援多個設定。注意:Chrome 132 以上版本支援此參數。 |
舉例來說,如果 IdP 端點是在 https://accounts.idp.example/
下提供服務,則必須在 https://idp.example/.well-known/web-identity
提供 well-known 檔案,以及IdP 設定檔。以下是知名檔案內容範例:
{
"provider_urls": ["https://accounts.idp.example/config.json"]
}
IdP 可以在 well-known 檔案中指定 accounts_endpoint
和 login_url
,以容納 IdP 的多個設定檔。這項功能在下列情況中相當實用:
- IdP 必須支援多種不同的測試和正式環境設定。
- IdP 必須支援各個區域的不同設定 (例如
eu-idp.example
和us-idp.example
)。
如要支援多項設定 (例如區分測試和實際工作環境),IdP 必須指定 accounts_endpoint
和 login_url
:
{
// This property is required, but will be ignored when IdP supports
// multiple configs (when `accounts_endpoint` and `login_url` are
// specified), as long as `accounts_endpoint` and `login_url` in
// that config file match those in the well-known file.
"provider_urls": [ "https://idp.example/fedcm.json" ],
// Specify accounts_endpoint and login_url properties to support
// multiple config files.
// Note: The accounts_endpoint and login_url must be identical
// across all config files. Otherwise,
// the configurations won't be supported.
"accounts_endpoint": "https://idp.example/accounts",
"login_url": "https://idp.example/login"
}
建立 IdP 設定檔和端點
IdP 設定檔會提供瀏覽器所需的端點清單。IdP 必須代管一或多個設定檔,以及必要的端點和網址。所有 JSON 回應都必須以 application/json
內容類型提供。
設定檔的網址取決於在 RP 上執行的 navigator.credentials.get()
呼叫所提供的值。RP 會傳遞每個身分識別提供者的設定檔網址:
// Executed on RP's side:
try {
const credential = await navigator.credentials.get({
identity: {
providers: [
{
// To allow users to sign in with the IdP1 using FedCM, RP specifies the IdP's config file URL:
configUrl: 'https://idp1.example/foo.json', // first IdP
clientId: '123',
},
// To allow users to sign in with the IdP2 using FedCM, RP specifies the IdP's config file URL.
// Note that multiple IdPs in a single get() are supported from Chrome 136.
{
configUrl: 'https://idp2.example/bar.json', // second IdP
clientId: '456',
},
],
},
});
const token = credential.token;
// Get the current IdP's configURL to identify which provider the user is signed in with
const currentIdpConfigUrl = credential.configURL;
if (currentIdpConfigUrl === 'https://idp1.example/foo.json') {
// handle the case where the user signed in with idp1
} else if (currentIdpConfigUrl === 'https://idp2.example/bar.json') {
// handle the case where the user signed in with idp2
}
} catch (error) {
// handle error
}
瀏覽器會透過 GET
要求擷取設定檔,但不會使用 Origin
標頭或 Referer
標頭。要求沒有 Cookie,也不會追蹤重新導向。這可有效防止 IdP 瞭解提出要求的使用者身分,以及嘗試連線的 RP。例如:
GET /config.json HTTP/1.1
Host: accounts.idp.example
Accept: application/json
Sec-Fetch-Dest: webidentity
IdP 必須實作設定端點,並以 JSON 格式回應。JSON 包含下列屬性:
屬性 | 說明 |
---|---|
accounts_endpoint (必填) |
帳戶端點的網址。 |
account_label (選填)
|
自訂帳戶標籤字串,用於判斷使用這個設定檔時應傳回哪些帳戶,例如:"account_label": "developer" 。
IdP 可以透過下列方式實作自訂帳戶標籤:
舉例來說,IdP 會實作指定 "account_label": "developer" 的 "https://idp.example/developer-config.json" 設定檔。IdP 也會使用帳戶端點中的 label_hints 參數,為部分帳戶加上 "developer" 標籤。當 RP 呼叫 navigator.credentials.get() 並指定 "https://idp.example/developer-config.json" 設定檔時,系統只會傳回具有 "developer" 標籤的帳戶。注意:Chrome 132 以上版本支援自訂帳戶標籤。 |
supports_use_other_account (非必要) |
布林值,指定使用者能否使用目前登入的帳戶以外的帳戶登入 (如果 IdP 支援多個帳戶)。這項設定僅適用於啟用模式。 |
client_metadata_endpoint (非必要) |
用戶端中繼資料端點的網址。 |
id_assertion_endpoint (必填) |
ID 聲明端點的網址。 |
disconnect (非必要) |
中斷連線端點的網址。 |
login_url (必填) |
使用者登入 IdP 的登入頁面網址。 |
branding (非必要) |
包含各種品牌選項的物件。 |
branding.background_color (非必要) |
品牌宣傳選項,可設定「以...身分繼續」按鈕的背景顏色。使用相關的 CSS 語法,也就是
hex-color 、
hsl() 、
rgb() 或
named-color 。 |
branding.color (非必要) |
品牌選項,可設定「以...身分繼續」按鈕的文字顏色。使用相關的 CSS 語法,也就是
hex-color 、
hsl() 、
rgb() 或
named-color 。 |
branding.icons (非必要) |
圖示物件的陣列。這些圖示會顯示在登入對話方塊中。圖示物件有兩個參數:
|
以下是 IdP 的回應主體範例:
{
"accounts_endpoint": "/accounts.example",
"client_metadata_endpoint": "/client_metadata.example",
"id_assertion_endpoint": "/assertion.example",
"disconnect_endpoint": "/disconnect.example",
"login_url": "/login",
// When RPs use this config file, only those accounts will be
//returned that include `developer` label in the accounts endpoint.
"account_label": "developer",
"supports_use_other_account": true,
"branding": {
"background_color": "green",
"color": "#FFEEAA",
"icons": [{
"url": "https://idp.example/icon.ico",
"size": 25
}]
}
}
瀏覽器擷取設定檔後,會將後續要求傳送至 IdP 端點:

使用其他帳戶
如果 IdP 支援多個帳戶或取代現有帳戶,使用者可以切換至目前登入帳戶以外的帳戶。
如要讓使用者選擇其他帳戶,IdP 必須在設定檔中指定這項功能:
{
"accounts_endpoint" : "/accounts.example",
"supports_use_other_account": true
}
帳戶端點
IdP 的帳戶端點會傳回使用者在 IdP 登入的帳戶清單。如果 IdP 支援多個帳戶,這個端點會傳回所有已登入的帳戶。
瀏覽器會傳送含有 SameSite=None
的 Cookie 的 GET
要求,但不會傳送 client_id
參數、Origin
標頭或 Referer
標頭。這項做法可有效防止 IdP 瞭解使用者嘗試登入的 RP。例如:
GET /accounts.example HTTP/1.1
Host: accounts.idp.example
Accept: application/json
Cookie: 0x23223
Sec-Fetch-Dest: webidentity
伺服器收到要求後,應執行下列操作:
- 確認要求包含
Sec-Fetch-Dest: webidentity
HTTP 標頭。 - 將工作階段 Cookie 與已登入帳戶的 ID 比對。
- 並提供帳戶清單。
瀏覽器會預期收到 JSON 回應,其中包含 accounts
屬性,以及具有下列屬性的帳戶資訊陣列:
屬性 | 說明 |
---|---|
id (必填) |
使用者的專屬 ID。 |
name |
根據使用者的語言代碼和偏好設定顯示全名。 注意:從 Chrome 141 開始,至少需要 name 、email 、username 或 tel 參數其中之一。在舊版 Chrome 中,name 和 email 都是必要條件。
|
username |
使用者選擇的使用者名稱。 注意:從 Chrome 141 開始,至少需要 name 、email 、username 或 tel 參數其中之一。
|
email |
使用者的電子郵件地址。 注意:從 Chrome 141 開始,至少需要 name 、email 、username 或 tel 參數其中之一。在舊版 Chrome 中,name 和 email 都是必要條件。
|
tel |
使用者的電話號碼。 注意:從 Chrome 141 開始,至少需要 name 、email 、username 或 tel 參數其中之一。
|
picture (非必要) |
使用者顯示圖片的網址。 |
given_name (非必要) |
使用者的名字。 |
approved_clients (非必要) |
使用者已向其註冊的 RP 用戶端 ID 陣列。 |
login_hints (非必要) |
IdP 支援的所有可能篩選器類型陣列,用於指定帳戶。RP 可以使用 loginHint 屬性叫用 navigator.credentials.get() ,選擇性地顯示指定帳戶。 |
domain_hints (非必要) |
與帳戶相關聯的所有網域陣列。RP 可以使用 domainHint 屬性呼叫 navigator.credentials.get() ,篩選帳戶。 |
label_hints (選填)
|
帳戶相關聯的自訂帳戶標籤字串陣列。 IdP 可以透過下列方式實作自訂帳戶標籤:
舉例來說,IdP 會實作指定 "account_label": "developer" 的 https://idp.example/developer-config.json 設定檔。IdP 也會使用 帳戶端點中的 label_hints 參數,為部分帳戶加上 "developer" 標籤。當 RP 呼叫 navigator.credentials.get() 並指定 https://idp.example/developer-config.json 設定檔時,系統只會傳回具有 "developer" 標籤的帳戶。自訂帳戶標籤與登入提示和網域提示不同,因為自訂帳戶標籤完全由 IdP 伺服器維護,RP 只會指定要使用的設定檔。 注意:Chrome 132 以上版本支援自訂帳戶標籤。 |
回應主體範例:
{
"accounts": [{
"id": "1234",
"given_name": "John",
"name": "John Doe",
"email": "john_doe@idp.example",
"picture": "https://idp.example/profile/123",
// Ids of those RPs where this account can be used
"approved_clients": ["123", "456", "789"],
// This account has 'login_hints`. When an RP calls `navigator.credentials.get()`
// with a `loginHint` value specified, for example, `exampleHint`, only those
// accounts will be shown to the user whose 'login_hints' array contains the `exampleHint`.
"login_hints": ["demo1", "exampleHint"],
// This account is labelled. IdP can implement a specific config file for a
// label, for example, `https://idp.example/developer-config.json`. Like that
// RPs can filter out accounts by calling `navigator.credentials.get()` with
// `https://idp.example/developer-config.json` config file.
"label_hints": ["enterprise", "developer"]
}, {
"id": "5678",
"given_name": "Johnny",
"name": "Johnny",
"email": "johnny@idp.example",
"picture": "https://idp.example/profile/456",
"approved_clients": ["abc", "def", "ghi"],
"login_hints": ["demo2"],
"domain_hints": ["@domain.example"]
}]
}
如果使用者未登入,請以 HTTP 401
(未授權) 回應。
瀏覽器會使用傳回的帳戶清單,RP 無法存取。
ID 聲明端點
IdP 的 ID 聲明端點會傳回已登入使用者的聲明。使用者透過 navigator.credentials.get()
呼叫登入 RP 網站時,瀏覽器會傳送 POST
要求,其中包含 SameSite=None
的 Cookie,以及內容類型為 application/x-www-form-urlencoded
的 Cookie,並附上下列資訊:
屬性 | 說明 |
---|---|
client_id (必填) |
RP 的用戶端 ID。 |
account_id (必填) |
登入使用者的專屬 ID。 |
disclosure_text_shown |
結果會以 "true" 或 "false" 字串 (而非布林值) 形式傳回。在下列情況下,結果為 "false" :
|
disclosure_shown_for |
列出瀏覽器在揭露文字中向使用者顯示的欄位,通知使用者 RP 向 IdP 要求哪些資料。 |
is_auto_selected |
如果 RP 執行自動重新驗證,is_auto_selected 會指出 "true" 。否則為 "false" 。這有助於支援更多安全性相關功能。舉例來說,部分使用者可能偏好較高的安全層級,因此需要明確的使用者仲介驗證。如果 IdP 收到沒有這類中介程序的權杖要求,可能會以不同方式處理要求。舉例來說,傳回錯誤代碼,讓 RP 可以使用 mediation: required 再次呼叫 FedCM API。 |
fields (選填)
|
字串陣列,指定 RP 要求 IdP 分享的使用者資訊。您可以選擇指定下列欄位:
fields 、disclosure_text_shown 和 disclosure_shown_for ,列出 POST 要求中指定的欄位,如以下範例所示。注意:Fields API 支援 Chrome 132 以上版本。Chrome 141 以上版本支援 `"username"` 和 `"tel"` 欄位。 |
params (選填)
|
任何有效的 JSON 物件,可指定其他自訂鍵/值參數,例如:
params 值會序列化為 JSON,然後進行百分比編碼。注意:Chrome 132 以上版本支援 Parameters API。 |
HTTP 標頭範例:
POST /assertion.example HTTP/1.1
Host: accounts.idp.example
Origin: https://rp.example/
Content-Type: application/x-www-form-urlencoded
Cookie: 0x23223
Sec-Fetch-Dest: webidentity
// disclosure_text_shown is set to 'false', as the 'name' field value is missing in 'fields' array
// params value is serialized to JSON and then percent-encoded.
account_id=123&client_id=client1234&disclosure_text_shown=false&is_auto_selected=true¶ms=%22%7B%5C%22nonce%5C%22%3A%5C%22nonce-value%5C%22%7D%22.%0D%0A4&fields=email,picture&disclosure_shown_for=email,picture
伺服器收到要求後,應執行下列操作:
- 使用 CORS (跨源資源共享) 回應要求。
- 確認要求包含
Sec-Fetch-Dest: webidentity
HTTP 標頭。 - 根據
client_id
判斷 RP 來源,並比對Origin
標頭。如果不相符,請拒絕。 - 將
account_id
與已登入帳戶的 ID 比對。如果不相符,請拒絕。 - 使用權杖回覆。如果要求遭到拒絕,請傳回錯誤回應。
IdP 可以決定如何核發權杖。一般來說,這類權杖會使用帳戶 ID、用戶端 ID、簽發者來源和隨機數等資訊簽署,因此 RP 可以驗證權杖是否為真。
瀏覽器會預期收到包含下列屬性的 JSON 回應:
屬性 | 說明 |
---|---|
token |
權杖是包含驗證相關聲明的字串。 |
continue_on |
重新導向網址,可啟用多步驟登入流程。 |
瀏覽器會將傳回的權杖傳遞給 RP,以便 RP 驗證驗證作業。
{
// IdP can respond with a token to authenticate the user
"token": "***********"
}
繼續使用這項功能
IdP 可以在 ID 聲明端點回應中提供重新導向網址,啟用多步驟登入流程。當 IdP 需要要求額外資訊或權限時,這項功能就相當實用,例如:
- 存取使用者伺服器端資源的權限。
- 確認聯絡資訊符合現況。
- 家長監護。
ID 聲明端點可以傳回 continue_on
屬性,其中包含 ID 聲明端點的絕對或相對路徑。
{
// In the id_assertion_endpoint, instead of returning a typical
// "token" response, the IdP decides that it needs the user to
// continue on a popup window:
"continue_on": "https://idp.example/continue_on_url"
}
如果回應包含 continue_on
參數,系統會開啟新的彈出式視窗,並將使用者導向指定路徑。使用者與 continue_on
頁面互動後,IdP 應呼叫 IdentityProvider.resolve()
,並將權杖做為引數傳遞,以便解析原始 navigator.credentials.get()
呼叫的 Promise:
document.getElementById('example-button').addEventListener('click', async () => {
let accessToken = await fetch('/generate_access_token.cgi');
// Closes the window and resolves the promise (that is still hanging
// in the relying party's renderer) with the value that is passed.
IdentityProvider.resolve(accessToken);
});
瀏覽器隨後會自動關閉彈出式視窗,並將權杖傳回給 API 呼叫端。只有一次性 IdentityProvider.resolve()
呼叫,才能讓父項視窗 (RP) 和彈出式視窗 (IdP) 進行通訊。
如果使用者拒絕要求,IdP 可以呼叫 IdentityProvider.close()
關閉視窗。
IdentityProvider.close();
必須經過明確的使用者互動 (點擊),Continuation API 才能運作。以下說明 Continuation API 如何搭配不同中介服務模式運作:
- 在
passive
模式下:mediation: 'optional'
(預設):Continuation API 只能搭配使用者手勢運作,例如點選網頁上的按鈕或 FedCM UI。如果系統在沒有使用者手勢的情況下觸發自動重新驗證,就不會開啟彈出式視窗,且承諾會遭到拒絕。mediation: 'required'
:一律要求使用者互動,因此 Continuation API 一律會運作。
- 在有效模式中:
- 一律需要啟用使用者。延續 API 一律相容。
如果使用者基於某些原因在彈出式視窗中變更帳戶 (例如 IdP 提供「使用其他帳戶」功能,或是在委派案例中),解析呼叫會採用選用的第二個引數,允許類似下列的內容:
IdentityProvider.resolve(token, {accountId: '1234');
傳回錯誤回應
id_assertion_endpoint
也可能會傳回「錯誤」回應,其中包含兩個選填欄位:
code
:IdP 可以從OAuth 2.0 指定的錯誤清單中選擇一個已知錯誤 (invalid_request
、unauthorized_client
、access_denied
、server_error
和temporarily_unavailable
),也可以使用任意字串。如果是後者,Chrome 會顯示含有一般錯誤訊息的錯誤使用者介面,並將代碼傳遞至 RP。url
:可識別使用者可自然閱讀的網頁,其中包含錯誤相關資訊,可為使用者提供錯誤的其他資訊。對使用者來說,這個欄位很有用,因為瀏覽器無法在內建 UI 中提供豐富的錯誤訊息。例如:後續步驟的連結或客戶服務聯絡資訊。如要進一步瞭解錯誤詳細資料和修正方法,使用者可以透過瀏覽器使用者介面前往提供的頁面查看詳細資料。網址必須與 IdPconfigURL
位於相同網站。
// id_assertion_endpoint response
{
"error" : {
"code": "access_denied",
"url" : "https://idp.example/error?type=access_denied"
}
}
自訂帳戶標籤
使用自訂帳戶標籤,IdP 可以使用標籤註解使用者帳戶,RP 則可指定該特定標籤的 configURL
,選擇只擷取具有特定標籤的帳戶。當 RP 需要依特定條件篩除帳戶時,這項功能就非常實用,例如只顯示特定角色專用的帳戶,如 "developer"
或 "hr"
。
您也可以使用「網域提示」和「登入提示」功能,在 navigator.credentials.get()
呼叫中指定這些提示,達到類似的篩選效果。不過,自訂帳戶標籤可以透過指定設定檔來篩選使用者,這在採用多個 configURL 時特別有用。自訂帳戶標籤也與登入或網域提示不同,因為這類標籤是由 IdP 伺服器提供,而非 RP 提供。
假設 IdP 想要區分 "developer"
和 "hr"
帳戶,如要達成這個目標,IdP 必須分別支援 "developer"
和 "hr"
的兩個 configURL:
- 開發人員設定檔
https://idp.example/developer/fedcm.json
具有"developer"
標籤,而企業設定檔https://idp.example/hr/fedcm.json
具有"hr"
標籤,如下所示:
// The developer config file at `https://idp.example/developer/fedcm.json`
{
"accounts_endpoint": "https://idp.example/accounts",
"client_metadata_endpoint": "/client_metadata",
"login_url": "https://idp.example/login",
"id_assertion_endpoint": "/assertion",
"account_label": "developer"
}
// The hr config file at `https://idp.example/hr/fedcm.json`
{
"accounts_endpoint": "https://idp.example/accounts",
"client_metadata_endpoint": "/client_metadata",
"login_url": "https://idp.example/login",
"id_assertion_endpoint": "/assertion",
"account_label": "hr"
}
- 完成這類設定後,well-known 檔案應包含
accounts_endpoint
和login_url
,以允許多個 configURL:
{
"provider_urls": [ "https://idp.example/fedcm.json" ],
"accounts_endpoint": "https://idp.example/accounts",
"login_url": "https://idp.example/login"
}
- 常見的 IdP 帳戶端點 (本例中為
https://idp.example/accounts
) 會傳回帳戶清單,其中包含label_hints
屬性,每個帳戶的指派標籤都會以陣列形式列出:
{
"accounts": [{
"id": "123",
"given_name": "John",
"name": "John Doe",
"email": "john_doe@idp.example",
"picture": "https://idp.example/profile/123",
"label_hints": ["developer"]
}], [{
"id": "4567",
"given_name": "Jane",
"name": "Jane Doe",
"email": "jane_doe@idp.example",
"picture": "https://idp.example/profile/4567",
"label_hints": ["hr"]
}]
}
當 RP 想要允許 "hr"
使用者登入時,可以在 navigator.credentials.get()
呼叫中指定 configURL https://idp.example/hr/fedcm.json
:
let { token } = await navigator.credentials.get({
identity: {
providers: [{
clientId: '1234',
nonce: '234234',
configURL: 'https://idp.example/hr/fedcm.json',
},
}
});
因此,使用者只能使用 4567
的帳戶 ID 登入。
瀏覽器會自動隱藏 123
的帳戶 ID,以免使用者在網站上使用 IdP 不支援的帳戶。
其他注意事項:
- 標籤是字串。如果
label_hints
陣列或account_label
欄位使用的值不是字串,系統會忽略該值。 - 如果
configURL
中未指定任何標籤,FedCM 帳戶選擇器會顯示所有帳戶。 - 如果帳戶未指定任何標籤,只有在
configURL
也未指定標籤時,該帳戶才會顯示在帳戶選擇器中。 - 如果沒有任何帳戶與被動模式中要求的標籤相符 (類似於網域提示功能),FedCM 對話方塊會顯示登入提示,讓使用者登入 IdP 帳戶。如果是啟用模式,系統會直接開啟登入彈出式視窗。
中斷端點連線
叫用 IdentityCredential.disconnect()
時,瀏覽器會傳送含有 SameSite=None
和 application/x-www-form-urlencoded
內容類型,以及 Cookie 的跨來源 POST
要求至這個中斷連線端點,並提供下列資訊:
屬性 | 說明 |
---|---|
account_hint |
IdP 帳戶的提示。 |
client_id |
RP 的用戶端 ID。 |
POST /disconnect.example HTTP/1.1
Host: idp.example
Origin: rp.example
Content-Type: application/x-www-form-urlencoded
Cookie: 0x123
Sec-Fetch-Dest: webidentity
account_hint=account456&client_id=rp123
伺服器收到要求後,應執行下列操作:
- 使用 CORS (跨源資源共享) 回應要求。
- 確認要求包含
Sec-Fetch-Dest: webidentity
HTTP 標頭。 - 根據
client_id
判斷 RP 來源,並比對Origin
標頭。如果不相符,請拒絕。 - 比對
account_hint
與已登入帳戶的 ID。 - 將使用者帳戶與 RP 中斷連結。
- 以 JSON 格式將識別的使用者帳戶資訊回傳給瀏覽器。
JSON 回應酬載範例如下:
{
"account_id": "account456"
}
如果 IdP 希望瀏覽器中斷與 RP 相關聯的所有帳戶,請傳遞與任何帳戶 ID 不符的字串,例如 "*"
。
用戶端中繼資料端點
IdP 的用戶端中繼資料端點會傳回信賴方的中繼資料,例如 RP 的隱私權政策、服務條款和標誌圖示。RP 應事先向 IdP 提供隱私權政策和服務條款的連結。如果使用者尚未在 RP 上向 IdP 註冊,登入對話方塊中就會顯示這些連結。
瀏覽器使用 client_id
navigator.credentials.get
傳送 GET
要求,但不含 Cookie。例如:
GET /client_metadata.example?client_id=1234 HTTP/1.1
Host: accounts.idp.example
Origin: https://rp.example/
Accept: application/json
Sec-Fetch-Dest: webidentity
伺服器收到要求後,應執行下列操作:
- 判斷
client_id
的 RP。 - 使用用戶端中繼資料回應。
用戶端中繼資料端點的屬性包括:
屬性 | 說明 |
---|---|
privacy_policy_url (非必要) |
RP 隱私權政策網址。 |
terms_of_service_url (非必要) |
RP 服務條款網址。 |
icons (非必要) |
物件陣列,例如 [{ "url": "https://rp.example/rp-icon.ico", "size": 40}] |
瀏覽器預期端點會傳回 JSON 回應:
{
"privacy_policy_url": "https://rp.example/privacy_policy.html",
"terms_of_service_url": "https://rp.example/terms_of_service.html",
"icons": [{
"url": "https://rp.example/rp-icon.ico",
"size": 40
}]
}
瀏覽器會使用傳回的用戶端中繼資料,RP 無法存取。
登入網址
這個端點用於讓使用者登入 IdP。
透過 Login Status API,IdP 必須將使用者的登入狀態告知瀏覽器。不過,狀態可能會不同步,例如工作階段過期時。在這種情況下,瀏覽器可以動態讓使用者透過 idp 設定檔的 login_url
指定的登入頁面網址登入 IdP。
FedCM 對話方塊會顯示建議登入的訊息,如下圖所示。

使用者點選「繼續」按鈕後,瀏覽器會開啟 IdP 登入頁面的彈出式視窗。

對話方塊是含有第一方 Cookie 的一般瀏覽器視窗。對話方塊中的任何動作都由 IdP 決定,且沒有可用的視窗控制代碼,可向 RP 頁面提出跨源通訊要求。使用者登入後,IdP 應執行下列操作:
- 傳送
Set-Login: logged-in
標頭或呼叫navigator.login.setStatus("logged-in")
API,通知瀏覽器使用者已登入。 - 呼叫
IdentityProvider.close()
關閉對話方塊。
向瀏覽器告知使用者的登入狀態
登入狀態 API 是一種機制,網站 (尤其是 IdP) 會透過這項機制將使用者在 IdP 的登入狀態告知瀏覽器。有了這項 API,瀏覽器就能減少對 IdP 發出不必要的要求,並降低潛在的時序攻擊風險。
當使用者登入 IdP 或登出所有 IdP 帳戶時,IdP 可以傳送 HTTP 標頭或呼叫 JavaScript API,向瀏覽器發出使用者登入狀態信號。針對每個 IdP (由設定網址識別),瀏覽器會保留三態變數,代表登入狀態,可能的值如下:
logged-in
logged-out
unknown
(預設)
登入狀態 | 說明 |
---|---|
logged-in |
如果使用者的登入狀態設為 logged-in ,呼叫 FedCM 的 RP 會向 IdP 的帳戶端點提出要求,並在 FedCM 對話方塊中向使用者顯示可用的帳戶。 |
logged-out |
如果使用者登入狀態為 logged-out ,系統會以無訊息方式呼叫 FedCM,但不會向 IdP 的帳戶端點提出要求。 |
unknown (預設) |
IdP 使用 Login Status API 傳送信號前,會先設定 unknown 狀態。當狀態為 unknown 時,瀏覽器會向 IdP 的帳戶端點發出要求,並根據帳戶端點的回應更新狀態。 |
如要發出使用者已登入的信號,請在 IdP 來源的頂層導覽或同網站子資源要求中,傳送 Set-Login: logged-in
HTTP 標頭:
Set-Login: logged-in
或者,您也可以在頂層導覽中,從 IdP 來源呼叫 JavaScript 方法 navigator.login.setStatus('logged-in')
:
navigator.login.setStatus('logged-in')
使用者的登入狀態會設為 logged-in
。
如要表示使用者已登出所有帳戶,請在 IdP 來源的頂層導覽或同網站子資源要求中,傳送 Set-Login: logged-out
HTTP 標頭:
Set-Login: logged-out
或者,您也可以在頂層導覽中,從 IdP 來源呼叫 JavaScript API navigator.login.setStatus('logged-out')
:
navigator.login.setStatus('logged-out')
使用者的登入狀態會設為 logged-out
。
IdP 使用 Login Status API 傳送信號前,會先設定 unknown
狀態。瀏覽器會向 IdP 的帳戶端點發出要求,並根據帳戶端點的回應更新狀態:
- 如果端點傳回有效帳戶清單,請將狀態更新為
logged-in
,並開啟 FedCM 對話方塊來顯示這些帳戶。 - 如果端點未傳回任何帳戶,請將狀態更新為
logged-out
,並讓 FedCM 呼叫失敗。
讓使用者透過動態登入流程登入
即使 IdP 持續向瀏覽器回報使用者的登入狀態,仍可能發生不同步的情況,例如工作階段到期時。登入狀態為 logged-in
時,瀏覽器會嘗試將憑證要求傳送至帳戶端點,但伺服器不會傳回任何帳戶,因為工作階段已失效。在這種情況下,瀏覽器可透過彈出式視窗,動態讓使用者登入 IdP。