bitwarden_core/client/
client.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::sync::{Arc, RwLock};

use reqwest::header::{self, HeaderValue};

use super::internal::InternalClient;
#[cfg(feature = "internal")]
use crate::client::flags::Flags;
use crate::client::{
    client_settings::ClientSettings,
    internal::{ApiConfigurations, Tokens},
};

/// The main struct to interact with the Bitwarden SDK.
#[derive(Debug)]
pub struct Client {
    #[doc(hidden)]
    pub internal: InternalClient,
}

impl Client {
    pub fn new(settings_input: Option<ClientSettings>) -> Self {
        let settings = settings_input.unwrap_or_default();

        fn new_client_builder() -> reqwest::ClientBuilder {
            #[allow(unused_mut)]
            let mut client_builder = reqwest::Client::builder();

            #[cfg(not(target_arch = "wasm32"))]
            {
                use rustls::ClientConfig;
                use rustls_platform_verifier::ConfigVerifierExt;
                client_builder =
                    client_builder.use_preconfigured_tls(ClientConfig::with_platform_verifier());
            }

            client_builder
        }

        let external_client = new_client_builder().build().expect("Build should not fail");

        let mut headers = header::HeaderMap::new();
        headers.append(
            "Device-Type",
            HeaderValue::from_str(&(settings.device_type as u8).to_string())
                .expect("All numbers are valid ASCII"),
        );
        let client_builder = new_client_builder().default_headers(headers);

        let client = client_builder.build().expect("Build should not fail");

        let identity = bitwarden_api_identity::apis::configuration::Configuration {
            base_path: settings.identity_url,
            user_agent: Some(settings.user_agent.clone()),
            client: client.clone(),
            basic_auth: None,
            oauth_access_token: None,
            bearer_access_token: None,
            api_key: None,
        };

        let api = bitwarden_api_api::apis::configuration::Configuration {
            base_path: settings.api_url,
            user_agent: Some(settings.user_agent),
            client,
            basic_auth: None,
            oauth_access_token: None,
            bearer_access_token: None,
            api_key: None,
        };

        Self {
            internal: InternalClient {
                tokens: RwLock::new(Tokens::default()),
                login_method: RwLock::new(None),
                #[cfg(feature = "internal")]
                flags: RwLock::new(Flags::default()),
                __api_configurations: RwLock::new(Arc::new(ApiConfigurations {
                    identity,
                    api,
                    device_type: settings.device_type,
                })),
                external_client,
                encryption_settings: RwLock::new(None),
            },
        }
    }
}