Skip to main content

authenticator_ctap2_2021/
status_update.rs

1use super::{u2ftypes, Pin, PinError};
2use serde::ser::{Serialize, SerializeStruct};
3use std::sync::mpsc::Sender;
4
5#[derive(Debug)]
6pub enum StatusUpdate {
7    /// Device found
8    DeviceAvailable { dev_info: u2ftypes::U2FDeviceInfo },
9    /// Device got removed
10    DeviceUnavailable { dev_info: u2ftypes::U2FDeviceInfo },
11    /// We successfully finished the register or sign request
12    Success { dev_info: u2ftypes::U2FDeviceInfo },
13    /// Sent if a PIN is needed (or was wrong), or some other kind of PIN-related
14    /// error occurred. The Sender is for sending back a PIN (if needed).
15    PinError(PinError, Sender<Pin>),
16    /// Sent, if multiple devices are found and the user has to select one
17    SelectDeviceNotice,
18    /// Sent, once a device was selected (either automatically or by user-interaction)
19    /// and the register or signing process continues with this device
20    DeviceSelected(u2ftypes::U2FDeviceInfo),
21}
22
23impl Serialize for StatusUpdate {
24    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
25    where
26        S: serde::Serializer,
27    {
28        let mut map = serializer.serialize_struct("StatusUpdate", 1)?;
29        match &*self {
30            StatusUpdate::DeviceAvailable { dev_info } => {
31                map.serialize_field("DeviceAvailable", &dev_info)?
32            }
33            StatusUpdate::DeviceUnavailable { dev_info } => {
34                map.serialize_field("DeviceUnavailable", &dev_info)?
35            }
36            StatusUpdate::Success { dev_info } => map.serialize_field("Success", &dev_info)?,
37            StatusUpdate::PinError(e, _) => map.serialize_field("PinError", &e)?,
38            StatusUpdate::SelectDeviceNotice => map.serialize_field("SelectDeviceNotice", &())?,
39            StatusUpdate::DeviceSelected(dev_info) => {
40                map.serialize_field("DeviceSelected", &dev_info)?
41            }
42        }
43        map.end()
44    }
45}
46
47pub(crate) fn send_status(status: &Sender<StatusUpdate>, msg: StatusUpdate) {
48    match status.send(msg) {
49        Ok(_) => {}
50        Err(e) => error!("Couldn't send status: {:?}", e),
51    };
52}
53
54#[cfg(test)]
55pub mod tests {
56    use crate::consts::U2F_AUTHENTICATE;
57
58    use super::*;
59    use crate::consts::Capability;
60    use serde_json::to_string;
61    use std::sync::mpsc::channel;
62
63    #[test]
64    fn serialize_select() {
65        let st = StatusUpdate::SelectDeviceNotice;
66        let json = to_string(&st).expect("Failed to serialize");
67        assert_eq!(&json, r#"{"SelectDeviceNotice":null}"#);
68    }
69
70    #[test]
71    fn serialize_invalid_pin() {
72        let (tx, _rx) = channel();
73        let st = StatusUpdate::PinError(PinError::InvalidPin(Some(3)), tx.clone());
74        let json = to_string(&st).expect("Failed to serialize");
75        assert_eq!(&json, r#"{"PinError":{"InvalidPin":3}}"#);
76
77        let st = StatusUpdate::PinError(PinError::InvalidPin(None), tx);
78        let json = to_string(&st).expect("Failed to serialize");
79        assert_eq!(&json, r#"{"PinError":{"InvalidPin":null}}"#);
80    }
81
82    #[test]
83    fn serialize_success() {
84        let cap = Capability::WINK | Capability::CBOR;
85        let dev = u2ftypes::U2FDeviceInfo {
86            vendor_name: String::from("ABC").into_bytes(),
87            device_name: String::from("DEF").into_bytes(),
88            version_interface: 2,
89            version_major: 5,
90            version_minor: 4,
91            version_build: 3,
92            cap_flags: cap,
93        };
94        let st = StatusUpdate::Success { dev_info: dev };
95        let json = to_string(&st).expect("Failed to serialize");
96        assert_eq!(
97            &json,
98            r#"{"Success":{"vendor_name":[65,66,67],"device_name":[68,69,70],"version_interface":2,"version_major":5,"version_minor":4,"version_build":3,"cap_flags":{"bits":5}}}"#
99        );
100    }
101}