@@ -7,14 +7,18 @@ use tokio::sync::{
77 broadcast,
88 mpsc:: { self , Sender } ,
99} ;
10+ use tokio_util:: sync:: CancellationToken ;
1011use tracing:: { debug, error} ;
1112
12- use libwebauthn:: transport:: cable:: channel:: { CableUpdate , CableUxUpdate } ;
1313use libwebauthn:: transport:: cable:: qr_code_device:: {
1414 CableQrCodeDevice , CableTransports , QrCodeOperationHint ,
1515} ;
1616use libwebauthn:: transport:: { Channel , ChannelSettings , Device } ;
1717use libwebauthn:: webauthn:: { WebAuthn , error:: WebAuthnError } ;
18+ use libwebauthn:: {
19+ proto:: CtapError ,
20+ transport:: cable:: channel:: { CableUpdate , CableUxUpdate } ,
21+ } ;
1822
1923use 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