PublicKeyCredential: response 屬性

Baseline 已廣泛支援

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

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

PublicKeyCredential 介面中只讀的 response 屬性是一個 AuthenticatorResponse 物件,它由驗證器傳送給使用者代理,用於建立/獲取憑據。此響應中包含的資訊將由依賴方伺服器用於驗證請求的合法性。

AuthenticatorResponse 要麼是

為了驗證憑據的建立,依賴方伺服器需要

注意: 在驗證現有憑據的獲取時,依賴方伺服器需要整個 PublicKeyCredential 物件以及客戶端擴充套件。

注意: 此屬性只能用於頂級上下文,在 <iframe> 中不可用。

一個 AuthenticatorResponse 物件,其中包含依賴方指令碼將接收到的資料,這些資料應傳送到依賴方伺服器以驗證建立或獲取請求。此物件包含來自客戶端(AuthenticatorResponse/clientDataJSON)和來自驗證器的資料。

示例

js
const options = {
  challenge: new Uint8Array(16) /* from the server */,
  rp: {
    name: "Example CORP",
    id: "login.example.com",
  },
  user: {
    id: new Uint8Array(16) /* from the server */,
    name: "canand@example.com",
    displayName: "Carina Anand",
  },
  pubKeyCredParams: [
    {
      type: "public-key",
      alg: -7,
    },
  ],
};

navigator.credentials
  .create({ publicKey: options })
  .then((pubKeyCredential) => {
    const response = pubKeyCredential.response;
    const clientExtResults = pubKeyCredential.getClientExtensionResults();
    // Send response and client extensions to the server so that it can validate
    // and create credentials
  })
  .catch((err) => {
    // Deal with any error
  });

規範

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

瀏覽器相容性