Skip to content

Commit 8c5f281

Browse files
Decouple CableChannel from CableDevice implementations
1 parent 640a344 commit 8c5f281

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

libwebauthn/src/transport/cable/channel.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,28 @@ pub enum CableChannelDevice<'d> {
2727
}
2828

2929
#[derive(Debug)]
30-
pub struct CableChannel<'d> {
30+
pub struct CableChannel {
3131
/// The WebSocket stream used for communication.
3232
// pub(crate) ws_stream: WebSocketStream<MaybeTlsStream<TcpStream>>,
3333

3434
/// The noise state used for encryption over the WebSocket stream.
3535
// pub(crate) noise_state: TransportState,
3636

3737
/// The device that this channel is connected to.
38-
pub device: CableChannelDevice<'d>,
39-
4038
pub(crate) handle_connection: task::JoinHandle<()>,
4139
pub(crate) cbor_sender: mpsc::Sender<CborRequest>,
4240
pub(crate) cbor_receiver: mpsc::Receiver<CborResponse>,
4341
pub(crate) tx: mpsc::Sender<UxUpdate>,
4442
}
4543

46-
impl Display for CableChannel<'_> {
44+
impl Display for CableChannel {
4745
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
4846
write!(f, "CableChannel")
4947
}
5048
}
5149

5250
#[async_trait]
53-
impl<'d> Channel for CableChannel<'d> {
51+
impl<'d> Channel for CableChannel {
5452
async fn supported_protocols(&self) -> Result<SupportedProtocols, Error> {
5553
Ok(SupportedProtocols::fido2_only())
5654
}
@@ -111,7 +109,7 @@ impl<'d> Channel for CableChannel<'d> {
111109
}
112110
}
113111

114-
impl<'d> Ctap2AuthTokenStore for CableChannel<'d> {
112+
impl<'d> Ctap2AuthTokenStore for CableChannel {
115113
fn store_auth_data(&mut self, _auth_token_data: AuthTokenData) {}
116114

117115
fn get_auth_data(&self) -> Option<&AuthTokenData> {

libwebauthn/src/transport/cable/known_devices.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ unsafe impl Send for CableKnownDevice {}
125125
unsafe impl Sync for CableKnownDevice {}
126126

127127
#[async_trait]
128-
impl<'d> Device<'d, Cable, CableChannel<'d>> for CableKnownDevice {
128+
impl<'d> Device<'d, Cable, CableChannel> for CableKnownDevice {
129129
async fn channel(&'d mut self) -> Result<(CableChannel, mpsc::Receiver<UxUpdate>), Error> {
130130
todo!()
131131
}

libwebauthn/src/transport/cable/qr_code_device.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ impl Display for CableQrCodeDevice {
243243
}
244244

245245
#[async_trait]
246-
impl<'d> Device<'d, Cable, CableChannel<'d>> for CableQrCodeDevice {
247-
async fn channel(&'d mut self) -> Result<(CableChannel<'d>, mpsc::Receiver<UxUpdate>), Error> {
246+
impl<'d> Device<'d, Cable, CableChannel> for CableQrCodeDevice {
247+
async fn channel(&'d mut self) -> Result<(CableChannel, mpsc::Receiver<UxUpdate>), Error> {
248248
let (_device, advert) = self.await_advertisement().await?;
249249

250250
let Some(tunnel_domain) =
@@ -270,7 +270,7 @@ impl<'d> Device<'d, Cable, CableChannel<'d>> for CableQrCodeDevice {
270270
.unwrap();
271271

272272
return tunnel::connect(
273-
self,
273+
&self.store,
274274
&tunnel_domain,
275275
&routing_id_str,
276276
&tunnel_id_str,

libwebauthn/src/transport/cable/tunnel.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ use tokio_tungstenite::{connect_async, MaybeTlsStream, WebSocketStream};
1919
use tracing::{debug, error, trace, warn};
2020
use tungstenite::client::IntoClientRequest;
2121

22-
use super::channel::{CableChannel, CableChannelDevice};
22+
use super::channel::CableChannel;
2323
use super::known_devices::{CableKnownDeviceInfo, CableKnownDeviceInfoStore};
24-
use super::qr_code_device::CableQrCodeDevice;
2524
use crate::proto::ctap2::cbor::{CborRequest, CborResponse};
2625
use crate::proto::ctap2::{Ctap2CommandCode, Ctap2GetInfoResponse};
2726
use crate::transport::cable::known_devices::CableKnownDeviceId;
@@ -157,13 +156,13 @@ pub fn decode_tunnel_server_domain(encoded: u16) -> Option<String> {
157156
}
158157

159158
pub async fn connect<'d>(
160-
device: &'d CableQrCodeDevice,
159+
maybe_known_device_store: &Option<Arc<dyn CableKnownDeviceInfoStore>>,
161160
tunnel_domain: &str,
162161
routing_id: &str,
163162
tunnel_id: &str,
164163
psk: &[u8; 32],
165164
private_key: &NonZeroScalar,
166-
) -> Result<(CableChannel<'d>, mpsc::Receiver<UxUpdate>), Error> {
165+
) -> Result<(CableChannel, mpsc::Receiver<UxUpdate>), Error> {
167166
let connect_url = format!(
168167
"wss://{}/cable/connect/{}/{}",
169168
tunnel_domain, routing_id, tunnel_id
@@ -207,13 +206,16 @@ pub async fn connect<'d>(
207206
// After this, the handshake should be complete and you can start sending/receiving encrypted messages.
208207
// ...
209208

210-
let (cbor_sender, cbor_receiver, handle_connection) =
211-
task_connection(tunnel_domain, device, ws_stream, noise_state)?;
209+
let (cbor_sender, cbor_receiver, handle_connection) = task_connection(
210+
tunnel_domain,
211+
maybe_known_device_store,
212+
ws_stream,
213+
noise_state,
214+
)?;
212215

213216
let (send, recv) = mpsc::channel(1);
214217
Ok((
215218
CableChannel {
216-
device: CableChannelDevice::QrCode(device),
217219
handle_connection,
218220
cbor_sender,
219221
cbor_receiver,
@@ -329,15 +331,15 @@ async fn do_handshake(
329331

330332
fn task_connection(
331333
tunnel_domain: &str,
332-
device: &CableQrCodeDevice,
334+
maybe_known_device_store: &Option<Arc<dyn CableKnownDeviceInfoStore>>,
333335
ws_stream: WebSocketStream<MaybeTlsStream<TcpStream>>,
334336
transport_state: TransportState,
335337
) -> Result<(Sender<CborRequest>, Receiver<CborResponse>, JoinHandle<()>), Error> {
336338
let (cbor_tx_send, cbor_tx_recv) = mpsc::channel(16);
337339
let (cbor_rx_send, cbor_rx_recv) = mpsc::channel(16);
338340

339341
let tunnel_domain: String = tunnel_domain.to_string();
340-
let maybe_known_device_store = device.store.to_owned();
342+
let maybe_known_device_store = maybe_known_device_store.clone();
341343
let handle_connection = task::spawn(async move {
342344
connection(
343345
&tunnel_domain,

0 commit comments

Comments
 (0)