diff options
| author | Robin Krahl <robin.krahl@ireas.org> | 2018-05-28 20:22:01 +0000 | 
|---|---|---|
| committer | Robin Krahl <robin.krahl@ireas.org> | 2018-05-28 22:22:54 +0200 | 
| commit | 15706f8708af4175a5376026cdd7c6f821945a5d (patch) | |
| tree | df66bd75aaff9410c7f0af60daa1ace2fc702c53 /src | |
| parent | 1ceea28c65b60c31c6c2667cd6de90935e4b9dc1 (diff) | |
| download | nitrokey-rs-15706f8708af4175a5376026cdd7c6f821945a5d.tar.gz nitrokey-rs-15706f8708af4175a5376026cdd7c6f821945a5d.tar.bz2 | |
Move set_time from Device to GenerateOtp
The set_time operation is only used with TOTP generation.  Therefore it
makes more sense to have it in the GenerateOtp trait than in the Device
trait.
Diffstat (limited to 'src')
| -rw-r--r-- | src/device.rs | 36 | ||||
| -rw-r--r-- | src/otp.rs | 38 | ||||
| -rw-r--r-- | src/tests/pro.rs | 2 | 
3 files changed, 38 insertions, 38 deletions
| diff --git a/src/device.rs b/src/device.rs index 7387dd9..25e4596 100644 --- a/src/device.rs +++ b/src/device.rs @@ -140,42 +140,6 @@ pub struct Admin<T: Device> {  /// This trait provides the commands that can be executed without authentication and that are  /// present on all supported Nitrokey devices.  pub trait Device: GenerateOtp { -    /// Sets the time on the Nitrokey.  This command may set the time to arbitrary values.  `time` -    /// is the number of seconds since January 1st, 1970 (Unix timestamp). -    /// -    /// The time is used for TOTP generation (see [`get_totp_code`][]). -    /// -    /// # Example -    /// -    /// ```ignore -    /// extern crate chrono; -    /// -    /// use chrono::Utc; -    /// use nitrokey::Device; -    /// # use nitrokey::CommandError; -    /// -    /// # fn try_main() -> Result<(), CommandError> { -    /// let device = nitrokey::connect()?; -    /// let time = Utc::now().timestamp(); -    /// if time < 0 { -    ///     println!("Timestamps before 1970-01-01 are not supported!"); -    /// } else { -    ///     device.set_time(time as u64); -    /// } -    /// #     Ok(()) -    /// # } -    /// ``` -    /// -    /// # Errors -    /// -    /// - [`Timestamp`][] if the time could not be set -    /// -    /// [`get_totp_code`]: trait.GenerateOtp.html#method.get_totp_code -    /// [`Timestamp`]: enum.CommandError.html#variant.Timestamp -    fn set_time(&self, time: u64) -> CommandStatus { -        unsafe { CommandStatus::from(nitrokey_sys::NK_totp_set_time(time)) } -    } -      /// Returns the serial number of the Nitrokey device.  The serial number is the string      /// representation of a hex number.      /// @@ -149,6 +149,42 @@ pub trait ConfigureOtp {  /// Provides methods to generate OTP codes and to query OTP slots on a Nitrokey  /// device.  pub trait GenerateOtp { +    /// Sets the time on the Nitrokey.  This command may set the time to arbitrary values.  `time` +    /// is the number of seconds since January 1st, 1970 (Unix timestamp). +    /// +    /// The time is used for TOTP generation (see [`get_totp_code`][]). +    /// +    /// # Example +    /// +    /// ```ignore +    /// extern crate chrono; +    /// +    /// use chrono::Utc; +    /// use nitrokey::Device; +    /// # use nitrokey::CommandError; +    /// +    /// # fn try_main() -> Result<(), CommandError> { +    /// let device = nitrokey::connect()?; +    /// let time = Utc::now().timestamp(); +    /// if time < 0 { +    ///     println!("Timestamps before 1970-01-01 are not supported!"); +    /// } else { +    ///     device.set_time(time as u64); +    /// } +    /// #     Ok(()) +    /// # } +    /// ``` +    /// +    /// # Errors +    /// +    /// - [`Timestamp`][] if the time could not be set +    /// +    /// [`get_totp_code`]: #method.get_totp_code +    /// [`Timestamp`]: enum.CommandError.html#variant.Timestamp +    fn set_time(&self, time: u64) -> CommandStatus { +        unsafe { CommandStatus::from(nitrokey_sys::NK_totp_set_time(time)) } +    } +      /// Returns the name of the given HOTP slot.      ///      /// # Errors @@ -274,7 +310,7 @@ pub trait GenerateOtp {      /// # }      /// ```      /// -    /// [`set_time`]: trait.Device.html#method.set_time +    /// [`set_time`]: #method.set_time      /// [`get_config`]: trait.Device.html#method.get_config      /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot      /// [`NotAuthorized`]: enum.CommandError.html#variant.NotAuthorized diff --git a/src/tests/pro.rs b/src/tests/pro.rs index 915b45b..e52c287 100644 --- a/src/tests/pro.rs +++ b/src/tests/pro.rs @@ -180,7 +180,7 @@ fn configure_totp(admin: &ConfigureOtp) {      assert_eq!(CommandStatus::Success, admin.write_totp_slot(slot_data, 30));  } -fn check_totp_codes<T: Device + GenerateOtp>(device: &T) { +fn check_totp_codes(device: &GenerateOtp) {      for (i, &(time, code)) in TOTP_CODES.iter().enumerate() {          assert_eq!(CommandStatus::Success, device.set_time(time));          let result = device.get_totp_code(1); | 
