Skip to content

Commit

Permalink
Merge otp_set function into commands.rs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
d-e-s-o committed Apr 10, 2020
1 parent 95358de commit eae8b2a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/arg_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]}
Expand Down
19 changes: 0 additions & 19 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> Result<()> {
use structopt::StructOpt;
Expand Down
27 changes: 15 additions & 12 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// *************************************************************************

use std::fmt;
use std::mem;
use std::result;
use std::thread;
use std::time;
Expand Down Expand Up @@ -694,16 +695,18 @@ fn prepare_base32_secret(secret: &str) -> Result<String> {
}

/// 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 => {
Expand All @@ -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(())
Expand Down

0 comments on commit eae8b2a

Please sign in to comment.