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
use std::ops::{Deref, DerefMut};

use crate::{transport::Token, ui::UiCallback};

use super::{
    commands::GetInfoResponse, ctap21_bio::BiometricAuthenticatorInfo,
    ctap21_cred::CredentialManagementAuthenticatorInfo, internal::CtapAuthenticatorVersion,
    Ctap20Authenticator,
};

#[cfg(any(all(doc, not(doctest)), feature = "ctap2-management"))]
use super::commands::{PrototypeBioEnrollmentRequest, PrototypeCredentialManagementRequest};

/// CTAP 2.1-PRE protocol implementation.
///
/// This contains only CTAP 2.1-PRE-specific functionality. All CTAP 2.0
/// functionality is avaliable via a [Deref] to [Ctap20Authenticator].
#[derive(Debug)]
pub struct Ctap21PreAuthenticator<'a, T: Token, U: UiCallback> {
    authenticator: Ctap20Authenticator<'a, T, U>,
}

/// For backwards compatibility, pretend to be a
/// [CTAP 2.0 authenticator][Ctap20Authenticator].
impl<'a, T: Token, U: UiCallback> Deref for Ctap21PreAuthenticator<'a, T, U> {
    type Target = Ctap20Authenticator<'a, T, U>;

    fn deref(&self) -> &Self::Target {
        &self.authenticator
    }
}

impl<'a, T: Token, U: UiCallback> DerefMut for Ctap21PreAuthenticator<'a, T, U> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.authenticator
    }
}

impl<'a, T: Token, U: UiCallback> CtapAuthenticatorVersion<'a, T, U>
    for Ctap21PreAuthenticator<'a, T, U>
{
    const VERSION: &'static str = "FIDO_2_1_PRE";
    fn new_with_info(info: GetInfoResponse, token: T, ui_callback: &'a U) -> Self {
        Self {
            authenticator: Ctap20Authenticator::new_with_info(info, token, ui_callback),
        }
    }
}

impl<'a, T: Token, U: UiCallback> BiometricAuthenticatorInfo<U>
    for Ctap21PreAuthenticator<'a, T, U>
{
    #[cfg(any(all(doc, not(doctest)), feature = "ctap2-management"))]
    type RequestType = PrototypeBioEnrollmentRequest;

    #[inline]
    fn biometrics(&self) -> Option<bool> {
        self.info.ctap21pre_biometrics()
    }
}

impl<'a, T: Token, U: UiCallback> CredentialManagementAuthenticatorInfo<U>
    for Ctap21PreAuthenticator<'a, T, U>
{
    #[cfg(any(all(doc, not(doctest)), feature = "ctap2-management"))]
    type RequestType = PrototypeCredentialManagementRequest;

    #[inline]
    fn supports_credential_management(&self) -> bool {
        self.info.ctap21pre_credential_management()
    }
}