diff --git a/src/apis/api_key_service_api.rs b/src/apis/api_key_service_api.rs index 5f8eaf6..479b135 100644 --- a/src/apis/api_key_service_api.rs +++ b/src/apis/api_key_service_api.rs @@ -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, diff --git a/src/models/validate_api_key_response.rs b/src/models/validate_api_key_response.rs index f5e6f4a..0f83fc0 100644 --- a/src/models/validate_api_key_response.rs +++ b/src/models/validate_api_key_response.rs @@ -21,6 +21,20 @@ impl ValidateApiKeyResponse { } } +#[derive(Clone, Debug, PartialEq, Deserialize)] +pub struct ValidatePersonalApiKeyResponse { + pub metadata: Option, + pub user_metadata: UserMetadata, +} + +#[derive(Clone, Debug, PartialEq, Deserialize)] +pub struct ValidateOrgApiKeyResponse { + pub metadata: Option, + pub user_metadata: Option, + pub org_metadata: OrgInternalMetadata, + pub user_role_in_org: Option, +} + #[derive(Clone, Debug, PartialEq, Deserialize)] pub struct OrgInternalMetadata { pub org_id: Uuid, diff --git a/src/propelauth/api_key.rs b/src/propelauth/api_key.rs index a01a132..df82400 100644 --- a/src/propelauth/api_key.rs +++ b/src/propelauth/api_key.rs @@ -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> { @@ -22,7 +23,7 @@ impl ApiKeyService<'_> { bad_request, )), ) => ApiKeyError::BadRequest(bad_request), - (401, _) => ApiKeyError::InvalidAPIKey, + (401, _) => ApiKeyError::InvalidIntegrationAPIKey, (404, _) => ApiKeyError::NotFound, _ => ApiKeyError::UnexpectedExceptionWithSDK, }, @@ -44,7 +45,7 @@ impl ApiKeyService<'_> { bad_request, )), ) => ApiKeyError::BadRequest(bad_request), - (401, _) => ApiKeyError::InvalidAPIKey, + (401, _) => ApiKeyError::InvalidIntegrationAPIKey, (404, _) => ApiKeyError::NotFound, _ => ApiKeyError::UnknownError, }, @@ -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, }, ) @@ -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, }, @@ -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, }, ) @@ -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, }, ) @@ -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 { + 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 { + 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, + }) + } }