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
6 changes: 4 additions & 2 deletions src/apis/api_key_service_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ pub struct ValidateApiKeyParams {
pub api_key_token: String,
}

/// struct for typed errors on the api keys service
// struct for typed errors on the api keys service
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum ApiKeyError {
BadRequest(crate::models::BadUpdateOrgRequest),
InvalidIntegrationAPIKey,
InvalidAPIKey,
InvalidEndUserAPIKey,
InvalidPersonalAPIKey,
InvalidOrgAPIKey,
NotFound,
UnknownValue(serde_json::Value),
UnknownError,
Expand Down
14 changes: 14 additions & 0 deletions src/models/validate_api_key_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ impl ValidateApiKeyResponse {
}
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct ValidatePersonalApiKeyResponse {
pub metadata: Option<serde_json::Value>,
pub user_metadata: UserMetadata,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct ValidateOrgApiKeyResponse {
pub metadata: Option<serde_json::Value>,
pub user_metadata: Option<UserMetadata>,
pub org_metadata: OrgInternalMetadata,
pub user_role_in_org: Option<OrgRole>,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct OrgInternalMetadata {
pub org_id: Uuid,
Expand Down
47 changes: 37 additions & 10 deletions src/propelauth/api_key.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::apis::api_key_service_api::{ApiKeyError, ApiKeyQueryParams, CreateApiKeyParams, UpdateApiKeyParams, ValidateApiKeyParams};
use crate::apis::configuration::Configuration;
use crate::models::{CreateApiKeyResponse, FetchApiKeyResponse, FetchApiKeysPagedResponse, ValidateApiKeyResponse};
use crate::models::validate_api_key_response::{ValidateOrgApiKeyResponse, ValidatePersonalApiKeyResponse};
use crate::propelauth::helpers::map_autogenerated_error;

pub struct ApiKeyService<'a> {
Expand All @@ -22,7 +23,7 @@ impl ApiKeyService<'_> {
bad_request,
)),
) => ApiKeyError::BadRequest(bad_request),
(401, _) => ApiKeyError::InvalidAPIKey,
(401, _) => ApiKeyError::InvalidIntegrationAPIKey,
(404, _) => ApiKeyError::NotFound,
_ => ApiKeyError::UnexpectedExceptionWithSDK,
},
Expand All @@ -44,7 +45,7 @@ impl ApiKeyService<'_> {
bad_request,
)),
) => ApiKeyError::BadRequest(bad_request),
(401, _) => ApiKeyError::InvalidAPIKey,
(401, _) => ApiKeyError::InvalidIntegrationAPIKey,
(404, _) => ApiKeyError::NotFound,
_ => ApiKeyError::UnknownError,
},
Expand All @@ -60,8 +61,8 @@ impl ApiKeyService<'_> {
err,
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidAPIKey,
404 => ApiKeyError::InvalidEndUserAPIKey,
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::InvalidAPIKey,
_ => ApiKeyError::UnknownError,
},
)
Expand All @@ -76,7 +77,7 @@ impl ApiKeyService<'_> {
err,
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidAPIKey,
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::NotFound,
_ => ApiKeyError::UnknownError,
},
Expand All @@ -92,8 +93,8 @@ impl ApiKeyService<'_> {
err,
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidAPIKey,
404 => ApiKeyError::InvalidEndUserAPIKey,
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::InvalidAPIKey,
_ => ApiKeyError::UnknownError,
},
)
Expand All @@ -110,8 +111,8 @@ impl ApiKeyService<'_> {
err,
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidAPIKey,
404 => ApiKeyError::InvalidEndUserAPIKey,
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::InvalidAPIKey,
_ => ApiKeyError::UnknownError,
},
)
Expand All @@ -128,11 +129,37 @@ impl ApiKeyService<'_> {
err,
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidAPIKey,
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::NotFound,
_ => ApiKeyError::UnknownError,
},
)
})
}

pub async fn validate_personal_api_key(&self, params: ValidateApiKeyParams) -> Result<ValidatePersonalApiKeyResponse, ApiKeyError> {
let resp = self.validate_api_key(params).await?;
if resp.user_metadata.is_none() || resp.org_metadata.is_some() {
return Err(ApiKeyError::InvalidPersonalAPIKey);
}

Ok(ValidatePersonalApiKeyResponse {
metadata: resp.metadata,
user_metadata: resp.user_metadata.unwrap(),
})
}

pub async fn validate_org_api_key(&self, params: ValidateApiKeyParams) -> Result<ValidateOrgApiKeyResponse, ApiKeyError> {
let resp = self.validate_api_key(params).await?;
if resp.org_metadata.is_none() {
return Err(ApiKeyError::InvalidOrgAPIKey);
}

Ok(ValidateOrgApiKeyResponse {
metadata: resp.metadata,
user_metadata: resp.user_metadata,
org_metadata: resp.org_metadata.unwrap(),
user_role_in_org: resp.user_role_in_org,
})
}
}