Skip to content

Commit 967a8b1

Browse files
authored
add specific functions for validating Personal and Org API Keys (#5)
Two new functions to help validate your API keys: validate_personal_api_key and validate_org_api_key. These work similarly to the more general validate_api_key, but will also validate that the API key is a personal or an org key. The return types are slightly different, to reflect that.
1 parent d2a75a5 commit 967a8b1

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

src/apis/api_key_service_api.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ pub struct ValidateApiKeyParams {
4343
pub api_key_token: String,
4444
}
4545

46-
/// struct for typed errors on the api keys service
46+
// struct for typed errors on the api keys service
4747
#[derive(Debug, Clone, Deserialize)]
4848
#[serde(untagged)]
4949
pub enum ApiKeyError {
5050
BadRequest(crate::models::BadUpdateOrgRequest),
51+
InvalidIntegrationAPIKey,
5152
InvalidAPIKey,
52-
InvalidEndUserAPIKey,
53+
InvalidPersonalAPIKey,
54+
InvalidOrgAPIKey,
5355
NotFound,
5456
UnknownValue(serde_json::Value),
5557
UnknownError,

src/models/validate_api_key_response.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ impl ValidateApiKeyResponse {
2121
}
2222
}
2323

24+
#[derive(Clone, Debug, PartialEq, Deserialize)]
25+
pub struct ValidatePersonalApiKeyResponse {
26+
pub metadata: Option<serde_json::Value>,
27+
pub user_metadata: UserMetadata,
28+
}
29+
30+
#[derive(Clone, Debug, PartialEq, Deserialize)]
31+
pub struct ValidateOrgApiKeyResponse {
32+
pub metadata: Option<serde_json::Value>,
33+
pub user_metadata: Option<UserMetadata>,
34+
pub org_metadata: OrgInternalMetadata,
35+
pub user_role_in_org: Option<OrgRole>,
36+
}
37+
2438
#[derive(Clone, Debug, PartialEq, Deserialize)]
2539
pub struct OrgInternalMetadata {
2640
pub org_id: Uuid,

src/propelauth/api_key.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::apis::api_key_service_api::{ApiKeyError, ApiKeyQueryParams, CreateApiKeyParams, UpdateApiKeyParams, ValidateApiKeyParams};
22
use crate::apis::configuration::Configuration;
33
use crate::models::{CreateApiKeyResponse, FetchApiKeyResponse, FetchApiKeysPagedResponse, ValidateApiKeyResponse};
4+
use crate::models::validate_api_key_response::{ValidateOrgApiKeyResponse, ValidatePersonalApiKeyResponse};
45
use crate::propelauth::helpers::map_autogenerated_error;
56

67
pub struct ApiKeyService<'a> {
@@ -22,7 +23,7 @@ impl ApiKeyService<'_> {
2223
bad_request,
2324
)),
2425
) => ApiKeyError::BadRequest(bad_request),
25-
(401, _) => ApiKeyError::InvalidAPIKey,
26+
(401, _) => ApiKeyError::InvalidIntegrationAPIKey,
2627
(404, _) => ApiKeyError::NotFound,
2728
_ => ApiKeyError::UnexpectedExceptionWithSDK,
2829
},
@@ -44,7 +45,7 @@ impl ApiKeyService<'_> {
4445
bad_request,
4546
)),
4647
) => ApiKeyError::BadRequest(bad_request),
47-
(401, _) => ApiKeyError::InvalidAPIKey,
48+
(401, _) => ApiKeyError::InvalidIntegrationAPIKey,
4849
(404, _) => ApiKeyError::NotFound,
4950
_ => ApiKeyError::UnknownError,
5051
},
@@ -60,8 +61,8 @@ impl ApiKeyService<'_> {
6061
err,
6162
ApiKeyError::UnexpectedExceptionWithSDK,
6263
|status_code, _| match status_code.as_u16() {
63-
401 => ApiKeyError::InvalidAPIKey,
64-
404 => ApiKeyError::InvalidEndUserAPIKey,
64+
401 => ApiKeyError::InvalidIntegrationAPIKey,
65+
404 => ApiKeyError::InvalidAPIKey,
6566
_ => ApiKeyError::UnknownError,
6667
},
6768
)
@@ -76,7 +77,7 @@ impl ApiKeyService<'_> {
7677
err,
7778
ApiKeyError::UnexpectedExceptionWithSDK,
7879
|status_code, _| match status_code.as_u16() {
79-
401 => ApiKeyError::InvalidAPIKey,
80+
401 => ApiKeyError::InvalidIntegrationAPIKey,
8081
404 => ApiKeyError::NotFound,
8182
_ => ApiKeyError::UnknownError,
8283
},
@@ -92,8 +93,8 @@ impl ApiKeyService<'_> {
9293
err,
9394
ApiKeyError::UnexpectedExceptionWithSDK,
9495
|status_code, _| match status_code.as_u16() {
95-
401 => ApiKeyError::InvalidAPIKey,
96-
404 => ApiKeyError::InvalidEndUserAPIKey,
96+
401 => ApiKeyError::InvalidIntegrationAPIKey,
97+
404 => ApiKeyError::InvalidAPIKey,
9798
_ => ApiKeyError::UnknownError,
9899
},
99100
)
@@ -110,8 +111,8 @@ impl ApiKeyService<'_> {
110111
err,
111112
ApiKeyError::UnexpectedExceptionWithSDK,
112113
|status_code, _| match status_code.as_u16() {
113-
401 => ApiKeyError::InvalidAPIKey,
114-
404 => ApiKeyError::InvalidEndUserAPIKey,
114+
401 => ApiKeyError::InvalidIntegrationAPIKey,
115+
404 => ApiKeyError::InvalidAPIKey,
115116
_ => ApiKeyError::UnknownError,
116117
},
117118
)
@@ -128,11 +129,37 @@ impl ApiKeyService<'_> {
128129
err,
129130
ApiKeyError::UnexpectedExceptionWithSDK,
130131
|status_code, _| match status_code.as_u16() {
131-
401 => ApiKeyError::InvalidAPIKey,
132+
401 => ApiKeyError::InvalidIntegrationAPIKey,
132133
404 => ApiKeyError::NotFound,
133134
_ => ApiKeyError::UnknownError,
134135
},
135136
)
136137
})
137138
}
139+
140+
pub async fn validate_personal_api_key(&self, params: ValidateApiKeyParams) -> Result<ValidatePersonalApiKeyResponse, ApiKeyError> {
141+
let resp = self.validate_api_key(params).await?;
142+
if resp.user_metadata.is_none() || resp.org_metadata.is_some() {
143+
return Err(ApiKeyError::InvalidPersonalAPIKey);
144+
}
145+
146+
Ok(ValidatePersonalApiKeyResponse {
147+
metadata: resp.metadata,
148+
user_metadata: resp.user_metadata.unwrap(),
149+
})
150+
}
151+
152+
pub async fn validate_org_api_key(&self, params: ValidateApiKeyParams) -> Result<ValidateOrgApiKeyResponse, ApiKeyError> {
153+
let resp = self.validate_api_key(params).await?;
154+
if resp.org_metadata.is_none() {
155+
return Err(ApiKeyError::InvalidOrgAPIKey);
156+
}
157+
158+
Ok(ValidateOrgApiKeyResponse {
159+
metadata: resp.metadata,
160+
user_metadata: resp.user_metadata,
161+
org_metadata: resp.org_metadata.unwrap(),
162+
user_role_in_org: resp.user_role_in_org,
163+
})
164+
}
138165
}

0 commit comments

Comments
 (0)