From 0e1942a6ca93aa3ab1b93778915320a055bb7744 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 11 Jul 2019 17:44:44 +0000 Subject: lifetimes for Device/DeviceWrapper/User/Admin --- nitrocli/src/commands.rs | 52 +++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) (limited to 'nitrocli/src/commands.rs') diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index 869e667..260774a 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -57,7 +57,7 @@ fn set_log_level(ctx: &mut args::ExecCtx<'_>) { fn with_device(ctx: &mut args::ExecCtx<'_>, op: F) -> Result<()> where - F: FnOnce(&mut args::ExecCtx<'_>, nitrokey::DeviceWrapper) -> Result<()>, + F: FnOnce(&mut args::ExecCtx<'_>, nitrokey::DeviceWrapper<'_>) -> Result<()>, { let mut manager = nitrokey::take()?; set_log_level(ctx); @@ -73,7 +73,7 @@ where fn with_storage_device(ctx: &mut args::ExecCtx<'_>, op: F) -> Result<()> where - F: FnOnce(&mut args::ExecCtx<'_>, nitrokey::Storage) -> Result<()>, + F: FnOnce(&mut args::ExecCtx<'_>, nitrokey::Storage<'_>) -> Result<()>, { let mut manager = nitrokey::take()?; set_log_level(ctx); @@ -92,38 +92,26 @@ where op(ctx, device) } -/// Open the password safe on the given device. -fn get_password_safe<'dev, D>( - ctx: &mut args::ExecCtx<'_>, - device: &'dev mut D, -) -> Result> +fn with_password_safe<'dev, D, F>(ctx: &mut args::ExecCtx<'_>, device: &mut D, op: F) -> Result<()> where - D: Device, + D: Device<'dev>, + F: FnOnce(&mut args::ExecCtx<'_>, nitrokey::PasswordSafe<'_, '_>) -> Result<()>, { let pin_entry = pinentry::PinEntry::from(pinentry::PinType::User, device)?; - - try_with_pin_and_data( + let pws = try_with_pin_and_data( ctx, &pin_entry, "Could not access the password safe", (), |_, pin| device.get_password_safe(pin).map_err(|err| ((), err)), - ) -} - -fn with_password_safe(ctx: &mut args::ExecCtx<'_>, device: &mut 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. -fn authenticate( +fn authenticate<'mgr, D, A, F>( ctx: &mut args::ExecCtx<'_>, device: D, pin_type: pinentry::PinType, @@ -131,7 +119,7 @@ fn authenticate( op: F, ) -> Result where - D: Device, + D: Device<'mgr>, F: Fn(D, &str) -> result::Result, { let pin_entry = pinentry::PinEntry::from(pin_type, &device)?; @@ -140,9 +128,12 @@ where } /// Authenticate the given device with the user PIN. -fn authenticate_user(ctx: &mut args::ExecCtx<'_>, device: T) -> Result> +fn authenticate_user<'mgr, T>( + ctx: &mut args::ExecCtx<'_>, + device: T, +) -> Result> where - T: Device, + T: Device<'mgr>, { authenticate( ctx, @@ -154,9 +145,12 @@ where } /// Authenticate the given device with the admin PIN. -fn authenticate_admin(ctx: &mut args::ExecCtx<'_>, device: T) -> Result> +fn authenticate_admin<'mgr, T>( + ctx: &mut args::ExecCtx<'_>, + device: T, +) -> Result> where - T: Device, + T: Device<'mgr>, { authenticate( ctx, @@ -313,7 +307,7 @@ fn print_storage_status( fn print_status( ctx: &mut args::ExecCtx<'_>, model: &'static str, - device: &nitrokey::DeviceWrapper, + device: &nitrokey::DeviceWrapper<'_>, ) -> Result<()> { let serial_number = device .get_serial_number() @@ -681,7 +675,7 @@ pub fn otp_clear( fn print_otp_status( ctx: &mut args::ExecCtx<'_>, algorithm: args::OtpAlgorithm, - device: &nitrokey::DeviceWrapper, + device: &nitrokey::DeviceWrapper<'_>, all: bool, ) -> Result<()> { let mut slot: u8 = 0; @@ -824,7 +818,7 @@ fn print_pws_data( Ok(()) } -fn check_slot(pws: &nitrokey::PasswordSafe<'_>, slot: u8) -> Result<()> { +fn check_slot(pws: &nitrokey::PasswordSafe<'_, '_>, slot: u8) -> Result<()> { if slot >= nitrokey::SLOT_COUNT { return Err(nitrokey::Error::from(nitrokey::LibraryError::InvalidSlot).into()); } @@ -899,7 +893,7 @@ pub fn pws_clear(ctx: &mut args::ExecCtx<'_>, slot: u8) -> Result<()> { fn print_pws_slot( ctx: &mut args::ExecCtx<'_>, - pws: &nitrokey::PasswordSafe<'_>, + pws: &nitrokey::PasswordSafe<'_, '_>, slot: usize, programmed: bool, ) -> Result<()> { -- cgit v1.2.1