Skip to main content

smbcloud_auth/
logout.rs

1use {
2    reqwest::{Client, StatusCode},
3    smbcloud_model::error_codes::{ErrorCode, ErrorResponse},
4    smbcloud_network::environment::Environment,
5    smbcloud_networking::{
6        constants::PATH_USERS_SIGN_OUT, smb_base_url_builder, smb_client::SmbClient,
7    },
8};
9
10pub async fn logout(
11    env: Environment,
12    client: (&SmbClient, &str),
13    access_token: String,
14) -> Result<(), ErrorResponse> {
15    let response = match Client::new()
16        .delete(build_smb_logout_url(env, client))
17        .header("Authorization", access_token)
18        .header("Accept", "application/json")
19        .header("Content-Type", "application/x-www-form-urlencoded")
20        .send()
21        .await
22    {
23        Ok(response) => response,
24        Err(_) => {
25            return Err(ErrorResponse::Error {
26                error_code: ErrorCode::Unknown,
27                message: ErrorCode::Unknown.message(None).to_string(),
28            });
29        }
30    };
31
32    match response.status() {
33        StatusCode::OK => Ok(()),
34        _ => Err(ErrorResponse::Error {
35            error_code: ErrorCode::Unauthorized,
36            message: ErrorCode::Unauthorized.message(None).to_string(),
37        }),
38    }
39}
40
41fn build_smb_logout_url(env: Environment, client: (&SmbClient, &str)) -> String {
42    let mut url_builder = smb_base_url_builder(env, client);
43    url_builder.add_route(PATH_USERS_SIGN_OUT);
44    url_builder.build()
45}