fix(access_token): use fallback to connector_name if merchant_connector_id is not present (#4503)

This commit is contained in:
Narayan Bhat
2024-05-01 16:10:30 +05:30
committed by GitHub
parent dd7b10a8bd
commit 632a00cb68
3 changed files with 28 additions and 18 deletions

View File

@ -5,7 +5,7 @@ use std::fmt::Display;
/// Create a key for fetching the access token from redis
pub fn create_access_token_key(
merchant_id: impl Display,
merchant_connector_id: impl Display,
merchant_connector_id_or_connector_name: impl Display,
) -> String {
format!("access_token_{merchant_id}_{merchant_connector_id}")
format!("access_token_{merchant_id}_{merchant_connector_id_or_connector_name}")
}

View File

@ -64,14 +64,20 @@ pub async fn add_access_token<
{
let merchant_id = &merchant_account.merchant_id;
let store = &*state.store;
let merchant_connector_id = connector
// `merchant_connector_id` may not be present in the below cases
// - when straight through routing is used without passing the `merchant_connector_id`
// - when creds identifier is passed
//
// In these cases fallback to `connector_name`.
// We cannot use multiple merchant connector account in these cases
let merchant_connector_id_or_connector_name = connector
.merchant_connector_id
.as_ref()
.ok_or(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Missing merchant_connector_id in ConnectorData")?;
.clone()
.unwrap_or(connector.connector_name.to_string());
let old_access_token = store
.get_access_token(merchant_id, merchant_connector_id)
.get_access_token(merchant_id, &merchant_connector_id_or_connector_name)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("DB error when accessing the access token")?;
@ -115,7 +121,7 @@ pub async fn add_access_token<
if let Err(access_token_set_error) = store
.set_access_token(
merchant_id,
merchant_connector_id.as_str(),
&merchant_connector_id_or_connector_name,
access_token.clone(),
)
.await

View File

@ -24,13 +24,13 @@ pub trait ConnectorAccessToken {
async fn get_access_token(
&self,
merchant_id: &str,
merchant_connector_id: &str,
merchant_connector_id_or_connector_name: &str,
) -> CustomResult<Option<types::AccessToken>, errors::StorageError>;
async fn set_access_token(
&self,
merchant_id: &str,
merchant_connector_id: &str,
merchant_connector_id_or_connector_name: &str,
access_token: types::AccessToken,
) -> CustomResult<(), errors::StorageError>;
}
@ -41,13 +41,15 @@ impl ConnectorAccessToken for Store {
async fn get_access_token(
&self,
merchant_id: &str,
merchant_connector_id: &str,
merchant_connector_id_or_connector_name: &str,
) -> CustomResult<Option<types::AccessToken>, errors::StorageError> {
//TODO: Handle race condition
// This function should acquire a global lock on some resource, if access token is already
// being refreshed by other request then wait till it finishes and use the same access token
let key =
common_utils::access_token::create_access_token_key(merchant_id, merchant_connector_id);
let key = common_utils::access_token::create_access_token_key(
merchant_id,
merchant_connector_id_or_connector_name,
);
let maybe_token = self
.get_redis_conn()
@ -69,11 +71,13 @@ impl ConnectorAccessToken for Store {
async fn set_access_token(
&self,
merchant_id: &str,
merchant_connector_id: &str,
merchant_connector_id_or_connector_name: &str,
access_token: types::AccessToken,
) -> CustomResult<(), errors::StorageError> {
let key =
common_utils::access_token::create_access_token_key(merchant_id, merchant_connector_id);
let key = common_utils::access_token::create_access_token_key(
merchant_id,
merchant_connector_id_or_connector_name,
);
let serialized_access_token = access_token
.encode_to_string_of_json()
.change_context(errors::StorageError::SerializationFailed)?;
@ -90,7 +94,7 @@ impl ConnectorAccessToken for MockDb {
async fn get_access_token(
&self,
_merchant_id: &str,
_merchant_connector_id: &str,
_merchant_connector_id_or_connector_name: &str,
) -> CustomResult<Option<types::AccessToken>, errors::StorageError> {
Ok(None)
}
@ -98,7 +102,7 @@ impl ConnectorAccessToken for MockDb {
async fn set_access_token(
&self,
_merchant_id: &str,
_merchant_connector_id: &str,
_merchant_connector_id_or_connector_name: &str,
_access_token: types::AccessToken,
) -> CustomResult<(), errors::StorageError> {
Ok(())