1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! Abstraction to merge all available transports for the platform.
//!
//! This is still a work in progress, and doesn't yet handle tokens quite as
//! well as we'd like.
#[cfg(doc)]
use crate::stubs::*;

use async_stream::stream;
use futures::{stream::FusedStream, StreamExt};

#[cfg(any(all(doc, not(doctest)), feature = "bluetooth"))]
use crate::bluetooth::*;
#[cfg(any(all(doc, not(doctest)), feature = "nfc"))]
use crate::nfc::*;
use crate::transport::*;
#[cfg(any(all(doc, not(doctest)), feature = "usb"))]
use crate::usb::*;

/// [AnyTransport] merges all available transports for the platform.
#[derive(Debug)]
pub struct AnyTransport {
    #[cfg(any(all(doc, not(doctest)), feature = "bluetooth"))]
    pub bluetooth: BluetoothTransport,
    #[cfg(any(all(doc, not(doctest)), feature = "nfc"))]
    pub nfc: Option<NFCTransport>,
    #[cfg(any(all(doc, not(doctest)), feature = "usb"))]
    pub usb: USBTransport,
}

/// [AnyToken] abstracts calls to physical authenticators.
#[derive(Debug)]
pub enum AnyToken {
    /// No-op stub entry, never used.
    Stub,
    #[cfg(any(all(doc, not(doctest)), feature = "bluetooth"))]
    Bluetooth(BluetoothToken),
    #[cfg(any(all(doc, not(doctest)), feature = "nfc"))]
    Nfc(NFCCard),
    #[cfg(any(all(doc, not(doctest)), feature = "usb"))]
    Usb(USBToken),
}

#[derive(Debug)]
pub enum AnyTokenId {
    /// No-op stub entry, never used.
    Stub,
    #[cfg(any(all(doc, not(doctest)), feature = "bluetooth"))]
    Bluetooth(<BluetoothToken as Token>::Id),
    #[cfg(any(all(doc, not(doctest)), feature = "nfc"))]
    Nfc(<NFCCard as Token>::Id),
    #[cfg(any(all(doc, not(doctest)), feature = "usb"))]
    Usb(<USBToken as Token>::Id),
}

impl AnyTransport {
    /// Creates connections to all available transports.
    ///
    /// For NFC, uses `Scope::User`, and [ignores unavailability of the PC/SC
    /// Service][0].
    ///
    /// [0]: crate::nfc#smart-card-service
    pub async fn new() -> Result<Self, WebauthnCError> {
        Ok(AnyTransport {
            #[cfg(feature = "bluetooth")]
            bluetooth: BluetoothTransport::new().await?,
            #[cfg(feature = "nfc")]
            nfc: match NFCTransport::new(pcsc::Scope::User) {
                Ok(reader) => Some(reader),
                Err(e) => {
                    warn!("PC/SC service not available ({e:?}), continuing without NFC support...");
                    None
                }
            },
            #[cfg(feature = "usb")]
            usb: USBTransport::new().await?,
        })
    }
}

#[async_trait]
impl<'b> Transport<'b> for AnyTransport {
    type Token = AnyToken;

