fix(chat): alter encryption logic for ai service (#9562)

This commit is contained in:
Apoorv Dixit
2025-10-01 17:49:31 +05:30
committed by GitHub
parent fbd92fa194
commit 382fae1a1c

View File

@ -1,10 +1,10 @@
use api_models::chat as chat_api; use api_models::chat as chat_api;
use common_utils::{type_name, types::keymanager::Identifier}; use common_utils::{
use diesel_models::hyperswitch_ai_interaction::{ crypto::{EncodeMessage, GcmAes256},
HyperswitchAiInteraction, HyperswitchAiInteractionNew, encryption::Encryption,
}; };
use diesel_models::hyperswitch_ai_interaction::HyperswitchAiInteractionNew;
use error_stack::ResultExt; use error_stack::ResultExt;
use hyperswitch_domain_models::type_encryption::{crypto_operation, CryptoOperation};
use masking::ExposeInterface; use masking::ExposeInterface;
use crate::{ use crate::{
@ -29,29 +29,20 @@ pub async fn construct_hyperswitch_ai_interaction(
encryption_key.as_bytes().to_vec() encryption_key.as_bytes().to_vec()
} }
}; };
let encrypted_user_query = crypto_operation::<String, masking::WithType>( let encrypted_user_query_bytes = GcmAes256
&state.into(), .encode_message(&key, &req.message.clone().expose().into_bytes())
type_name!(HyperswitchAiInteraction), .change_context(errors::ApiErrorResponse::InternalServerError)
CryptoOperation::Encrypt(req.message.clone()), .attach_printable("Failed to encrypt user query")?;
Identifier::Merchant(user_from_token.merchant_id.clone()),
&key,
)
.await
.and_then(|val| val.try_into_operation())
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to encrypt user query")?;
let encrypted_response = crypto_operation::<serde_json::Value, masking::WithType>( let encrypted_response_bytes = serde_json::to_vec(&response.response.clone())
&state.into(), .change_context(errors::ApiErrorResponse::InternalServerError)
type_name!(HyperswitchAiInteraction), .attach_printable("Failed to serialize response for encryption")
CryptoOperation::Encrypt(response.response.clone()), .and_then(|bytes| {
Identifier::Merchant(user_from_token.merchant_id.clone()), GcmAes256
&key, .encode_message(&key, &bytes)
) .change_context(errors::ApiErrorResponse::InternalServerError)
.await })
.and_then(|val| val.try_into_operation()) .attach_printable("Failed to encrypt response")?;
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to encrypt response")?;
Ok(HyperswitchAiInteractionNew { Ok(HyperswitchAiInteractionNew {
id: request_id.to_owned(), id: request_id.to_owned(),
@ -61,8 +52,8 @@ pub async fn construct_hyperswitch_ai_interaction(
profile_id: Some(user_from_token.profile_id.get_string_repr().to_string()), profile_id: Some(user_from_token.profile_id.get_string_repr().to_string()),
org_id: Some(user_from_token.org_id.get_string_repr().to_string()), org_id: Some(user_from_token.org_id.get_string_repr().to_string()),
role_id: Some(user_from_token.role_id.clone()), role_id: Some(user_from_token.role_id.clone()),
user_query: Some(encrypted_user_query.into()), user_query: Some(Encryption::new(encrypted_user_query_bytes.into())),
response: Some(encrypted_response.into()), response: Some(Encryption::new(encrypted_response_bytes.into())),
database_query: response.query_executed.clone().map(|q| q.expose()), database_query: response.query_executed.clone().map(|q| q.expose()),
interaction_status: Some(response.status.clone()), interaction_status: Some(response.status.clone()),
created_at: common_utils::date_time::now(), created_at: common_utils::date_time::now(),