files/en-us/web/api/publickeycredential/index.md
{{APIRef("Web Authentication API")}}{{securecontext_header}}
The PublicKeyCredential interface provides information about a public key / private key pair, which is a credential for logging in to a service using an un-phishable and data-breach resistant asymmetric key pair instead of a password. It inherits from {{domxref("Credential")}}, and is part of the Web Authentication API extension to the Credential Management API.
{{InheritanceDiagram}}
[!NOTE] This API is restricted to top-level contexts. Use from within an {{HTMLElement("iframe")}} element will not have any effect.
{{domxref("PublicKeyCredential.authenticatorAttachment")}} {{ReadOnlyInline}}
{{domxref("PublicKeyCredential.id")}} {{ReadOnlyInline}}
{{domxref("PublicKeyCredential.rawId")}} {{ReadOnlyInline}}
PublicKeyCredential. This identifier can be used to look up credentials for future calls to {{domxref("CredentialsContainer.get()","navigator.credentials.get()")}}.{{domxref("PublicKeyCredential.response")}} {{ReadOnlyInline}}
PublicKeyCredential was the results of a {{domxref("CredentialsContainer.create()","navigator.credentials.create()")}} call, or of type {{domxref("AuthenticatorAssertionResponse")}} if the PublicKeyCredential was the result of a {{domxref("CredentialsContainer.get()","navigator.credentials.get()")}} call.PublicKeyCredential.type {{ReadOnlyInline}}
public-key for PublicKeyCredential instances.true if conditional mediation is available.true if an authenticator bound to the platform is capable of verifying the user.PublicKeyCredential for sending to the server when registering a user with credentials and authenticating a registered user.Here, we use {{domxref("CredentialsContainer.create()","navigator.credentials.create()")}} to generate a new credential.
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: "[email protected]",
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);
});
Here, we fetch an existing credential from an authenticator, using {{domxref("CredentialsContainer.get()","navigator.credentials.get()")}}.
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);
});
{{Specifications}}
{{Compat}}