    #[allow(unreachable_code)]
    async fn watch(&self) -> Result<BoxStream<TokenEvent<Self::Token>>, WebauthnCError> {
        // Bluetooth
        let mut bluetooth_complete = !cfg!(feature = "bluetooth");
        #[cfg(feature = "bluetooth")]
        let bluetooth: BoxStream<TokenEvent<BluetoothToken>> = match self.bluetooth.watch().await {
            Err(e) => {
                error!("Bluetooth transport failure: {e:?}");
                bluetooth_complete = true;
                Box::pin(futures::stream::empty())
            }
            Ok(s) => s,
        };

        #[cfg(not(feature = "bluetooth"))]
        let bluetooth: BoxStream<TokenEvent<AnyToken>> = Box::pin(futures::stream::empty());

        let mut bluetooth = bluetooth.fuse();

        // NFC
        let mut nfc_complete = !cfg!(feature = "nfc");
        #[cfg(feature = "nfc")]
        let nfc: BoxStream<TokenEvent<NFCCard>> = if let Some(nfc) = &self.nfc {
            match nfc.watch().await {
                Err(e) => {
                    error!("NFC transport failure: {e:?}");
                    nfc_complete = true;
                    Box::pin(futures::stream::empty())
                }
                Ok(s) => s,
            }
        } else {
            nfc_complete = true;
            Box::pin(futures::stream::empty())
        };

        #[cfg(not(feature = "nfc"))]
        let nfc: BoxStream<TokenEvent<AnyToken>> = Box::pin(futures::stream::empty());

        let mut nfc = nfc.fuse();

        // USB HID
        let mut usb_complete = !cfg!(feature = "usb");
        #[cfg(feature = "usb")]
        let usb: BoxStream<TokenEvent<USBToken>> = match self.usb.watch().await {
            Err(e) => {
                error!("USB transport failure: {e:?}");
                usb_complete = true;
                Box::pin(futures::stream::empty())
            }
            Ok(s) => s,
        };

        #[cfg(not(feature = "usb"))]
        let usb: BoxStream<TokenEvent<AnyToken>> = Box::pin(futures::stream::empty());

        let mut usb = usb.fuse();

        if bluetooth_complete && nfc_complete && usb_complete {
            error!("no transports available!");
            return Err(WebauthnCError::NotSupported);
        }

        // Main stream
        let s = stream! {
            #[cfg(not(doc))]
            while !bluetooth.is_terminated() || !nfc.is_terminated() || !usb.is_terminated() {
                tokio::select! {
                    Some(b) = bluetooth.next() => {
                        #[cfg(feature = "bluetooth")]
                        let b: TokenEvent<AnyToken> = b.into();
                        if matches!(b, TokenEvent::EnumerationComplete) {
                            if nfc_complete && usb_complete {
                                trace!("Sending enumeration complete from Bluetooth");
                                yield TokenEvent::EnumerationComplete;
                            }
                            bluetooth_complete = true;
                        } else {
                            yield b;
                        }
                    }

                    Some(n) = nfc.next() => {
                        #[cfg(feature = "nfc")]
                        let n: TokenEvent<AnyToken> = n.into();
                        if matches!(n, TokenEvent::EnumerationComplete) {
                            if bluetooth_complete && usb_complete {
                                trace!("Sending enumeration complete from NFC");
                                yield TokenEvent::EnumerationComplete;
                            }
                            nfc_complete = true;
                        } else {
                            yield n;
                        }
                    }

                    Some(u) = usb.next() => {
                        #[cfg(feature = "usb")]
                        let u: TokenEvent<AnyToken> = u.into();
                        if matches!(u, TokenEvent::EnumerationComplete) {
                            if bluetooth_complete && nfc_complete {
                                trace!("Sending enumeration complete from USB");
                                yield TokenEvent::EnumerationComplete;
                            }
                            usb_complete = true;
                        } else {
                            yield u;
                        }
                    }

                    else => continue,
                }
            }
        };

        Ok(Box::pin(s))
    }

    async fn tokens(&self) -> Result<Vec<Self::Token>, WebauthnCError> {
        #[cfg(not(any(feature = "bluetooth", feature = "nfc", feature = "usb")))]
        {
            error!("No transports available!");
            return Err(WebauthnCError::NotSupported);
        }

        let mut o: Vec<Self::Token> = Vec::new();

        #[cfg(feature = "bluetooth")]
        o.extend(
            self.bluetooth
                .tokens()
                .await?
                .into_iter()
                .map(AnyToken::Bluetooth),
        );

        #[cfg(feature = "nfc")]
        if let Some(nfc) = &self.nfc {
            o.extend(nfc.tokens().await?.into_iter().map(AnyToken::Nfc));
        }

        #[cfg(feature = "usb")]
        o.extend(self.usb.tokens().await?.into_iter().map(AnyToken::Usb));

        Ok(o)
    }
}

#[async_trait]
#[allow(clippy::unimplemented)]
impl Token for AnyToken {
    type Id = AnyTokenId;

