From eab4b20d16760043a1ec4d80d2e239fc1559bb2f Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Thu, 2 Apr 2020 08:52:45 -0700 Subject: Move PinType type into arg_defs module The PinType struct, despite being intended for the pinentry module, is ultimately part of the argument handling definitions. Because our goal is to consolidate all of those in a single file, this change moves the definition of this type from the pinentry module into the newly introduced arg_defs. --- src/arg_defs.rs | 15 ++++++++++++--- src/commands.rs | 36 ++++++++++++++++++------------------ src/pinentry.rs | 33 ++++++++++++--------------------- 3 files changed, 42 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/arg_defs.rs b/src/arg_defs.rs index d195841..0172a81 100644 --- a/src/arg_defs.rs +++ b/src/arg_defs.rs @@ -20,7 +20,6 @@ use crate::args; use crate::commands; use crate::error::Error; -use crate::pinentry; /// Provides access to a Nitrokey device #[derive(structopt::StructOpt)] @@ -331,11 +330,21 @@ Command! {PinCommand, [ Unblock => commands::pin_unblock, ]} +/// PIN type requested from pinentry. +/// +/// The available PIN types correspond to the PIN types used by the Nitrokey devices: user and +/// admin. +#[allow(unused_doc_comments)] +Enum! {PinType, [ + Admin => "admin", + User => "user", +]} + #[derive(Debug, PartialEq, structopt::StructOpt)] pub struct PinSetArgs { /// The PIN type to change - #[structopt(name = "type", possible_values = &pinentry::PinType::all_str())] - pub pintype: pinentry::PinType, + #[structopt(name = "type", possible_values = &PinType::all_str())] + pub pintype: PinType, } #[derive(Debug, PartialEq, structopt::StructOpt)] diff --git a/src/commands.rs b/src/commands.rs index 31f4c26..5b5fce1 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -108,7 +108,7 @@ where F: FnMut(&mut args::ExecCtx<'_>, nitrokey::PasswordSafe<'_, '_>) -> Result<()>, { with_device(ctx, |ctx, mut device| { - let pin_entry = pinentry::PinEntry::from(pinentry::PinType::User, &device)?; + let pin_entry = pinentry::PinEntry::from(arg_defs::PinType::User, &device)?; try_with_pin_and_data( ctx, &pin_entry, @@ -132,7 +132,7 @@ where fn authenticate<'mgr, D, A, F>( ctx: &mut args::ExecCtx<'_>, device: D, - pin_type: pinentry::PinType, + pin_type: arg_defs::PinType, msg: &'static str, op: F, ) -> Result @@ -156,7 +156,7 @@ where authenticate( ctx, device, - pinentry::PinType::User, + arg_defs::PinType::User, "Could not authenticate as user", |_ctx, device, pin| device.authenticate_user(pin), ) @@ -173,7 +173,7 @@ where authenticate( ctx, device, - pinentry::PinType::Admin, + arg_defs::PinType::Admin, "Could not authenticate as admin", |_ctx, device, pin| device.authenticate_admin(pin), ) @@ -261,8 +261,8 @@ where // Ideally we would not clone here, but that would require us to // restrict op to work with an immutable ExecCtx, which is not // possible given that some clients print data. - pinentry::PinType::Admin => ctx.admin_pin.clone(), - pinentry::PinType::User => ctx.user_pin.clone(), + arg_defs::PinType::Admin => ctx.admin_pin.clone(), + arg_defs::PinType::User => ctx.user_pin.clone(), }; if let Some(pin) = pin { @@ -418,7 +418,7 @@ pub fn list(ctx: &mut args::ExecCtx<'_>, no_connect: bool) -> Result<()> { /// Perform a factory reset. pub fn reset(ctx: &mut args::ExecCtx<'_>) -> Result<()> { with_device(ctx, |ctx, mut device| { - let pin_entry = pinentry::PinEntry::from(pinentry::PinType::Admin, &device)?; + let pin_entry = pinentry::PinEntry::from(arg_defs::PinType::Admin, &device)?; // To force the user to enter the admin PIN before performing a // factory reset, we clear the pinentry cache for the admin PIN. @@ -445,7 +445,7 @@ pub fn unencrypted_set( mode: arg_defs::UnencryptedVolumeMode, ) -> Result<()> { with_storage_device(ctx, |ctx, mut device| { - let pin_entry = pinentry::PinEntry::from(pinentry::PinType::Admin, &device)?; + let pin_entry = pinentry::PinEntry::from(arg_defs::PinType::Admin, &device)?; let mode = match mode { arg_defs::UnencryptedVolumeMode::ReadWrite => nitrokey::VolumeMode::ReadWrite, arg_defs::UnencryptedVolumeMode::ReadOnly => nitrokey::VolumeMode::ReadOnly, @@ -467,7 +467,7 @@ pub fn unencrypted_set( /// Open the encrypted volume on the Nitrokey. pub fn encrypted_open(ctx: &mut args::ExecCtx<'_>) -> Result<()> { with_storage_device(ctx, |ctx, mut device| { - let pin_entry = pinentry::PinEntry::from(pinentry::PinType::User, &device)?; + let pin_entry = pinentry::PinEntry::from(arg_defs::PinType::User, &device)?; // We may forcefully close a hidden volume, if active, so be sure to // flush caches to disk. @@ -795,10 +795,10 @@ pub fn otp_status(ctx: &mut args::ExecCtx<'_>, all: bool) -> Result<()> { pub fn pin_clear(ctx: &mut args::ExecCtx<'_>) -> Result<()> { with_device(ctx, |_ctx, device| { pinentry::clear(&pinentry::PinEntry::from( - pinentry::PinType::Admin, + arg_defs::PinType::Admin, &device, )?)?; - pinentry::clear(&pinentry::PinEntry::from(pinentry::PinType::User, &device)?)?; + pinentry::clear(&pinentry::PinEntry::from(arg_defs::PinType::User, &device)?)?; Ok(()) }) } @@ -813,14 +813,14 @@ fn choose_pin( new: bool, ) -> Result { let new_pin = match pin_entry.pin_type() { - pinentry::PinType::Admin => { + arg_defs::PinType::Admin => { if new { &ctx.new_admin_pin } else { &ctx.admin_pin } } - pinentry::PinType::User => { + arg_defs::PinType::User => { if new { &ctx.new_user_pin } else { @@ -840,7 +840,7 @@ fn choose_pin( } /// Change a PIN. -pub fn pin_set(ctx: &mut args::ExecCtx<'_>, pin_type: pinentry::PinType) -> Result<()> { +pub fn pin_set(ctx: &mut args::ExecCtx<'_>, pin_type: arg_defs::PinType) -> Result<()> { with_device(ctx, |ctx, mut device| { let pin_entry = pinentry::PinEntry::from(pin_type, &device)?; let new_pin = choose_pin(ctx, &pin_entry, true)?; @@ -850,8 +850,8 @@ pub fn pin_set(ctx: &mut args::ExecCtx<'_>, pin_type: pinentry::PinType) -> Resu &pin_entry, "Could not change the PIN", |current_pin| match pin_type { - pinentry::PinType::Admin => device.change_admin_pin(¤t_pin, &new_pin), - pinentry::PinType::User => device.change_user_pin(¤t_pin, &new_pin), + arg_defs::PinType::Admin => device.change_admin_pin(¤t_pin, &new_pin), + arg_defs::PinType::User => device.change_user_pin(¤t_pin, &new_pin), }, )?; @@ -865,9 +865,9 @@ pub fn pin_set(ctx: &mut args::ExecCtx<'_>, pin_type: pinentry::PinType) -> Resu /// Unblock and reset the user PIN. pub fn pin_unblock(ctx: &mut args::ExecCtx<'_>) -> Result<()> { with_device(ctx, |ctx, mut device| { - let pin_entry = pinentry::PinEntry::from(pinentry::PinType::User, &device)?; + let pin_entry = pinentry::PinEntry::from(arg_defs::PinType::User, &device)?; let user_pin = choose_pin(ctx, &pin_entry, false)?; - let pin_entry = pinentry::PinEntry::from(pinentry::PinType::Admin, &device)?; + let pin_entry = pinentry::PinEntry::from(arg_defs::PinType::Admin, &device)?; try_with_pin( ctx, diff --git a/src/pinentry.rs b/src/pinentry.rs index af2b4dc..878ed9e 100644 --- a/src/pinentry.rs +++ b/src/pinentry.rs @@ -23,21 +23,12 @@ use std::io; use std::process; use std::str; +use crate::arg_defs; use crate::args; use crate::error::Error; type CowStr = borrow::Cow<'static, str>; -/// PIN type requested from pinentry. -/// -/// The available PIN types correspond to the PIN types used by the Nitrokey devices: user and -/// admin. -#[allow(unused_doc_comments)] -Enum! {PinType, [ - Admin => "admin", - User => "user", -]} - /// A trait representing a secret to be entered by the user. pub trait SecretEntry: fmt::Debug { /// The cache ID to use for this secret. @@ -52,13 +43,13 @@ pub trait SecretEntry: fmt::Debug { #[derive(Debug)] pub struct PinEntry { - pin_type: PinType, + pin_type: arg_defs::PinType, model: nitrokey::Model, serial: nitrokey::SerialNumber, } impl PinEntry { - pub fn from<'mgr, D>(pin_type: PinType, device: &D) -> crate::Result + pub fn from<'mgr, D>(pin_type: arg_defs::PinType, device: &D) -> crate::Result where D: nitrokey::Device<'mgr>, { @@ -71,7 +62,7 @@ impl PinEntry { }) } - pub fn pin_type(&self) -> PinType { + pub fn pin_type(&self) -> arg_defs::PinType { self.pin_type } } @@ -81,16 +72,16 @@ impl SecretEntry for PinEntry { let model = self.model.to_string().to_lowercase(); let suffix = format!("{}:{}", model, self.serial); let cache_id = match self.pin_type { - PinType::Admin => format!("nitrocli:admin:{}", suffix), - PinType::User => format!("nitrocli:user:{}", suffix), + arg_defs::PinType::Admin => format!("nitrocli:admin:{}", suffix), + arg_defs::PinType::User => format!("nitrocli:user:{}", suffix), }; Some(cache_id.into()) } fn prompt(&self) -> CowStr { match self.pin_type { - PinType::Admin => "Admin PIN", - PinType::User => "User PIN", + arg_defs::PinType::Admin => "Admin PIN", + arg_defs::PinType::User => "User PIN", } .into() } @@ -99,12 +90,12 @@ impl SecretEntry for PinEntry { format!( "{} for\rNitrokey {} {}", match self.pin_type { - PinType::Admin => match mode { + arg_defs::PinType::Admin => match mode { Mode::Choose => "Please enter a new admin PIN", Mode::Confirm => "Please confirm the new admin PIN", Mode::Query => "Please enter the admin PIN", }, - PinType::User => match mode { + arg_defs::PinType::User => match mode { Mode::Choose => "Please enter a new user PIN", Mode::Confirm => "Please confirm the new user PIN", Mode::Query => "Please enter the user PIN", @@ -118,8 +109,8 @@ impl SecretEntry for PinEntry { fn min_len(&self) -> u8 { match self.pin_type { - PinType::Admin => 8, - PinType::User => 6, + arg_defs::PinType::Admin => 8, + arg_defs::PinType::User => 6, } } } -- cgit v1.2.3