From 50b4c6bf1bc69ca669593d9d925324e3522651dd Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Sat, 19 Jan 2019 19:04:35 -0800 Subject: 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. --- nitrocli/src/pinentry.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'nitrocli/src/pinentry.rs') 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 { + 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(response: R) -> Result<(), Error> where R: AsRef, -- cgit v1.2.1