    #[allow(unused_variables)]
    async fn transmit_raw<U>(&mut self, cmd: &[u8], ui: &U) -> Result<Vec<u8>, WebauthnCError>
    where
        U: UiCallback,
    {
        match self {
            AnyToken::Stub => unimplemented!(),
            #[cfg(feature = "bluetooth")]
            AnyToken::Bluetooth(b) => Token::transmit_raw(b, cmd, ui).await,
            #[cfg(feature = "nfc")]
            AnyToken::Nfc(n) => Token::transmit_raw(n, cmd, ui).await,
            #[cfg(feature = "usb")]
            AnyToken::Usb(u) => Token::transmit_raw(u, cmd, ui).await,
        }
    }

    async fn init(&mut self) -> Result<(), WebauthnCError> {
        match self {
            AnyToken::Stub => unimplemented!(),
            #[cfg(feature = "bluetooth")]
            AnyToken::Bluetooth(b) => b.init().await,
            #[cfg(feature = "nfc")]
            AnyToken::Nfc(n) => n.init().await,
            #[cfg(feature = "usb")]
            AnyToken::Usb(u) => u.init().await,
        }
    }

    async fn close(&mut self) -> Result<(), WebauthnCError> {
        match self {
            AnyToken::Stub => unimplemented!(),
            #[cfg(feature = "bluetooth")]
            AnyToken::Bluetooth(b) => b.close().await,
            #[cfg(feature = "nfc")]
            AnyToken::Nfc(n) => n.close().await,
            #[cfg(feature = "usb")]
            AnyToken::Usb(u) => u.close().await,
        }
    }

    fn get_transport(&self) -> AuthenticatorTransport {
        match self {
            AnyToken::Stub => unimplemented!(),
            #[cfg(feature = "bluetooth")]
            AnyToken::Bluetooth(b) => b.get_transport(),
            #[cfg(feature = "nfc")]
            AnyToken::Nfc(n) => n.get_transport(),
            #[cfg(feature = "usb")]
            AnyToken::Usb(u) => u.get_transport(),
        }
    }

    async fn cancel(&mut self) -> Result<(), WebauthnCError> {
        match self {
            AnyToken::Stub => unimplemented!(),
            #[cfg(feature = "bluetooth")]
            AnyToken::Bluetooth(b) => b.cancel().await,
            #[cfg(feature = "nfc")]
            AnyToken::Nfc(n) => n.cancel().await,
            #[cfg(feature = "usb")]
            AnyToken::Usb(u) => u.cancel().await,
        }
    }

    fn has_button(&self) -> bool {
        match self {
            AnyToken::Stub => unimplemented!(),
            #[cfg(feature = "bluetooth")]
            AnyToken::Bluetooth(b) => b.has_button(),
            #[cfg(feature = "nfc")]
            AnyToken::Nfc(n) => n.has_button(),
            #[cfg(feature = "usb")]
            AnyToken::Usb(u) => u.has_button(),
        }
    }
}

#[cfg(feature = "bluetooth")]
impl From<TokenEvent<BluetoothToken>> for TokenEvent<AnyToken> {
    fn from(e: TokenEvent<BluetoothToken>) -> Self {
        match e {
            TokenEvent::Added(t) => TokenEvent::Added(AnyToken::Bluetooth(t)),
            TokenEvent::Removed(i) => TokenEvent::Removed(AnyTokenId::Bluetooth(i)),
            TokenEvent::EnumerationComplete => TokenEvent::EnumerationComplete,
        }
    }
}

#[cfg(feature = "nfc")]
impl From<TokenEvent<NFCCard>> for TokenEvent<AnyToken> {
    fn from(e: TokenEvent<NFCCard>) -> Self {
        match e {
            TokenEvent::Added(t) => TokenEvent::Added(AnyToken::Nfc(t)),
            TokenEvent::Removed(i) => TokenEvent::Removed(AnyTokenId::Nfc(i)),
            TokenEvent::EnumerationComplete => TokenEvent::EnumerationComplete,
        }
    }
}

#[cfg(feature = "usb")]
impl From<TokenEvent<USBToken>> for TokenEvent<AnyToken> {
    fn from(e: TokenEvent<USBToken>) -> Self {
        match e {
            TokenEvent::Added(t) => TokenEvent::Added(AnyToken::Usb(t)),
            TokenEvent::Removed(i) => TokenEvent::Removed(AnyTokenId::Usb(i)),
            TokenEvent::EnumerationComplete => TokenEvent::EnumerationComplete,
        }
    }
}