liveBook · Manning

WebAuthn registration

As explained in Software Security for Developers

WebAuthn registration is the ceremony in which an authenticator creates a public key credential and the relying party stores its credential data.

WebAuthn registration creates a public-key credential through the browser and authenticator, then sends the credential data to the relying party for association with a user. The private key remains with the authenticator; the server stores the credential information needed to verify later responses. section 15.3

A basic registration flow proceeds as follows:

  • Offer a Register WebAuthn Credential action on a web page. section 15.3
  • Check browser support through window.PublicKeyCredential and stop with an error when WebAuthn is unavailable. section 15.3
  • Prepare creation options containing a challenge, relying-party name, user identity, public-key algorithm, authenticator preference, timeout, and attestation setting. section 15.3
  • Call navigator.credentials.create(), handle a missing credential, and collect the resulting credential data. section 15.3
  • Send and associate the registration details, including the credential ID and returned attestation or client data, with the account. section 15.3 section 15.3.1

A local passkey registration

Serve the page from localhost with the JDK jwebserver command, click Register WebAuthn Credential, and confirm the platform-authentication prompt. The browser then produces an attestation object and client data that can be displayed as Base64-encoded output and sent to a Java backend. section 15.3

Listing 15.4 Requesting creation of the public key
async function registerWebAuthn() {
  if (!window.PublicKeyCredential) {
     console.error("WebAuthn is not supported in this browser.");
     return;
  }

const publicKeyCredentialCreationOptions = {
     challenge: new Uint8Array(32), 
     rp: { name: "My WebAuthn App" }, 
     user: {
       id: new Uint8Array(16), 
       name: "user@example.com",
       displayName: "User Example"
     },
     pubKeyCredParams: [{ type: "public-key", alg: -7 }], 
     authenticatorSelection: { authenticatorAttachment: "platform" }, 
     timeout: 60000, // Timeout in milliseconds
     attestation: "direct"
  };

  try {
    const credential = await navigator.credentials
    .create({   #1
      publicKey: publicKeyCredentialCreationOptions
    });

    if (!credential) {   #2
      console.error("Credential creation failed.");
      return;
    }


    const attestationObject =   #3
       btoa(String.fromCharCode(...new 
         Uint8Array(credential.response.attestationObject)));
    const clientDataJSON = 
       btoa(String.fromCharCode(...new 
         Uint8Array(credential.response.clientDataJSON)));
    const credentialId = 
       btoa(String.fromCharCode(...new 
          Uint8Array(credential.rawId)));

    console.log("WebAuthn Registration Data:");
    console.log("Attestation Object:", attestationObject);
    console.log("Client Data JSON:", clientDataJSON);
    console.log("Credential ID:", credentialId);
   } catch (error) {
    console.error("WebAuthn registration failed:", error);            
   }  

}

Inspect the complete browser-side creation call, including the WebAuthn options, null-credential check, and asynchronous navigator.credentials.create() operation. section 15.3

How Software Security for Developers covers it

How deep it goes
Works through browser checks, creation options, credential creation, output handling, and backend storage, while deferring polished errors and production UX.
Examples
Uses a plain JavaScript page, localhost jwebserver, a platform passkey prompt, Base64 credential output, and a Java registration endpoint.
What it assumes
Assumes basic web-page and JavaScript familiarity while introducing browser support checks, credential creation, secure-context requirements, and backend association.
What you can do after
Build it from scratch with JavaScript and a simple Java backend that checks support, creates credentials, displays data, and associates credentials with users.
How it connects
Connects WebAuthn registration with authenticators, public-key credentials, relying parties, attestation, secure contexts, and WebAuthn authentication.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Security for Developers. The section links point to the parts of the book it draws on.