generic request_multiple_devices

This commit is contained in:
shimun 2020-04-01 17:48:03 +02:00
parent 501b28e0d9
commit 1202fed98d
Signed by: shimun
GPG Key ID: E81D8382DC2F971B

View File

@ -1,24 +1,24 @@
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;
/// 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")]
pub fn get_assertion_devices<'a>( pub fn request_multiple_devices<
assertion: &'a FidoAssertionRequest, 'a,
devices: impl Iterator<Item = &'a mut FidoDevice>, T: Send + 'a,
) -> FidoResult<(&'a FidoCredential, AuthenticatorData)> { F: Fn(&mut FidoDevice) -> FidoResult<T> + 'a + Sync,
thread::scope( >(
|scope| -> FidoResult<(&'a FidoCredential, AuthenticatorData)> { devices: impl Iterator<Item = (&'a mut FidoDevice, &'a F)>,
) -> FidoResult<T> {
thread::scope(|scope| -> FidoResult<T> {
let (tx, rx) = channel(); let (tx, rx) = channel();
let handles = devices let handles = devices
.map(|device| { .map(|(device, fn_)| {
let cancel = device.cancel_handle()?; let cancel = device.cancel_handle()?;
let tx = tx.clone(); let tx = tx.clone();
let thread_handle = let thread_handle = scope.spawn(move |_| tx.send(fn_(device)));
scope.spawn(move |_| tx.send(device.get_assertion(assertion)));
Ok((cancel, thread_handle)) Ok((cancel, thread_handle))
}) })
.collect::<FidoResult<Vec<_>>>()?; .collect::<FidoResult<Vec<_>>>()?;
@ -38,7 +38,21 @@ pub fn get_assertion_devices<'a>(
} }
} }
err.unwrap_or(Err(FidoErrorKind::DeviceUnsupported.into())) err.unwrap_or(Err(FidoErrorKind::DeviceUnsupported.into()))
}, })
)
.unwrap() .unwrap()
} }
/// 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)> {
let get_assertion = |device: &mut FidoDevice| device.get_assertion(assertion);
request_multiple_devices(devices.map(|device| {
(
device,
&get_assertion,
)
}))
}