Merge pull request #69 from iqlusioninc/open-by-serial
yubikey: add `open_by_serial` method
This commit is contained in:
+21
-2
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user