aboutsummaryrefslogtreecommitdiff
path: root/nitrocli
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-07-14 13:42:42 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-07-14 13:49:47 +0000
commit784e3da6a345f9730a34227370a716a4940ff9c6 (patch)
treef5eb5a01521ce47f0a23eca5b1ac9c6be7c4105d /nitrocli
parent322853b9b4d7ab3338e10582f576a4cab7ac7969 (diff)
downloadnitrocli-784e3da6a345f9730a34227370a716a4940ff9c6.tar.gz
nitrocli-784e3da6a345f9730a34227370a716a4940ff9c6.tar.bz2
Introduce with_password_manager function
Diffstat (limited to 'nitrocli')
-rw-r--r--nitrocli/src/commands.rs81
1 files changed, 47 insertions, 34 deletions
diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs
index 05bf0b3..eb071ff 100644
--- a/nitrocli/src/commands.rs
+++ b/nitrocli/src/commands.rs
@@ -118,6 +118,15 @@ where
)
}
+fn with_password_safe<D, F>(ctx: &mut args::ExecCtx<'_>, device: &D, op: F) -> Result<()>
+where
+ D: Device,
+ F: FnOnce(&mut args::ExecCtx<'_>, nitrokey::PasswordSafe<'_>) -> Result<()>,
+{
+ let pws = get_password_safe(ctx, device)?;
+ op(ctx, pws)
+}
+
/// Authenticate the given device using the given PIN type and operation.
///
/// If an error occurs, the error message `msg` is used.
@@ -846,20 +855,21 @@ pub fn pws_get(
quiet: bool,
) -> Result<()> {
with_device(ctx, |ctx, device| {
- let pws = get_password_safe(ctx, &device)?;
- check_slot(&pws, slot)?;
+ with_password_safe(ctx, &device, |ctx, pws| {
+ check_slot(&pws, slot)?;
- let show_all = !show_name && !show_login && !show_password;
- if show_all || show_name {
- print_pws_data(ctx, "name: ", pws.get_slot_name(slot), quiet)?;
- }
- if show_all || show_login {
- print_pws_data(ctx, "login: ", pws.get_slot_login(slot), quiet)?;
- }
- if show_all || show_password {
- print_pws_data(ctx, "password:", pws.get_slot_password(slot), quiet)?;
- }
- Ok(())
+ let show_all = !show_name && !show_login && !show_password;
+ if show_all || show_name {
+ print_pws_data(ctx, "name: ", pws.get_slot_name(slot), quiet)?;
+ }
+ if show_all || show_login {
+ print_pws_data(ctx, "login: ", pws.get_slot_login(slot), quiet)?;
+ }
+ if show_all || show_password {
+ print_pws_data(ctx, "password:", pws.get_slot_password(slot), quiet)?;
+ }
+ Ok(())
+ })
})
}
@@ -872,20 +882,22 @@ pub fn pws_set(
password: &str,
) -> Result<()> {
with_device(ctx, |ctx, device| {
- let pws = get_password_safe(ctx, &device)?;
- pws
- .write_slot(slot, name, login, password)
- .map_err(|err| get_error("Could not write PWS slot", err))
+ with_password_safe(ctx, &device, |_ctx, pws| {
+ pws
+ .write_slot(slot, name, login, password)
+ .map_err(|err| get_error("Could not write PWS slot", err))
+ })
})
}
/// Clear a PWS slot.
pub fn pws_clear(ctx: &mut args::ExecCtx<'_>, slot: u8) -> Result<()> {
with_device(ctx, |ctx, device| {
- let pws = get_password_safe(ctx, &device)?;
- pws
- .erase_slot(slot)
- .map_err(|err| get_error("Could not clear PWS slot", err))
+ with_password_safe(ctx, &device, |_ctx, pws| {
+ pws
+ .erase_slot(slot)
+ .map_err(|err| get_error("Could not clear PWS slot", err))
+ })
})
}
@@ -913,19 +925,20 @@ fn print_pws_slot(
/// Print the status of all PWS slots.
pub fn pws_status(ctx: &mut args::ExecCtx<'_>, all: bool) -> Result<()> {
with_device(ctx, |ctx, device| {
- let pws = get_password_safe(ctx, &device)?;
- let slots = pws
- .get_slot_status()
- .map_err(|err| get_error("Could not read PWS slot status", err))?;
- println!(ctx, "slot\tname")?;
- for (i, &value) in slots
- .into_iter()
- .enumerate()
- .filter(|(_, &value)| all || value)
- {
- print_pws_slot(ctx, &pws, i, value)?;
- }
- Ok(())
+ with_password_safe(ctx, &device, |ctx, pws| {
+ let slots = pws
+ .get_slot_status()
+ .map_err(|err| get_error("Could not read PWS slot status", err))?;
+ println!(ctx, "slot\tname")?;
+ for (i, &value) in slots
+ .into_iter()
+ .enumerate()
+ .filter(|(_, &value)| all || value)
+ {
+ print_pws_slot(ctx, &pws, i, value)?;
+ }
+ Ok(())
+ })
})
}