Skip to content

Commit 53b5551

Browse files
authored
refactor(auth): Pass profile_id from the auth to core functions (#5520)
1 parent 3fea00c commit 53b5551

File tree

5 files changed

+121
-23
lines changed

5 files changed

+121
-23
lines changed

crates/router/src/routes/disputes.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ pub async fn retrieve_dispute(
4343
state,
4444
&req,
4545
dispute_id,
46-
|state, auth, req, _| disputes::retrieve_dispute(state, auth.merchant_account, None, req),
46+
|state, auth, req, _| {
47+
disputes::retrieve_dispute(state, auth.merchant_account, auth.profile_id, req)
48+
},
4749
auth::auth_type(
4850
&auth::HeaderAuth(auth::ApiKeyAuth),
4951
&auth::JWTAuth(Permission::DisputeRead),
@@ -133,7 +135,13 @@ pub async fn accept_dispute(
133135
&req,
134136
dispute_id,
135137
|state, auth, req, _| {
136-
disputes::accept_dispute(state, auth.merchant_account, None, auth.key_store, req)
138+
disputes::accept_dispute(
139+
state,
140+
auth.merchant_account,
141+
auth.profile_id,
142+
auth.key_store,
143+
req,
144+
)
137145
},
138146
auth::auth_type(
139147
&auth::HeaderAuth(auth::ApiKeyAuth),
@@ -170,7 +178,13 @@ pub async fn submit_dispute_evidence(
170178
&req,
171179
json_payload.into_inner(),
172180
|state, auth, req, _| {
173-
disputes::submit_evidence(state, auth.merchant_account, None, auth.key_store, req)
181+
disputes::submit_evidence(
182+
state,
183+
auth.merchant_account,
184+
auth.profile_id,
185+
auth.key_store,
186+
req,
187+
)
174188
},
175189
auth::auth_type(
176190
&auth::HeaderAuth(auth::ApiKeyAuth),
@@ -215,7 +229,13 @@ pub async fn attach_dispute_evidence(
215229
&req,
216230
attach_evidence_request,
217231
|state, auth, req, _| {
218-
disputes::attach_evidence(state, auth.merchant_account, None, auth.key_store, req)
232+
disputes::attach_evidence(
233+
state,
234+
auth.merchant_account,
235+
auth.profile_id,
236+
auth.key_store,
237+
req,
238+
)
219239
},
220240
auth::auth_type(
221241
&auth::HeaderAuth(auth::ApiKeyAuth),
@@ -258,7 +278,7 @@ pub async fn retrieve_dispute_evidence(
258278
&req,
259279
dispute_id,
260280
|state, auth, req, _| {
261-
disputes::retrieve_dispute_evidence(state, auth.merchant_account, None, req)
281+
disputes::retrieve_dispute_evidence(state, auth.merchant_account, auth.profile_id, req)
262282
},
263283
auth::auth_type(
264284
&auth::HeaderAuth(auth::ApiKeyAuth),

crates/router/src/routes/payments.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ pub async fn payments_start(
200200
state,
201201
req_state,
202202
auth.merchant_account,
203-
None,
203+
auth.profile_id,
204204
auth.key_store,
205205
payments::operations::PaymentStart,
206206
req,
@@ -276,7 +276,7 @@ pub async fn payments_retrieve(
276276
state,
277277
req_state,
278278
auth.merchant_account,
279-
None,
279+
auth.profile_id,
280280
auth.key_store,
281281
payments::PaymentStatus,
282282
req,
@@ -350,7 +350,7 @@ pub async fn payments_retrieve_with_gateway_creds(
350350
state,
351351
req_state,
352352
auth.merchant_account,
353-
None,
353+
auth.profile_id,
354354
auth.key_store,
355355
payments::PaymentStatus,
356356
req,
@@ -558,7 +558,7 @@ pub async fn payments_capture(
558558
state,
559559
req_state,
560560
auth.merchant_account,
561-
None,
561+
auth.profile_id,
562562
auth.key_store,
563563
payments::PaymentCapture,
564564
payload,
@@ -628,7 +628,7 @@ pub async fn payments_connector_session(
628628
state,
629629
req_state,
630630
auth.merchant_account,
631-
None,
631+
auth.profile_id,
632632
auth.key_store,
633633
payments::PaymentSession,
634634
payload,
@@ -861,7 +861,7 @@ pub async fn payments_complete_authorize(
861861
state.clone(),
862862
req_state,
863863
auth.merchant_account,
864-
None,
864+
auth.profile_id,
865865
auth.key_store,
866866
payments::operations::payment_complete_authorize::CompleteAuthorize,
867867
payment_confirm_req.clone(),
@@ -921,7 +921,7 @@ pub async fn payments_cancel(
921921
state,
922922
req_state,
923923
auth.merchant_account,
924-
None,
924+
auth.profile_id,
925925
auth.key_store,
926926
payments::PaymentCancel,
927927
req,
@@ -1088,7 +1088,7 @@ pub async fn payments_approve(
10881088
state,
10891089
req_state,
10901090
auth.merchant_account,
1091-
None,
1091+
auth.profile_id,
10921092
auth.key_store,
10931093
payments::PaymentApprove,
10941094
payment_types::PaymentsCaptureRequest {
@@ -1143,7 +1143,7 @@ pub async fn payments_reject(
11431143
state,
11441144
req_state,
11451145
auth.merchant_account,
1146-
None,
1146+
auth.profile_id,
11471147
auth.key_store,
11481148
payments::PaymentReject,
11491149
payment_types::PaymentsCancelRequest {
@@ -1295,7 +1295,7 @@ pub async fn payments_incremental_authorization(
12951295
state,
12961296
req_state,
12971297
auth.merchant_account,
1298-
None,
1298+
auth.profile_id,
12991299
auth.key_store,
13001300
payments::PaymentIncrementalAuthorization,
13011301
req,

crates/router/src/routes/payouts.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ pub async fn payouts_retrieve(
8080
&req,
8181
payout_retrieve_request,
8282
|state, auth, req, _| {
83-
payouts_retrieve_core(state, auth.merchant_account, None, auth.key_store, req)
83+
payouts_retrieve_core(
84+
state,
85+
auth.merchant_account,
86+
auth.profile_id,
87+
auth.key_store,
88+
req,
89+
)
8490
},
8591
auth::auth_type(
8692
&auth::HeaderAuth(auth::ApiKeyAuth),

crates/router/src/routes/refunds.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ pub async fn refunds_create(
3737
&req,
3838
json_payload.into_inner(),
3939
|state, auth, req, _| {
40-
refund_create_core(state, auth.merchant_account, None, auth.key_store, req)
40+
refund_create_core(
41+
state,
42+
auth.merchant_account,
43+
auth.profile_id,
44+
auth.key_store,
45+
req,
46+
)
4147
},
4248
auth::auth_type(
4349
&auth::HeaderAuth(auth::ApiKeyAuth),
@@ -94,7 +100,7 @@ pub async fn refunds_retrieve(
94100
refund_response_wrapper(
95101
state,
96102
auth.merchant_account,
97-
None,
103+
auth.profile_id,
98104
auth.key_store,
99105
refund_request,
100106
refund_retrieve_core,
@@ -146,7 +152,7 @@ pub async fn refunds_retrieve_with_body(
146152
refund_response_wrapper(
147153
state,
148154
auth.merchant_account,
149-
None,
155+
auth.profile_id,
150156
auth.key_store,
151157
req,
152158
refund_retrieve_core,

crates/router/src/services/authentication.rs

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub struct AuthenticationData {
5757
pub profile_id: Option<String>,
5858
}
5959

60-
#[derive(Clone)]
60+
#[derive(Clone, Debug)]
6161
pub struct AuthenticationDataWithMultipleProfiles {
6262
pub merchant_account: domain::MerchantAccount,
6363
pub key_store: domain::MerchantKeyStore,
@@ -251,6 +251,12 @@ impl AuthInfo for AuthenticationData {
251251
}
252252
}
253253

254+
impl AuthInfo for AuthenticationDataWithMultipleProfiles {
255+
fn get_merchant_id(&self) -> Option<&id_type::MerchantId> {
256+
Some(self.merchant_account.get_id())
257+
}
258+
}
259+
254260
#[async_trait]
255261
pub trait AuthenticateAndFetch<T, A>
256262
where
@@ -968,6 +974,65 @@ where
968974
}
969975
}
970976

977+
#[async_trait]
978+
impl<A> AuthenticateAndFetch<AuthenticationData, A> for JWTAuthMerchantFromRoute
979+
where
980+
A: SessionStateInfo + Sync,
981+
{
982+
async fn authenticate_and_fetch(
983+
&self,
984+
request_headers: &HeaderMap,
985+
state: &A,
986+
) -> RouterResult<(AuthenticationData, AuthenticationType)> {
987+
let payload = parse_jwt_payload::<A, AuthToken>(request_headers, state).await?;
988+
if payload.check_in_blacklist(state).await? {
989+
return Err(errors::ApiErrorResponse::InvalidJwtToken.into());
990+
}
991+
992+
if payload.merchant_id != self.merchant_id {
993+
return Err(report!(errors::ApiErrorResponse::InvalidJwtToken));
994+
}
995+
996+
let permissions = authorization::get_permissions(state, &payload).await?;
997+
authorization::check_authorization(&self.required_permission, &permissions)?;
998+
let key_manager_state = &(&state.session_state()).into();
999+
let key_store = state
1000+
.store()
1001+
.get_merchant_key_store_by_merchant_id(
1002+
key_manager_state,
1003+
&payload.merchant_id,
1004+
&state.store().get_master_key().to_vec().into(),
1005+
)
1006+
.await
1007+
.to_not_found_response(errors::ApiErrorResponse::InvalidJwtToken)
1008+
.attach_printable("Failed to fetch merchant key store for the merchant id")?;
1009+
1010+
let merchant = state
1011+
.store()
1012+
.find_merchant_account_by_merchant_id(
1013+
key_manager_state,
1014+
&payload.merchant_id,
1015+
&key_store,
1016+
)
1017+
.await
1018+
.to_not_found_response(errors::ApiErrorResponse::InvalidJwtToken)
1019+
.attach_printable("Failed to fetch merchant account for the merchant id")?;
1020+
1021+
let auth = AuthenticationData {
1022+
merchant_account: merchant,
1023+
key_store,
1024+
profile_id: payload.profile_id,
1025+
};
1026+
Ok((
1027+
auth.clone(),
1028+
AuthenticationType::MerchantJwt {
1029+
merchant_id: auth.merchant_account.get_id().clone(),
1030+
user_id: Some(payload.user_id),
1031+
},
1032+
))
1033+
}
1034+
}
1035+
9711036
pub struct JWTAuthMerchantOrProfileFromRoute {
9721037
pub merchant_id_or_profile_id: String,
9731038
pub required_permission: Permission,
@@ -1074,7 +1139,7 @@ where
10741139
&state.store().get_master_key().to_vec().into(),
10751140
)
10761141
.await
1077-
.change_context(errors::ApiErrorResponse::InvalidJwtToken)
1142+
.to_not_found_response(errors::ApiErrorResponse::InvalidJwtToken)
10781143
.attach_printable("Failed to fetch merchant key store for the merchant id")?;
10791144

10801145
let merchant = state
@@ -1085,7 +1150,8 @@ where
10851150
&key_store,
10861151
)
10871152
.await
1088-
.change_context(errors::ApiErrorResponse::InvalidJwtToken)?;
1153+
.to_not_found_response(errors::ApiErrorResponse::InvalidJwtToken)
1154+
.attach_printable("Failed to fetch merchant account for the merchant id")?;
10891155

10901156
let auth = AuthenticationData {
10911157
merchant_account: merchant,
@@ -1096,7 +1162,7 @@ where
10961162
auth.clone(),
10971163
AuthenticationType::MerchantJwt {
10981164
merchant_id: auth.merchant_account.get_id().clone(),
1099-
user_id: None,
1165+
user_id: Some(payload.user_id),
11001166
},
11011167
))
11021168
}

0 commit comments

Comments
 (0)