W3cubDocs

/Web APIs

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.

Credential PublicKeyCredential

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() or navigator.credentials.get() call completes.

PublicKeyCredential.id Read only Secure context

Inherited from Credential and overridden to be the base64url encoding of PublicKeyCredential.rawId.

PublicKeyCredential.rawId Read only Secure context

An ArrayBuffer that holds the globally unique identifier for this PublicKeyCredential. This identifier can be used to look up credentials for future calls to navigator.credentials.get().

PublicKeyCredential.response Read only Secure context

An instance of an AuthenticatorResponse object. It is either of type AuthenticatorAttestationResponse if the PublicKeyCredential was the results of a navigator.credentials.create() call, or of type AuthenticatorAssertionResponse if the PublicKeyCredential was the result of a navigator.credentials.get() call.

PublicKeyCredential.type Read only Secure context

Inherited from Credential. Always set to public-key for PublicKeyCredential instances.

Static methods

PublicKeyCredential.isConditionalMediationAvailable() Secure context

Returns a Promise which resolves to true if conditional mediation is available.

PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable() Secure context

Returns a Promise which resolves to true if an authenticator bound to the platform is capable of verifying the user.

PublicKeyCredential.parseCreationOptionsFromJSON() Experimental Secure context

Convenience method for deserializing server-sent credential registration data when registering a user with credentials.

PublicKeyCredential.parseRequestOptionsFromJSON() Experimental Secure context

Convenience method for deserializing server-sent credential request data when authenticating a (registered) user.

Instance methods

PublicKeyCredential.getClientExtensionResults() Secure context

If any extensions were requested, this method will return the results of processing those extensions.

PublicKeyCredential.toJSON() Experimental Secure context

Convenience method for creating a JSON string representation of a PublicKeyCredential for sending to the server when registering a user with credentials and authenticating a registered user.

Examples

Creating a new instance of PublicKeyCredential

Here, we use navigator.credentials.create() to generate a new credential.

js

const createCredentialOptions = {
  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);
  });

Getting an existing instance of PublicKeyCredential

Here, we fetch an existing credential from an authenticator, using navigator.credentials.get().

js

const requestCredentialOptions = {
  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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
PublicKeyCredential 67 18
60Only supports USB U2F tokens.
No 54 13 No 70 92
60–92Only supports USB U2F tokens.
49 13 10.0
authenticatorAttachment 98 98 No No 84 15.5 No 98 No 68 15.5 18.0
getClientExtensionResults 67 18
60Only supports USB U2F tokens.
No 54 13 No 70 92
60–92Only supports USB U2F tokens.
49 13 10.0
isConditionalMediationAvailable_static 108 108 No No 94 16 No 108 No 73 No 21.0
isUserVerifyingPlatformAuthenticatorAvailable_static 67 18
60Only supports USB U2F tokens.
No 54 13 No 70 92
60–92Only supports USB U2F tokens.
49 13 10.0
parseCreationOptionsFromJSON_static No No 119 No No No No No 119 No No No
parseRequestOptionsFromJSON_static No No 119 No No No No No 119 No No No
rawId 67 18
60Only supports USB U2F tokens.
No 54 13 No 70 92
60–92Only supports USB U2F tokens.
49 13 10.0
response 67 18
60Only supports USB U2F tokens.
No 54 13 No 70 92
60–92Only supports USB U2F tokens.
49 13 10.0
toJSON No No 119 No No No No No 119 No No No

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredential