Skip to main content

bitwarden_core/auth/api/request/
mod.rs

1#[cfg(feature = "secrets")]
2mod access_token_request;
3#[cfg(feature = "secrets")]
4pub(crate) use access_token_request::*;
5
6mod api_token_request;
7pub(crate) use api_token_request::*;
8
9#[cfg(feature = "internal")]
10mod password_token_request;
11use bitwarden_api_api::Configuration;
12#[cfg(feature = "internal")]
13pub(crate) use password_token_request::*;
14
15mod renew_token_request;
16pub(crate) use renew_token_request::*;
17
18mod auth_request_token_request;
19#[cfg(feature = "internal")]
20pub(crate) use auth_request_token_request::*;
21
22use crate::{
23    ApiError,
24    auth::{
25        api::response::{IdentityTokenResponse, parse_identity_response},
26        login::LoginError,
27    },
28};
29
30pub(crate) async fn send_identity_connect_request(
31    identity_config: &Configuration,
32    body: impl serde::Serialize,
33) -> Result<IdentityTokenResponse, LoginError> {
34    let response = identity_config
35        .client
36        .post(format!("{}/connect/token", identity_config.base_path))
37        .header(
38            reqwest::header::CONTENT_TYPE,
39            "application/x-www-form-urlencoded; charset=utf-8",
40        )
41        .header(reqwest::header::ACCEPT, "application/json")
42        .body(serde_qs::to_string(&body).expect("Serialize should be infallible"))
43        .send()
44        .await
45        .map_err(ApiError::from)?;
46
47    let status = response.status();
48    let text = response.text().await.map_err(ApiError::from)?;
49
50    parse_identity_response(status, text)
51}
52
53#[cfg(test)]
54mod tests {
55    use serde::Serialize;
56    use wiremock::{Mock, MockServer, ResponseTemplate, matchers};
57
58    use super::*;
59
60    #[derive(Serialize)]
61    struct Body {
62        password: String,
63        client_secret: String,
64        scope: String,
65    }
66
67    /// Identity decodes the body as `application/x-www-form-urlencoded`, so every value must
68    /// survive that decoding unchanged, including characters with special meaning in the format.
69    #[tokio::test]
70    async fn identity_request_body_roundtrips_form_special_characters() {
71        let server = MockServer::start().await;
72        Mock::given(matchers::method("POST"))
73            .and(matchers::path("/connect/token"))
74            .respond_with(ResponseTemplate::new(400).set_body_string("{}"))
75            .mount(&server)
76            .await;
77
78        let config = Configuration {
79            base_path: server.uri(),
80            client: reqwest_middleware::ClientBuilder::new(bitwarden_api_base::new_http_client())
81                .build(),
82        };
83        let body = Body {
84            password: "a+b/c==%41&x=y #é".to_string(),
85            client_secret: "100%".to_string(),
86            scope: "api offline_access".to_string(),
87        };
88
89        let _ = send_identity_connect_request(&config, &body).await;
90
91        let requests = server
92            .received_requests()
93            .await
94            .expect("requests are recorded");
95        assert_eq!(requests.len(), 1);
96        let decoded: Vec<(String, String)> = url::form_urlencoded::parse(&requests[0].body)
97            .into_owned()
98            .collect();
99        assert_eq!(
100            decoded,
101            vec![
102                ("password".to_string(), body.password),
103                ("client_secret".to_string(), body.client_secret),
104                ("scope".to_string(), body.scope),
105            ]
106        );
107    }
108}