chore: reorg code

This commit is contained in:
2022-03-27 12:40:20 +08:00
parent b9dbcb8f80
commit 8ab2126034
11 changed files with 30 additions and 36 deletions

63
src/cmd_challconfig.rs Normal file
View File

@@ -0,0 +1,63 @@
use clap::{ArgMatches, SubCommand, App, Arg};
use yubico_manager::Yubico;
use yubico_manager::config::Config;
use yubico_manager::hmacmode::HmacKey;
use yubico_manager::configure::DeviceModeConfig;
use rust_util::util_clap::{Command, CommandError};
pub struct CommandImpl;
impl Command for CommandImpl {
fn name(&self) -> &str { "chall-config" }
fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("Yubikey challenge-response hmac config")
.arg(Arg::with_name("secret").short("s").long("secret").takes_value(true).help("Secret"))
.arg(Arg::with_name("secret-hex").short("x").long("secret-hex").takes_value(true).help("Secret HEX"))
.arg(Arg::with_name("yes-config-chall").long("yes-config-chall").help("Config chall key"))
}
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
if !sub_arg_matches.is_present("yes-config-chall") {
return simple_error!("--yes-config-chall is not configed");
}
let secret_bytes: Vec<u8>;
if let Some(secret) = sub_arg_matches.value_of("secret") {
secret_bytes = secret.as_bytes().to_vec();
} else if let Some(secret_hex) = sub_arg_matches.value_of("secret-hex") {
secret_bytes = hex::decode(secret_hex)?;
} else {
return simple_error!("Secret must assigned");
}
// Secret must have 20 bytes
if secret_bytes.len() != 20 {
return simple_error!("Challenge bytes is: {}, is not 20", secret_bytes.len());
}
let mut yubi = Yubico::new();
if let Ok(device) = yubi.find_yubikey() {
success!("Found key, Vendor ID: {:?} Product ID {:?}", device.vendor_id, device.product_id);
let config = Config::default()
.set_vendor_id(device.vendor_id)
.set_product_id(device.product_id)
.set_command(yubico_manager::config::Command::Configuration2);
let hmac_key: HmacKey = HmacKey::from_slice(&secret_bytes);
let mut device_config = DeviceModeConfig::default();
device_config.challenge_response_hmac(&hmac_key, false, false);
if let Err(err) = yubi.write_config(config, &mut device_config) {
failure!("{:?}", err);
} else {
success!("Device configured");
}
} else {
warning!("Yubikey not found");
}
Ok(None)
}
}