keyfork-shard: add prompt for loading any smartcard

This commit is contained in:
Ryan Heywood 2024-01-09 19:28:02 -05:00
parent 4384964ea5
commit 1b33e3cf0e
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
1 changed files with 26 additions and 20 deletions

View File

@ -24,7 +24,7 @@ pub enum Error {
#[error("Smart card could not decrypt any matching PKESK packets")]
SmartCardCouldNotDecrypt,
#[error("No smart card backend was stored")]
#[error("No smart card was found")]
SmartCardNotFound,
#[error("Selected smart card has no decryption key")]
@ -92,12 +92,18 @@ impl SmartcardManager {
/// Load any backend.
pub fn load_any_card(&mut self) -> Result<Fingerprint> {
PcscBackend::cards(None)?
.next()
.transpose()?
.ok_or(Error::SmartCardNotFound)
.and_then(|backend| {
let mut card = Card::<Open>::new(backend).map_err(Error::OpenSmartCard)?;
let card_backend = loop {
self.pm.prompt_message(&Message::Text(
"Please plug in a smart card and press enter".to_string(),
))?;
if let Some(c) = PcscBackend::cards(None)?.next().transpose()? {
break c;
}
self.pm.prompt_message(&Message::Text(
"No smart card was found".to_string(),
))?;
};
let mut card = Card::<Open>::new(card_backend).map_err(Error::OpenSmartCard)?;
let transaction = card.transaction().map_err(Error::Transaction)?;
let fingerprint = transaction
.fingerprints()
@ -108,7 +114,6 @@ impl SmartcardManager {
drop(transaction);
self.current_card.replace(card);
Ok(fingerprint)
})
}
/// Load a backend if any [`Fingerprint`] has been matched by a currently active card.
@ -150,8 +155,9 @@ impl SmartcardManager {
}
}
#[rustfmt::skip]
self.pm.prompt_message(&Message::Text("Please plug in a smart card and press enter".to_string()))?;
self.pm.prompt_message(&Message::Text(
"Please plug in a smart card and press enter".to_string(),
))?;
}
Ok(None)