thunderstore_api/apis/v2/
authenticate.rs

1////////////////////////////////////////////////////////////////////////////////
2// This Source Code Form is subject to the terms of the Mozilla Public         /
3// License, v. 2.0. If a copy of the MPL was not distributed with this         /
4// file, You can obtain one at https://mozilla.org/MPL/2.0/.                   /
5////////////////////////////////////////////////////////////////////////////////
6
7use crate::apis::configuration::Configuration;
8use crate::apis::{urlencode, Error, ResponseContent};
9use crate::models::v2::authenticate::{RequestBody, ResponseBody};
10
11/// struct for typed errors of method [`experimental_period_auth_period_complete`]
12#[derive(Debug, Clone, Serialize, Deserialize)]
13#[serde(untagged)]
14pub enum AuthCompleteError {
15    UnknownValue(serde_json::Value),
16}
17
18/// struct for typed errors of method [`experimental_period_auth_period_validate`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum AuthValidateError {
22    Status401(),
23    UnknownValue(serde_json::Value),
24}
25
26/// Complete OAuth login process initiated by a client.
27pub async fn experimental_period_auth_period_complete(
28    configuration: &Configuration,
29    provider: &str,
30    data: RequestBody,
31) -> Result<ResponseBody, Error<AuthCompleteError>> {
32    let local_var_configuration = configuration;
33
34    let local_var_client = &local_var_configuration.client;
35
36    let local_var_uri_str = format!(
37        "{}/api/experimental/auth/complete/{provider}/",
38        local_var_configuration.base_path,
39        provider = urlencode(provider)
40    );
41    let mut local_var_req_builder =
42        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
43
44    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
45        local_var_req_builder =
46            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
47    }
48    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
49        local_var_req_builder = local_var_req_builder
50            .basic_auth(local_var_auth_conf.0.clone(), local_var_auth_conf.1.clone());
51    };
52    local_var_req_builder = local_var_req_builder.json(&data);
53
54    let local_var_req = local_var_req_builder.build()?;
55    let local_var_resp = local_var_client.execute(local_var_req).await?;
56
57    let local_var_status = local_var_resp.status();
58    let local_var_content = local_var_resp.text().await?;
59
60    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
61        serde_json::from_str(&local_var_content).map_err(Error::from)
62    } else {
63        let local_var_entity: Option<AuthCompleteError> =
64            serde_json::from_str(&local_var_content).ok();
65        let local_var_error = ResponseContent {
66            status: local_var_status,
67            content: local_var_content,
68            entity: local_var_entity,
69        };
70        Err(Error::ResponseError(local_var_error))
71    }
72}
73
74/// Check that valid session key is provided in Authorization header.
75pub async fn auth_validate(configuration: &Configuration) -> Result<(), Error<AuthValidateError>> {
76    let local_var_configuration = configuration;
77
78    let local_var_client = &local_var_configuration.client;
79
80    let local_var_uri_str = format!(
81        "{}/api/experimental/auth/validate/",
82        local_var_configuration.base_path
83    );
84    let mut local_var_req_builder =
85        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
86
87    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
88        local_var_req_builder =
89            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
90    }
91    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
92        local_var_req_builder = local_var_req_builder
93            .basic_auth(local_var_auth_conf.0.clone(), local_var_auth_conf.1.clone());
94    };
95
96    let local_var_req = local_var_req_builder.build()?;
97    let local_var_resp = local_var_client.execute(local_var_req).await?;
98
99    let local_var_status = local_var_resp.status();
100    let local_var_content = local_var_resp.text().await?;
101
102    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
103        Ok(())
104    } else {
105        let local_var_entity: Option<AuthValidateError> =
106            serde_json::from_str(&local_var_content).ok();
107        let local_var_error = ResponseContent {
108            status: local_var_status,
109            content: local_var_content,
110            entity: local_var_entity,
111        };
112        Err(Error::ResponseError(local_var_error))
113    }
114}