1use super::{Error, configuration};
12use crate::apis::ContentType;
13use crate::{apis::ResponseContent, models};
14use async_trait::async_trait;
15#[cfg(feature = "mockall")]
16use mockall::automock;
17use reqwest;
18use serde::{Deserialize, Serialize, de::Error as _};
19use std::sync::Arc;
20
21#[cfg_attr(feature = "mockall", automock)]
22#[async_trait]
23pub trait AuthApi: Send + Sync {
24 async fn delete_session<'id, 'refresh_token>(
28 &self,
29 id: Option<&'id str>,
30 refresh_token: Option<&'refresh_token str>,
31 ) -> Result<(), Error<DeleteSessionError>>;
32
33 async fn get_api_tokens(
37 &self,
38 ) -> Result<Vec<models::UserApiTokenDto>, Error<GetApiTokensError>>;
39
40 async fn get_o_auth_token<
44 'use_cookie,
45 'client_id,
46 'code,
47 'code_verifier,
48 'grant_type,
49 'redirect_uri,
50 'refresh_token,
51 >(
52 &self,
53 use_cookie: Option<bool>,
54 client_id: Option<&'client_id str>,
55 code: Option<&'code str>,
56 code_verifier: Option<&'code_verifier str>,
57 grant_type: Option<&'grant_type str>,
58 redirect_uri: Option<&'redirect_uri str>,
59 refresh_token: Option<&'refresh_token str>,
60 ) -> Result<models::TokenResponseDto, Error<GetOAuthTokenError>>;
61
62 async fn get_sessions_for_current_user(
66 &self,
67 ) -> Result<Vec<models::UserSessionDto>, Error<GetSessionsForCurrentUserError>>;
68
69 async fn remove_api_token<'name>(
73 &self,
74 name: &'name str,
75 ) -> Result<(), Error<RemoveApiTokenError>>;
76}
77
78pub struct AuthApiClient {
79 configuration: Arc<configuration::Configuration>,
80}
81
82impl AuthApiClient {
83 pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
84 Self { configuration }
85 }
86}
87
88#[async_trait]
89impl AuthApi for AuthApiClient {
90 async fn delete_session<'id, 'refresh_token>(
91 &self,
92 id: Option<&'id str>,
93 refresh_token: Option<&'refresh_token str>,
94 ) -> Result<(), Error<DeleteSessionError>> {
95 let local_var_configuration = &self.configuration;
96
97 let local_var_client = &local_var_configuration.client;
98
99 let local_var_uri_str = format!("{}/auth/logout", local_var_configuration.base_path);
100 let mut local_var_req_builder =
101 local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
102
103 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
104 local_var_req_builder = local_var_req_builder
105 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
106 }
107 let mut local_var_form_params = std::collections::HashMap::new();
108 if let Some(local_var_param_value) = id {
109 local_var_form_params.insert("id", local_var_param_value.to_string());
110 }
111 if let Some(local_var_param_value) = refresh_token {
112 local_var_form_params.insert("refresh_token", local_var_param_value.to_string());
113 }
114 local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
115
116 let local_var_req = local_var_req_builder.build()?;
117 let local_var_resp = local_var_client.execute(local_var_req).await?;
118
119 let local_var_status = local_var_resp.status();
120 let local_var_content = local_var_resp.text().await?;
121
122 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
123 Ok(())
124 } else {
125 let local_var_entity: Option<DeleteSessionError> =
126 serde_json::from_str(&local_var_content).ok();
127 let local_var_error = ResponseContent {
128 status: local_var_status,
129 content: local_var_content,
130 entity: local_var_entity,
131 };
132 Err(Error::ResponseError(local_var_error))
133 }
134 }
135
136 async fn get_api_tokens(
137 &self,
138 ) -> Result<Vec<models::UserApiTokenDto>, Error<GetApiTokensError>> {
139 let local_var_configuration = &self.configuration;
140
141 let local_var_client = &local_var_configuration.client;
142
143 let local_var_uri_str = format!("{}/auth/apitokens", local_var_configuration.base_path);
144 let mut local_var_req_builder =
145 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
146
147 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
148 local_var_req_builder = local_var_req_builder
149 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
150 }
151
152 let local_var_req = local_var_req_builder.build()?;
153 let local_var_resp = local_var_client.execute(local_var_req).await?;
154
155 let local_var_status = local_var_resp.status();
156 let local_var_content_type = local_var_resp
157 .headers()
158 .get("content-type")
159 .and_then(|v| v.to_str().ok())
160 .unwrap_or("application/octet-stream");
161 let local_var_content_type = super::ContentType::from(local_var_content_type);
162 let local_var_content = local_var_resp.text().await?;
163
164 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
165 match local_var_content_type {
166 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
167 ContentType::Text => {
168 return Err(Error::from(serde_json::Error::custom(
169 "Received `text/plain` content type response that cannot be converted to `Vec<models::UserApiTokenDto>`",
170 )));
171 }
172 ContentType::Unsupported(local_var_unknown_type) => {
173 return Err(Error::from(serde_json::Error::custom(format!(
174 "Received `{local_var_unknown_type}` content type response that cannot be converted to `Vec<models::UserApiTokenDto>`"
175 ))));
176 }
177 }
178 } else {
179 let local_var_entity: Option<GetApiTokensError> =
180 serde_json::from_str(&local_var_content).ok();
181 let local_var_error = ResponseContent {
182 status: local_var_status,
183 content: local_var_content,
184 entity: local_var_entity,
185 };
186 Err(Error::ResponseError(local_var_error))
187 }
188 }
189
190 async fn get_o_auth_token<
191 'use_cookie,
192 'client_id,
193 'code,
194 'code_verifier,
195 'grant_type,
196 'redirect_uri,
197 'refresh_token,
198 >(
199 &self,
200 use_cookie: Option<bool>,
201 client_id: Option<&'client_id str>,
202 code: Option<&'code str>,
203 code_verifier: Option<&'code_verifier str>,
204 grant_type: Option<&'grant_type str>,
205 redirect_uri: Option<&'redirect_uri str>,
206 refresh_token: Option<&'refresh_token str>,
207 ) -> Result<models::TokenResponseDto, Error<GetOAuthTokenError>> {
208 let local_var_configuration = &self.configuration;
209
210 let local_var_client = &local_var_configuration.client;
211
212 let local_var_uri_str = format!("{}/auth/token", local_var_configuration.base_path);
213 let mut local_var_req_builder =
214 local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
215
216 if let Some(ref local_var_str) = use_cookie {
217 local_var_req_builder =
218 local_var_req_builder.query(&[("useCookie", &local_var_str.to_string())]);
219 }
220 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
221 local_var_req_builder = local_var_req_builder
222 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
223 }
224 let mut local_var_form_params = std::collections::HashMap::new();
225 if let Some(local_var_param_value) = client_id {
226 local_var_form_params.insert("client_id", local_var_param_value.to_string());
227 }
228 if let Some(local_var_param_value) = code {
229 local_var_form_params.insert("code", local_var_param_value.to_string());
230 }
231 if let Some(local_var_param_value) = code_verifier {
232 local_var_form_params.insert("code_verifier", local_var_param_value.to_string());
233 }
234 if let Some(local_var_param_value) = grant_type {
235 local_var_form_params.insert("grant_type", local_var_param_value.to_string());
236 }
237 if let Some(local_var_param_value) = redirect_uri {
238 local_var_form_params.insert("redirect_uri", local_var_param_value.to_string());
239 }
240 if let Some(local_var_param_value) = refresh_token {
241 local_var_form_params.insert("refresh_token", local_var_param_value.to_string());
242 }
243 local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
244
245 let local_var_req = local_var_req_builder.build()?;
246 let local_var_resp = local_var_client.execute(local_var_req).await?;
247
248 let local_var_status = local_var_resp.status();
249 let local_var_content_type = local_var_resp
250 .headers()
251 .get("content-type")
252 .and_then(|v| v.to_str().ok())
253 .unwrap_or("application/octet-stream");
254 let local_var_content_type = super::ContentType::from(local_var_content_type);
255 let local_var_content = local_var_resp.text().await?;
256
257 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
258 match local_var_content_type {
259 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
260 ContentType::Text => {
261 return Err(Error::from(serde_json::Error::custom(
262 "Received `text/plain` content type response that cannot be converted to `models::TokenResponseDto`",
263 )));
264 }
265 ContentType::Unsupported(local_var_unknown_type) => {
266 return Err(Error::from(serde_json::Error::custom(format!(
267 "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::TokenResponseDto`"
268 ))));
269 }
270 }
271 } else {
272 let local_var_entity: Option<GetOAuthTokenError> =
273 serde_json::from_str(&local_var_content).ok();
274 let local_var_error = ResponseContent {
275 status: local_var_status,
276 content: local_var_content,
277 entity: local_var_entity,
278 };
279 Err(Error::ResponseError(local_var_error))
280 }
281 }
282
283 async fn get_sessions_for_current_user(
284 &self,
285 ) -> Result<Vec<models::UserSessionDto>, Error<GetSessionsForCurrentUserError>> {
286 let local_var_configuration = &self.configuration;
287
288 let local_var_client = &local_var_configuration.client;
289
290 let local_var_uri_str = format!("{}/auth/sessions", local_var_configuration.base_path);
291 let mut local_var_req_builder =
292 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
293
294 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
295 local_var_req_builder = local_var_req_builder
296 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
297 }
298
299 let local_var_req = local_var_req_builder.build()?;
300 let local_var_resp = local_var_client.execute(local_var_req).await?;
301
302 let local_var_status = local_var_resp.status();
303 let local_var_content_type = local_var_resp
304 .headers()
305 .get("content-type")
306 .and_then(|v| v.to_str().ok())
307 .unwrap_or("application/octet-stream");
308 let local_var_content_type = super::ContentType::from(local_var_content_type);
309 let local_var_content = local_var_resp.text().await?;
310
311 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
312 match local_var_content_type {
313 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
314 ContentType::Text => {
315 return Err(Error::from(serde_json::Error::custom(
316 "Received `text/plain` content type response that cannot be converted to `Vec<models::UserSessionDto>`",
317 )));
318 }
319 ContentType::Unsupported(local_var_unknown_type) => {
320 return Err(Error::from(serde_json::Error::custom(format!(
321 "Received `{local_var_unknown_type}` content type response that cannot be converted to `Vec<models::UserSessionDto>`"
322 ))));
323 }
324 }
325 } else {
326 let local_var_entity: Option<GetSessionsForCurrentUserError> =
327 serde_json::from_str(&local_var_content).ok();
328 let local_var_error = ResponseContent {
329 status: local_var_status,
330 content: local_var_content,
331 entity: local_var_entity,
332 };
333 Err(Error::ResponseError(local_var_error))
334 }
335 }
336
337 async fn remove_api_token<'name>(
338 &self,
339 name: &'name str,
340 ) -> Result<(), Error<RemoveApiTokenError>> {
341 let local_var_configuration = &self.configuration;
342
343 let local_var_client = &local_var_configuration.client;
344
345 let local_var_uri_str = format!(
346 "{}/auth/apitokens/{name}",
347 local_var_configuration.base_path,
348 name = crate::apis::urlencode(name)
349 );
350 let mut local_var_req_builder =
351 local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
352
353 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
354 local_var_req_builder = local_var_req_builder
355 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
356 }
357
358 let local_var_req = local_var_req_builder.build()?;
359 let local_var_resp = local_var_client.execute(local_var_req).await?;
360
361 let local_var_status = local_var_resp.status();
362 let local_var_content = local_var_resp.text().await?;
363
364 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
365 Ok(())
366 } else {
367 let local_var_entity: Option<RemoveApiTokenError> =
368 serde_json::from_str(&local_var_content).ok();
369 let local_var_error = ResponseContent {
370 status: local_var_status,
371 content: local_var_content,
372 entity: local_var_entity,
373 };
374 Err(Error::ResponseError(local_var_error))
375 }
376 }
377}
378
379#[derive(Debug, Clone, Serialize, Deserialize)]
381#[serde(untagged)]
382pub enum DeleteSessionError {
383 Status401(),
384 Status404(),
385 UnknownValue(serde_json::Value),
386}
387
388#[derive(Debug, Clone, Serialize, Deserialize)]
390#[serde(untagged)]
391pub enum GetApiTokensError {
392 Status401(),
393 Status404(),
394 UnknownValue(serde_json::Value),
395}
396
397#[derive(Debug, Clone, Serialize, Deserialize)]
399#[serde(untagged)]
400pub enum GetOAuthTokenError {
401 Status400(),
402 UnknownValue(serde_json::Value),
403}
404
405#[derive(Debug, Clone, Serialize, Deserialize)]
407#[serde(untagged)]
408pub enum GetSessionsForCurrentUserError {
409 Status401(),
410 Status404(),
411 UnknownValue(serde_json::Value),
412}
413
414#[derive(Debug, Clone, Serialize, Deserialize)]
416#[serde(untagged)]
417pub enum RemoveApiTokenError {
418 Status401(),
419 Status404(),
420 UnknownValue(serde_json::Value),
421}