summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2019-01-19 18:57:13 -0800
committerDaniel Mueller <deso@posteo.net>2019-01-19 18:57:13 -0800
commit0ab262382e9b7fd4c3f637cd831b6c1641846347 (patch)
tree84b1dec145d3ddd9e9b6fb2ad5dd36790ea71541
parente106613dbd94bf0580b873da0ad3c3751e156f24 (diff)
downloadnitrocli-0ab262382e9b7fd4c3f637cd831b6c1641846347.tar.gz
nitrocli-0ab262382e9b7fd4c3f637cd831b6c1641846347.tar.bz2
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.
-rw-r--r--nitrocli/src/pinentry.rs29
1 files 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<E>(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<String> {
+pub fn choose<E>(entry: &E) -> crate::Result<String>
+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)?;