summaryrefslogtreecommitdiff
path: root/src/otp.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/otp.rs')
-rw-r--r--src/otp.rs38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/otp.rs b/src/otp.rs
index 0d04e27..b89d2bf 100644
--- a/src/otp.rs
+++ b/src/otp.rs
@@ -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