Skip to content

Commit 6b40b4f

Browse files
refactor(cable): namespace tunnel errors under CableTunnelError
1 parent 9ef8e96 commit 6b40b4f

4 files changed

Lines changed: 35 additions & 11 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Errors specific to the caBLE tunnel-server transport.
2+
3+
#[derive(thiserror::Error, Debug, PartialEq, Clone)]
4+
pub enum CableTunnelError {
5+
/// The tunnel server returned HTTP 410 Gone for the contacted resource.
6+
#[error("tunnel server reported the resource is gone (HTTP 410)")]
7+
Gone,
8+
/// The tunnel server returned an unexpected, non-success HTTP status.
9+
#[error("tunnel server returned unexpected HTTP status {0}")]
10+
UnexpectedStatus(u16),
11+
/// The tunnel server kept redirecting past the allowed limit.
12+
#[error("tunnel server exceeded the maximum number of redirects")]
13+
TooManyRedirects,
14+
}

libwebauthn/src/transport/cable/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod protocol;
99
pub mod advertisement;
1010
pub mod channel;
1111
pub mod connection_stages;
12+
pub mod error;
1213
pub mod known_devices;
1314
pub mod qr_code_device;
1415
pub mod tunnel;

libwebauthn/src/transport/cable/tunnel.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use tracing::{debug, error, trace};
99
use tungstenite::client::IntoClientRequest;
1010
use url::Url;
1111

12+
use super::error::CableTunnelError;
1213
use super::known_devices::CableKnownDeviceId;
1314
use super::protocol::CableTunnelConnectionType;
1415
use crate::proto::ctap2::cbor;
@@ -102,9 +103,9 @@ fn resolve_redirect_target(base: &str, location: &str) -> Result<String, Transpo
102103
/// Maps a non-101 tunnel handshake status to a transport error, distinguishing 410 Gone.
103104
fn tunnel_status_error(status: StatusCode) -> TransportError {
104105
if status == StatusCode::GONE {
105-
TransportError::TunnelServerGone
106+
CableTunnelError::Gone.into()
106107
} else {
107-
TransportError::ConnectionFailed
108+
CableTunnelError::UnexpectedStatus(status.as_u16()).into()
108109
}
109110
}
110111

@@ -115,7 +116,7 @@ pub(crate) fn known_device_id_to_forget(
115116
) -> Option<CableKnownDeviceId> {
116117
match (error, connection_type) {
117118
(
118-
TransportError::TunnelServerGone,
119+
TransportError::CableTunnel(CableTunnelError::Gone),
119120
CableTunnelConnectionType::KnownDevice {
120121
authenticator_public_key,
121122
..
@@ -188,7 +189,7 @@ pub(crate) async fn connect(
188189
}
189190

190191
error!("Exceeded the maximum number of tunnel redirects");
191-
Err(TransportError::ConnectionFailed)
192+
Err(CableTunnelError::TooManyRedirects.into())
192193
}
193194

194195
#[cfg(test)]
@@ -289,7 +290,10 @@ mod tests {
289290
let public_key = vec![7u8; 65];
290291
let connection_type = known_device_connection_type(public_key.clone());
291292
assert_eq!(
292-
known_device_id_to_forget(&TransportError::TunnelServerGone, &connection_type),
293+
known_device_id_to_forget(
294+
&TransportError::CableTunnel(CableTunnelError::Gone),
295+
&connection_type
296+
),
293297
Some(hex::encode(&public_key))
294298
);
295299
}
@@ -298,7 +302,10 @@ mod tests {
298302
fn gone_does_not_forget_qr_code() {
299303
let connection_type = qr_connection_type();
300304
assert_eq!(
301-
known_device_id_to_forget(&TransportError::TunnelServerGone, &connection_type),
305+
known_device_id_to_forget(
306+
&TransportError::CableTunnel(CableTunnelError::Gone),
307+
&connection_type
308+
),
302309
None
303310
);
304311
}
@@ -316,11 +323,11 @@ mod tests {
316323
fn gone_status_maps_to_distinct_error() {
317324
assert_eq!(
318325
tunnel_status_error(StatusCode::GONE),
319-
TransportError::TunnelServerGone
326+
TransportError::CableTunnel(CableTunnelError::Gone)
320327
);
321328
assert_eq!(
322329
tunnel_status_error(StatusCode::BAD_GATEWAY),
323-
TransportError::ConnectionFailed
330+
TransportError::CableTunnel(CableTunnelError::UnexpectedStatus(502))
324331
);
325332
}
326333
}

libwebauthn/src/transport/error.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
use crate::transport::cable::error::CableTunnelError;
2+
13
#[derive(thiserror::Error, Debug, PartialEq, Clone)]
24
pub enum TransportError {
35
#[error("connection failed")]
46
ConnectionFailed,
57
#[error("connection lost")]
68
ConnectionLost,
7-
/// The tunnel server returned HTTP 410 Gone for the contacted resource.
8-
#[error("tunnel server reported the resource is gone")]
9-
TunnelServerGone,
9+
/// An error from the caBLE tunnel-server transport.
10+
#[error(transparent)]
11+
CableTunnel(#[from] CableTunnelError),
1012
#[error("invalid endpoint")]
1113
InvalidEndpoint,
1214
#[error("invalid framing")]

0 commit comments

Comments
 (0)