yubikey: add open_by_serial method
Support for opening a `YubiKey` with a specific serial number.
This commit is contained in:
+21
-2
@@ -159,8 +159,8 @@ impl YubiKey {
|
|||||||
/// Returns an error if there is more than one YubiKey detected.
|
/// Returns an error if there is more than one YubiKey detected.
|
||||||
///
|
///
|
||||||
/// If you need to operate in environments with more than one YubiKey
|
/// If you need to operate in environments with more than one YubiKey
|
||||||
/// attached to the same system, use [`yubikey_piv::Readers`] to select
|
/// attached to the same system, use [`YubiKey::open_by_serial`] or
|
||||||
/// from the available PC/SC readers connected.
|
///[`yubikey_piv::Readers`] to select from the available PC/SC readers.
|
||||||
pub fn open() -> Result<Self, Error> {
|
pub fn open() -> Result<Self, Error> {
|
||||||
let mut readers = Readers::open()?;
|
let mut readers = Readers::open()?;
|
||||||
let mut reader_iter = readers.iter()?;
|
let mut reader_iter = readers.iter()?;
|
||||||
@@ -178,6 +178,25 @@ impl YubiKey {
|
|||||||
Err(Error::NotFound)
|
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
|
/// Reconnect to a YubiKey
|
||||||
#[cfg(feature = "untested")]
|
#[cfg(feature = "untested")]
|
||||||
pub fn reconnect(&mut self) -> Result<(), Error> {
|
pub fn reconnect(&mut self) -> Result<(), Error> {
|
||||||
|
|||||||
Reference in New Issue
Block a user