1 use {
2 crate::client_credentials::{base_url_builder as tenant_base_url_builder, ClientCredentials},
3 reqwest::{Client, StatusCode},
4 smbcloud_model::error_codes::{ErrorCode, ErrorResponse},
5 smbcloud_network::environment::Environment,
6 smbcloud_networking::{
7 constants::PATH_USERS_SIGN_OUT, smb_base_url_builder, smb_client::SmbClient,
8 },
9 };
10
11 pub async fn logout(
12 env: Environment,
13 client: (&SmbClient, &str),
14 access_token: String,
15 ) -> Result<(), ErrorResponse> {
16 let response = match Client::new()
17 .delete(build_smb_logout_url(env, client))
18 .header("Authorization", access_token)
19 .header("Accept", "application/json")
20 .header("Content-Type", "application/x-www-form-urlencoded")
21 .send()
22 .await
23 {
24 Ok(response) => response,
25 Err(_) => {
26 return Err(ErrorResponse::Error {
27 error_code: ErrorCode::Unknown,
28 message: ErrorCode::Unknown.message(None).to_string(),
29 });
30 }
31 };
32
33 match response.status() {
34 StatusCode::OK => Ok(()),
35 _ => Err(ErrorResponse::Error {
36 error_code: ErrorCode::Unauthorized,
37 message: ErrorCode::Unauthorized.message(None).to_string(),
38 }),
39 }
40 }
41
42 pub async fn logout_with_client(
43 env: Environment,
44 client: ClientCredentials<'_>,
45 access_token: String,
46 ) -> Result<(), ErrorResponse> {
47 let response = match Client::new()
48 .delete(build_logout_url_with_client(env, client))
49 .header("Authorization", access_token)
50 .header("Accept", "application/json")
51 .header("Content-Type", "application/x-www-form-urlencoded")
52 .send()
53 .await
54 {
55 Ok(response) => response,
56 Err(_) => {
57 return Err(ErrorResponse::Error {
58 error_code: ErrorCode::Unknown,
59 message: ErrorCode::Unknown.message(None).to_string(),
60 });
61 }
62 };
63
64 match response.status() {
65 StatusCode::OK => Ok(()),
66 _ => Err(ErrorResponse::Error {
67 error_code: ErrorCode::Unauthorized,
68 message: ErrorCode::Unauthorized.message(None).to_string(),
69 }),
70 }
71 }
72
73 fn build_smb_logout_url(env: Environment, client: (&SmbClient, &str)) -> String {
74 let mut url_builder = smb_base_url_builder(env, client);
75 url_builder.add_route(PATH_USERS_SIGN_OUT);
76 url_builder.build()
77 }
78
79 fn build_logout_url_with_client(env: Environment, client: ClientCredentials<'_>) -> String {
80 let mut url_builder = tenant_base_url_builder(env, client);
81 url_builder.add_route("v1/client/users/sign_out");
82 url_builder.build()
83 }