summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/auth.rs24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/auth.rs b/src/auth.rs
index d1eb049..541de35 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -111,6 +111,8 @@ pub trait Authenticate {
trait AuthenticatedDevice<T> {
fn new(device: T, temp_password: Vec<u8>) -> Self;
+
+ fn temp_password_ptr(&self) -> *const c_char;
}
/// A Nitrokey device with user authentication.
@@ -217,20 +219,19 @@ impl<T: Device> Deref for User<T> {
impl<T: Device> GenerateOtp for User<T> {
fn get_hotp_code(&self, slot: u8) -> Result<String, Error> {
unsafe {
- let temp_password_ptr = self.temp_password.as_ptr() as *const c_char;
+ let temp_password_ptr = self.temp_password_ptr();
return result_from_string(nitrokey_sys::NK_get_hotp_code_PIN(slot, temp_password_ptr));
}
}
fn get_totp_code(&self, slot: u8) -> Result<String, Error> {
unsafe {
- let temp_password_ptr = self.temp_password.as_ptr() as *const c_char;
return result_from_string(nitrokey_sys::NK_get_totp_code_PIN(
slot,
0,
0,
0,
- temp_password_ptr,
+ self.temp_password_ptr(),
));
}
}
@@ -243,6 +244,10 @@ impl<T: Device> AuthenticatedDevice<T> for User<T> {
temp_password,
}
}
+
+ fn temp_password_ptr(&self) -> *const c_char {
+ self.temp_password.as_ptr() as *const c_char
+ }
}
impl<T: Device> Deref for Admin<T> {
@@ -297,7 +302,7 @@ impl<T: Device> Admin<T> {
raw_config.scrollock,
raw_config.user_password,
false,
- self.temp_password.as_ptr() as *const c_char,
+ self.temp_password_ptr(),
))
}
}
@@ -307,8 +312,7 @@ impl<T: Device> Admin<T> {
C: Fn(RawOtpSlotData, *const c_char) -> c_int,
{
let raw_data = RawOtpSlotData::new(data)?;
- let temp_password_ptr = self.temp_password.as_ptr() as *const c_char;
- get_command_result(callback(raw_data, temp_password_ptr))
+ get_command_result(callback(raw_data, self.temp_password_ptr()))
}
}
@@ -346,12 +350,12 @@ impl<T: Device> ConfigureOtp for Admin<T> {
}
fn erase_hotp_slot(&self, slot: u8) -> Result<(), Error> {
- let temp_password_ptr = self.temp_password.as_ptr() as *const c_char;
+ let temp_password_ptr = self.temp_password_ptr();
unsafe { get_command_result(nitrokey_sys::NK_erase_hotp_slot(slot, temp_password_ptr)) }
}
fn erase_totp_slot(&self, slot: u8) -> Result<(), Error> {
- let temp_password_ptr = self.temp_password.as_ptr() as *const c_char;
+ let temp_password_ptr = self.temp_password_ptr();
unsafe { get_command_result(nitrokey_sys::NK_erase_totp_slot(slot, temp_password_ptr)) }
}
}
@@ -363,6 +367,10 @@ impl<T: Device> AuthenticatedDevice<T> for Admin<T> {
temp_password,
}
}
+
+ fn temp_password_ptr(&self) -> *const c_char {
+ self.temp_password.as_ptr() as *const c_char
+ }
}
impl Authenticate for DeviceWrapper {