summaryrefslogtreecommitdiff
path: root/src/commands.rs
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2020-04-10 12:20:18 -0700
committerDaniel Mueller <deso@posteo.net>2020-04-10 12:20:18 -0700
commiteae8b2a1f0d1faf70776fccc12c93267a2143021 (patch)
tree57285013a7efa36ebeac9a25300412a731eb355d /src/commands.rs
parent95358de8ec4e2c9fb7254d65fb9147fb118e2f78 (diff)
downloadnitrocli-eae8b2a1f0d1faf70776fccc12c93267a2143021.tar.gz
nitrocli-eae8b2a1f0d1faf70776fccc12c93267a2143021.tar.bz2
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.
Diffstat (limited to 'src/commands.rs')
-rw-r--r--src/commands.rs27
1 files changed, 15 insertions, 12 deletions
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<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 => {
@@ -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(())