diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2018-05-21 23:17:46 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2018-05-22 01:19:42 +0200 |
commit | aac8f14edf4986c4881267a5b76a58d6800945bb (patch) | |
tree | 3ecf8243bb354bff8d737cc44495ce31e96793f6 | |
parent | a9f51585b909c805bd441200c65e622217dc1a38 (diff) | |
download | nitrokey-rs-aac8f14edf4986c4881267a5b76a58d6800945bb.tar.gz nitrokey-rs-aac8f14edf4986c4881267a5b76a58d6800945bb.tar.bz2 |
Use drop() instead of disconnect()
Previously, the user had to explicitly call diconnect() to terminate the
connection to the Nitrokey. Now NK_logout() is called automatically
once the device is out of scope as UnauthenticatedDevice implements
Drop. AdminAuthenticatedDevice and UserAuthenticatedDevice do not have
to implement Drop, as it will be called recursively.
-rw-r--r-- | TODO.md | 2 | ||||
-rw-r--r-- | src/lib.rs | 30 | ||||
-rw-r--r-- | src/tests/pro.rs | 2 |
3 files changed, 8 insertions, 26 deletions
@@ -36,10 +36,10 @@ - `NK_list_devices_by_cpuID` - `NK_connect_with_ID` - Fix timing issues with the `totp` and `totp_pin` test cases. -- Consider implementing `Drop` instead of the method `disconnect`. - Find an example for `set_time`, also adapt `get_totp_code`. - Improve log level documentation. - Clear passwords from memory. - Find a nicer syntax for the `write_config` test. - Fix segmentation faults when freeing string literals with old Nitrokey versions (fixed in libnitrokey commit 7a8550d). +- Prevent construction of internal types. @@ -329,30 +329,6 @@ static TEMPORARY_PASSWORD_LENGTH: usize = 25; /// [`get_hotp_code`]: #method.get_hotp_code /// [`get_totp_code`]: #method.get_totp_code pub trait Device { - /// Closes the connection to this device. This method consumes the device. - /// - /// # Example - /// - /// ```no_run - /// use nitrokey::Device; - /// # use nitrokey::CommandError; - /// - /// # fn try_main() -> Result<(), CommandError> { - /// let device = nitrokey::connect()?; - /// // perform tasks ... - /// device.disconnect(); - /// # Ok(()) - /// # } - /// ``` - fn disconnect(self) - where - Self: std::marker::Sized, - { - unsafe { - nitrokey_sys::NK_logout(); - } - } - /// 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). @@ -1110,6 +1086,12 @@ impl UnauthenticatedDevice { } } +impl Drop for UnauthenticatedDevice { + fn drop(&mut self) { + unsafe { nitrokey_sys::NK_logout(); } + } +} + impl Device for UnauthenticatedDevice {} impl UserAuthenticatedDevice { diff --git a/src/tests/pro.rs b/src/tests/pro.rs index 732c248..feadcbc 100644 --- a/src/tests/pro.rs +++ b/src/tests/pro.rs @@ -48,7 +48,7 @@ fn connect() { #[cfg_attr(not(feature = "test-pro"), ignore)] fn disconnect() { set_debug(false); - ::connect().unwrap().disconnect(); + ::connect().unwrap(); unsafe { let ptr = ::nitrokey_sys::NK_device_serial_number(); assert!(!ptr.is_null()); |