generic request_multiple_devices
This commit is contained in:
parent
501b28e0d9
commit
1202fed98d
78
src/util.rs
78
src/util.rs
@ -1,8 +1,46 @@
|
|||||||
use crate::cbor::AuthenticatorData;
|
use crate::cbor::AuthenticatorData;
|
||||||
use crate::{FidoAssertionRequest, FidoCredential, FidoDevice, FidoErrorKind, FidoResult};
|
use crate::{FidoAssertionRequest, FidoCredential, FidoDevice, FidoErrorKind, FidoResult};
|
||||||
use std::sync::mpsc::channel;
|
|
||||||
#[cfg(feature = "assert_devices")]
|
#[cfg(feature = "assert_devices")]
|
||||||
use crossbeam::thread;
|
use crossbeam::thread;
|
||||||
|
use std::sync::mpsc::channel;
|
||||||
|
|
||||||
|
#[cfg(feature = "assert_devices")]
|
||||||
|
pub fn request_multiple_devices<
|
||||||
|
'a,
|
||||||
|
T: Send + 'a,
|
||||||
|
F: Fn(&mut FidoDevice) -> FidoResult<T> + 'a + Sync,
|
||||||
|
>(
|
||||||
|
devices: impl Iterator<Item = (&'a mut FidoDevice, &'a F)>,
|
||||||
|
) -> FidoResult<T> {
|
||||||
|
thread::scope(|scope| -> FidoResult<T> {
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
let handles = devices
|
||||||
|
.map(|(device, fn_)| {
|
||||||
|
let cancel = device.cancel_handle()?;
|
||||||
|
let tx = tx.clone();
|
||||||
|
let thread_handle = scope.spawn(move |_| tx.send(fn_(device)));
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
/// Will send the `assertion` to all supplied `devices` and return either the first successful assertion or the last error
|
/// Will send the `assertion` to all supplied `devices` and return either the first successful assertion or the last error
|
||||||
#[cfg(feature = "assert_devices")]
|
#[cfg(feature = "assert_devices")]
|
||||||
@ -10,35 +48,11 @@ pub fn get_assertion_devices<'a>(
|
|||||||
assertion: &'a FidoAssertionRequest,
|
assertion: &'a FidoAssertionRequest,
|
||||||
devices: impl Iterator<Item = &'a mut FidoDevice>,
|
devices: impl Iterator<Item = &'a mut FidoDevice>,
|
||||||
) -> FidoResult<(&'a FidoCredential, AuthenticatorData)> {
|
) -> FidoResult<(&'a FidoCredential, AuthenticatorData)> {
|
||||||
thread::scope(
|
let get_assertion = |device: &mut FidoDevice| device.get_assertion(assertion);
|
||||||
|scope| -> FidoResult<(&'a FidoCredential, AuthenticatorData)> {
|
request_multiple_devices(devices.map(|device| {
|
||||||
let (tx, rx) = channel();
|
(
|
||||||
let handles = devices
|
device,
|
||||||
.map(|device| {
|
&get_assertion,
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user