Skip to content

Commit 723009c

Browse files
feat(transport): thread persistent token store through HID channel
1 parent 77284f7 commit 723009c

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

libwebauthn/src/transport/hid/channel.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use tokio::sync::mpsc::{self, Receiver, Sender};
1515
use tokio::time::sleep;
1616
use tracing::{debug, info, instrument, trace, warn, Level};
1717

18+
use crate::pin::persistent_token::PersistentTokenStore;
1819
use crate::proto::ctap1::apdu::{ApduRequest, ApduResponse};
1920
use crate::proto::ctap1::{Ctap1, Ctap1RegisterRequest};
2021
use crate::proto::ctap2::cbor::{CborRequest, CborResponse};
@@ -79,14 +80,18 @@ pub struct HidChannel<'d> {
7980
open_device: OpenHidDevice,
8081
init: InitResponse,
8182
auth_token_data: Option<AuthTokenData>,
83+
persistent_token_store: Option<Arc<dyn PersistentTokenStore>>,
8284
ux_update_sender: broadcast::Sender<UvUpdate>,
8385
handle: HidChannelHandle,
8486
#[cfg(feature = "virt")]
8587
pin_protocol_override: Option<Ctap2PinUvAuthProtocol>,
8688
}
8789

8890
impl<'d> HidChannel<'d> {
89-
pub async fn new(device: &'d HidDevice) -> Result<HidChannel<'d>, Error> {
91+
pub async fn new(
92+
device: &'d HidDevice,
93+
persistent_token_store: Option<Arc<dyn PersistentTokenStore>>,
94+
) -> Result<HidChannel<'d>, Error> {
9095
let (ux_update_sender, _) = broadcast::channel(16);
9196
let (handle_tx, handle_rx) = mpsc::channel(1);
9297
let handle = HidChannelHandle { tx: handle_tx };
@@ -106,6 +111,7 @@ impl<'d> HidChannel<'d> {
106111
},
107112
init: InitResponse::default(),
108113
auth_token_data: None,
114+
persistent_token_store,
109115
ux_update_sender,
110116
handle,
111117
#[cfg(feature = "virt")]
@@ -600,4 +606,8 @@ impl Ctap2AuthTokenStore for HidChannel<'_> {
600606
fn clear_uv_auth_token_store(&mut self) {
601607
self.auth_token_data = None;
602608
}
609+
610+
fn persistent_token_store(&self) -> Option<Arc<dyn PersistentTokenStore>> {
611+
self.persistent_token_store.clone()
612+
}
603613
}

libwebauthn/src/transport/hid/device.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ use async_trait::async_trait;
44
use hidapi::DeviceInfo;
55
use hidapi::HidApi;
66
use std::fmt;
7+
use std::sync::Arc;
78
#[cfg(feature = "virt")]
8-
use std::sync::{Arc, Mutex};
9+
use std::sync::Mutex;
910
#[allow(unused_imports)]
1011
use tracing::{debug, info, instrument};
1112

1213
#[cfg(feature = "virt")]
1314
use super::framing::HidMessage;
15+
use crate::pin::persistent_token::PersistentTokenStore;
1416
use crate::transport::error::TransportError;
1517
use crate::transport::Device;
1618
use crate::webauthn::error::Error;
@@ -24,6 +26,16 @@ pub trait HidPipeBackend: fmt::Debug + Send {
2426
#[derive(Debug, Clone)]
2527
pub struct HidDevice {
2628
pub backend: HidBackendDevice,
29+
persistent_token_store: Option<Arc<dyn PersistentTokenStore>>,
30+
}
31+
32+
impl HidDevice {
33+
/// Attach a caller-supplied persistent pinUvAuthToken (pcmr) store. The store is
34+
/// forwarded to every channel opened from this device.
35+
pub fn with_persistent_token_store(mut self, store: Arc<dyn PersistentTokenStore>) -> Self {
36+
self.persistent_token_store = Some(store);
37+
self
38+
}
2739
}
2840

2941
#[derive(Debug, Clone)]
@@ -37,6 +49,7 @@ impl From<&DeviceInfo> for HidDevice {
3749
fn from(hidapi_device: &DeviceInfo) -> Self {
3850
Self {
3951
backend: HidBackendDevice::HidApiDevice(hidapi_device.clone()),
52+
persistent_token_store: None,
4053
}
4154
}
4255
}
@@ -77,13 +90,15 @@ pub async fn list_devices() -> Result<Vec<HidDevice>, Error> {
7790
pub fn virtual_device<B: HidPipeBackend + 'static>(backend: B) -> HidDevice {
7891
HidDevice {
7992
backend: HidBackendDevice::VirtualDevice(Arc::new(Mutex::new(backend))),
93+
persistent_token_store: None,
8094
}
8195
}
8296

8397
#[async_trait]
8498
impl<'d> Device<'d, Hid, HidChannel<'d>> for HidDevice {
8599
async fn channel(&'d mut self) -> Result<HidChannel<'d>, Error> {
86-
let channel = HidChannel::new(self).await?;
100+
let store = self.persistent_token_store.clone();
101+
let channel = HidChannel::new(self, store).await?;
87102
Ok(channel)
88103
}
89104

0 commit comments

Comments
 (0)