fix(login): improve webauthn error handling (#9474)

This PR improves error handling around webauthn functions in the login.

(cherry picked from commit a82f5805b6)
This commit is contained in:
Max Peintner
2025-03-05 15:47:48 +01:00
committed by Livio Spring
parent 122b5f3e0e
commit 52bb9ca3a5
3 changed files with 68 additions and 50 deletions

View File

@@ -3,40 +3,41 @@ document.addEventListener(
checkWebauthnSupported("btn-register", registerCredential)
);
function registerCredential() {
async function registerCredential() {
document.getElementById("wa-error").classList.add("hidden");
let opt = JSON.parse(
atob(document.getElementsByName("credentialCreationData")[0].value)
);
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"
);
let opt;
try {
opt = JSON.parse(atob(document.getElementsByName("credentialCreationData")[0].value));
} 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");
}
}
}
} catch (e) {
webauthnError({ message: "Failed to decode buffer data." });
return;
}
navigator.credentials
.create({
try {
const credential = await navigator.credentials.create({
publicKey: opt.publicKey,
})
.then(function (credential) {
createCredential(credential);
})
.catch(function (err) {
webauthnError(err);
});
createCredential(credential);
} catch (err) {
webauthnError(err);
}
}
function createCredential(newCredential) {
@@ -56,6 +57,6 @@ function createCredential(newCredential) {
},
});
document.getElementsByName("credentialData")[0].value = btoa(data);
document.getElementsByName("credentialData")[0].value = window.btoa(data);
document.getElementsByTagName("form")[0].submit();
}