Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/apis/api_key_service_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ pub struct ValidateApiKeyParams {
pub enum ApiKeyError {
BadRequest(crate::models::BadUpdateOrgRequest),
InvalidIntegrationAPIKey,
InvalidAPIKey,
InvalidAPIKey {
message: String,
},
RateLimited {
wait_seconds: f64,
user_facing_error: String,
},
InvalidPersonalAPIKey,
InvalidOrgAPIKey,
NotFound,
Expand All @@ -58,6 +64,19 @@ pub enum ApiKeyError {
UnexpectedExceptionWithSDK,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum ApiKeyValidationErrorResponse {
InvalidEndUserApiKey {
api_key_token: String,
},
EndUserApiKeyRateLimited {
wait_seconds: f64,
error_code: String,
user_facing_error: String,
},
}

pub async fn fetch_current_api_keys(
configuration: &configuration::Configuration,
params: ApiKeyQueryParams,
Expand Down Expand Up @@ -340,11 +359,7 @@ pub async fn delete_api_key(
pub async fn validate_api_key(
configuration: &configuration::Configuration,
params: ValidateApiKeyParams,
) -> Result<crate::models::ValidateApiKeyResponse, Error<ApiKeyError>> {
if hex::decode(&params.api_key_token).is_err() {
return Err(Error::Params("Invalid API key ID format".to_string()));
}

) -> Result<crate::models::ValidateApiKeyResponse, Error<ApiKeyValidationErrorResponse>> {
let client = &configuration.client;

let uri = format!(
Expand All @@ -371,7 +386,7 @@ pub async fn validate_api_key(
if !status.is_client_error() && !status.is_server_error() {
serde_json::from_str(&content).map_err(Error::from)
} else {
let entity: Option<ApiKeyError> = serde_json::from_str(&content).ok();
let entity: Option<ApiKeyValidationErrorResponse> = serde_json::from_str(&content).ok();
let error = ResponseContent {
status,
content,
Expand Down
42 changes: 33 additions & 9 deletions src/propelauth/api_key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::apis::api_key_service_api::{ApiKeyError, ApiKeyQueryParams, CreateApiKeyParams, UpdateApiKeyParams, ValidateApiKeyParams};
use crate::apis::api_key_service_api::{ApiKeyError, ApiKeyQueryParams, ApiKeyValidationErrorResponse, CreateApiKeyParams, UpdateApiKeyParams, ValidateApiKeyParams};
use crate::apis::configuration::Configuration;
use crate::models::{CreateApiKeyResponse, FetchApiKeyResponse, FetchApiKeysPagedResponse, ValidateApiKeyResponse};
use crate::models::validate_api_key_response::{ValidateOrgApiKeyResponse, ValidatePersonalApiKeyResponse};
Expand Down Expand Up @@ -62,7 +62,7 @@ impl ApiKeyService<'_> {
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::InvalidAPIKey,
404 => ApiKeyError::NotFound,
_ => ApiKeyError::UnknownError,
},
)
Expand Down Expand Up @@ -94,7 +94,7 @@ impl ApiKeyService<'_> {
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::InvalidAPIKey,
404 => ApiKeyError::NotFound,
_ => ApiKeyError::UnknownError,
},
)
Expand All @@ -112,7 +112,7 @@ impl ApiKeyService<'_> {
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::InvalidAPIKey,
404 => ApiKeyError::NotFound,
_ => ApiKeyError::UnknownError,
},
)
Expand All @@ -121,17 +121,41 @@ impl ApiKeyService<'_> {
Ok(())
}

pub async fn validate_api_key(&self, params: ValidateApiKeyParams) -> Result<ValidateApiKeyResponse, ApiKeyError> {
pub async fn validate_api_key(
&self,
params: ValidateApiKeyParams,
) -> Result<ValidateApiKeyResponse, ApiKeyError> {
if hex::decode(&params.api_key_token).is_err() {
return Err(ApiKeyError::InvalidAPIKey {
message: "Invalid API key format.".to_string()
});
}

crate::apis::api_key_service_api::validate_api_key(&self.config, params)
.await
.map_err(|err| {
map_autogenerated_error(
err,
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::NotFound,
_ => ApiKeyError::UnknownError,
|status_code, error_response_body| match error_response_body {
Some(ApiKeyValidationErrorResponse::InvalidEndUserApiKey {
api_key_token,
}) => ApiKeyError::InvalidAPIKey {
message: api_key_token,
},
Some(ApiKeyValidationErrorResponse::EndUserApiKeyRateLimited {
wait_seconds,
user_facing_error,
..
}) => ApiKeyError::RateLimited {
wait_seconds,
user_facing_error,
},
None => match status_code.as_u16() {
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::NotFound,
_ => ApiKeyError::UnknownError,
},
},
)
})
Expand Down
Loading