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