From 0ab262382e9b7fd4c3f637cd831b6c1641846347 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Sat, 19 Jan 2019 18:57:13 -0800 Subject: Add minimum length to SecretEntry trait Another commonality between a password and a PIN is that they typically both have a minimum length. To accommodate for this requirement, this change introduces another method to the SecretEntry trait that represents the secret's minimum character length. --- nitrocli/src/pinentry.rs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/nitrocli/src/pinentry.rs b/nitrocli/src/pinentry.rs index 1a3982e..ac978fc 100644 --- a/nitrocli/src/pinentry.rs +++ b/nitrocli/src/pinentry.rs @@ -43,6 +43,8 @@ pub trait SecretEntry: fmt::Debug { fn prompt(&self) -> CowStr; /// The description to display when asking for the secret. fn description(&self, mode: Mode) -> CowStr; + /// The minimum number of characters the secret needs to have. + fn min_len(&self) -> u8; } #[derive(Debug)] @@ -110,6 +112,13 @@ impl SecretEntry for PinEntry { ) .into() } + + fn min_len(&self) -> u8 { + match self.pin_type { + PinType::Admin => 8, + PinType::User => 6, + } + } } /// Secret entry mode for pinentry. @@ -199,26 +208,28 @@ where parse_pinentry_pin(str::from_utf8(&output.stdout)?) } -fn check(pin_type: PinType, secret: &str) -> crate::Result<()> { - let minimum_length = match pin_type { - PinType::Admin => 8, - PinType::User => 6, - }; - if secret.len() < minimum_length { +fn check(entry: &E, secret: &str) -> crate::Result<()> +where + E: SecretEntry, +{ + if secret.len() < usize::from(entry.min_len()) { Err(Error::Error(format!( "The secret must be at least {} characters long", - minimum_length + entry.min_len() ))) } else { Ok(()) } } -pub fn choose(entry: &PinEntry) -> crate::Result { +pub fn choose(entry: &E) -> crate::Result +where + E: SecretEntry, +{ clear(entry)?; let chosen = inquire(entry, Mode::Choose, None)?; clear(entry)?; - check(entry.pin_type(), &chosen)?; + check(entry, &chosen)?; let confirmed = inquire(entry, Mode::Confirm, None)?; clear(entry)?; -- cgit v1.2.3