The parseCreationOptionsFromJSON()
static method of the PublicKeyCredential
interface converts a JSON type representation into its corresponding publicKey
create credentials options object structure.
The method is a convenience function for converting credential options information provided by a relying party server to the form that a web app can use to create a credential.
PublicKeyCredential.parseCreationOptionsFromJSON(options)
The Web Authentication process for creating a key pair and registering a user involves a relying party server sending the web app information needed to create a credential, including details about the user identity, the relying party, and a "challenge". The web app passes this information to an authenticator to create the credential, by calling navigator.credentials.create()
with an argument that contains the server-supplied data in the publicKey
create credentials options object structure.
The specification does not define how the information needed for creating a credential is sent. A convenient approach is for the server to encapsulate the information in a JSON type representation of the publicKey
create credentials options object that mirrors its structure but encodes buffer properties such as the challenge
and user.id
as base64url strings. This object can be serialized to a JSON string, sent to the web app and deserialized, and then converted to the publicKey
create credentials options object structure using parseCreationOptionsFromJSON()
.
When registering a new user, a relying party server will supply information about the expected credentials to the web app. The code below defines this information in the form described in the options
parameter above (taken from the "getting an AuthenticatorAttestationResponse" in AuthenticatorResponse
):
const createCredentialOptionsJSON = {
challenge:
"21, 31, 105, " ,
rp: {
name: "Example CORP",
id: "login.example.com",
},
user: {
id: "16",
name: "[email protected]",
displayName: "Carina Anand",
},
pubKeyCredParams: [
{
type: "public-key",
alg: -7,
},
],
};
Because this object only uses JSON data types, it can be be serialized to JSON using JSON.stringify()
and sent to the web app.
JSON.stringify(createCredentialOptionsJSON);
The web app can deserialize the JSON string back to a createCredentialOptionsJSON
object (not shown). The parseCreationOptionsFromJSON()
method is used to convert that object to the form that can be used in navigator.credentials.create()
:
const createCredentialOptions =
PublicKeyCredential.parseCreationOptionsFromJSON(
createCredentialOptionsJSON,
);
navigator.credentials
.create({ createCredentialOptions })
.then((newCredentialInfo) => {
})
.catch((err) => {
console.error(err);
});