/*
* CrowdStrike API Specification
*
* Use this API specification as a reference for the API endpoints you can use to interact with your Falcon environment. These endpoints support authentication via OAuth2 and interact with detections and network containment. For detailed usage guides and examples, see our [documentation inside the Falcon console](https://falcon.crowdstrike.com/support/documentation). To use the APIs described below, combine the base URL with the path shown for each API endpoint. For commercial cloud customers, your base URL is `https://api.crowdstrike.com`. Each API endpoint requires authorization via an OAuth2 token. Your first API request should retrieve an OAuth2 token using the `oauth2/token` endpoint, such as `https://api.crowdstrike.com/oauth2/token`. For subsequent requests, include the OAuth2 token in an HTTP authorization header. Tokens expire after 30 minutes, after which you should make a new token request to continue making API requests.
*
* The version of the OpenAPI document: rolling
*
* Generated by: https://openapi-generator.tech
*/
use super::{ContentType, Error, configuration};
use crate::{apis::ResponseContent, models};
use reqwest;
use serde::de::Error as _;
/// struct for typed errors of method [`oauth2_access_token`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Oauth2AccessTokenError {
Status400(models::MsaspecResponseFields),
Status403(models::MsaspecResponseFields),
Status429(models::MsaReplyMetaOnly),
Status500(models::MsaspecResponseFields),
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`oauth2_revoke_token`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Oauth2RevokeTokenError {
Status400(models::MsaspecResponseFields),
Status403(models::MsaspecResponseFields),
Status429(models::MsaReplyMetaOnly),
Status500(models::MsaspecResponseFields),
UnknownValue(serde_json::Value),
}
pub async fn oauth2_access_token(
configuration: &configuration::Configuration,
client_id: &str,
client_secret: &str,
member_cid: Option<&str>,
) -> Result<models::DomainAccessTokenResponseV1, Error<Oauth2AccessTokenError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_form_client_id = client_id;
let p_form_client_secret = client_secret;
let p_form_member_cid = member_cid;
let uri_str = format!("{}/oauth2/token", configuration.base_path);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
let mut multipart_form_params = std::collections::HashMap::new();
multipart_form_params.insert("client_id", p_form_client_id.to_string());
multipart_form_params.insert("client_secret", p_form_client_secret.to_string());
if let Some(param_value) = p_form_member_cid {
multipart_form_params.insert("member_cid", param_value.to_string());
}
req_builder = req_builder.form(&multipart_form_params);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => {
return Err(Error::from(serde_json::Error::custom(
"Received `text/plain` content type response that cannot be converted to `models::DomainAccessTokenResponseV1`",
)));
}
ContentType::Unsupported(unknown_type) => {
return Err(Error::from(serde_json::Error::custom(format!(
"Received `{unknown_type}` content type response that cannot be converted to `models::DomainAccessTokenResponseV1`"
))));
}
}
} else {
let content = resp.text().await?;
let entity: Option<Oauth2AccessTokenError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent {
status,
content,
entity,
}))
}
}
pub async fn oauth2_revoke_token(
configuration: &configuration::Configuration,
token: &str,
client_id: Option<&str>,
) -> Result<models::MsaspecResponseFields, Error<Oauth2RevokeTokenError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_form_token = token;
let p_form_client_id = client_id;
let uri_str = format!("{}/oauth2/revoke", configuration.base_path);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
let mut multipart_form_params = std::collections::HashMap::new();
if let Some(param_value) = p_form_client_id {
multipart_form_params.insert("client_id", param_value.to_string());
}
multipart_form_params.insert("token", p_form_token.to_string());
req_builder = req_builder.form(&multipart_form_params);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => {
return Err(Error::from(serde_json::Error::custom(
"Received `text/plain` content type response that cannot be converted to `models::MsaspecResponseFields`",
)));
}
ContentType::Unsupported(unknown_type) => {
return Err(Error::from(serde_json::Error::custom(format!(
"Received `{unknown_type}` content type response that cannot be converted to `models::MsaspecResponseFields`"
))));
}
}
} else {
let content = resp.text().await?;
let entity: Option<Oauth2RevokeTokenError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent {
status,
content,
entity,
}))
}
}