Skip to content

Commit 625f96b

Browse files
authored
Merge pull request #223 from msirringhaus/more_cancellation
Cancellation part 2: Don't emit failed states on cancellation
2 parents 861e04c + 0b7047b commit 625f96b

6 files changed

Lines changed: 343 additions & 179 deletions

File tree

‎credentialsd-common/src/model.rs‎

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -105,55 +105,6 @@ pub enum PinNotSetError {
105105
PinNotSet,
106106
}
107107

108-
#[derive(Debug, Clone)]
109-
pub enum Error {
110-
/// Some unknown error with the authenticator occurred.
111-
AuthenticatorError,
112-
/// No matching credentials were found on the device.
113-
NoCredentials,
114-
/// Credential was already registered with this device (credential ID contained in excludeCredentials)
115-
CredentialExcluded,
116-
/// Too many incorrect PIN attempts, and authenticator must be removed and
117-
/// reinserted to continue any more PIN attempts.
118-
///
119-
/// Note that this is different than exhausting the PIN count that fully
120-
/// locks out the device.
121-
PinAttemptsExhausted,
122-
// TODO: We may want to hide the details on this variant from the public API.
123-
/// Something went wrong with the credential service itself, not the authenticator.
124-
Internal(String),
125-
}
126-
127-
impl std::error::Error for Error {}
128-
129-
impl Display for Error {
130-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
131-
match self {
132-
Self::AuthenticatorError => f.write_str("AuthenticatorError"),
133-
Self::NoCredentials => f.write_str("NoCredentials"),
134-
Self::CredentialExcluded => f.write_str("CredentialExcluded"),
135-
Self::PinAttemptsExhausted => f.write_str("PinAttemptsExhausted"),
136-
Self::Internal(s) => write!(f, "InternalError: {s}"),
137-
}
138-
}
139-
}
140-
141-
impl TryFrom<&Value<'_>> for Error {
142-
type Error = zvariant::Error;
143-
144-
fn try_from(value: &Value<'_>) -> Result<Self, Self::Error> {
145-
let err_code: &str = value.downcast_ref()?;
146-
let err = match err_code {
147-
"AuthenticatorError" => crate::model::Error::AuthenticatorError,
148-
"NoCredentials" => crate::model::Error::NoCredentials,
149-
"CredentialExcluded" => crate::model::Error::CredentialExcluded,
150-
"PinAttemptsExhausted" => crate::model::Error::PinAttemptsExhausted,
151-
s => crate::model::Error::Internal(String::from(s)),
152-
};
153-
Ok(err)
154-
}
155-
}
156-
157108
#[derive(Debug, PartialEq, SerializeDict, DeserializeDict, Type)]
158109
#[zvariant(signature = "dict")]
159110
pub struct NotifyNeedsPinOptions {}

‎credentialsd/src/credential_service/hybrid.rs‎

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ use tokio::sync::{
2121
use tokio_util::sync::CancellationToken;
2222
use tracing::{debug, error};
2323

24-
use credentialsd_common::{
25-
memfd::write_secret,
26-
model::{BackgroundEvent, Error},
27-
};
24+
use super::CredentialServiceError;
25+
use credentialsd_common::{memfd::write_secret, model::BackgroundEvent};
2826

2927
use crate::model::{CredentialRequest, CredentialResponse};
3028

@@ -148,13 +146,15 @@ impl HybridHandler for InternalHybridHandler {
148146
}
149147
.map_err(|err| match err {
150148
WebAuthnError::Ctap(CtapError::PINAuthBlocked) => {
151-
Error::PinAttemptsExhausted
149+
CredentialServiceError::PinAttemptsExhausted
150+
}
151+
WebAuthnError::Ctap(CtapError::NoCredentials) => {
152+
CredentialServiceError::NoCredentials
152153
}
153-
WebAuthnError::Ctap(CtapError::NoCredentials) => Error::NoCredentials,
154154
WebAuthnError::Ctap(CtapError::CredentialExcluded) => {
155-
Error::CredentialExcluded
155+
CredentialServiceError::CredentialExcluded
156156
}
157-
_ => Error::AuthenticatorError,
157+
_ => CredentialServiceError::AuthenticatorError,
158158
})
159159
};
160160

@@ -166,15 +166,26 @@ impl HybridHandler for InternalHybridHandler {
166166
Some(resp) => resp,
167167
None => {
168168
tracing::debug!("Hybrid handler cancelled, stopping processing");
169-
Err(Error::Internal("Request cancelled".to_string()))
169+
Err(CredentialServiceError::RequestCancelled)
170170
}
171171
};
172172

