let clientData = JSON.parse(
new TextDecoder().decode(publicKeyCredential.response.clientDataJSON),
);
console.log(clientData.origin);
// Chrome: chrome-extension://mabekielmoibbmlepeohhncklpnjmcpk
// Firefox: moz-extension://ngpncaopklanhjklijieoihgbhbgknjjdklmlpagjoaobbpmknfgmhgghbadgoaiUse Web Authn API in web extensions / notebook
Use Web Authn API in web extensions
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>WebAuthn Extension</title>
</head>
<body>
<h1>WebAuthn</h1>
<textarea id="optionsText" placeholder="Enter options JSON here"></textarea>
<button id="registerButton">Register (navigator.credentials.create)</button>
<button id="authButton">Authenticate (navigator.credentials.get)</button>
<script src="popup.js"></script>
</body>
</html>// Registration
async function register(optionsJSON) {
const options = JSON.parse(optionsJSON);
options.challenge = Uint8Array.fromBase64(options.challenge);
options.user.id = Uint8Array.fromBase64(options.user.id);
const credential = await navigator.credentials.create({ publicKey: options });
console.log("Credential created:", credential);
return credential;
}async function authenticate(optionsJSON) {
const options = JSON.parse(optionsJSON);
options.challenge = Uint8Array.fromBase64(options.challenge);
if (Array.isArray(options?.allowCredentials)) {
for (const ac of options.allowCredentials) {
ac.id = Uint8Array.fromBase64(ac.id);
}
}
const assertion = await navigator.credentials.get({ publicKey: options });
const clientDataJSON = new TextDecoder().decode(
assertion.response.clientDataJSON,
);
console.log("Client data:", clientDataJSON);
// The origin field in clientDataJSON will be the extension's origin
// (chrome-extension://... or moz-extension://...)
return assertion;
}