diff options
author | Daniel Mueller <deso@posteo.net> | 2019-01-19 19:04:35 -0800 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2019-01-19 19:04:35 -0800 |
commit | 50b4c6bf1bc69ca669593d9d925324e3522651dd (patch) | |
tree | 7e410c7f98c1471917104505aa027b482fa7b9a9 | |
parent | b637dd7b4e2e8fccdfe212eb3e94bd5e2f6cd09e (diff) | |
download | nitrocli-50b4c6bf1bc69ca669593d9d925324e3522651dd.tar.gz nitrocli-50b4c6bf1bc69ca669593d9d925324e3522651dd.tar.bz2 |
Move PIN choosing functionality into pinentry module
The functionality we have in place for choosing a PIN can arguably be
moved into the pinentry module: it can be considered logic directly
related to working with PINs or secrets and that has no dependencies to
unrelated modules of the program.
This patch moves the choose_pin and check_pin functions into the
pinentry module.
-rw-r--r-- | nitrocli/src/commands.rs | 33 | ||||
-rw-r--r-- | nitrocli/src/pinentry.rs | 31 |
2 files changed, 32 insertions, 32 deletions
diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index 00a594f..7d698cc 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -590,37 +590,6 @@ pub fn pin_clear(ctx: &mut args::ExecCtx<'_>) -> Result<()> { Ok(()) } -fn check_pin(pin_type: pinentry::PinType, pin: &str) -> Result<()> { - let minimum_length = match pin_type { - pinentry::PinType::Admin => 8, - pinentry::PinType::User => 6, - }; - if pin.len() < minimum_length { - Err(Error::Error(format!( - "The PIN must be at least {} characters long", - minimum_length - ))) - } else { - Ok(()) - } -} - -fn choose_pin_with_pinentry(pin_entry: &pinentry::PinEntry) -> Result<String> { - pinentry::clear_pin(pin_entry)?; - let new_pin = pinentry::inquire_pin(pin_entry, pinentry::Mode::Choose, None)?; - pinentry::clear_pin(pin_entry)?; - check_pin(pin_entry.pin_type(), &new_pin)?; - - let confirm_pin = pinentry::inquire_pin(pin_entry, pinentry::Mode::Confirm, None)?; - pinentry::clear_pin(pin_entry)?; - - if new_pin != confirm_pin { - Err(Error::from("Entered PINs do not match")) - } else { - Ok(new_pin) - } -} - /// Choose a PIN of the given type. /// /// If the user has set the respective environment variable for the @@ -653,7 +622,7 @@ fn choose_pin( .ok_or_else(|| Error::from("Failed to read PIN: invalid Unicode data found")) .map(ToOwned::to_owned) } else { - choose_pin_with_pinentry(pin_entry) + pinentry::choose_pin(pin_entry) } } diff --git a/nitrocli/src/pinentry.rs b/nitrocli/src/pinentry.rs index d6f000c..e4d2b0d 100644 --- a/nitrocli/src/pinentry.rs +++ b/nitrocli/src/pinentry.rs @@ -181,6 +181,37 @@ pub fn inquire_pin( parse_pinentry_pin(str::from_utf8(&output.stdout)?) } +fn check_pin(pin_type: PinType, pin: &str) -> crate::Result<()> { + let minimum_length = match pin_type { + PinType::Admin => 8, + PinType::User => 6, + }; + if pin.len() < minimum_length { + Err(Error::Error(format!( + "The PIN must be at least {} characters long", + minimum_length + ))) + } else { + Ok(()) + } +} + +pub fn choose_pin(pin_entry: &PinEntry) -> crate::Result<String> { + clear_pin(pin_entry)?; + let new_pin = inquire_pin(pin_entry, Mode::Choose, None)?; + clear_pin(pin_entry)?; + check_pin(pin_entry.pin_type(), &new_pin)?; + + let confirm_pin = inquire_pin(pin_entry, Mode::Confirm, None)?; + clear_pin(pin_entry)?; + + if new_pin != confirm_pin { + Err(Error::from("Entered PINs do not match")) + } else { + Ok(new_pin) + } +} + fn parse_pinentry_response<R>(response: R) -> Result<(), Error> where R: AsRef<str>, |