aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/device.rs64
-rw-r--r--src/tests/pro.rs5
2 files changed, 30 insertions, 39 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
}
}
diff --git a/src/tests/pro.rs b/src/tests/pro.rs
index e52c287..5415ff9 100644
--- a/src/tests/pro.rs
+++ b/src/tests/pro.rs
@@ -1,4 +1,5 @@
use std::ffi::CStr;
+use std::ops::Deref;
use {Admin, Authenticate, CommandError, CommandStatus, Config, ConfigureOtp, Device, GenerateOtp,
OtpMode, OtpSlotData, Pro};
@@ -101,7 +102,7 @@ fn hotp() {
assert_eq!(CommandStatus::Success, admin.write_config(config));
configure_hotp(&admin);
- check_hotp_codes(&admin);
+ check_hotp_codes(admin.deref());
configure_hotp(&admin);
check_hotp_codes(&admin.device());
@@ -203,7 +204,7 @@ fn totp() {
assert_eq!(CommandStatus::Success, admin.write_config(config));
configure_totp(&admin);
- check_totp_codes(&admin);
+ check_totp_codes(admin.deref());
configure_totp(&admin);
check_totp_codes(&admin.device());