|
| 1 | +use std::error::Error; |
| 2 | +use std::fmt::Display; |
| 3 | +use std::time::Duration; |
| 4 | + |
| 5 | +use libwebauthn::management::AuthenticatorConfig; |
| 6 | +use libwebauthn::pin::{PinProvider, StdinPromptPinProvider}; |
| 7 | +use libwebauthn::proto::ctap2::{Ctap2, Ctap2GetInfoResponse}; |
| 8 | +use libwebauthn::transport::hid::list_devices; |
| 9 | +use libwebauthn::transport::Device; |
| 10 | +use libwebauthn::webauthn::Error as WebAuthnError; |
| 11 | +use std::io::{self, Write}; |
| 12 | +use text_io::read; |
| 13 | +use tracing_subscriber::{self, EnvFilter}; |
| 14 | + |
| 15 | +const TIMEOUT: Duration = Duration::from_secs(10); |
| 16 | + |
| 17 | +fn setup_logging() { |
| 18 | + tracing_subscriber::fmt() |
| 19 | + .with_env_filter(EnvFilter::from_default_env()) |
| 20 | + .without_time() |
| 21 | + .init(); |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 25 | +enum Operation { |
| 26 | + ToggleAlwaysUv, |
| 27 | + EnableForceChangePin, |
| 28 | + DisableForceChangePin, |
| 29 | + SetMinPinLength(Option<u32>), |
| 30 | + SetMinPinLengthRpids, |
| 31 | + EnableEnterpriseAttestation, |
| 32 | +} |
| 33 | + |
| 34 | +impl Display for Operation { |
| 35 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 36 | + match self { |
| 37 | + Operation::ToggleAlwaysUv => f.write_str("Toggle AlwaysUV"), |
| 38 | + Operation::EnableForceChangePin => f.write_str("Enable force change pin"), |
| 39 | + Operation::DisableForceChangePin => f.write_str("Disable force change pin"), |
| 40 | + Operation::SetMinPinLength(l) => { |
| 41 | + if let Some(length) = l { |
| 42 | + f.write_fmt(format_args!("Set min PIN length. Current length: {length}")) |
| 43 | + } else { |
| 44 | + f.write_str("Set min PIN length.") |
| 45 | + } |
| 46 | + } |
| 47 | + Operation::SetMinPinLengthRpids => f.write_str("Set min PIN length RPIDs"), |
| 48 | + Operation::EnableEnterpriseAttestation => f.write_str("Enable enterprise attestation"), |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fn get_supported_options(info: &Ctap2GetInfoResponse) -> Vec<Operation> { |
| 54 | + let mut configure_ops = vec![]; |
| 55 | + if let Some(options) = &info.options { |
| 56 | + if options.get("authnrCfg") == Some(&true) && options.get("alwaysUv").is_some() { |
| 57 | + configure_ops.push(Operation::ToggleAlwaysUv); |
| 58 | + } |
| 59 | + if options.get("authnrCfg") == Some(&true) && options.get("setMinPINLength").is_some() { |
| 60 | + if info.force_pin_change == Some(true) { |
| 61 | + configure_ops.push(Operation::DisableForceChangePin); |
| 62 | + } else { |
| 63 | + configure_ops.push(Operation::EnableForceChangePin); |
| 64 | + } |
| 65 | + configure_ops.push(Operation::SetMinPinLength(info.min_pin_length)); |
| 66 | + configure_ops.push(Operation::SetMinPinLengthRpids); |
| 67 | + } |
| 68 | + if options.get("ep").is_some() { |
| 69 | + configure_ops.push(Operation::EnableEnterpriseAttestation); |
| 70 | + } |
| 71 | + } |
| 72 | + configure_ops |
| 73 | +} |
| 74 | + |
| 75 | +#[tokio::main] |
| 76 | +pub async fn main() -> Result<(), Box<dyn Error>> { |
| 77 | + setup_logging(); |
| 78 | + |
| 79 | + let devices = list_devices().await.unwrap(); |
| 80 | + println!("Devices found: {:?}", devices); |
| 81 | + let pin_provider: Box<dyn PinProvider> = Box::new(StdinPromptPinProvider::new()); |
| 82 | + |
| 83 | + for mut device in devices { |
| 84 | + println!("Selected HID authenticator: {}", &device); |
| 85 | + device.wink(TIMEOUT).await?; |
| 86 | + |
| 87 | + let mut channel = device.channel().await?; |
| 88 | + let info = channel.ctap2_get_info().await?; |
| 89 | + let options = get_supported_options(&info); |
| 90 | + |
| 91 | + println!("What do you want to do?"); |
| 92 | + println!(); |
| 93 | + for (idx, op) in options.iter().enumerate() { |
| 94 | + println!("({idx}) {op}"); |
| 95 | + } |
| 96 | + |
| 97 | + let idx = loop { |
| 98 | + print!("Your choice: "); |
| 99 | + io::stdout().flush().expect("Failed to flush stdout!"); |
| 100 | + let input: String = read!("{}\n"); |
| 101 | + if let Ok(idx) = input.trim().parse::<usize>() { |
| 102 | + if idx < options.len() { |
| 103 | + println!(); |
| 104 | + break idx; |
| 105 | + } |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + let mut min_pin_length_rpids = Vec::new(); |
| 110 | + if options[idx] == Operation::SetMinPinLengthRpids { |
| 111 | + loop { |
| 112 | + print!("Add RPIDs to list (enter empty string once finished): "); |
| 113 | + io::stdout().flush().expect("Failed to flush stdout!"); |
| 114 | + let input: String = read!("{}\n"); |
| 115 | + let trimmed = input.trim().to_string(); |
| 116 | + if trimmed.is_empty() { |
| 117 | + break; |
| 118 | + } else { |
| 119 | + min_pin_length_rpids.push(trimmed); |
| 120 | + } |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + let new_pin_length = if matches!(options[idx], Operation::SetMinPinLength(..)) { |
| 125 | + loop { |
| 126 | + print!("New minimum PIN length: "); |
| 127 | + io::stdout().flush().expect("Failed to flush stdout!"); |
| 128 | + let input: String = read!("{}\n"); |
| 129 | + match input.trim().parse::<u64>() { |
| 130 | + Ok(l) => { |
| 131 | + break l; |
| 132 | + } |
| 133 | + Err(_) => continue, |
| 134 | + } |
| 135 | + } |
| 136 | + } else { |
| 137 | + 0 |
| 138 | + }; |
| 139 | + |
| 140 | + loop { |
| 141 | + let action = match options[idx] { |
| 142 | + Operation::ToggleAlwaysUv => channel.toggle_always_uv(&pin_provider, TIMEOUT), |
| 143 | + Operation::SetMinPinLengthRpids => channel.set_min_pin_length_rpids( |
| 144 | + min_pin_length_rpids.clone(), |
| 145 | + &pin_provider, |
| 146 | + TIMEOUT, |
| 147 | + ), |
| 148 | + Operation::SetMinPinLength(..) => { |
| 149 | + channel.set_min_pin_length(new_pin_length, &pin_provider, TIMEOUT) |
| 150 | + } |
| 151 | + Operation::EnableEnterpriseAttestation => { |
| 152 | + channel.enable_enterprise_attestation(&pin_provider, TIMEOUT) |
| 153 | + } |
| 154 | + Operation::EnableForceChangePin => { |
| 155 | + channel.force_change_pin(true, &pin_provider, TIMEOUT) |
| 156 | + } |
| 157 | + Operation::DisableForceChangePin => { |
| 158 | + channel.force_change_pin(false, &pin_provider, TIMEOUT) |
| 159 | + } |
| 160 | + }; |
| 161 | + match action.await { |
| 162 | + Ok(_) => break Ok(()), |
| 163 | + Err(WebAuthnError::Ctap(ctap_error)) => { |
| 164 | + if ctap_error.is_retryable_user_error() { |
| 165 | + println!("Oops, try again! Error: {}", ctap_error); |
| 166 | + continue; |
| 167 | + } |
| 168 | + break Err(WebAuthnError::Ctap(ctap_error)); |
| 169 | + } |
| 170 | + Err(err) => break Err(err), |
| 171 | + }; |
| 172 | + } |
| 173 | + .unwrap(); |
| 174 | + println!("Authenticator config done!"); |
| 175 | + } |
| 176 | + |
| 177 | + Ok(()) |
| 178 | +} |
0 commit comments