bitwarden_core/auth/api/request/
mod.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
#[cfg(feature = "secrets")]
mod access_token_request;
#[cfg(feature = "secrets")]
pub(crate) use access_token_request::*;

mod api_token_request;
pub(crate) use api_token_request::*;

#[cfg(feature = "internal")]
mod password_token_request;
#[cfg(feature = "internal")]
pub(crate) use password_token_request::*;

mod renew_token_request;
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
pub(crate) use renew_token_request::*;

mod auth_request_token_request;
#[cfg(feature = "internal")]
pub(crate) use auth_request_token_request::*;

use crate::{
    auth::api::response::{parse_identity_response, IdentityTokenResponse},
    client::ApiConfigurations,
    error::Result,
};

async fn send_identity_connect_request(
    configurations: &ApiConfigurations,
    email: Option<&str>,
    body: impl serde::Serialize,
) -> Result<IdentityTokenResponse> {
    let mut request = configurations
        .identity
        .client
        .post(format!(
            "{}/connect/token",
            &configurations.identity.base_path
        ))
        .header(
            reqwest::header::CONTENT_TYPE,
            "application/x-www-form-urlencoded; charset=utf-8",
        )
        .header(reqwest::header::ACCEPT, "application/json")
        .header("Device-Type", configurations.device_type as usize);

    if let Some(ref user_agent) = configurations.identity.user_agent {
        request = request.header(reqwest::header::USER_AGENT, user_agent.clone());
    }

    if let Some(email) = email {
        request = request.header("Auth-Email", URL_SAFE_NO_PAD.encode(email.as_bytes()));
    }

    let response = request
        .body(serde_qs::to_string(&body).expect("Serialize should be infallible"))
        .send()
        .await?;

    let status = response.status();
    let text = response.text().await?;

    parse_identity_response(status, text)
}