From eae8b2a1f0d1faf70776fccc12c93267a2143021 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Fri, 10 Apr 2020 12:20:18 -0700 Subject: Merge otp_set function into commands.rs Now that we have isolated and separated out structopt specific definitions into a new module, arg_defs, args.rs is actually so small that we do not want to keep it around any longer. This change marks a first step moving us closer towards removing it. Specifically, it merges the otp_set function from args.rs into the function of the same name in commands.rs. --- src/arg_defs.rs | 2 +- src/args.rs | 19 ------------------- src/commands.rs | 27 +++++++++++++++------------ 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/arg_defs.rs b/src/arg_defs.rs index c63fd82..fd296e4 100644 --- a/src/arg_defs.rs +++ b/src/arg_defs.rs @@ -222,7 +222,7 @@ Command! {OtpCommand, [ crate::commands::otp_get(ctx, args.slot, args.algorithm, args.time) }, /// Configures a one-time password slot - Set(OtpSetArgs) => crate::args::otp_set, + Set(OtpSetArgs) => crate::commands::otp_set, /// Prints the status of the one-time password slots Status(OtpStatusArgs) => |ctx, args: OtpStatusArgs| crate::commands::otp_status(ctx, args.all), ]} diff --git a/src/args.rs b/src/args.rs index 720fabf..3a1f2c8 100644 --- a/src/args.rs +++ b/src/args.rs @@ -82,25 +82,6 @@ pub fn config_set(ctx: &mut ExecCtx<'_>, args: arg_defs::ConfigSetArgs) -> Resul commands::config_set(ctx, numlock, capslock, scrollock, otp_pin) } -pub fn otp_set(ctx: &mut ExecCtx<'_>, args: arg_defs::OtpSetArgs) -> Result<()> { - let data = nitrokey::OtpSlotData { - number: args.slot, - name: args.name, - secret: args.secret, - mode: args.digits.into(), - use_enter: false, - token_id: None, - }; - commands::otp_set( - ctx, - data, - args.algorithm, - args.counter, - args.time_window, - args.format, - ) -} - /// Parse the command-line arguments and execute the selected command. pub(crate) fn handle_arguments(ctx: &mut RunCtx<'_>, args: Vec) -> Result<()> { use structopt::StructOpt; diff --git a/src/commands.rs b/src/commands.rs index 5b5fce1..55e6707 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -18,6 +18,7 @@ // ************************************************************************* use std::fmt; +use std::mem; use std::result; use std::thread; use std::time; @@ -694,16 +695,18 @@ fn prepare_base32_secret(secret: &str) -> Result { } /// Configure a one-time password slot on the Nitrokey device. -pub fn otp_set( - ctx: &mut args::ExecCtx<'_>, - mut data: nitrokey::OtpSlotData, - algorithm: arg_defs::OtpAlgorithm, - counter: u64, - time_window: u16, - secret_format: arg_defs::OtpSecretFormat, -) -> Result<()> { +pub fn otp_set(ctx: &mut args::ExecCtx<'_>, mut args: arg_defs::OtpSetArgs) -> Result<()> { + let mut data = nitrokey::OtpSlotData { + number: args.slot, + name: mem::take(&mut args.name), + secret: mem::take(&mut args.secret), + mode: args.digits.into(), + use_enter: false, + token_id: None, + }; + with_device(ctx, |ctx, device| { - let secret = match secret_format { + let secret = match args.format { arg_defs::OtpSecretFormat::Ascii => prepare_ascii_secret(&data.secret)?, arg_defs::OtpSecretFormat::Base32 => prepare_base32_secret(&data.secret)?, arg_defs::OtpSecretFormat::Hex => { @@ -721,9 +724,9 @@ pub fn otp_set( }; let data = nitrokey::OtpSlotData { secret, ..data }; let mut device = authenticate_admin(ctx, device)?; - match algorithm { - arg_defs::OtpAlgorithm::Hotp => device.write_hotp_slot(data, counter), - arg_defs::OtpAlgorithm::Totp => device.write_totp_slot(data, time_window), + match args.algorithm { + arg_defs::OtpAlgorithm::Hotp => device.write_hotp_slot(data, args.counter), + arg_defs::OtpAlgorithm::Totp => device.write_totp_slot(data, args.time_window), } .map_err(|err| get_error("Could not write OTP slot", err))?; Ok(()) -- cgit v1.2.1