added timeout option
This commit is contained in:
parent
cc48719cfa
commit
a00f8fbe3b
@ -6,6 +6,7 @@ use ctap::{
|
|||||||
|
|
||||||
use hex;
|
use hex;
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
const RP_ID: &str = "ctap_demo";
|
const RP_ID: &str = "ctap_demo";
|
||||||
|
|
||||||
@ -40,7 +41,8 @@ fn main() -> ctap::FidoResult<()> {
|
|||||||
.map(|handle| FidoDevice::new(&handle))
|
.map(|handle| FidoDevice::new(&handle))
|
||||||
.collect::<FidoResult<Vec<_>>>()?;
|
.collect::<FidoResult<Vec<_>>>()?;
|
||||||
// run with --features request_multiple
|
// run with --features request_multiple
|
||||||
let (cred, _) = ctap::get_assertion_devices(&req, devices.iter_mut())?;
|
let (cred, _) =
|
||||||
|
ctap::get_assertion_devices(&req, devices.iter_mut(), Some(Duration::from_secs(10)))?;
|
||||||
println!("Success, got assertion for: {}", hex::encode(&cred.id));
|
println!("Success, got assertion for: {}", hex::encode(&cred.id));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,8 @@ pub struct CborErrorCode(u8);
|
|||||||
pub enum FidoErrorKind {
|
pub enum FidoErrorKind {
|
||||||
#[fail(display = "Read/write error with device.")]
|
#[fail(display = "Read/write error with device.")]
|
||||||
Io,
|
Io,
|
||||||
|
#[fail(display = "Operation timed out")]
|
||||||
|
Timeout,
|
||||||
#[fail(display = "Error while reading packet from device.")]
|
#[fail(display = "Error while reading packet from device.")]
|
||||||
ReadPacket,
|
ReadPacket,
|
||||||
#[fail(display = "Error while writing packet to device.")]
|
#[fail(display = "Error while writing packet to device.")]
|
||||||
|
55
src/util.rs
55
src/util.rs
@ -7,7 +7,8 @@ use crate::{
|
|||||||
use crossbeam::thread;
|
use crossbeam::thread;
|
||||||
#[cfg(feature = "request_multiple")]
|
#[cfg(feature = "request_multiple")]
|
||||||
use std::sync::mpsc::channel;
|
use std::sync::mpsc::channel;
|
||||||
|
#[cfg(feature = "request_multiple")]
|
||||||
|
use std::time::Duration;
|
||||||
#[cfg(feature = "request_multiple")]
|
#[cfg(feature = "request_multiple")]
|
||||||
pub fn request_multiple_devices<
|
pub fn request_multiple_devices<
|
||||||
'a,
|
'a,
|
||||||
@ -15,6 +16,7 @@ pub fn request_multiple_devices<
|
|||||||
F: Fn(&mut FidoDevice) -> FidoResult<T> + 'a + Sync,
|
F: Fn(&mut FidoDevice) -> FidoResult<T> + 'a + Sync,
|
||||||
>(
|
>(
|
||||||
devices: impl Iterator<Item = (&'a mut FidoDevice, &'a F)>,
|
devices: impl Iterator<Item = (&'a mut FidoDevice, &'a F)>,
|
||||||
|
timeout: Option<Duration>,
|
||||||
) -> FidoResult<T> {
|
) -> FidoResult<T> {
|
||||||
thread::scope(|scope| -> FidoResult<T> {
|
thread::scope(|scope| -> FidoResult<T> {
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
@ -26,22 +28,41 @@ pub fn request_multiple_devices<
|
|||||||
Ok((cancel, thread_handle))
|
Ok((cancel, thread_handle))
|
||||||
})
|
})
|
||||||
.collect::<FidoResult<Vec<_>>>()?;
|
.collect::<FidoResult<Vec<_>>>()?;
|
||||||
|
|
||||||
let mut err = None;
|
let mut err = None;
|
||||||
for res in rx.iter().take(handles.len()) {
|
let mut slept = Duration::from_millis(0);
|
||||||
match res {
|
let interval = Duration::from_millis(10);
|
||||||
Ok(_) => {
|
let mut received = 0usize;
|
||||||
for (mut cancel, join) in handles {
|
let res = loop {
|
||||||
// Canceling out of courtesy don't care if it fails
|
if timeout.map(|t| t < slept).unwrap_or(true) {
|
||||||
let _ = cancel.cancel();
|
break if let Some(cause) = err {
|
||||||
let _ = join.join();
|
cause
|
||||||
}
|
} else {
|
||||||
return res;
|
Err(FidoErrorKind::Timeout.into())
|
||||||
}
|
};
|
||||||
e => err = Some(e),
|
|
||||||
}
|
}
|
||||||
|
if received == handles.len() {
|
||||||
|
break err.unwrap();
|
||||||
|
}
|
||||||
|
if let Ok(msg) = rx.recv_timeout(interval) {
|
||||||
|
received += 1;
|
||||||
|
match msg {
|
||||||
|
e @ Err(_) => {
|
||||||
|
err = Some(e);
|
||||||
|
}
|
||||||
|
res @ Ok(_) => {
|
||||||
|
break res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
slept += interval;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
for (mut cancel, join) in handles {
|
||||||
|
// Canceling out of courtesy don't care if it fails
|
||||||
|
let _ = cancel.cancel();
|
||||||
|
let _ = join.join();
|
||||||
}
|
}
|
||||||
err.unwrap_or(Err(FidoErrorKind::DeviceUnsupported.into()))
|
res
|
||||||
})
|
})
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
@ -51,9 +72,10 @@ pub fn request_multiple_devices<
|
|||||||
pub fn get_assertion_devices<'a>(
|
pub fn get_assertion_devices<'a>(
|
||||||
assertion_request: &'a FidoAssertionRequest,
|
assertion_request: &'a FidoAssertionRequest,
|
||||||
devices: impl Iterator<Item = &'a mut FidoDevice>,
|
devices: impl Iterator<Item = &'a mut FidoDevice>,
|
||||||
|
timeout: Option<Duration>,
|
||||||
) -> FidoResult<(&'a FidoCredential, AuthenticatorData)> {
|
) -> FidoResult<(&'a FidoCredential, AuthenticatorData)> {
|
||||||
let get_assertion = |device: &mut FidoDevice| device.get_assertion(assertion_request);
|
let get_assertion = |device: &mut FidoDevice| device.get_assertion(assertion_request);
|
||||||
request_multiple_devices(devices.map(|device| (device, &get_assertion)))
|
request_multiple_devices(devices.map(|device| (device, &get_assertion)), timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Will send the `credential_request` to all supplied `devices` and return either the first credential or the last error
|
/// Will send the `credential_request` to all supplied `devices` and return either the first credential or the last error
|
||||||
@ -61,7 +83,8 @@ pub fn get_assertion_devices<'a>(
|
|||||||
pub fn make_credential_devices<'a>(
|
pub fn make_credential_devices<'a>(
|
||||||
credential_request: &'a FidoCredentialRequest,
|
credential_request: &'a FidoCredentialRequest,
|
||||||
devices: impl Iterator<Item = &'a mut FidoDevice>,
|
devices: impl Iterator<Item = &'a mut FidoDevice>,
|
||||||
|
timeout: Option<Duration>,
|
||||||
) -> FidoResult<FidoCredential> {
|
) -> FidoResult<FidoCredential> {
|
||||||
let make_credential = |device: &mut FidoDevice| device.make_credential(credential_request);
|
let make_credential = |device: &mut FidoDevice| device.make_credential(credential_request);
|
||||||
request_multiple_devices(devices.map(|device| (device, &make_credential)))
|
request_multiple_devices(devices.map(|device| (device, &make_credential)), timeout)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user