AuthenticatorResponse: clientDataJSON 屬性

Baseline 已廣泛支援

此功能已成熟,並可在多種裝置和瀏覽器版本上執行。自 2021 年 9 月起,所有瀏覽器均已支援此功能。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

AuthenticatorResponse 介面的 clientDataJSON 屬性以 ArrayBuffer 的形式儲存一個 JSON 字串,該字串表示傳遞給 navigator.credentials.create()navigator.credentials.get() 的客戶端資料。此屬性僅在 AuthenticatorResponse 的子物件上訪問,具體是 AuthenticatorAttestationResponseAuthenticatorAssertionResponse

一個 ArrayBuffer

例項屬性

clientDataJSON 物件從 ArrayBuffer 轉換為 JavaScript 物件後,它將具有以下屬性:

challenge

從依賴方伺服器傳送的加密挑戰的 base64url 編碼版本。原始值作為 challenge 選項在 CredentialsContainer.get()CredentialsContainer.create() 中傳遞。

crossOrigin 可選

一個布林值。如果設定為 true,則表示呼叫上下文是一個與祖先幀不同源的 <iframe>

origin

客戶端/瀏覽器提供的依賴方的完全限定源。我們應該期望依賴方的 ID 是此值的字尾。

tokenBinding 可選 已棄用

一個描述與依賴方通訊的 令牌繫結協議 狀態的物件。它有兩個屬性:

  • status:一個字串,可以是 "supported"(表示客戶端支援令牌繫結但未與依賴方協商)或 "present"(表示已使用令牌繫結)。
  • id:一個字串,是用於通訊的令牌繫結 ID 的 base64url 編碼。

如果缺少此屬性,則表示客戶端不支援令牌繫結。

注意:根據規範的 Level 3,tokenBinding 已被棄用,但該欄位被保留,以防將來用於其他目的。

topOrigin 可選

包含依賴方的完全限定頂級源。僅當 crossOrigintrue 時設定。

type

一個字串,當檢索現有憑證時為 "webauthn.get",當建立新憑證時為 "webauthn.create"

示例

js
function arrayBufferToStr(buf) {
  return String.fromCharCode.apply(null, new Uint8Array(buf));
}

// pk is a PublicKeyCredential that is the result of a create() or get() Promise
const clientDataStr = arrayBufferToStr(pk.response.clientDataJSON);
const clientDataObj = JSON.parse(clientDataStr);

console.log(clientDataObj.type); // "webauthn.create" or "webauthn.get"
console.log(clientDataObj.challenge); // base64 encoded String containing the original challenge
console.log(clientDataObj.origin); // the window.origin

規範

規範
Web Authentication:訪問公鑰憑證的 API - 第 3 級
# dom-authenticatorresponse-clientdatajson

瀏覽器相容性