173173
let terminal_state = match response {
174-
Ok(auth_response) => HybridStateInternal::Completed(auth_response),
175-
Err(err) => HybridStateInternal::Failed(err),
174+
Ok(auth_response) => Some(HybridStateInternal::Completed(auth_response)),
175+
Err(CredentialServiceError::RequestCancelled) => {
176+
// Cancelled by another transport winning or an explicit user cancel.
177+
// Do not emit a Failed state — complete_request was already called
178+
// by the winning path, and emitting Failed here would produce a
179+
// spurious ErrorAuthenticator in the UI and a redundant
180+
// complete_request invocation.
181+
tracing::debug!("Hybrid handler cancelled, exiting silently");
182+
None
183+
}
184+
Err(err) => Some(HybridStateInternal::Failed(err)),
176185
};
177-
if let Err(err) = tx.send(terminal_state).await {
186+
if let Some(state) = terminal_state
187+
&& let Err(err) = tx.send(state).await
188+
{
178189
tracing::error!("Failed to send caBLE update: {:?}", err)
179190
}
180191
});
@@ -203,11 +214,7 @@ pub(super) enum HybridStateInternal {
203214
/// Authenticator data
204215
Completed(CredentialResponse),
205216

206-
Failed(Error),
207-
// TODO(cancellation)
208-
// This isn't actually sent from the server.
209-
#[allow(dead_code)]
210-
UserCancelled,
217+
Failed(CredentialServiceError),
211218
}
212219

213220
// this is here to prevent making HybridStateInternal public to the whole crate.
@@ -233,10 +240,7 @@ pub enum HybridState {
233240
Completed,
234241

235242
/// Hybrid operation failed.
236-
Failed(Error),
237-
238-
// This isn't actually sent from the server.
239-
UserCancelled,
243+
Failed(CredentialServiceError),
240244
}
241245

242246
impl From<HybridStateInternal> for HybridState {
@@ -246,7 +250,6 @@ impl From<HybridStateInternal> for HybridState {
246250
HybridStateInternal::Connecting => HybridState::Connecting,
247251
HybridStateInternal::Connected => HybridState::Connected,
248252
HybridStateInternal::Completed(_) => HybridState::Completed,
249-
HybridStateInternal::UserCancelled => HybridState::UserCancelled,
250253
HybridStateInternal::Failed(err) => HybridState::Failed(err),
251254
}
252255
}
@@ -269,14 +272,25 @@ impl From<&HybridState> for BackgroundEvent {
269272
HybridState::Connecting => BackgroundEvent::HybridConnecting,
270273
HybridState::Connected => BackgroundEvent::HybridConnected,
271274
HybridState::Completed => BackgroundEvent::CeremonyCompleted,
272-
HybridState::UserCancelled => BackgroundEvent::ErrorCancelled,
273-
HybridState::Failed(Error::AuthenticatorError) => BackgroundEvent::ErrorAuthenticator,
274-
HybridState::Failed(Error::NoCredentials) => BackgroundEvent::ErrorNoCredentials,
275-
HybridState::Failed(Error::CredentialExcluded) => {
275+
HybridState::Failed(CredentialServiceError::AuthenticatorError) => {
276+
BackgroundEvent::ErrorAuthenticator
277+
}
278+
HybridState::Failed(CredentialServiceError::NoCredentials) => {
279+
BackgroundEvent::ErrorNoCredentials
280+
}
281+
HybridState::Failed(CredentialServiceError::CredentialExcluded) => {
276282
BackgroundEvent::ErrorCredentialExcluded
277283
}
278-
HybridState::Failed(Error::PinAttemptsExhausted) => BackgroundEvent::ErrorAuthenticator,
279-
HybridState::Failed(Error::Internal(_)) => BackgroundEvent::ErrorInternal,
284+
HybridState::Failed(CredentialServiceError::PinAttemptsExhausted) => {
285+
BackgroundEvent::ErrorAuthenticator
286+
}
287+
// This should currently never be reached, but we'll likely use it in future refactoring
288+
HybridState::Failed(CredentialServiceError::RequestCancelled) => {
289+
BackgroundEvent::ErrorCancelled
290+
}
291+
HybridState::Failed(CredentialServiceError::Internal(_)) => {
292+
BackgroundEvent::ErrorInternal
293+
}
280294
}
281295
}
282296
}
@@ -302,7 +316,9 @@ async fn handle_hybrid_updates(
302316
CableUpdate::Connected => Some(HybridStateInternal::Connected),
303317
CableUpdate::Error(transport_error) => {
304318
error!(?transport_error, "Hybrid transport error");
305-
Some(HybridStateInternal::Failed(Error::AuthenticatorError))
319+
Some(HybridStateInternal::Failed(
320+
CredentialServiceError::AuthenticatorError,
321+
))
306322
}
307323
},
308324
};

0 commit comments

Comments
 (0)