From 18eb4bf4f425ee9e286f53e767e7eb3fb3afc068 Mon Sep 17 00:00:00 2001 From: str4d Date: Mon, 2 Jan 2023 18:15:31 +0000 Subject: [PATCH] Add `YubiKey::disconnect` (#462) This exposes `pcsc::Card::disconnect` to allow alternate disposition methods. --- CHANGELOG.md | 1 + src/yubikey.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 683add6..4c5845b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased ### Added +- `YubiKey::disconnect` - `impl Debug for {Context, YubiKey}` ## 0.7.0 (2022-11-14) diff --git a/src/yubikey.rs b/src/yubikey.rs index 9e93298..089d7a7 100644 --- a/src/yubikey.rs +++ b/src/yubikey.rs @@ -42,7 +42,7 @@ use crate::{ transaction::Transaction, }; use log::{error, info}; -use pcsc::Card; +use pcsc::{Card, Disposition}; use rand_core::{OsRng, RngCore}; use std::{ fmt::{self, Display}, @@ -248,6 +248,38 @@ impl YubiKey { Ok(()) } + /// Disconnect from the YubiKey. + /// + /// In case of error, ownership of the YubiKey is returned to the caller. + /// + /// # Note + /// + /// `YubiKey` implements `Drop` which automatically disconnects the card using + /// `Disposition::ResetCard`; you only need to call this function if you want to + /// handle errors or use a different disposition method. + pub fn disconnect(self, disposition: Disposition) -> core::result::Result<(), (Self, Error)> { + let Self { + card, + name, + pin, + version, + serial, + } = self; + + card.disconnect(disposition).map_err(|(card, e)| { + ( + Self { + card, + name, + pin, + version, + serial, + }, + e.into(), + ) + }) + } + /// Begin a transaction. pub(crate) fn begin_transaction(&mut self) -> Result> { // TODO(tarcieri): reconnect support