added feature assert_devices
This commit is contained in:
44
src/util.rs
Normal file
44
src/util.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use crate::cbor::AuthenticatorData;
|
||||
use crate::{FidoAssertionRequest, FidoCredential, FidoDevice, FidoErrorKind, FidoResult};
|
||||
use std::sync::mpsc::channel;
|
||||
#[cfg(feature = "assert_devices")]
|
||||
use crossbeam::thread;
|
||||
|
||||
/// Will send the `assertion` to all supplied `devices` and return either the first successful assertion or the last error
|
||||
#[cfg(feature = "assert_devices")]
|
||||
pub fn get_assertion_devices<'a>(
|
||||
assertion: &'a FidoAssertionRequest,
|
||||
devices: impl Iterator<Item = &'a mut FidoDevice>,
|
||||
) -> FidoResult<(&'a FidoCredential, AuthenticatorData)> {
|
||||
thread::scope(
|
||||
|scope| -> FidoResult<(&'a FidoCredential, AuthenticatorData)> {
|
||||
let (tx, rx) = channel();
|
||||
let handles = devices
|
||||
.map(|device| {
|
||||
let cancel = device.cancel_handle()?;
|
||||
let tx = tx.clone();
|
||||
let thread_handle =
|
||||
scope.spawn(move |_| tx.send(device.get_assertion(assertion)));
|
||||
Ok((cancel, thread_handle))
|
||||
})
|
||||
.collect::<FidoResult<Vec<_>>>()?;
|
||||
|
||||
let mut err = None;
|
||||
for res in rx.iter().take(handles.len()) {
|
||||
match res {
|
||||
Ok(_) => {
|
||||
for (mut cancel, join) in handles {
|
||||
// Canceling out of courtesy don't care if it fails
|
||||
let _ = cancel.cancel();
|
||||
let _ = join.join();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
e => err = Some(e),
|
||||
}
|
||||
}
|
||||
err.unwrap_or(Err(FidoErrorKind::DeviceUnsupported.into()))
|
||||
},
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
Reference in New Issue
Block a user