refactor(router): Introduce ApiKeyId id type (#6324)

This commit is contained in:
Anurag Thakur
2024-10-21 19:19:31 +05:30
committed by GitHub
parent 58296ffae6
commit b3ce373f8e
14 changed files with 166 additions and 75 deletions

View File

@ -20,20 +20,20 @@ pub trait ApiKeyInterface {
async fn update_api_key(
&self,
merchant_id: common_utils::id_type::MerchantId,
key_id: String,
key_id: common_utils::id_type::ApiKeyId,
api_key: storage::ApiKeyUpdate,
) -> CustomResult<storage::ApiKey, errors::StorageError>;
async fn revoke_api_key(
&self,
merchant_id: &common_utils::id_type::MerchantId,
key_id: &str,
key_id: &common_utils::id_type::ApiKeyId,
) -> CustomResult<bool, errors::StorageError>;
async fn find_api_key_by_merchant_id_key_id_optional(
&self,
merchant_id: &common_utils::id_type::MerchantId,
key_id: &str,
key_id: &common_utils::id_type::ApiKeyId,
) -> CustomResult<Option<storage::ApiKey>, errors::StorageError>;
async fn find_api_key_by_hash_optional(
@ -67,7 +67,7 @@ impl ApiKeyInterface for Store {
async fn update_api_key(
&self,
merchant_id: common_utils::id_type::MerchantId,
key_id: String,
key_id: common_utils::id_type::ApiKeyId,
api_key: storage::ApiKeyUpdate,
) -> CustomResult<storage::ApiKey, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
@ -99,7 +99,8 @@ impl ApiKeyInterface for Store {
.await
.map_err(|error| report!(errors::StorageError::from(error)))?
.ok_or(report!(errors::StorageError::ValueNotFound(format!(
"ApiKey of {_key_id} not found"
"ApiKey of {} not found",
_key_id.get_string_repr()
))))?;
cache::publish_and_redact(
@ -115,7 +116,7 @@ impl ApiKeyInterface for Store {
async fn revoke_api_key(
&self,
merchant_id: &common_utils::id_type::MerchantId,
key_id: &str,
key_id: &common_utils::id_type::ApiKeyId,
) -> CustomResult<bool, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
let delete_call = || async {
@ -141,7 +142,8 @@ impl ApiKeyInterface for Store {
.await
.map_err(|error| report!(errors::StorageError::from(error)))?
.ok_or(report!(errors::StorageError::ValueNotFound(format!(
"ApiKey of {key_id} not found"
"ApiKey of {} not found",
key_id.get_string_repr()
))))?;
cache::publish_and_redact(
@ -157,7 +159,7 @@ impl ApiKeyInterface for Store {
async fn find_api_key_by_merchant_id_key_id_optional(
&self,
merchant_id: &common_utils::id_type::MerchantId,
key_id: &str,
key_id: &common_utils::id_type::ApiKeyId,
) -> CustomResult<Option<storage::ApiKey>, errors::StorageError> {
let conn = connection::pg_connection_read(self).await?;
storage::ApiKey::find_optional_by_merchant_id_key_id(&conn, merchant_id, key_id)
@ -240,7 +242,7 @@ impl ApiKeyInterface for MockDb {
async fn update_api_key(
&self,
merchant_id: common_utils::id_type::MerchantId,
key_id: String,
key_id: common_utils::id_type::ApiKeyId,
api_key: storage::ApiKeyUpdate,
) -> CustomResult<storage::ApiKey, errors::StorageError> {
let mut locked_api_keys = self.api_keys.lock().await;
@ -282,13 +284,13 @@ impl ApiKeyInterface for MockDb {
async fn revoke_api_key(
&self,
merchant_id: &common_utils::id_type::MerchantId,
key_id: &str,
key_id: &common_utils::id_type::ApiKeyId,
) -> CustomResult<bool, errors::StorageError> {
let mut locked_api_keys = self.api_keys.lock().await;
// find the key to remove, if it exists
if let Some(pos) = locked_api_keys
.iter()
.position(|k| k.merchant_id == *merchant_id && k.key_id == key_id)
.position(|k| k.merchant_id == *merchant_id && k.key_id == *key_id)
{
// use `remove` instead of `swap_remove` so we have a consistent order, which might
// matter to someone using limit/offset in `list_api_keys_by_merchant_id`
@ -302,14 +304,14 @@ impl ApiKeyInterface for MockDb {
async fn find_api_key_by_merchant_id_key_id_optional(
&self,
merchant_id: &common_utils::id_type::MerchantId,
key_id: &str,
key_id: &common_utils::id_type::ApiKeyId,
) -> CustomResult<Option<storage::ApiKey>, errors::StorageError> {
Ok(self
.api_keys
.lock()
.await
.iter()
.find(|k| k.merchant_id == *merchant_id && k.key_id == key_id)
.find(|k| k.merchant_id == *merchant_id && k.key_id == *key_id)
.cloned())
}
@ -397,9 +399,16 @@ mod tests {
let merchant_id =
common_utils::id_type::MerchantId::try_from(Cow::from("merchant1")).unwrap();
let key_id1 = common_utils::id_type::ApiKeyId::try_from(Cow::from("key_id1")).unwrap();
let key_id2 = common_utils::id_type::ApiKeyId::try_from(Cow::from("key_id2")).unwrap();
let non_existent_key_id =
common_utils::id_type::ApiKeyId::try_from(Cow::from("does_not_exist")).unwrap();
let key1 = mockdb
.insert_api_key(storage::ApiKeyNew {
key_id: "key_id1".into(),
key_id: key_id1.clone(),
merchant_id: merchant_id.clone(),
name: "Key 1".into(),
description: None,
@ -414,7 +423,7 @@ mod tests {
mockdb
.insert_api_key(storage::ApiKeyNew {
key_id: "key_id2".into(),
key_id: key_id2.clone(),
merchant_id: merchant_id.clone(),
name: "Key 2".into(),
description: None,
@ -428,13 +437,13 @@ mod tests {
.unwrap();
let found_key1 = mockdb
.find_api_key_by_merchant_id_key_id_optional(&merchant_id, "key_id1")
.find_api_key_by_merchant_id_key_id_optional(&merchant_id, &key_id1)
.await
.unwrap()
.unwrap();
assert_eq!(found_key1.key_id, key1.key_id);
assert!(mockdb
.find_api_key_by_merchant_id_key_id_optional(&merchant_id, "does_not_exist")
.find_api_key_by_merchant_id_key_id_optional(&merchant_id, &non_existent_key_id)
.await
.unwrap()
.is_none());
@ -442,7 +451,7 @@ mod tests {
mockdb
.update_api_key(
merchant_id.clone(),
"key_id1".into(),
key_id1.clone(),
storage::ApiKeyUpdate::LastUsedUpdate {
last_used: datetime!(2023-02-04 1:11),
},
@ -450,7 +459,7 @@ mod tests {
.await
.unwrap();
let updated_key1 = mockdb
.find_api_key_by_merchant_id_key_id_optional(&merchant_id, "key_id1")
.find_api_key_by_merchant_id_key_id_optional(&merchant_id, &key_id1)
.await
.unwrap()
.unwrap();
@ -464,10 +473,7 @@ mod tests {
.len(),
2
);
mockdb
.revoke_api_key(&merchant_id, "key_id1")
.await
.unwrap();
mockdb.revoke_api_key(&merchant_id, &key_id1).await.unwrap();
assert_eq!(
mockdb
.list_api_keys_by_merchant_id(&merchant_id, None, None)
@ -495,8 +501,10 @@ mod tests {
.await
.unwrap();
let test_key = common_utils::id_type::ApiKeyId::try_from(Cow::from("test_ey")).unwrap();
let api = storage::ApiKeyNew {
key_id: "test_key".into(),
key_id: test_key.clone(),
merchant_id: merchant_id.clone(),
name: "My test key".into(),
description: None,