Skip to content

Commit a99393d

Browse files
authored
catch rate-limiting errors and fix other error-parsing for api key validation (#63)
* catch rate-limiting errors and fix other error-parsing for api key validation * add back 404 case * fix error handling of invalid / non-hex api key inputs
1 parent 8483856 commit a99393d

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

src/apis/api_key_service_api.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ pub struct ValidateApiKeyParams {
4949
pub enum ApiKeyError {
5050
BadRequest(crate::models::BadUpdateOrgRequest),
5151
InvalidIntegrationAPIKey,
52-
InvalidAPIKey,
52+
InvalidAPIKey {
53+
message: String,
54+
},
55+
RateLimited {
56+
wait_seconds: f64,
57+
user_facing_error: String,
58+
},
5359
InvalidPersonalAPIKey,
5460
InvalidOrgAPIKey,
5561
NotFound,
@@ -58,6 +64,19 @@ pub enum ApiKeyError {
5864
UnexpectedExceptionWithSDK,
5965
}
6066

67+
#[derive(Debug, Clone, Deserialize)]
68+
#[serde(untagged)]
69+
pub enum ApiKeyValidationErrorResponse {
70+
InvalidEndUserApiKey {
71+
api_key_token: String,
72+
},
73+
EndUserApiKeyRateLimited {
74+
wait_seconds: f64,
75+
error_code: String,
76+
user_facing_error: String,
77+
},
78+
}
79+
6180
pub async fn fetch_current_api_keys(
6281
configuration: &configuration::Configuration,
6382
params: ApiKeyQueryParams,
@@ -340,11 +359,7 @@ pub async fn delete_api_key(
340359
pub async fn validate_api_key(
341360
configuration: &configuration::Configuration,
342361
params: ValidateApiKeyParams,
343-
) -> Result<crate::models::ValidateApiKeyResponse, Error<ApiKeyError>> {
344-
if hex::decode(&params.api_key_token).is_err() {
345-
return Err(Error::Params("Invalid API key ID format".to_string()));
346-
}
347-
362+
) -> Result<crate::models::ValidateApiKeyResponse, Error<ApiKeyValidationErrorResponse>> {
348363
let client = &configuration.client;
349364

350365
let uri = format!(
@@ -371,7 +386,7 @@ pub async fn validate_api_key(
371386
if !status.is_client_error() && !status.is_server_error() {
372387
serde_json::from_str(&content).map_err(Error::from)
373388
} else {
374-
let entity: Option<ApiKeyError> = serde_json::from_str(&content).ok();
389+
let entity: Option<ApiKeyValidationErrorResponse> = serde_json::from_str(&content).ok();
375390
let error = ResponseContent {
376391
status,
377392
content,

src/propelauth/api_key.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::apis::api_key_service_api::{ApiKeyError, ApiKeyQueryParams, CreateApiKeyParams, UpdateApiKeyParams, ValidateApiKeyParams};
1+
use crate::apis::api_key_service_api::{ApiKeyError, ApiKeyQueryParams, ApiKeyValidationErrorResponse, CreateApiKeyParams, UpdateApiKeyParams, ValidateApiKeyParams};
22
use crate::apis::configuration::Configuration;
33
use crate::models::{CreateApiKeyResponse, FetchApiKeyResponse, FetchApiKeysPagedResponse, ValidateApiKeyResponse};
44
use crate::models::validate_api_key_response::{ValidateOrgApiKeyResponse, ValidatePersonalApiKeyResponse};
@@ -62,7 +62,7 @@ impl ApiKeyService<'_> {
6262
ApiKeyError::UnexpectedExceptionWithSDK,
6363
|status_code, _| match status_code.as_u16() {
6464
401 => ApiKeyError::InvalidIntegrationAPIKey,
65-
404 => ApiKeyError::InvalidAPIKey,
65+
404 => ApiKeyError::NotFound,
6666
_ => ApiKeyError::UnknownError,
6767
},
6868
)
@@ -94,7 +94,7 @@ impl ApiKeyService<'_> {
9494
ApiKeyError::UnexpectedExceptionWithSDK,
9595
|status_code, _| match status_code.as_u16() {
9696
401 => ApiKeyError::InvalidIntegrationAPIKey,
97-
404 => ApiKeyError::InvalidAPIKey,
97+
404 => ApiKeyError::NotFound,
9898
_ => ApiKeyError::UnknownError,
9999
},
100100
)
@@ -112,7 +112,7 @@ impl ApiKeyService<'_> {
112112
ApiKeyError::UnexpectedExceptionWithSDK,
113113
|status_code, _| match status_code.as_u16() {
114114
401 => ApiKeyError::InvalidIntegrationAPIKey,
115-
404 => ApiKeyError::InvalidAPIKey,
115+
404 => ApiKeyError::NotFound,
116116
_ => ApiKeyError::UnknownError,
117117
},
118118
)
@@ -121,17 +121,41 @@ impl ApiKeyService<'_> {
121121
Ok(())
122122
}
123123

124-
pub async fn validate_api_key(&self, params: ValidateApiKeyParams) -> Result<ValidateApiKeyResponse, ApiKeyError> {
124+
pub async fn validate_api_key(
125+
&self,
126+
params: ValidateApiKeyParams,
127+
) -> Result<ValidateApiKeyResponse, ApiKeyError> {
128+
if hex::decode(&params.api_key_token).is_err() {
129+
return Err(ApiKeyError::InvalidAPIKey {
130+
message: "Invalid API key format.".to_string()
131+
});
132+
}
133+
125134
crate::apis::api_key_service_api::validate_api_key(&self.config, params)
126135
.await
127136
.map_err(|err| {
128137
map_autogenerated_error(
129138
err,
130139
ApiKeyError::UnexpectedExceptionWithSDK,
131-
|status_code, _| match status_code.as_u16() {
132-
401 => ApiKeyError::InvalidIntegrationAPIKey,
133-
404 => ApiKeyError::NotFound,
134-
_ => ApiKeyError::UnknownError,
140+
|status_code, error_response_body| match error_response_body {
141+
Some(ApiKeyValidationErrorResponse::InvalidEndUserApiKey {
142+
api_key_token,
143+
}) => ApiKeyError::InvalidAPIKey {
144+
message: api_key_token,
145+
},
146+
Some(ApiKeyValidationErrorResponse::EndUserApiKeyRateLimited {
147+
wait_seconds,
148+
user_facing_error,
149+
..
150+
}) => ApiKeyError::RateLimited {
151+
wait_seconds,
152+
user_facing_error,
153+
},
154+
None => match status_code.as_u16() {
155+
401 => ApiKeyError::InvalidIntegrationAPIKey,
156+
404 => ApiKeyError::NotFound,
157+
_ => ApiKeyError::UnknownError,
158+
},
135159
},
136160
)
137161
})

0 commit comments

Comments
 (0)