PublicKeyCredential:parseRequestOptionsFromJSON() 靜態方法
PublicKeyCredential 介面的 parseRequestOptionsFromJSON() 靜態方法將 JSON 型別表示 轉換為 PublicKeyCredentialRequestOptions 例項。
此方法是一個便捷函式,用於轉換由依賴方伺服器提供給 Web 應用的資訊,以便請求現有的憑證。
語法
PublicKeyCredential.parseRequestOptionsFromJSON(options)
引數
options-
一個結構與
PublicKeyCredentialRequestOptions例項相同的物件,但其中 buffer 屬性使用了 base64url 編碼的字串。
返回值
異常
EncodingErrorDOMException-
如果
options物件中的任何部分無法轉換為PublicKeyCredentialRequestOptions例項,則會丟擲此異常。 SecurityErrorDOMException-
RP 域無效。
描述
Web 身份驗證過程中,用於 驗證(已註冊)使用者 的過程涉及一個依賴方伺服器向 Web 應用傳送查詢現有憑證所需的資訊,包括使用者身份、依賴方、一個 "challenge"(挑戰)的詳細資訊,以及可選的查詢憑證的位置:例如,在本地內建的身份驗證器上,或透過 USB、BLE 等連線的外部身份驗證器上。Web 應用透過呼叫 navigator.credentials.get() 並傳入一個包含伺服器提供資料的 PublicKeyCredentialRequestOptions 例項作為引數,將此資訊傳遞給身份驗證器以查詢憑證。
規範並未定義請求憑證所需的資訊是如何傳送的。一種便捷的方法是讓伺服器將資訊封裝在 PublicKeyCredentialRequestOptions 例項的 JSON 型別表示 中,該表示與其結構相匹配,但將 challenge 等 buffer 屬性編碼為 base64url 字串。然後,此物件可以被序列化為 JSON 字串,傳送到 Web 應用並反序列化,然後使用 parseRequestOptionsFromJSON() 轉換為 PublicKeyCredentialRequestOptions 例項。
示例
在授權一個已註冊使用者時,依賴方伺服器將向 Web 應用提供有關請求的憑證、依賴方和 challenge 的資訊。下面的程式碼按照 options 引數 中所述的格式定義了這些資訊。
const requestCredentialOptionsJSON = {
challenge: new Uint8Array([139, 66, 181, 87, 7, 203 /* … */]),
rpId: "acme.com",
allowCredentials: [
{
type: "public-key",
id: new Uint8Array([64, 66, 25, 78, 168, 226, 174 /* … */]),
},
],
userVerification: "required",
};
由於此物件僅使用 JSON 資料型別,因此可以使用 JSON.stringify() 將其序列化為 JSON 併發送到 Web 應用。
JSON.stringify(requestCredentialOptionsJSON);
Web 應用可以將 JSON 字串反序列化回 requestCredentialOptionsJSON 物件(此處未顯示)。parseRequestOptionsFromJSON() 方法用於將該物件轉換為可在 navigator.credentials.get() 中使用的格式。
// Convert options to form used by get()
const publicKey = PublicKeyCredential.parseRequestOptionsFromJSON(
requestCredentialOptionsJSON, // JSON-type representation
);
navigator.credentials
.get({ publicKey })
.then((returnedCredentialInfo) => {
// Handle the returned credential information here.
})
.catch((err) => {
console.error(err);
});
規範
| 規範 |
|---|
| Web Authentication:訪問公鑰憑證的 API - 第 3 級 # dom-publickeycredential-parserequestoptionsfromjson |
瀏覽器相容性
載入中…