From 20be1c3ff7127f8bff5899c716b3445eaff4c74c Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Fri, 11 Jan 2019 03:33:47 +0000 Subject: Store the parsed data on a Nitrokey device --- src/main.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index ba5298d..bcd6db1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -188,6 +188,27 @@ fn query_name(label: &str, issuer: Option<&str>) -> Result { } } +fn store_data(slot: u8, name: &str, data: &UrlData) -> Result<(), Error> { + let slot = slot.to_string(); + let mut args = vec!["otp", "set", slot.as_ref(), name, data.secret.as_ref()]; + args.append(&mut vec!["--algorithm", data.otp_type.as_ref()]); + args.append(&mut vec!["--format", "base32"]); + // TODO: parse digits + if let Some(ref counter) = data.counter { + args.append(&mut vec!["--counter", counter]); + } + if let Some(ref period) = data.period { + args.append(&mut vec!["--time-window", period]); + } + + let status = process::Command::new("nitrocli").args(args).status()?; + if status.success() { + Ok(()) + } else { + Err(Error::from("Could not set OTP data using nitrocli")) + } +} + fn run(options: Options) -> Result<(), Error> { let path = match options.file { Some(ref file) => path::PathBuf::from(file), @@ -199,7 +220,7 @@ fn run(options: Options) -> Result<(), Error> { Some(name) => name, None => query_name(&url_data.label, url_data.issuer.as_ref().map(|x| &**x))?, }; - println!("{}: {:?}", name, url_data); + store_data(options.slot, &name, &url_data)?; if options.file.is_none() { fs::remove_file(&path)?; } -- cgit v1.2.3