Skip to main content

tapis_authenticator/apis/
tokens_api.rs

1/*
2 * Authenticator
3 *
4 * REST API and web server providing authentication for a Tapis v3 instance.
5 *
6 * The version of the OpenAPI document: 1
7 * Contact: cicsupport@tacc.utexas.edu
8 * Generated by: https://openapi-generator.tech
9 */
10
11use super::{configuration, ContentType, Error};
12use crate::{apis::ResponseContent, models};
13use reqwest;
14use serde::{de::Error as _, Deserialize, Serialize};
15
16/// struct for typed errors of method [`create_token`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum CreateTokenError {
20    UnknownValue(serde_json::Value),
21}
22
23/// struct for typed errors of method [`create_v2_token`]
24#[derive(Debug, Clone, Serialize, Deserialize)]
25#[serde(untagged)]
26pub enum CreateV2TokenError {
27    UnknownValue(serde_json::Value),
28}
29
30/// struct for typed errors of method [`generate_device_code`]
31#[derive(Debug, Clone, Serialize, Deserialize)]
32#[serde(untagged)]
33pub enum GenerateDeviceCodeError {
34    UnknownValue(serde_json::Value),
35}
36
37/// struct for typed errors of method [`revoke_token`]
38#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum RevokeTokenError {
41    UnknownValue(serde_json::Value),
42}
43
44/// Generate a Tapis JWT using some OAuth2 grant type. Typically, a request to this endpoint is the last step in the token generation process. The fields required in the request payload depend on the grant type being used (see details below).
45pub async fn create_token(
46    configuration: &configuration::Configuration,
47    new_token: models::NewToken,
48) -> Result<models::CreateToken201Response, Error<CreateTokenError>> {
49    // add a prefix to parameters to efficiently prevent name collisions
50    let p_body_new_token = new_token;
51
52    let uri_str = format!("{}/v3/oauth2/tokens", configuration.base_path);
53    let mut req_builder = configuration
54        .client
55        .request(reqwest::Method::POST, &uri_str);
56
57    if let Some(ref user_agent) = configuration.user_agent {
58        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
59    }
60    req_builder = req_builder.json(&p_body_new_token);
61
62    let req = req_builder.build()?;
63    let resp = configuration.client.execute(req).await?;
64
65    let status = resp.status();
66    let content_type = resp
67        .headers()
68        .get("content-type")
69        .and_then(|v| v.to_str().ok())
70        .unwrap_or("application/octet-stream");
71    let content_type = super::ContentType::from(content_type);
72
73    if !status.is_client_error() && !status.is_server_error() {
74        let content = resp.text().await?;
75        match content_type {
76            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
77            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateToken201Response`"))),
78            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CreateToken201Response`")))),
79        }
80    } else {
81        let content = resp.text().await?;
82        let entity: Option<CreateTokenError> = serde_json::from_str(&content).ok();
83        Err(Error::ResponseError(ResponseContent {
84            status,
85            content,
86            entity,
87        }))
88    }
89}
90
91/// Create a v2 token from a Tapis v3 JWT. Note that only some tenants for both v2 and v3 are supported.
92pub async fn create_v2_token(
93    configuration: &configuration::Configuration,
94    v2_token: models::V2Token,
95) -> Result<models::CreateV2Token200Response, Error<CreateV2TokenError>> {
96    // add a prefix to parameters to efficiently prevent name collisions
97    let p_body_v2_token = v2_token;
98
99    let uri_str = format!("{}/v3/oauth2/v2/token", configuration.base_path);
100    let mut req_builder = configuration
101        .client
102        .request(reqwest::Method::POST, &uri_str);
103
104    if let Some(ref user_agent) = configuration.user_agent {
105        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
106    }
107    if let Some(ref apikey) = configuration.api_key {
108        let key = apikey.key.clone();
109        let value = match apikey.prefix {
110            Some(ref prefix) => format!("{} {}", prefix, key),
111            None => key,
112        };
113        req_builder = req_builder.header("X-Tapis-Token", value);
114    };
115    req_builder = req_builder.json(&p_body_v2_token);
116
117    let req = req_builder.build()?;
118    let resp = configuration.client.execute(req).await?;
119
120    let status = resp.status();
121    let content_type = resp
122        .headers()
123        .get("content-type")
124        .and_then(|v| v.to_str().ok())
125        .unwrap_or("application/octet-stream");
126    let content_type = super::ContentType::from(content_type);
127
128    if !status.is_client_error() && !status.is_server_error() {
129        let content = resp.text().await?;
130        match content_type {
131            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
132            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateV2Token200Response`"))),
133            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CreateV2Token200Response`")))),
134        }
135    } else {
136        let content = resp.text().await?;
137        let entity: Option<CreateV2TokenError> = serde_json::from_str(&content).ok();
138        Err(Error::ResponseError(ResponseContent {
139            status,
140            content,
141            entity,
142        }))
143    }
144}
145
146/// Generate a device code; this is the first step in the device_code grant type. See the OAuth2 documentation for details.
147pub async fn generate_device_code(
148    configuration: &configuration::Configuration,
149    new_device_code: models::NewDeviceCode,
150) -> Result<models::GenerateDeviceCode200Response, Error<GenerateDeviceCodeError>> {
151    // add a prefix to parameters to efficiently prevent name collisions
152    let p_body_new_device_code = new_device_code;
153
154    let uri_str = format!("{}/v3/oauth2/device/code", configuration.base_path);
155    let mut req_builder = configuration
156        .client
157        .request(reqwest::Method::POST, &uri_str);
158
159    if let Some(ref user_agent) = configuration.user_agent {
160        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
161    }
162    req_builder = req_builder.json(&p_body_new_device_code);
163
164    let req = req_builder.build()?;
165    let resp = configuration.client.execute(req).await?;
166
167    let status = resp.status();
168    let content_type = resp
169        .headers()
170        .get("content-type")
171        .and_then(|v| v.to_str().ok())
172        .unwrap_or("application/octet-stream");
173    let content_type = super::ContentType::from(content_type);
174
175    if !status.is_client_error() && !status.is_server_error() {
176        let content = resp.text().await?;
177        match content_type {
178            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
179            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GenerateDeviceCode200Response`"))),
180            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::GenerateDeviceCode200Response`")))),
181        }
182    } else {
183        let content = resp.text().await?;
184        let entity: Option<GenerateDeviceCodeError> = serde_json::from_str(&content).ok();
185        Err(Error::ResponseError(ResponseContent {
186            status,
187            content,
188            entity,
189        }))
190    }
191}
192
193/// Revoke a Tapis JWT. Pass the token to revoke in the body of the request. Once revoked, a token cannot be unrevoked. Only user tokens generated by this Authenticator can be revoked with this endpoint.
194pub async fn revoke_token(
195    configuration: &configuration::Configuration,
196    revoke_token_request: models::RevokeTokenRequest,
197) -> Result<models::BasicResponse, Error<RevokeTokenError>> {
198    // add a prefix to parameters to efficiently prevent name collisions
199    let p_body_revoke_token_request = revoke_token_request;
200
201    let uri_str = format!("{}/v3/oauth2/tokens/revoke", configuration.base_path);
202    let mut req_builder = configuration
203        .client
204        .request(reqwest::Method::POST, &uri_str);
205
206    if let Some(ref user_agent) = configuration.user_agent {
207        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
208    }
209    req_builder = req_builder.json(&p_body_revoke_token_request);
210
211    let req = req_builder.build()?;
212    let resp = configuration.client.execute(req).await?;
213
214    let status = resp.status();
215    let content_type = resp
216        .headers()
217        .get("content-type")
218        .and_then(|v| v.to_str().ok())
219        .unwrap_or("application/octet-stream");
220    let content_type = super::ContentType::from(content_type);
221
222    if !status.is_client_error() && !status.is_server_error() {
223        let content = resp.text().await?;
224        match content_type {
225            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
226            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::BasicResponse`"))),
227            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::BasicResponse`")))),
228        }
229    } else {
230        let content = resp.text().await?;
231        let entity: Option<RevokeTokenError> = serde_json::from_str(&content).ok();
232        Err(Error::ResponseError(ResponseContent {
233            status,
234            content,
235            entity,
236        }))
237    }
238}