yubikey: add open_by_serial method

Support for opening a `YubiKey` with a specific serial number.
This commit is contained in:
Tony Arcieri
2019-12-08 12:12:03 -08:00
parent fb7e95e6d1
commit 4663cffb96
+21 -2
View File
@@ -159,8 +159,8 @@ impl YubiKey {
/// Returns an error if there is more than one YubiKey detected.
///
/// If you need to operate in environments with more than one YubiKey
/// attached to the same system, use [`yubikey_piv::Readers`] to select
/// from the available PC/SC readers connected.
/// attached to the same system, use [`YubiKey::open_by_serial`] or
///[`yubikey_piv::Readers`] to select from the available PC/SC readers.
pub fn open() -> Result<Self, Error> {
let mut readers = Readers::open()?;
let mut reader_iter = readers.iter()?;
@@ -178,6 +178,25 @@ impl YubiKey {
Err(Error::NotFound)
}
/// Open a YubiKey with a specific serial number.
pub fn open_by_serial(serial: Serial) -> Result<Self, Error> {
let mut readers = Readers::open()?;
for reader in readers.iter()? {
let mut yubikey = match reader.open() {
Ok(yk) => yk,
Err(_) => continue,
};
if serial == yubikey.serial() {
return Ok(yubikey);
}
}
error!("no YubiKey detected with serial: {}", serial);
Err(Error::NotFound)
}
/// Reconnect to a YubiKey
#[cfg(feature = "untested")]
pub fn reconnect(&mut self) -> Result<(), Error> {