2023-06-08 09:27:03 +02:00
|
|
|
document.addEventListener(
|
|
|
|
"DOMContentLoaded",
|
2025-03-07 10:51:39 +01:00
|
|
|
() => {
|
|
|
|
const form = document.getElementsByTagName("form")[0];
|
|
|
|
if (form) {
|
|
|
|
form.addEventListener("submit", (event) => {
|
|
|
|
event.preventDefault(); // Prevent the default form submission
|
|
|
|
checkWebauthnSupported(registerCredential);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-06-08 09:27:03 +02:00
|
|
|
);
|
2020-12-02 17:00:04 +01:00
|
|
|
|
2025-03-05 15:47:48 +01:00
|
|
|
async function registerCredential() {
|
2023-06-08 09:27:03 +02:00
|
|
|
document.getElementById("wa-error").classList.add("hidden");
|
2020-12-02 17:00:04 +01:00
|
|
|
|
2025-03-05 15:47:48 +01:00
|
|
|
let opt;
|
|
|
|
try {
|
2025-03-07 10:51:39 +01:00
|
|
|
opt = JSON.parse(window.atob(document.getElementsByName("credentialCreationData")[0].value));
|
2025-03-05 15:47:48 +01:00
|
|
|
} catch (e) {
|
|
|
|
webauthnError({ message: "Failed to parse credential creation data." });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
opt.publicKey.challenge = bufferDecode(opt.publicKey.challenge, "publicKey.challenge");
|
|
|
|
opt.publicKey.user.id = bufferDecode(opt.publicKey.user.id, "publicKey.user.id");
|
|
|
|
if (opt.publicKey.excludeCredentials) {
|
|
|
|
for (let i = 0; i < opt.publicKey.excludeCredentials.length; i++) {
|
|
|
|
if (opt.publicKey.excludeCredentials[i].id !== null) {
|
|
|
|
opt.publicKey.excludeCredentials[i].id = bufferDecode(opt.publicKey.excludeCredentials[i].id, "publicKey.excludeCredentials");
|
|
|
|
}
|
2023-06-08 09:27:03 +02:00
|
|
|
}
|
2020-12-02 17:00:04 +01:00
|
|
|
}
|
2025-03-05 15:47:48 +01:00
|
|
|
} catch (e) {
|
|
|
|
webauthnError({ message: "Failed to decode buffer data." });
|
|
|
|
return;
|
2023-06-08 09:27:03 +02:00
|
|
|
}
|
2025-03-05 15:47:48 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
const credential = await navigator.credentials.create({
|
2023-06-08 09:27:03 +02:00
|
|
|
publicKey: opt.publicKey,
|
2020-12-02 17:00:04 +01:00
|
|
|
});
|
2025-03-05 15:47:48 +01:00
|
|
|
|
|
|
|
createCredential(credential);
|
|
|
|
} catch (err) {
|
|
|
|
webauthnError(err);
|
|
|
|
}
|
2020-12-02 17:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function createCredential(newCredential) {
|
2023-06-08 09:27:03 +02:00
|
|
|
let attestationObject = new Uint8Array(
|
|
|
|
newCredential.response.attestationObject
|
|
|
|
);
|
|
|
|
let clientDataJSON = new Uint8Array(newCredential.response.clientDataJSON);
|
|
|
|
let rawId = new Uint8Array(newCredential.rawId);
|
2020-12-02 17:00:04 +01:00
|
|
|
|
2023-06-08 09:27:03 +02:00
|
|
|
let data = JSON.stringify({
|
|
|
|
id: newCredential.id,
|
|
|
|
rawId: bufferEncode(rawId),
|
|
|
|
type: newCredential.type,
|
|
|
|
response: {
|
|
|
|
attestationObject: bufferEncode(attestationObject),
|
|
|
|
clientDataJSON: bufferEncode(clientDataJSON),
|
|
|
|
},
|
|
|
|
});
|
2020-12-02 17:00:04 +01:00
|
|
|
|
2025-03-05 15:47:48 +01:00
|
|
|
document.getElementsByName("credentialData")[0].value = window.btoa(data);
|
2023-06-08 09:27:03 +02:00
|
|
|
document.getElementsByTagName("form")[0].submit();
|
|
|
|
}
|