PublicKeyCredential
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
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 Credential
, and is part of the Web Authentication API extension to the Credential Management API.
Note: This API is restricted to top-level contexts. Use from within an <iframe>
element will not have any effect.
Instance properties
PublicKeyCredential.authenticatorAttachment
Read only Secure context-
A string that indicates the mechanism by which the WebAuthn implementation is attached to the authenticator at the time the associated
navigator.credentials.create()
ornavigator.credentials.get()
call completes. PublicKeyCredential.id
Read only Secure context-
Inherited from
Credential
and overridden to be the base64url encoding ofPublicKeyCredential.rawId
. PublicKeyCredential.rawId
Read only Secure context-
An
ArrayBuffer
that holds the globally unique identifier for thisPublicKeyCredential
. This identifier can be used to look up credentials for future calls tonavigator.credentials.get()
. PublicKeyCredential.response
Read only Secure context-
An instance of an
AuthenticatorResponse
object. It is either of typeAuthenticatorAttestationResponse
if thePublicKeyCredential
was the results of anavigator.credentials.create()
call, or of typeAuthenticatorAssertionResponse
if thePublicKeyCredential
was the result of anavigator.credentials.get()
call. PublicKeyCredential.type
Read only Secure context-
Inherited from
Credential
. Always set topublic-key
forPublicKeyCredential
instances.
Static methods
-
Returns a
Promise
which resolves totrue
if conditional mediation is available. PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable()
Secure context-
Returns a
Promise
which resolves totrue
if an authenticator bound to the platform is capable of verifying the user.
Instance methods
PublicKeyCredential.getClientExtensionResults()
Secure context-
If any extensions were requested, this method will return the results of processing those extensions.
Examples
Creating a new instance of PublicKeyCredential
Here, we use navigator.credentials.create()
to generate a new credential.
js
const 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({ publicKey })
.then((newCredentialInfo) => {
const response = newCredentialInfo.response;
const clientExtensionsResults =
newCredentialInfo.getClientExtensionResults();
})
.catch((err) => {
console.error(err);
});
Getting an existing instance of PublicKeyCredential
Here, we fetch an existing credential from an authenticator, using navigator.credentials.get()
.
js
const options = {
challenge: new Uint8Array([
/* bytes sent from the server */
]),
};
navigator.credentials
.get({ publicKey: options })
.then((credentialInfoAssertion) => {
// send assertion response back to the server
// to proceed with the control of the credential
})
.catch((err) => {
console.error(err);
});
Specifications
Specification |
---|
Web Authentication: An API for accessing Public Key Credentials - Level 3 # iface-pkcredential |
Browser compatibility
BCD tables only load in the browser
See also
- The parent interface
Credential