Skip to content

Commit 8c53f8c

Browse files
authored
catch PropelAuthRateLimit error on all requests (#66)
* catch PropelAuthRateLimit error on all requests * bump minor version to 0.22.0
1 parent 1d385ab commit 8c53f8c

File tree

8 files changed

+154
-19
lines changed

8 files changed

+154
-19
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "propelauth"
3-
version = "0.21.0"
3+
version = "0.22.0"
44
authors = ["support@propelauth.com"]
55
description = "A Rust crate for managing authentication and authorization with support for multi-tenant / B2B products, powered by PropelAuth"
66
keywords = ["authentication", "auth", "authorization", "b2b", "tenant"]

src/apis/api_key_service_api.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub enum ApiKeyError {
5656
wait_seconds: f64,
5757
user_facing_error: String,
5858
},
59+
PropelAuthRateLimit,
5960
InvalidPersonalAPIKey,
6061
InvalidOrgAPIKey,
6162
NotFound,

src/propelauth/access_token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ impl AccessTokenService<'_> {
3232
)),
3333
) => CreateAccessTokenError::BadRequest(bad_request),
3434
(401, _) => CreateAccessTokenError::InvalidApiKey,
35+
(429, _) => CreateAccessTokenError::PropelAuthRateLimit,
3536
(404, _) => CreateAccessTokenError::NotFound,
3637
_ => CreateAccessTokenError::UnexpectedException,
3738
},
@@ -61,6 +62,7 @@ impl AccessTokenService<'_> {
6162
)),
6263
) => CreateAccessTokenError::BadRequest(bad_request),
6364
(401, _) => CreateAccessTokenError::InvalidApiKey,
65+
(429, _) => CreateAccessTokenError::PropelAuthRateLimit,
6466
(404, _) => CreateAccessTokenError::NotFound,
6567
_ => CreateAccessTokenError::UnexpectedException,
6668
},

src/propelauth/api_key.rs

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
use crate::apis::api_key_service_api::{ApiKeyError, ApiKeyQueryParams, ApiKeyValidationErrorResponse, CreateApiKeyParams, UpdateApiKeyParams, ValidateApiKeyParams};
1+
use crate::apis::api_key_service_api::{
2+
ApiKeyError, ApiKeyQueryParams, ApiKeyValidationErrorResponse, CreateApiKeyParams,
3+
UpdateApiKeyParams, ValidateApiKeyParams,
4+
};
25
use crate::apis::configuration::Configuration;
3-
use crate::models::{CreateApiKeyResponse, FetchApiKeyResponse, FetchApiKeysPagedResponse, ValidateApiKeyResponse};
4-
use crate::models::validate_api_key_response::{ValidateOrgApiKeyResponse, ValidatePersonalApiKeyResponse};
6+
use crate::models::validate_api_key_response::{
7+
ValidateOrgApiKeyResponse, ValidatePersonalApiKeyResponse,
8+
};
9+
use crate::models::{
10+
CreateApiKeyResponse, FetchApiKeyResponse, FetchApiKeysPagedResponse, ValidateApiKeyResponse,
11+
};
512
use crate::propelauth::helpers::map_autogenerated_error;
613

714
pub struct ApiKeyService<'a> {
815
pub(crate) config: &'a Configuration,
916
}
1017

1118
impl ApiKeyService<'_> {
12-
pub async fn fetch_current_api_keys(&self, params: ApiKeyQueryParams) -> Result<FetchApiKeysPagedResponse, ApiKeyError> {
19+
pub async fn fetch_current_api_keys(
20+
&self,
21+
params: ApiKeyQueryParams,
22+
) -> Result<FetchApiKeysPagedResponse, ApiKeyError> {
1323
crate::apis::api_key_service_api::fetch_current_api_keys(&self.config, params)
1424
.await
1525
.map_err(|err| {
@@ -20,18 +30,22 @@ impl ApiKeyService<'_> {
2030
(
2131
_,
2232
Some(crate::apis::api_key_service_api::ApiKeyError::BadRequest(
23-
bad_request,
24-
)),
33+
bad_request,
34+
)),
2535
) => ApiKeyError::BadRequest(bad_request),
2636
(401, _) => ApiKeyError::InvalidIntegrationAPIKey,
37+
(429, _) => ApiKeyError::PropelAuthRateLimit,
2738
(404, _) => ApiKeyError::NotFound,
2839
_ => ApiKeyError::UnexpectedExceptionWithSDK,
2940
},
3041
)
3142
})
3243
}
3344

