From 08897ec7c9ef528901b2656956ff7730e4e7f82f Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Tue, 10 Dec 2019 08:36:44 -0800 Subject: [PATCH] cli: print reader name as part of `status` command --- cli/src/commands/readers.rs | 2 +- cli/src/commands/status.rs | 1 + src/yubikey.rs | 13 ++++++++++--- tests/integration.rs | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cli/src/commands/readers.rs b/cli/src/commands/readers.rs index a7f5bc6..4b4624e 100644 --- a/cli/src/commands/readers.rs +++ b/cli/src/commands/readers.rs @@ -36,7 +36,7 @@ impl ReadersCmd { for (i, reader) in readers_iter.enumerate() { let name = reader.name(); - let mut yubikey = match reader.open() { + let yubikey = match reader.open() { Ok(yk) => yk, Err(_) => continue, }; diff --git a/cli/src/commands/status.rs b/cli/src/commands/status.rs index 6eccc22..19df839 100644 --- a/cli/src/commands/status.rs +++ b/cli/src/commands/status.rs @@ -19,6 +19,7 @@ impl StatusCmd { let mut s = STDOUT.lock(); s.reset().unwrap(); + self.attr(&mut s, "name", yk.name()).unwrap(); self.attr(&mut s, "version", yk.version()).unwrap(); self.attr(&mut s, "serial", yk.serial()).unwrap(); diff --git a/src/yubikey.rs b/src/yubikey.rs index 8478e22..ba90d81 100644 --- a/src/yubikey.rs +++ b/src/yubikey.rs @@ -157,6 +157,7 @@ impl Display for Version { #[cfg_attr(not(feature = "untested"), allow(dead_code))] pub struct YubiKey { pub(crate) card: Card, + pub(crate) name: String, pub(crate) pin: Option, pub(crate) version: Version, pub(crate) serial: Serial, @@ -192,7 +193,7 @@ impl YubiKey { let mut readers = Readers::open()?; for reader in readers.iter()? { - let mut yubikey = match reader.open() { + let yubikey = match reader.open() { Ok(yk) => yk, Err(_) => continue, }; @@ -238,17 +239,22 @@ impl YubiKey { Ok(Transaction::new(&mut self.card)?) } + /// Get the name of the associated PC/SC card reader + pub fn name(&self) -> &str { + &self.name + } + /// Get the YubiKey's PIV application version. /// /// This always uses the cached version queried when the key is initialized. - pub fn version(&mut self) -> Version { + pub fn version(&self) -> Version { self.version } /// Get YubiKey device serial number. /// /// This always uses the cached version queried when the key is initialized. - pub fn serial(&mut self) -> Serial { + pub fn serial(&self) -> Serial { self.serial } @@ -637,6 +643,7 @@ impl<'a> TryFrom<&'a Reader<'_>> for YubiKey { let yubikey = YubiKey { card, + name: String::from(reader.name()), pin: None, version, serial, diff --git a/tests/integration.rs b/tests/integration.rs index 4662756..2527f92 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -20,7 +20,7 @@ fn init_yubikey() -> Mutex { env_logger::builder().format_timestamp(None).init(); } - let mut yubikey = YubiKey::open().unwrap(); + let yubikey = YubiKey::open().unwrap(); trace!("serial: {}", yubikey.serial()); trace!("version: {}", yubikey.version());