Skip to content

Commit d236753

Browse files
authored
Merge pull request #204 from msirringhaus/cancellation
Implement first step of cancellation: Stop all handler when one completes/fails
2 parents 9a4e07c + ca6847c commit d236753

8 files changed

Lines changed: 1295 additions & 353 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ run xdg-desktop-portal while we work on upstreaming the changes.
3232
- daemon: Add support for CTAP2 hybrid over BLE behind a feature flag.
3333
- daemon: Deduplicate USB state events emitted over D-Bus.
3434
- daemon: Don't use hybrid when not available
35+
- daemon: Cancel other transports, if one succeeded/failed
3536
- ui: Add Georgian translations. (Thank you, @EkaterinePopova!)
3637
- ui: Add a portal backend API to credentialsd-ui.
3738
- ui: Allow setting client PIN during the flow when required.

‎Cargo.lock‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎credentialsd/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ring = "=0.17.14"
2525
serde_json = "=1.0.151"
2626
tokio = { version = "=1.53.1", features = ["rt-multi-thread"] }
2727
tokio-stream = "=0.1.19"
28+
tokio-util = "=0.7.13"
2829

2930
[dev-dependencies]
3031
gio = "=0.22.8"

‎credentialsd/src/credential_service/hybrid.rs‎

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ use tokio::sync::{
77
broadcast,
88
mpsc::{self, Sender},
99
};
10+
use tokio_util::sync::CancellationToken;
1011
use tracing::{debug, error};
1112

12-
use libwebauthn::transport::cable::channel::{CableUpdate, CableUxUpdate};
1313
use libwebauthn::transport::cable::qr_code_device::{
1414
CableQrCodeDevice, CableTransports, QrCodeOperationHint,
1515
};
1616
use libwebauthn::transport::{Channel, ChannelSettings, Device};
1717
use libwebauthn::webauthn::{WebAuthn, error::WebAuthnError};
18+
use libwebauthn::{
19+
proto::CtapError,
20+
transport::cable::channel::{CableUpdate, CableUxUpdate},
21+
};
1822

1923
use credentialsd_common::{
2024
memfd::write_secret,
@@ -29,6 +33,7 @@ pub(crate) trait HybridHandler {
2933
fn start(
3034
&self,
3135
request: &CredentialRequest,
36+
cancellation: CancellationToken,
3237
) -> impl Stream<Item = HybridEvent> + Unpin + Send + Sized + 'static;
3338
}
3439

@@ -44,6 +49,7 @@ impl HybridHandler for InternalHybridHandler {
4449
fn start(
4550
&self,
4651
request: &CredentialRequest,
52+
cancellation: CancellationToken,
4753
) -> impl Stream<Item = HybridEvent> + Unpin + Send + Sized + 'static {
4854
tracing::debug!("Starting hybrid operation");
4955
let request = request.clone();
@@ -93,65 +99,65 @@ impl HybridHandler for InternalHybridHandler {
9399
debug!("Reached end of Hybrid updates stream.");
94100
});
95101

96-
tracing::debug!("Polling hybrid channel for updates.");
97-
let response: Result<AuthenticatorResponse, Error> = loop {
98-
match &request {
99-
CredentialRequest::CreatePublicKeyCredentialRequest(make_request) => {
100-
match channel.webauthn_make_credential(make_request).await {
101-
Ok(response) => break Ok(response.into()),
102-
Err(WebAuthnError::Ctap(ctap_error)) => {
103-
if ctap_error.is_retryable_user_error() {
104-
tracing::debug!(
105-
"Retrying credential creation operation because of CTAP error: {:?}",
106-
ctap_error
107-
);
108-
continue;
109-
} else {
110-
tracing::error!(
111-
"Received CTAP unrecoverable CTAP error: {:?}",
112-
ctap_error
113-
);
114-
break Err(Error::AuthenticatorError);
115-
}
116-
}
117-
Err(err) => {
118-
tracing::error!(
119-
"Received unrecoverable error from authenticator: {:?}",
120-
err
121-
);
122-
break Err(Error::AuthenticatorError);
123-
}
124-
};
102+
let wait_for_response_fut = async {
103+
loop {
104+
let response: Result<AuthenticatorResponse, _> = match &request {
105+
CredentialRequest::CreatePublicKeyCredentialRequest(make_request) => {
106+
channel
107+
.webauthn_make_credential(make_request)
108+
.await
109+
.map(|response| response.into())
110+
}
111+
CredentialRequest::GetPublicKeyCredentialRequest(get_request) => {
112+
channel
113+
.webauthn_get_assertion(get_request)
114+
.await
115+
.map(|response| response.into())
116+
}
117+
};
118+
match response {
119+
Ok(response) => {
120+
tracing::debug!("Received credential from hybrid authenticator");
121+
break Ok(response);
122+
}
123+
Err(WebAuthnError::Ctap(ctap_error))
124+
if ctap_error.is_retryable_user_error() =>
125+
{
126+
tracing::debug!(%ctap_error, "Retrying WebAuthn operation");
127+
continue;
128+
}
129+
Err(err) => {
130+
tracing::error!(%err,
131+
"Failed to make/get credential with hybrid authenticator"
132+
);
133+
break Err(err);
134+
}
135+
}
136+
}
137+
.map_err(|err| match err {
138+
WebAuthnError::Ctap(CtapError::PINAuthBlocked) => {
139+
Error::PinAttemptsExhausted
125140
}
126-
CredentialRequest::GetPublicKeyCredentialRequest(get_request) => {
127-
match channel.webauthn_get_assertion(get_request).await {
128-
Ok(response) => break Ok(response.into()),
129-
Err(WebAuthnError::Ctap(ctap_error)) => {
130-
if ctap_error.is_retryable_user_error() {
131-
tracing::debug!(
132-
"Retrying assertion operation because of CTAP error: {:?}",
133-
ctap_error
134-
);
135-
continue;
136-
} else {
137-
tracing::error!(
138-
"Received CTAP unrecoverable CTAP error: {:?}",
139-
ctap_error
140-
);
141-
break Err(Error::AuthenticatorError);
142-
}
143-
}
144-
Err(err) => {
145-
tracing::error!(
146-
"Received unrecoverable error from authenticator: {:?}",
147-
err
148-
);
149-
break Err(Error::AuthenticatorError);
150-
}
151-
};
141+
WebAuthnError::Ctap(CtapError::NoCredentials) => Error::NoCredentials,
142+
WebAuthnError::Ctap(CtapError::CredentialExcluded) => {
143+
Error::CredentialExcluded
152144
}
145+
_ => Error::AuthenticatorError,
146+
})
147+
};
148+
149+
tracing::debug!("Polling hybrid channel for updates.");
150+
let response = match cancellation
151+
.run_until_cancelled(wait_for_response_fut)
152+
.await
153+
{
154+
Some(resp) => resp,
155+
None => {
156+
tracing::debug!("Hybrid handler cancelled, stopping processing");
157+
Err(Error::Internal("Request cancelled".to_string()))
153158
}
154159
};
160+
155161
let terminal_state = match response {
156162
Ok(auth_response) => HybridStateInternal::Completed(Box::new(auth_response)),
157163
Err(_) => HybridStateInternal::Failed,

0 commit comments

Comments
 (0)