From 5578b57036cae94c5b1346323e1cb49c4b21d810 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 27 Mar 2022 11:01:22 +0800 Subject: [PATCH] chore: only print json when success --- src/chall.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/chall.rs b/src/chall.rs index 9b8e12f..afe4684 100644 --- a/src/chall.rs +++ b/src/chall.rs @@ -44,8 +44,6 @@ impl Command for CommandImpl { let mut yubi = Yubico::new(); - let mut json = BTreeMap::<&'_ str, String>::new(); - if let Ok(device) = yubi.find_yubikey() { success!("Found key, Vendor ID: {:?} Product ID {:?}", device.vendor_id, device.product_id); @@ -67,11 +65,14 @@ impl Command for CommandImpl { let hex_sha512 = iff!(sha512_output, Some(crate::digest::sha512_bytes(v)), None); if json_output { + let mut json = BTreeMap::<&'_ str, String>::new(); json.insert("challenge_hex", hex::encode(challenge_bytes)); json.insert("response_hex", hex_string); hex_sha256.map(|hex_sha256| json.insert("response_sha256_hex", hex::encode(hex_sha256))); hex_sha384.map(|hex_sha384| json.insert("response_sha384_hex", hex::encode(hex_sha384))); hex_sha512.map(|hex_sha512| json.insert("response_sha512_hex", hex::encode(hex_sha512))); + + println!("{}", serde_json::to_string_pretty(&json).unwrap()); } else { success!("Challenge HEX: {}", hex::encode(challenge_bytes)); success!("Response HEX: {}", hex_string); @@ -83,10 +84,6 @@ impl Command for CommandImpl { warning!("Yubikey not found"); } - if json_output { - println!("{}", serde_json::to_string_pretty(&json).unwrap()); - } - Ok(None) } }