feat: register outputs json

This commit is contained in:
2021-07-03 11:29:23 +08:00
parent 9c2cc1d3f3
commit a528985ffd
3 changed files with 106 additions and 19 deletions

View File

@@ -6,7 +6,7 @@ use authenticator::RegisterFlags;
use std::sync::mpsc::channel;
use crate::fido;
use crate::digest;
use crate::fido::U2fV2Challenge;
use crate::fido::{U2fV2Challenge, U2fRegistrationData};
pub struct CommandImpl;
@@ -17,6 +17,7 @@ impl Command for CommandImpl {
SubCommand::with_name(self.name()).about("Register subcommand")
.arg(Arg::with_name("app-id").long("app-id").default_value("https://example.com").help("App id"))
.arg(Arg::with_name("timeout").long("timeout").default_value("10").help("Timeout in seconds"))
.arg(Arg::with_name("json").long("json").help("JSON output"))
}
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
@@ -25,6 +26,7 @@ impl Command for CommandImpl {
Ok(t) => (t * 1000) as u64,
Err(e) => return simple_error!("Timeout should be a number: {}", e),
};
let json_output = sub_arg_matches.is_present("json");
let u2fv2_challenge = U2fV2Challenge::new_random(app_id);
let u2fv2_challenge_str = u2fv2_challenge.to_json();
@@ -57,24 +59,29 @@ impl Command for CommandImpl {
)?;
let register_result = register_rx.recv()?;
let (register_data, device_info) = register_result?;
success!("Device info: {}", &device_info);
success!("Register result: {}", base64::encode(&register_data));
let u2f_registration_data = U2fRegistrationData::from(app_id, &u2fv2_challenge_str, register_result?);
let client_data = &u2fv2_challenge_str;
let rr = match u2f::register::parse_registration(app_id.to_string(), client_data.as_bytes().to_vec(), register_data) {
Ok(rr) => rr,
Err(e) => return simple_error!("Parse registration data failed: {}", e),
};
if let Some(cert) = rr.attestation_cert {
success!("Certificate: {}", fido::to_pem(&cert, "CERTIFICATE", 64));
match u2f_registration_data {
Ok(data) => {
if json_output {
success!("{}", serde_json::to_string_pretty(&data).unwrap());
} else {
success!("Device info: {}", data.u2f_device_info);
if let Some(cert) = data.attestation_cert_pem {
success!("Certificate: {}", cert);
}
if let Some(device_name) = data.device_name {
success!("Device name: {}", device_name);
}
success!("Public key: {}", hex::encode(data.pub_key));
success!("Key handle: {}", hex::encode(data.key_handle));
}
}
Err(e) => {
return simple_error!("Parse registration data failed: {}", e);
}
}
if let Some(device_name) = rr.device_name {
success!("Device name: {}", device_name);
}
success!("Public key: {}", hex::encode(rr.pub_key));
success!("Key handle: {}", hex::encode(rr.key_handle));
Ok(())
}
}