aboutsummaryrefslogtreecommitdiff
path: root/src/otp.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-20 22:16:36 +0100
committerRobin Krahl <robin.krahl@ireas.org>2019-01-20 22:17:37 +0100
commit8c57d3ace0215adb3cd5edd0f18e8cc513f65ff1 (patch)
tree27eb8ac46b130e08d917a7b91da8b48a14b644b6 /src/otp.rs
parent5da2735cfb8ccfaea1ebdee850ddd9fbd0bfb003 (diff)
parentd87859975dc158919ecd5bf11a1111a2da5fcb30 (diff)
downloadnitrokey-rs-8c57d3ace0215adb3cd5edd0f18e8cc513f65ff1.tar.gz
nitrokey-rs-8c57d3ace0215adb3cd5edd0f18e8cc513f65ff1.tar.bz2
Merge branch 'feature/error-refactoring' into next
RFC: https://lists.sr.ht/~ireas/nitrokey-rs-dev/%3C20190117000856.slgb6jwkwd3qu6ey%40localhost%3E
Diffstat (limited to 'src/otp.rs')
-rw-r--r--src/otp.rs83
1 files changed, 42 insertions, 41 deletions
diff --git a/src/otp.rs b/src/otp.rs
index 901bef9..7535a77 100644
--- a/src/otp.rs
+++ b/src/otp.rs
@@ -2,7 +2,8 @@ use std::ffi::CString;
use nitrokey_sys;
-use crate::util::{get_command_result, get_cstring, result_from_string, CommandError};
+use crate::error::Error;
+use crate::util::{get_command_result, get_cstring, result_from_string};
/// Modes for one-time password generation.
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -28,9 +29,9 @@ pub trait ConfigureOtp {
///
/// ```no_run
/// use nitrokey::{Authenticate, ConfigureOtp, OtpMode, OtpSlotData};
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), (CommandError)> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// let slot_data = OtpSlotData::new(1, "test", "01234567890123456689", OtpMode::SixDigits);
/// match device.authenticate_admin("12345678") {
@@ -46,10 +47,10 @@ pub trait ConfigureOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`NoName`]: enum.CommandError.html#variant.NoName
- fn write_hotp_slot(&self, data: OtpSlotData, counter: u64) -> Result<(), CommandError>;
+ fn write_hotp_slot(&self, data: OtpSlotData, counter: u64) -> Result<(), Error>;
/// Configure a TOTP slot with the given data and set the TOTP time window to the given value
/// (default 30).
@@ -64,9 +65,9 @@ pub trait ConfigureOtp {
///
/// ```no_run
/// use nitrokey::{Authenticate, ConfigureOtp, OtpMode, OtpSlotData};
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), (CommandError)> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// let slot_data = OtpSlotData::new(1, "test", "01234567890123456689", OtpMode::EightDigits);
/// match device.authenticate_admin("12345678") {
@@ -82,10 +83,10 @@ pub trait ConfigureOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`NoName`]: enum.CommandError.html#variant.NoName
- fn write_totp_slot(&self, data: OtpSlotData, time_window: u16) -> Result<(), CommandError>;
+ fn write_totp_slot(&self, data: OtpSlotData, time_window: u16) -> Result<(), Error>;
/// Erases an HOTP slot.
///
@@ -97,9 +98,9 @@ pub trait ConfigureOtp {
///
/// ```no_run
/// use nitrokey::{Authenticate, ConfigureOtp};
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), (CommandError)> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// match device.authenticate_admin("12345678") {
/// Ok(admin) => {
@@ -114,8 +115,8 @@ pub trait ConfigureOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
- fn erase_hotp_slot(&self, slot: u8) -> Result<(), CommandError>;
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
+ fn erase_hotp_slot(&self, slot: u8) -> Result<(), Error>;
/// Erases a TOTP slot.
///
@@ -127,9 +128,9 @@ pub trait ConfigureOtp {
///
/// ```no_run
/// use nitrokey::{Authenticate, ConfigureOtp};
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), (CommandError)> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// match device.authenticate_admin("12345678") {
/// Ok(admin) => {
@@ -144,8 +145,8 @@ pub trait ConfigureOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
- fn erase_totp_slot(&self, slot: u8) -> Result<(), CommandError>;
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
+ fn erase_totp_slot(&self, slot: u8) -> Result<(), Error>;
}
/// Provides methods to generate OTP codes and to query OTP slots on a Nitrokey
@@ -164,9 +165,9 @@ pub trait GenerateOtp {
/// ```no_run
/// use std::time;
/// use nitrokey::GenerateOtp;
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), CommandError> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// let time = time::SystemTime::now().duration_since(time::UNIX_EPOCH);
/// match time {
@@ -183,7 +184,7 @@ pub trait GenerateOtp {
///
/// [`get_totp_code`]: #method.get_totp_code
/// [`Timestamp`]: enum.CommandError.html#variant.Timestamp
- fn set_time(&self, time: u64, force: bool) -> Result<(), CommandError> {
+ fn set_time(&self, time: u64, force: bool) -> Result<(), Error> {
let result = if force {
unsafe { nitrokey_sys::NK_totp_set_time(time) }
} else {
@@ -202,22 +203,22 @@ pub trait GenerateOtp {
/// # Example
///
/// ```no_run
- /// use nitrokey::{CommandError, GenerateOtp};
+ /// use nitrokey::{CommandError, Error, GenerateOtp};
///
- /// # fn try_main() -> Result<(), CommandError> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// match device.get_hotp_slot_name(1) {
/// Ok(name) => println!("HOTP slot 1: {}", name),
- /// Err(CommandError::SlotNotProgrammed) => println!("HOTP slot 1 not programmed"),
+ /// Err(Error::CommandError(CommandError::SlotNotProgrammed)) => println!("HOTP slot 1 not programmed"),
/// Err(err) => println!("Could not get slot name: {}", err),
/// };
/// # Ok(())
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
- fn get_hotp_slot_name(&self, slot: u8) -> Result<String, CommandError> {
+ fn get_hotp_slot_name(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_hotp_slot_name(slot)) }
}
@@ -231,22 +232,22 @@ pub trait GenerateOtp {
/// # Example
///
/// ```no_run
- /// use nitrokey::{CommandError, GenerateOtp};
+ /// use nitrokey::{CommandError, Error, GenerateOtp};
///
- /// # fn try_main() -> Result<(), CommandError> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// match device.get_totp_slot_name(1) {
/// Ok(name) => println!("TOTP slot 1: {}", name),
- /// Err(CommandError::SlotNotProgrammed) => println!("TOTP slot 1 not programmed"),
+ /// Err(Error::CommandError(CommandError::SlotNotProgrammed)) => println!("TOTP slot 1 not programmed"),
/// Err(err) => println!("Could not get slot name: {}", err),
/// };
/// # Ok(())
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
- fn get_totp_slot_name(&self, slot: u8) -> Result<String, CommandError> {
+ fn get_totp_slot_name(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_totp_slot_name(slot)) }
}
@@ -263,9 +264,9 @@ pub trait GenerateOtp {
///
/// ```no_run
/// use nitrokey::GenerateOtp;
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), CommandError> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// let code = device.get_hotp_code(1)?;
/// println!("Generated HOTP code on slot 1: {}", code);
@@ -274,10 +275,10 @@ pub trait GenerateOtp {
/// ```
///
/// [`get_config`]: trait.Device.html#method.get_config
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`NotAuthorized`]: enum.CommandError.html#variant.NotAuthorized
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
- fn get_hotp_code(&self, slot: u8) -> Result<String, CommandError> {
+ fn get_hotp_code(&self, slot: u8) -> Result<String, Error> {
unsafe {
return result_from_string(nitrokey_sys::NK_get_hotp_code(slot));
}
@@ -300,9 +301,9 @@ pub trait GenerateOtp {
/// ```no_run
/// use std::time;
/// use nitrokey::GenerateOtp;
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), CommandError> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// let time = time::SystemTime::now().duration_since(time::UNIX_EPOCH);
/// match time {
@@ -319,10 +320,10 @@ pub trait GenerateOtp {
///
/// [`set_time`]: #method.set_time
/// [`get_config`]: trait.Device.html#method.get_config
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`NotAuthorized`]: enum.CommandError.html#variant.NotAuthorized
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
- fn get_totp_code(&self, slot: u8) -> Result<String, CommandError> {
+ fn get_totp_code(&self, slot: u8) -> Result<String, Error> {
unsafe {
return result_from_string(nitrokey_sys::NK_get_totp_code(slot, 0, 0, 0));
}
@@ -395,7 +396,7 @@ impl OtpSlotData {
}
impl RawOtpSlotData {
- pub fn new(data: OtpSlotData) -> Result<RawOtpSlotData, CommandError> {
+ pub fn new(data: OtpSlotData) -> Result<RawOtpSlotData, Error> {
let name = get_cstring(data.name)?;
let secret = get_cstring(data.secret)?;
let use_token_id = data.token_id.is_some();