PublicKeyCredential

Baseline 廣泛可用 *

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

* 此特性的某些部分可能存在不同級別的支援。

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

PublicKeyCredential 介面提供了有關公鑰/私鑰對的資訊,這是一個用於透過防釣魚和抗資料洩露的不對稱金鑰對(而不是密碼)登入服務的憑證。它繼承自 Credential,並且是 Web Authentication APICredential Management API 的擴充套件的一部分。

Credential PublicKeyCredential

注意: 此 API 僅限於頂層上下文。在 <iframe> 元素內使用將不起作用。

例項屬性

PublicKeyCredential.authenticatorAttachment 只讀

一個字串,指示在相應的 navigator.credentials.create()navigator.credentials.get() 呼叫完成時,WebAuthn 實現連線到身份驗證器的方式。

PublicKeyCredential.id 只讀

繼承自 Credential,並被重寫為 base64url 編碼PublicKeyCredential.rawId

PublicKeyCredential.rawId 只讀

一個 ArrayBuffer,其中包含此 PublicKeyCredential 的全域性唯一識別符號。此識別符號可用於在將來的 navigator.credentials.get() 呼叫中查詢憑證。

PublicKeyCredential.response 只讀

一個 AuthenticatorResponse 物件的例項。如果 PublicKeyCredentialnavigator.credentials.create() 呼叫結果,則其型別為 AuthenticatorAttestationResponse;如果 PublicKeyCredentialnavigator.credentials.get() 呼叫結果,則其型別為 AuthenticatorAssertionResponse

PublicKeyCredential.type 只讀

繼承自 Credential。對於 PublicKeyCredential 例項,始終設定為 public-key

靜態方法

PublicKeyCredential.getClientCapabilities()

返回一個 Promise,該 Promise 解析為一個物件,可用於檢查是否支援特定的 WebAuthn 功能和 擴充套件

PublicKeyCredential.isConditionalMediationAvailable()

返回一個 Promise,該 Promise 解析為 true(如果條件中介可用)。

PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable()

返回一個 Promise,該 Promise 解析為 true(如果繫結到平臺的身份驗證器能夠驗證使用者)。

PublicKeyCredential.parseCreationOptionsFromJSON()

註冊使用者憑證時,用於反序列化伺服器傳送的憑證註冊資料的便捷方法。

PublicKeyCredential.parseRequestOptionsFromJSON()

認證(已註冊)使用者時,用於反序列化伺服器傳送的憑證請求資料的便捷方法。

PublicKeyCredential.signalAllAcceptedCredentials()

向身份驗證器發出訊號,告知所有受信任的 憑證 ID,這些憑證是 依賴方伺服器仍然為特定使用者持有的。

PublicKeyCredential.signalCurrentUserDetails()

向身份驗證器發出訊號,表明特定使用者已更新其使用者名稱和/或顯示名稱。

PublicKeyCredential.signalUnknownCredential()

向身份驗證器發出訊號,表明一個 憑證 ID 未被 依賴方伺服器識別,例如因為它已被刪除。

例項方法

PublicKeyCredential.getClientExtensionResults()

如果請求了任何擴充套件,此方法將返回處理這些擴充套件的結果。

PublicKeyCredential.toJSON()

註冊使用者憑證認證已註冊使用者時,用於建立 PublicKeyCredential 的 JSON 字串表示併發送到伺服器的便捷方法。

示例

建立 PublicKeyCredential 的新例項

在這裡,我們使用 navigator.credentials.create() 來生成一個新憑證。

js
const createCredentialOptions = {
  publicKey: {
    challenge: new Uint8Array([
      21, 31, 105 /* 29 more random bytes generated by the server */,
    ]),
    rp: {
      name: "Example CORP",
      id: "login.example.com",
    },
    user: {
      id: new Uint8Array(16),
      name: "canand@example.com",
      displayName: "Carina Anand",
    },
    pubKeyCredParams: [
      {
        type: "public-key",
        alg: -7,
      },
    ],
  },
};

navigator.credentials
  .create(createCredentialOptions)
  .then((newCredentialInfo) => {
    const response = newCredentialInfo.response;
    const clientExtensionsResults =
      newCredentialInfo.getClientExtensionResults();
  })
  .catch((err) => {
    console.error(err);
  });

獲取 PublicKeyCredential 的現有例項

在這裡,我們使用 navigator.credentials.get() 從身份驗證器獲取一個現有憑證。

js
const requestCredentialOptions = {
  publicKey: {
    challenge: new Uint8Array([
      /* bytes sent from the server */
    ]),
  },
};

navigator.credentials
  .get(requestCredentialOptions)
  .then((credentialInfoAssertion) => {
    // send assertion response back to the server
    // to proceed with the control of the credential
  })
  .catch((err) => {
    console.error(err);
  });

規範

規範
Web Authentication:訪問公鑰憑證的 API - 第 3 級
# iface-pkcredential

瀏覽器相容性

另見