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

浏览器兼容性