diff --git a/src/config/tfa.rs b/src/config/tfa.rs index 29e0fb48..7c656d20 100644 --- a/src/config/tfa.rs +++ b/src/config/tfa.rs @@ -803,9 +803,20 @@ impl TfaUserData { userid: &Userid, description: String, ) -> Result { + let cred_ids: Vec<_> = self + .enabled_webauthn_entries() + .map(|cred| cred.cred_id.clone()) + .collect(); + let userid_str = userid.to_string(); - let (challenge, state) = webauthn - .generate_challenge_register(&userid_str, Some(UserVerificationPolicy::Discouraged))?; + let (challenge, state) = webauthn.generate_challenge_register_options( + userid_str.as_bytes().to_vec(), + userid_str.clone(), + userid_str.clone(), + Some(cred_ids), + Some(UserVerificationPolicy::Discouraged), + )?; + let challenge_string = challenge.public_key.challenge.to_string(); let challenge = serde_json::to_string(&challenge)?; diff --git a/www/window/AddWebauthn.js b/www/window/AddWebauthn.js index 16731a63..f6d1df61 100644 --- a/www/window/AddWebauthn.js +++ b/www/window/AddWebauthn.js @@ -82,13 +82,38 @@ Ext.define('PBS.window.AddWebauthn', { challenge_obj.publicKey.user.id = PBS.Utils.base64url_to_bytes(challenge_obj.publicKey.user.id); + // convert existing authenticators structure + challenge_obj.publicKey.excludeCredentials = + (challenge_obj.publicKey.excludeCredentials || []).map((cred) => ({ + id: PBS.Utils.base64url_to_bytes(cred.id), + type: cred.type, + })); + let msg = Ext.Msg.show({ title: `Webauthn: ${gettext('Setup')}`, message: gettext('Please press the button on your Webauthn Device'), buttons: [], }); - let token_response = await navigator.credentials.create(challenge_obj); + let token_response; + try { + token_response = await navigator.credentials.create(challenge_obj); + } catch (error) { + let errmsg = ` + ${error.name}: ${error.message}`; + if (error.name === 'InvalidStateError') { + // probably a duplicate token + throw `${gettext('There was an error during authenticator registration.')} +
+ ${gettext('This probably means that this authenticator is already registered.')} +

+ ${errmsg}`; + } else { + throw `${gettext('There was an error during token registration.')} +

+ ${errmsg}`; + } + } // We cannot pass ArrayBuffers to the API, so extract & convert the data. let response = {