34-
pub async fn fetch_archived_api_keys(&self, params: ApiKeyQueryParams) -> Result<FetchApiKeysPagedResponse, ApiKeyError> {
45+
pub async fn fetch_archived_api_keys(
46+
&self,
47+
params: ApiKeyQueryParams,
48+
) -> Result<FetchApiKeysPagedResponse, ApiKeyError> {
3549
crate::apis::api_key_service_api::fetch_archived_api_keys(&self.config, params)
3650
.await
3751
.map_err(|err| {
@@ -42,18 +56,22 @@ impl ApiKeyService<'_> {
4256
(
4357
_,
4458
Some(crate::apis::api_key_service_api::ApiKeyError::BadRequest(
45-
bad_request,
46-
)),
59+
bad_request,
60+
)),
4761
) => ApiKeyError::BadRequest(bad_request),
4862
(401, _) => ApiKeyError::InvalidIntegrationAPIKey,
63+
(429, _) => ApiKeyError::PropelAuthRateLimit,
4964
(404, _) => ApiKeyError::NotFound,
5065
_ => ApiKeyError::UnknownError,
5166
},
5267
)
5368
})
5469
}
5570

56-
pub async fn fetch_api_key(&self, api_key_id: String) -> Result<FetchApiKeyResponse, ApiKeyError> {
71+
pub async fn fetch_api_key(
72+
&self,
73+
api_key_id: String,
74+
) -> Result<FetchApiKeyResponse, ApiKeyError> {
5775
crate::apis::api_key_service_api::fetch_api_key(&self.config, api_key_id)
5876
.await
5977
.map_err(|err| {
@@ -62,14 +80,18 @@ impl ApiKeyService<'_> {
6280
ApiKeyError::UnexpectedExceptionWithSDK,
6381
|status_code, _| match status_code.as_u16() {
6482
401 => ApiKeyError::InvalidIntegrationAPIKey,
83+
429 => ApiKeyError::PropelAuthRateLimit,
6584
404 => ApiKeyError::NotFound,
6685
_ => ApiKeyError::UnknownError,
6786
},
6887
)
6988
})
7089
}
7190

72-
pub async fn create_api_key(&self, params: CreateApiKeyParams) -> Result<CreateApiKeyResponse, ApiKeyError> {
91+
pub async fn create_api_key(
92+
&self,
93+
params: CreateApiKeyParams,
94+
) -> Result<CreateApiKeyResponse, ApiKeyError> {
7395
crate::apis::api_key_service_api::create_api_key(&self.config, params)
7496
.await
7597
.map_err(|err| {
@@ -78,14 +100,19 @@ impl ApiKeyService<'_> {
78100
ApiKeyError::UnexpectedExceptionWithSDK,
79101
|status_code, _| match status_code.as_u16() {
80102
401 => ApiKeyError::InvalidIntegrationAPIKey,
103+
429 => ApiKeyError::PropelAuthRateLimit,
81104
404 => ApiKeyError::NotFound,
82105
_ => ApiKeyError::UnknownError,
83106
},
84107
)
85108
})
86109
}
87110

