aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2018-05-22 00:32:12 +0000
committerRobin Krahl <robin.krahl@ireas.org>2018-05-22 02:35:28 +0200
commita9657c48c8c92f6c82834a2abd90e27904e2cf6b (patch)
tree6991987c5fd21a169a233c30b17706330ae085c6 /src/config.rs
parent2a736067581dd44288b43a01bf47e0d28801e0a8 (diff)
downloadnitrokey-rs-a9657c48c8c92f6c82834a2abd90e27904e2cf6b.tar.gz
nitrokey-rs-a9657c48c8c92f6c82834a2abd90e27904e2cf6b.tar.bz2
Restructure code by functionality
In future versions, we want to support not only the Nitrokey Pro, but also the Nitrokey Storage. This requires a better code layout. This patch introduces two main changes: First, the OTP-specific methods are moved from the Device trait and the AdminAuthenticatedDevice struct to the functionality-based traits ConfigureOtp and GenerateOtp. This will hopefully make it easier to integrate the Nitrokey Storage. Secondly, the code is split into separate modules. These modules are currently all private and re-exported in the lib module, but we can consider making them public in the future.
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..b37c9d3
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,100 @@
+use util::CommandError;
+
+/// The configuration for a Nitrokey.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct Config {
+ /// If set, the stick will generate a code from the HOTP slot with the
+ /// given number if numlock is pressed. The slot number must be 0, 1 or 2.
+ pub numlock: Option<u8>,
+ /// If set, the stick will generate a code from the HOTP slot with the
+ /// given number if capslock is pressed. The slot number must be 0, 1 or 2.
+ pub capslock: Option<u8>,
+ /// If set, the stick will generate a code from the HOTP slot with the
+ /// given number if scrollock is pressed. The slot number must be 0, 1 or 2.
+ pub scrollock: Option<u8>,
+ /// If set, OTP generation using [`get_hotp_code`][] or [`get_totp_code`][]
+ /// requires user authentication. Otherwise, OTPs can be generated without
+ /// authentication.
+ ///
+ /// [`get_hotp_code`]: trait.ProvideOtp.html#method.get_hotp_code
+ /// [`get_totp_code`]: trait.ProvideOtp.html#method.get_totp_code
+ pub user_password: bool,
+}
+
+#[derive(Debug)]
+pub struct RawConfig {
+ pub numlock: u8,
+ pub capslock: u8,
+ pub scrollock: u8,
+ pub user_password: bool,
+}
+
+fn config_otp_slot_to_option(value: u8) -> Option<u8> {
+ if value < 3 {
+ return Some(value);
+ }
+ None
+}
+
+fn option_to_config_otp_slot(value: Option<u8>) -> Result<u8, CommandError> {
+ match value {
+ Some(value) => {
+ if value < 3 {
+ Ok(value)
+ } else {
+ Err(CommandError::InvalidSlot)
+ }
+ }
+ None => Ok(255),
+ }
+}
+
+impl Config {
+ /// Constructs a new instance of this struct.
+ pub fn new(
+ numlock: Option<u8>,
+ capslock: Option<u8>,
+ scrollock: Option<u8>,
+ user_password: bool,
+ ) -> Config {
+ Config {
+ numlock,
+ capslock,
+ scrollock,
+ user_password,
+ }
+ }
+}
+
+impl RawConfig {
+ pub fn try_from(config: Config) -> Result<RawConfig, CommandError> {
+ Ok(RawConfig {
+ numlock: option_to_config_otp_slot(config.numlock)?,
+ capslock: option_to_config_otp_slot(config.capslock)?,
+ scrollock: option_to_config_otp_slot(config.scrollock)?,
+ user_password: config.user_password,
+ })
+ }
+}
+
+impl From<[u8; 5]> for RawConfig {
+ fn from(data: [u8; 5]) -> Self {
+ RawConfig {
+ numlock: data[0],
+ capslock: data[1],
+ scrollock: data[2],
+ user_password: data[3] != 0,
+ }
+ }
+}
+
+impl Into<Config> for RawConfig {
+ fn into(self) -> Config {
+ Config {
+ numlock: config_otp_slot_to_option(self.numlock),
+ capslock: config_otp_slot_to_option(self.capslock),
+ scrollock: config_otp_slot_to_option(self.scrollock),
+ user_password: self.user_password,
+ }
+ }
+}