bitwarden_fido/
client_fido.rs

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
use bitwarden_core::Client;
use bitwarden_vault::CipherView;
use thiserror::Error;

use crate::{
    Fido2Authenticator, Fido2Client, Fido2CredentialAutofillView, Fido2CredentialAutofillViewError,
    Fido2CredentialStore, Fido2UserInterface,
};

pub struct ClientFido2<'a> {
    #[allow(dead_code)]
    pub(crate) client: &'a Client,
}

#[derive(Debug, Error)]
pub enum DecryptFido2AutofillCredentialsError {
    #[error(transparent)]
    VaultLocked(#[from] bitwarden_core::VaultLocked),
    #[error(transparent)]
    Fido2CredentialAutofillViewError(#[from] Fido2CredentialAutofillViewError),
}

impl<'a> ClientFido2<'a> {
    pub fn new(client: &'a Client) -> Self {
        Self { client }
    }

    pub fn create_authenticator(
        &'a self,
        user_interface: &'a dyn Fido2UserInterface,
        credential_store: &'a dyn Fido2CredentialStore,
    ) -> Fido2Authenticator<'a> {
        Fido2Authenticator::new(self.client, user_interface, credential_store)
    }

    pub fn create_client(
        &'a self,
        user_interface: &'a dyn Fido2UserInterface,
        credential_store: &'a dyn Fido2CredentialStore,
    ) -> Fido2Client<'a> {
        Fido2Client {
            authenticator: self.create_authenticator(user_interface, credential_store),
        }
    }

    pub fn decrypt_fido2_autofill_credentials(
        &'a self,
        cipher_view: CipherView,
    ) -> Result<Vec<Fido2CredentialAutofillView>, DecryptFido2AutofillCredentialsError> {
        let enc = self.client.internal.get_encryption_settings()?;

        Ok(Fido2CredentialAutofillView::from_cipher_view(
            &cipher_view,
            &*enc,
        )?)
    }
}

pub trait ClientFido2Ext<'a> {
    fn fido2(&'a self) -> ClientFido2<'a>;
}

impl<'a> ClientFido2Ext<'a> for Client {
    fn fido2(&'a self) -> ClientFido2<'a> {
        ClientFido2::new(self)
    }
}