docs.rodeo

MDN Web Docs mirror

PublicKeyCredentialCreationOptions

{{APIRef("Web Authentication API")}} {{securecontext_header}} 

The PublicKeyCredentialCreationOptions dictionary represents the object passed to {{domxref("CredentialsContainer.create()")}}  as the value of the publicKey option: that is, when using create() to create a public key credential using the Web Authentication API.

Instance properties

Examples

Creating a public key credential

This example creates a PublicKeyCredentialCreationOptions, specifying only the required properties, and using defaults for the rest.

It then passes the object into navigator.credentials.create(), to create a new public key credential.

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 });

A successful create() call returns a promise that resolves with a {{domxref("PublicKeyCredential")}}  object instance, representing a public key credential that can later be used to authenticate a user via a WebAuthn {{domxref("CredentialsContainer.get()", "get()")}}  call. Its {{domxref("PublicKeyCredential.response")}}  property contains an {{domxref("AuthenticatorAttestationResponse")}}  object providing access to several useful pieces of information including the authenticator data, public key, transport mechanisms, and more.

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();
});

Some of this data will need to be stored on the server for future authentication operations against this credential — for example the public key, the algorithm used, and the permissible transports.

See Creating a key pair and registering a user for more information about how the overall flow works.

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

In this article

View on MDN