feat: add a wrapper for encryption and decryption (#5502)

This commit is contained in:
Kartikeya Hegde
2024-08-07 17:50:36 +05:30
committed by GitHub
parent 3604b4ffac
commit f51b6c91a7
30 changed files with 746 additions and 358 deletions

View File

@ -22,8 +22,6 @@ use api_models::{
};
use base64::Engine;
use common_utils::types::keymanager::KeyManagerState;
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))]
use common_utils::types::keymanager::{Identifier, ToEncryptable};
pub use common_utils::{
crypto,
ext_traits::{ByteSliceExt, BytesExt, Encode, StringExt, ValueExt},
@ -31,10 +29,15 @@ pub use common_utils::{
id_type,
validation::validate_email,
};
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))]
use common_utils::{
type_name,
types::keymanager::{Identifier, ToEncryptable},
};
use error_stack::ResultExt;
use hyperswitch_domain_models::payments::PaymentIntent;
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))]
use hyperswitch_domain_models::type_encryption::batch_encrypt;
use hyperswitch_domain_models::type_encryption::{crypto_operation, CryptoOperation};
use image::Luma;
use nanoid::nanoid;
use qrcode;
@ -794,17 +797,21 @@ impl CustomerAddress for api_models::customers::CustomerRequest {
storage_scheme: storage::enums::MerchantStorageScheme,
merchant_id: id_type::MerchantId,
) -> CustomResult<storage::AddressUpdate, common_utils::errors::CryptoError> {
let encrypted_data = batch_encrypt(
let encrypted_data = crypto_operation(
&state.into(),
AddressDetailsWithPhone::to_encryptable(AddressDetailsWithPhone {
address: Some(address_details.clone()),
phone_number: self.phone.clone(),
email: self.email.clone(),
}),
type_name!(storage::Address),
CryptoOperation::BatchEncrypt(AddressDetailsWithPhone::to_encryptable(
AddressDetailsWithPhone {
address: Some(address_details.clone()),
phone_number: self.phone.clone(),
email: self.email.clone(),
},
)),
Identifier::Merchant(merchant_id),
key,
)
.await?;
.await
.and_then(|val| val.try_into_batchoperation())?;
let encryptable_address = AddressDetailsWithPhone::from_encryptable(encrypted_data)
.change_context(common_utils::errors::CryptoError::EncodingFailed)?;
Ok(storage::AddressUpdate::Update {
@ -833,17 +840,21 @@ impl CustomerAddress for api_models::customers::CustomerRequest {
key: &[u8],
storage_scheme: storage::enums::MerchantStorageScheme,
) -> CustomResult<domain::CustomerAddress, common_utils::errors::CryptoError> {
let encrypted_data = batch_encrypt(
let encrypted_data = crypto_operation(
&state.into(),
AddressDetailsWithPhone::to_encryptable(AddressDetailsWithPhone {
address: Some(address_details.clone()),
phone_number: self.phone.clone(),
email: self.email.clone(),
}),
type_name!(storage::Address),
CryptoOperation::BatchEncrypt(AddressDetailsWithPhone::to_encryptable(
AddressDetailsWithPhone {
address: Some(address_details.clone()),
phone_number: self.phone.clone(),
email: self.email.clone(),
},
)),
Identifier::Merchant(merchant_id.to_owned()),
key,
)
.await?;
.await
.and_then(|val| val.try_into_batchoperation())?;
let encryptable_address = AddressDetailsWithPhone::from_encryptable(encrypted_data)
.change_context(common_utils::errors::CryptoError::EncodingFailed)?;
let address = domain::Address {