88-
pub async fn update_api_key(&self, api_key_id: String, params: UpdateApiKeyParams) -> Result<(), ApiKeyError> {
111+
pub async fn update_api_key(
112+
&self,
113+
api_key_id: String,
114+
params: UpdateApiKeyParams,
115+
) -> Result<(), ApiKeyError> {
89116
crate::apis::api_key_service_api::update_api_key(&self.config, api_key_id, params)
90117
.await
91118
.map_err(|err| {
@@ -94,6 +121,7 @@ impl ApiKeyService<'_> {
94121
ApiKeyError::UnexpectedExceptionWithSDK,
95122
|status_code, _| match status_code.as_u16() {
96123
401 => ApiKeyError::InvalidIntegrationAPIKey,
124+
429 => ApiKeyError::PropelAuthRateLimit,
97125
404 => ApiKeyError::NotFound,
98126
_ => ApiKeyError::UnknownError,
99127
},
@@ -112,6 +140,7 @@ impl ApiKeyService<'_> {
112140
ApiKeyError::UnexpectedExceptionWithSDK,
113141
|status_code, _| match status_code.as_u16() {
114142
401 => ApiKeyError::InvalidIntegrationAPIKey,
143+
429 => ApiKeyError::PropelAuthRateLimit,
115144
404 => ApiKeyError::NotFound,
116145
_ => ApiKeyError::UnknownError,
117146
},
@@ -127,10 +156,10 @@ impl ApiKeyService<'_> {
127156
) -> Result<ValidateApiKeyResponse, ApiKeyError> {
128157
if hex::decode(&params.api_key_token).is_err() {
129158
return Err(ApiKeyError::InvalidAPIKey {
130-
message: "Invalid API key format.".to_string()
131-
});
159+
message: "Invalid API key format.".to_string(),
160+
});
132161
}
133-
162+
134163
crate::apis::api_key_service_api::validate_api_key(&self.config, params)
135164
.await
136165
.map_err(|err| {
@@ -153,6 +182,7 @@ impl ApiKeyService<'_> {
153182
},
154183
None => match status_code.as_u16() {
155184
401 => ApiKeyError::InvalidIntegrationAPIKey,
185+
429 => ApiKeyError::PropelAuthRateLimit,
156186
404 => ApiKeyError::NotFound,
157187
_ => ApiKeyError::UnknownError,
158188
},
@@ -161,7 +191,10 @@ impl ApiKeyService<'_> {
161191
})
162192
}
163193

164-
pub async fn validate_personal_api_key(&self, params: ValidateApiKeyParams) -> Result<ValidatePersonalApiKeyResponse, ApiKeyError> {
194+
pub async fn validate_personal_api_key(
195+
&self,
196+
params: ValidateApiKeyParams,
197+
) -> Result<ValidatePersonalApiKeyResponse, ApiKeyError> {
165198
let resp = self.validate_api_key(params).await?;
166199
if resp.user.is_none() || resp.org.is_some() {
167200
return Err(ApiKeyError::InvalidPersonalAPIKey);
@@ -173,7 +206,10 @@ impl ApiKeyService<'_> {
173206
})
174207
}
175208

176-
pub async fn validate_org_api_key(&self, params: ValidateApiKeyParams) -> Result<ValidateOrgApiKeyResponse, ApiKeyError> {
209+
pub async fn validate_org_api_key(
210+
&self,
211+
params: ValidateApiKeyParams,
212+
) -> Result<ValidateOrgApiKeyResponse, ApiKeyError> {
177213
let resp = self.validate_api_key(params).await?;
178214
if resp.org.is_none() {
179215
return Err(ApiKeyError::InvalidOrgAPIKey);

src/propelauth/auth.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use url::Url;
33
use crate::apis::auth_service_api::token_verification_metadata;
44
use crate::apis::configuration::Configuration;
55
use crate::models::AuthTokenVerificationMetadata;
6+
use crate::propelauth::access_token::AccessTokenService;
67
use crate::propelauth::api_key::ApiKeyService;
78
use crate::propelauth::errors::InitializationError;
89
use crate::propelauth::helpers::map_autogenerated_error;
910
use crate::propelauth::options::{AuthOptions, AuthOptionsWithTokenVerification};
1011
use crate::propelauth::org::OrgService;
1112
use crate::propelauth::token::TokenService;
1213
use crate::propelauth::user::UserService;
13-
use crate::propelauth::access_token::AccessTokenService;
1414

1515
/// The main entrypoint of this library.
1616
/// All authentication, authorization and API requests starts from this struct
@@ -57,6 +57,7 @@ impl PropelAuth {
5757
InitializationError::UnexpectedException,
5858
|status, _| match status.as_u16() {
5959
401 => InitializationError::InvalidApiKey,
60+
429 => InitializationError::PropelAuthRateLimit,
6061
_ => InitializationError::UnexpectedException,
6162
},
6263
)

0 commit comments

Comments
 (0)