mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-29 09:07:09 +08:00
fix(access_token): use fallback to connector_name if merchant_connector_id is not present (#4503)
This commit is contained in:
@ -5,7 +5,7 @@ use std::fmt::Display;
|
|||||||
/// Create a key for fetching the access token from redis
|
/// Create a key for fetching the access token from redis
|
||||||
pub fn create_access_token_key(
|
pub fn create_access_token_key(
|
||||||
merchant_id: impl Display,
|
merchant_id: impl Display,
|
||||||
merchant_connector_id: impl Display,
|
merchant_connector_id_or_connector_name: impl Display,
|
||||||
) -> String {
|
) -> String {
|
||||||
format!("access_token_{merchant_id}_{merchant_connector_id}")
|
format!("access_token_{merchant_id}_{merchant_connector_id_or_connector_name}")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -64,14 +64,20 @@ pub async fn add_access_token<
|
|||||||
{
|
{
|
||||||
let merchant_id = &merchant_account.merchant_id;
|
let merchant_id = &merchant_account.merchant_id;
|
||||||
let store = &*state.store;
|
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
|
.merchant_connector_id
|
||||||
.as_ref()
|
.clone()
|
||||||
.ok_or(errors::ApiErrorResponse::InternalServerError)
|
.unwrap_or(connector.connector_name.to_string());
|
||||||
.attach_printable("Missing merchant_connector_id in ConnectorData")?;
|
|
||||||
|
|
||||||
let old_access_token = store
|
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
|
.await
|
||||||
.change_context(errors::ApiErrorResponse::InternalServerError)
|
.change_context(errors::ApiErrorResponse::InternalServerError)
|
||||||
.attach_printable("DB error when accessing the access token")?;
|
.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
|
if let Err(access_token_set_error) = store
|
||||||
.set_access_token(
|
.set_access_token(
|
||||||
merchant_id,
|
merchant_id,
|
||||||
merchant_connector_id.as_str(),
|
&merchant_connector_id_or_connector_name,
|
||||||
access_token.clone(),
|
access_token.clone(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@ -24,13 +24,13 @@ pub trait ConnectorAccessToken {
|
|||||||
async fn get_access_token(
|
async fn get_access_token(
|
||||||
&self,
|
&self,
|
||||||
merchant_id: &str,
|
merchant_id: &str,
|
||||||
merchant_connector_id: &str,
|
merchant_connector_id_or_connector_name: &str,
|
||||||
) -> CustomResult<Option<types::AccessToken>, errors::StorageError>;
|
) -> CustomResult<Option<types::AccessToken>, errors::StorageError>;
|
||||||
|
|
||||||
async fn set_access_token(
|
async fn set_access_token(
|
||||||
&self,
|
&self,
|
||||||
merchant_id: &str,
|
merchant_id: &str,
|
||||||
merchant_connector_id: &str,
|
merchant_connector_id_or_connector_name: &str,
|
||||||
access_token: types::AccessToken,
|
access_token: types::AccessToken,
|
||||||
) -> CustomResult<(), errors::StorageError>;
|
) -> CustomResult<(), errors::StorageError>;
|
||||||
}
|
}
|
||||||
@ -41,13 +41,15 @@ impl ConnectorAccessToken for Store {
|
|||||||
async fn get_access_token(
|
async fn get_access_token(
|
||||||
&self,
|
&self,
|
||||||
merchant_id: &str,
|
merchant_id: &str,
|
||||||
merchant_connector_id: &str,
|
merchant_connector_id_or_connector_name: &str,
|
||||||
) -> CustomResult<Option<types::AccessToken>, errors::StorageError> {
|
) -> CustomResult<Option<types::AccessToken>, errors::StorageError> {
|
||||||
//TODO: Handle race condition
|
//TODO: Handle race condition
|
||||||
// This function should acquire a global lock on some resource, if access token is already
|
// 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
|
// being refreshed by other request then wait till it finishes and use the same access token
|
||||||
let key =
|
let key = common_utils::access_token::create_access_token_key(
|
||||||
common_utils::access_token::create_access_token_key(merchant_id, merchant_connector_id);
|
merchant_id,
|
||||||
|
merchant_connector_id_or_connector_name,
|
||||||
|
);
|
||||||
|
|
||||||
let maybe_token = self
|
let maybe_token = self
|
||||||
.get_redis_conn()
|
.get_redis_conn()
|
||||||
@ -69,11 +71,13 @@ impl ConnectorAccessToken for Store {
|
|||||||
async fn set_access_token(
|
async fn set_access_token(
|
||||||
&self,
|
&self,
|
||||||
merchant_id: &str,
|
merchant_id: &str,
|
||||||
merchant_connector_id: &str,
|
merchant_connector_id_or_connector_name: &str,
|
||||||
access_token: types::AccessToken,
|
access_token: types::AccessToken,
|
||||||
) -> CustomResult<(), errors::StorageError> {
|
) -> CustomResult<(), errors::StorageError> {
|
||||||
let key =
|
let key = common_utils::access_token::create_access_token_key(
|
||||||
common_utils::access_token::create_access_token_key(merchant_id, merchant_connector_id);
|
merchant_id,
|
||||||
|
merchant_connector_id_or_connector_name,
|
||||||
|
);
|
||||||
let serialized_access_token = access_token
|
let serialized_access_token = access_token
|
||||||
.encode_to_string_of_json()
|
.encode_to_string_of_json()
|
||||||
.change_context(errors::StorageError::SerializationFailed)?;
|
.change_context(errors::StorageError::SerializationFailed)?;
|
||||||
@ -90,7 +94,7 @@ impl ConnectorAccessToken for MockDb {
|
|||||||
async fn get_access_token(
|
async fn get_access_token(
|
||||||
&self,
|
&self,
|
||||||
_merchant_id: &str,
|
_merchant_id: &str,
|
||||||
_merchant_connector_id: &str,
|
_merchant_connector_id_or_connector_name: &str,
|
||||||
) -> CustomResult<Option<types::AccessToken>, errors::StorageError> {
|
) -> CustomResult<Option<types::AccessToken>, errors::StorageError> {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
@ -98,7 +102,7 @@ impl ConnectorAccessToken for MockDb {
|
|||||||
async fn set_access_token(
|
async fn set_access_token(
|
||||||
&self,
|
&self,
|
||||||
_merchant_id: &str,
|
_merchant_id: &str,
|
||||||
_merchant_connector_id: &str,
|
_merchant_connector_id_or_connector_name: &str,
|
||||||
_access_token: types::AccessToken,
|
_access_token: types::AccessToken,
|
||||||
) -> CustomResult<(), errors::StorageError> {
|
) -> CustomResult<(), errors::StorageError> {
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user