CredentialsContainer:create() 方法

Baseline 廣泛可用 *

此功能已成熟,並可在多種裝置和瀏覽器版本上使用。自 2019 年 9 月以來,它已在各種瀏覽器中可用。

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

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

CredentialsContainer 介面的 create() 方法會建立一個新的 憑證,該憑證可以儲存起來,之後透過 navigator.credentials.get() 方法檢索。檢索到的憑證隨後可供網站用於對使用者進行身份驗證。

此方法支援三種不同型別的憑證

  • 密碼憑證,允許使用者使用密碼登入。
  • 聯合身份憑證,允許使用者使用聯合身份提供商登入。
  • 公鑰憑證,允許使用者使用身份驗證器(例如內置於平臺的生物識別讀取器或可移動硬體令牌)登入。

請注意,聯合憑證管理 API (FedCM) 已取代了聯合身份憑證型別。

語法

js
create()
create(options)

引數

options 可選

一個包含所請求的新 Credentials 物件選項的物件。它可以包含以下屬性

signal 可選

一個 AbortSignal 物件例項,允許中止正在進行的 create() 操作。中止的操作可能會正常完成(通常是在操作完成後收到中止請求),或者以 AbortError DOMException 拒絕。

以下每個屬性代表一個正在建立的憑證型別。必須且只能指定其中一個

federated 可選

一個 FederatedCredentialInit 物件,包含建立聯合身份提供商憑證的要求。

password 可選

一個 PasswordCredentialInit 物件,包含建立密碼憑證的要求。

publicKey 可選

一個 PublicKeyCredentialCreationOptions 物件,包含建立公鑰憑證的要求。這將導致 create() 呼叫請求使用者代理透過身份驗證器建立新憑證 — 用於註冊新帳戶或將新的非對稱金鑰對與現有帳戶關聯。

注意:使用帶有 publicKey 引數的 create() 可能會被伺服器上設定的 publickey-credentials-create 許可權策略阻止。

返回值

一個 Promise,它將解析為以下之一:

如果無法建立憑證物件,則 Promise 將以 null 解析。

異常

TypeError

PasswordCredential 建立請求的情況下,未提供 idoriginpassword(為空)。

NotAllowedError DOMException

可能的原因包括:

AbortError DOMException

操作已被中止。

示例

建立密碼憑證

此示例使用 PasswordCredentialInit 物件建立密碼憑證。

js
const credInit = {
  id: "1234",
  name: "Serpentina",
  origin: "https://example.org",
  password: "the last visible dog",
};

const makeCredential = document.querySelector("#make-credential");

makeCredential.addEventListener("click", async () => {
  const cred = await navigator.credentials.create({
    password: credInit,
  });
  console.log(cred.name);
  // Serpentina
  console.log(cred.password);
  // the last visible dog
});

建立聯合身份憑證

此示例使用 FederatedCredentialInit 物件建立聯合身份憑證。

js
const credInit = {
  id: "1234",
  name: "Serpentina",
  origin: "https://example.org",
  protocol: "openidconnect",
  provider: "https://provider.example.org",
};

const makeCredential = document.querySelector("#make-credential");

makeCredential.addEventListener("click", async () => {
  const cred = await navigator.credentials.create({
    federated: credInit,
  });
  console.log(cred.name);
  console.log(cred.provider);
});

建立公鑰憑證

此示例使用 PublicKeyCredentialCreationOptions 物件建立公鑰憑證。

js
const publicKey = {
  challenge: challengeFromServer,
  rp: { id: "acme.com", name: "ACME Corporation" },
  user: {
    id: new Uint8Array([79, 252, 83, 72, 214, 7, 89, 26]),
    name: "jamiedoe",
    displayName: "Jamie Doe",
  },
  pubKeyCredParams: [{ type: "public-key", alg: -7 }],
};

const publicKeyCredential = await navigator.credentials.create({ publicKey });

如果成功,create() 呼叫將返回一個 Promise,該 Promise 解析為一個 PublicKeyCredential 物件例項,該例項代表一個公鑰憑證,以後可透過 WebAuthn get() 呼叫用於對使用者進行身份驗證。其 PublicKeyCredential.response 屬性包含一個 AuthenticatorAttestationResponse 物件,該物件提供了對幾個有用資訊的訪問,包括身份驗證器資料、公鑰、傳輸機制等。

js
navigator.credentials.create({ publicKey }).then((publicKeyCredential) => {
  const response = publicKeyCredential.response;

  // Access attestationObject ArrayBuffer
  const attestationObj = response.attestationObject;

  // Access client JSON
  const clientJSON = response.clientDataJSON;

  // Return authenticator data ArrayBuffer
  const authenticatorData = response.getAuthenticatorData();

  // Return public key ArrayBuffer
  const pk = response.getPublicKey();

  // Return public key algorithm identifier
  const pkAlgo = response.getPublicKeyAlgorithm();

  // Return permissible transports array
  const transports = response.getTransports();
});

其中一些資料需要在伺服器上儲存,以便將來對該憑證進行身份驗證操作 — 例如公鑰、使用的演算法以及允許的傳輸方式。

注意:有關整個流程如何工作的更多資訊,請參閱 建立金鑰對並註冊使用者

規範

規範
Credential Management Level 1
# dom-credentialscontainer-create

瀏覽器相容性