diff options
Diffstat (limited to 'src/device.rs')
-rw-r--r-- | src/device.rs | 64 |
1 files changed, 27 insertions, 37 deletions
diff --git a/src/device.rs b/src/device.rs index 25e4596..8f7dc00 100644 --- a/src/device.rs +++ b/src/device.rs @@ -2,6 +2,7 @@ use config::{Config, RawConfig}; use libc; use nitrokey_sys; use std::ffi::CString; +use std::ops::Deref; use std::os::raw::c_int; use misc::Authenticate; use otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData, RawOtpSlotData}; @@ -403,43 +404,34 @@ fn connect_model(model: Model) -> bool { unsafe { nitrokey_sys::NK_login_enum(model) == 1 } } -impl<T: AsRef<GenerateOtp>> GenerateOtp for T { +impl DeviceWrapper { + fn device(&self) -> &Device { + match *self { + DeviceWrapper::Storage(_) => panic!("..."), + DeviceWrapper::Pro(ref pro) => pro, + } + } +} + +impl GenerateOtp for DeviceWrapper { fn get_hotp_slot_name(&self, slot: u8) -> Result<String, CommandError> { - self.as_ref().get_hotp_slot_name(slot) + self.device().get_hotp_slot_name(slot) } fn get_totp_slot_name(&self, slot: u8) -> Result<String, CommandError> { - self.as_ref().get_totp_slot_name(slot) + self.device().get_totp_slot_name(slot) } fn get_hotp_code(&self, slot: u8) -> Result<String, CommandError> { - self.as_ref().get_hotp_code(slot) + self.device().get_hotp_code(slot) } fn get_totp_code(&self, slot: u8) -> Result<String, CommandError> { - self.as_ref().get_totp_code(slot) + self.device().get_totp_code(slot) } } -impl<T: AsRef<Device> + GenerateOtp> Device for T {} - -impl AsRef<GenerateOtp> for DeviceWrapper { - fn as_ref(&self) -> &(GenerateOtp + 'static) { - match *self { - DeviceWrapper::Storage(_) => panic!("..."), - DeviceWrapper::Pro(ref pro) => pro, - } - } -} - -impl AsRef<Device> for DeviceWrapper { - fn as_ref(&self) -> &(Device + 'static) { - match *self { - DeviceWrapper::Storage(_) => panic!("..."), - DeviceWrapper::Pro(ref pro) => pro, - } - } -} +impl Device for DeviceWrapper {} impl Authenticate for DeviceWrapper { fn authenticate_user(self, password: &str) -> Result<User<Self>, (Self, CommandError)> { @@ -534,12 +526,6 @@ impl Device for Pro {} impl GenerateOtp for Pro {} -impl<T: Device + 'static> AsRef<Device> for User<T> { - fn as_ref(&self) -> &(Device + 'static) { - &self.device - } -} - impl<T: Device> User<T> { /// Forgets the user authentication and returns an unauthenticated device. This method /// consumes the authenticated device. It does not perform any actual commands on the @@ -549,6 +535,14 @@ impl<T: Device> User<T> { } } +impl<T: Device> Deref for User<T> { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.device + } +} + impl<T: Device> GenerateOtp for User<T> { fn get_hotp_code(&self, slot: u8) -> Result<String, CommandError> { unsafe { @@ -580,14 +574,10 @@ impl<T: Device> AuthenticatedDevice<T> for User<T> { } } -impl<T: Device + 'static> AsRef<GenerateOtp> for Admin<T> { - fn as_ref(&self) -> &(GenerateOtp + 'static) { - &self.device - } -} +impl<T: Device> Deref for Admin<T> { + type Target = T; -impl<T: Device + 'static> AsRef<Device> for Admin<T> { - fn as_ref(&self) -> &(Device + 'static) { + fn deref(&self) -> &Self::Target { &self.device } } |