From 2914f790dcdedd2668ede47bdfcdba12d1bacc83 Mon Sep 17 00:00:00 2001 From: aka4rKO Date: Wed, 4 Dec 2024 15:14:13 +0530 Subject: [PATCH 1/2] fix for recipientCount not updating issue --- .../impl/persist/CDSConsentPersistStep.java | 9 ++++++++- .../event/executor/CDSConsentEventExecutor.java | 13 ++++++++++++- .../CDSCustomerRecipientMetricsApp.siddhi | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/authorize/impl/persist/CDSConsentPersistStep.java b/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/authorize/impl/persist/CDSConsentPersistStep.java index 51c5cc59..1c4d1b76 100644 --- a/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/authorize/impl/persist/CDSConsentPersistStep.java +++ b/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/authorize/impl/persist/CDSConsentPersistStep.java @@ -259,7 +259,14 @@ protected DetailedConsentResource createConsent(ConsentCoreServiceImpl consentCo ConsentResource requestedConsent, ConsentData consentData) throws ConsentManagementException { - return consentCoreService.createAuthorizableConsent(requestedConsent, consentData.getUserId(), + String userId = consentData.getUserId(); + + // Adding a consent attribute to identify the primary user of the consent + Map consentAttributes = requestedConsent.getConsentAttributes(); + consentAttributes.put(CDSConsentExtensionConstants.AUTH_RESOURCE_TYPE_PRIMARY, userId); + requestedConsent.setConsentAttributes(consentAttributes); + + return consentCoreService.createAuthorizableConsent(requestedConsent, userId, CDSConsentExtensionConstants.CREATED_STATUS, CDSConsentExtensionConstants.AUTH_RESOURCE_TYPE_PRIMARY, true); } diff --git a/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutor.java b/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutor.java index 6532aeb3..58574004 100644 --- a/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutor.java +++ b/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutor.java @@ -132,10 +132,19 @@ public void processEvent(OBEvent obEvent) { log.debug("Publishing consent data for metrics."); HashMap consentData = new HashMap<>(); consentData.put(CONSENT_ID_KEY, eventData.get(CONSENT_ID)); - consentData.put(USER_ID_KEY, eventData.get(USER_ID)); consentData.put(CLIENT_ID_KEY, eventData.get(CLIENT_ID)); consentData.put(STATUS_KEY, obEvent.getEventType()); + String userId = null; + if (consentResource != null) { + userId = consentResource.getConsentAttributes() + .get(CDSConsentExtensionConstants.AUTH_RESOURCE_TYPE_PRIMARY); + } + + if (StringUtils.isBlank(userId)) { + userId = (String) eventData.get(USER_ID); + } + long expiryTime; if (AUTHORIZED_STATE.equalsIgnoreCase(obEvent.getEventType())) { if (consentResource != null) { @@ -148,6 +157,8 @@ public void processEvent(OBEvent obEvent) { } else { expiryTime = 0; } + + consentData.put(USER_ID_KEY, userId); consentData.put(EXPIRY_TIME_KEY, expiryTime); dataPublishingService.publishConsentData(consentData); } diff --git a/toolkits/ob-bi/carbon-home/deployment/siddhi-files/CDSCustomerRecipientMetricsApp.siddhi b/toolkits/ob-bi/carbon-home/deployment/siddhi-files/CDSCustomerRecipientMetricsApp.siddhi index 2e8209ac..9ef3fa21 100644 --- a/toolkits/ob-bi/carbon-home/deployment/siddhi-files/CDSCustomerRecipientMetricsApp.siddhi +++ b/toolkits/ob-bi/carbon-home/deployment/siddhi-files/CDSCustomerRecipientMetricsApp.siddhi @@ -93,7 +93,7 @@ select CDR_ARRANGEMENT_ID as CONSENT_ID, CLIENT_ID, USER_ID insert into CountableConsentAuthStream; -- Filter revoked authorizations that should be counted -from ConsentInputStream[STATUS == "Revoked"]#window.unique:first(CDR_ARRANGEMENT_ID) as I join CONSENT_RAW_DATA as T +from ConsentInputStream[str:equalsIgnoreCase(STATUS, "Revoked")]#window.unique:first(CDR_ARRANGEMENT_ID) as I join CONSENT_RAW_DATA as T on I.CDR_ARRANGEMENT_ID == T.CONSENT_ID select I.CDR_ARRANGEMENT_ID as CONSENT_ID, T.CLIENT_ID as CLIENT_ID, T.USER_ID as USER_ID, T.STATUS as CURRENT_STATUS having CURRENT_STATUS == "authorized" From f83009340e9caa2929ec13a5faf9858ae7afce7b Mon Sep 17 00:00:00 2001 From: aka4rKO Date: Wed, 4 Dec 2024 17:20:48 +0530 Subject: [PATCH 2/2] changed implementation to retrieve primary user from auth resources --- .../impl/persist/CDSConsentPersistStep.java | 9 +-- .../executor/CDSConsentEventExecutor.java | 78 +++++++++++++------ .../CDSConsentEventExecutorTests.java | 18 ++++- 3 files changed, 73 insertions(+), 32 deletions(-) diff --git a/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/authorize/impl/persist/CDSConsentPersistStep.java b/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/authorize/impl/persist/CDSConsentPersistStep.java index 1c4d1b76..51c5cc59 100644 --- a/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/authorize/impl/persist/CDSConsentPersistStep.java +++ b/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/authorize/impl/persist/CDSConsentPersistStep.java @@ -259,14 +259,7 @@ protected DetailedConsentResource createConsent(ConsentCoreServiceImpl consentCo ConsentResource requestedConsent, ConsentData consentData) throws ConsentManagementException { - String userId = consentData.getUserId(); - - // Adding a consent attribute to identify the primary user of the consent - Map consentAttributes = requestedConsent.getConsentAttributes(); - consentAttributes.put(CDSConsentExtensionConstants.AUTH_RESOURCE_TYPE_PRIMARY, userId); - requestedConsent.setConsentAttributes(consentAttributes); - - return consentCoreService.createAuthorizableConsent(requestedConsent, userId, + return consentCoreService.createAuthorizableConsent(requestedConsent, consentData.getUserId(), CDSConsentExtensionConstants.CREATED_STATUS, CDSConsentExtensionConstants.AUTH_RESOURCE_TYPE_PRIMARY, true); } diff --git a/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutor.java b/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutor.java index 58574004..8edb8148 100644 --- a/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutor.java +++ b/components/org.wso2.openbanking.cds.consent.extensions/src/main/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutor.java @@ -20,11 +20,14 @@ import com.wso2.openbanking.accelerator.common.event.executor.OBEventExecutor; import com.wso2.openbanking.accelerator.common.event.executor.model.OBEvent; +import com.wso2.openbanking.accelerator.common.exception.ConsentManagementException; import com.wso2.openbanking.accelerator.common.exception.OpenBankingException; import com.wso2.openbanking.accelerator.common.util.Generated; +import com.wso2.openbanking.accelerator.consent.mgt.dao.models.AuthorizationResource; import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; import com.wso2.openbanking.accelerator.consent.mgt.dao.models.DetailedConsentResource; import com.wso2.openbanking.accelerator.consent.mgt.service.constants.ConsentCoreServiceConstants; +import com.wso2.openbanking.accelerator.consent.mgt.service.impl.ConsentCoreServiceImpl; import com.wso2.openbanking.accelerator.identity.util.HTTPClientUtils; import com.wso2.openbanking.accelerator.identity.util.IdentityCommonHelper; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -71,6 +74,7 @@ public class CDSConsentEventExecutor implements OBEventExecutor { private static final Log log = LogFactory.getLog(CDSConsentEventExecutor.class); private CDSDataPublishingService dataPublishingService = CDSDataPublishingService.getCDSDataPublishingService(); + private ConsentCoreServiceImpl consentCoreService = new ConsentCoreServiceImpl(); private static final String DATA_RECIPIENT_CDR_ARRANGEMENT_REVOCATION_PATH = "/arrangements/revoke"; private static final String REVOKED_STATE = "revoked"; private static final String EXPIRED_STATE = "expired"; @@ -130,35 +134,27 @@ public void processEvent(OBEvent obEvent) { AUTHORIZED_STATE.equalsIgnoreCase(obEvent.getEventType())) { log.debug("Publishing consent data for metrics."); - HashMap consentData = new HashMap<>(); - consentData.put(CONSENT_ID_KEY, eventData.get(CONSENT_ID)); - consentData.put(CLIENT_ID_KEY, eventData.get(CLIENT_ID)); - consentData.put(STATUS_KEY, obEvent.getEventType()); - String userId = null; - if (consentResource != null) { - userId = consentResource.getConsentAttributes() - .get(CDSConsentExtensionConstants.AUTH_RESOURCE_TYPE_PRIMARY); + String consentId = (String) eventData.get(CONSENT_ID); + String primaryUserId; + try { + primaryUserId = getPrimaryUserForConsent(detailedConsentResource, consentId); + } catch (ConsentManagementException e) { + log.error("Error while trying to retrieve consent data", e); + return; } - if (StringUtils.isBlank(userId)) { - userId = (String) eventData.get(USER_ID); + if (StringUtils.isBlank(primaryUserId)) { + return; } - long expiryTime; - if (AUTHORIZED_STATE.equalsIgnoreCase(obEvent.getEventType())) { - if (consentResource != null) { - expiryTime = consentResource.getValidityPeriod(); - } else if (detailedConsentResource != null) { - expiryTime = detailedConsentResource.getValidityPeriod(); - } else { - expiryTime = OffsetDateTime.now(ZoneOffset.UTC).toEpochSecond(); - } - } else { - expiryTime = 0; - } + long expiryTime = getExpiryTime(obEvent, consentResource, detailedConsentResource); - consentData.put(USER_ID_KEY, userId); + HashMap consentData = new HashMap<>(); + consentData.put(CONSENT_ID_KEY, consentId); + consentData.put(USER_ID_KEY, primaryUserId); + consentData.put(CLIENT_ID_KEY, eventData.get(CLIENT_ID)); + consentData.put(STATUS_KEY, obEvent.getEventType()); consentData.put(EXPIRY_TIME_KEY, expiryTime); dataPublishingService.publishConsentData(consentData); } @@ -207,6 +203,42 @@ public void processEvent(OBEvent obEvent) { } + private String getPrimaryUserForConsent(DetailedConsentResource detailedConsentResource, String consentId) + throws ConsentManagementException { + + String primaryUser = null; + if (detailedConsentResource == null) { + detailedConsentResource = this.consentCoreService.getDetailedConsent(consentId); + } + + ArrayList authorizationResources = detailedConsentResource.getAuthorizationResources(); + for (AuthorizationResource authorizationResource : authorizationResources) { + if (CDSConsentExtensionConstants.AUTH_RESOURCE_TYPE_PRIMARY + .equalsIgnoreCase(authorizationResource.getAuthorizationType())) { + primaryUser = authorizationResource.getUserID(); + } + } + return primaryUser; + } + + private static long getExpiryTime(OBEvent obEvent, ConsentResource consentResource, + DetailedConsentResource detailedConsentResource) { + + long expiryTime; + if (AUTHORIZED_STATE.equalsIgnoreCase(obEvent.getEventType())) { + if (consentResource != null) { + expiryTime = consentResource.getValidityPeriod(); + } else if (detailedConsentResource != null) { + expiryTime = detailedConsentResource.getValidityPeriod(); + } else { + expiryTime = OffsetDateTime.now(ZoneOffset.UTC).toEpochSecond(); + } + } else { + expiryTime = 0; + } + return expiryTime; + } + /** * CDS Data Holder initiated CDR Arrangement Revocation: * to notify the Data Recipient of the consent withdrawn by a Customer via the Data Holder’s consent dashboard. diff --git a/components/org.wso2.openbanking.cds.consent.extensions/src/test/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutorTests.java b/components/org.wso2.openbanking.cds.consent.extensions/src/test/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutorTests.java index a70fb038..911ba667 100644 --- a/components/org.wso2.openbanking.cds.consent.extensions/src/test/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutorTests.java +++ b/components/org.wso2.openbanking.cds.consent.extensions/src/test/java/org/wso2/openbanking/cds/consent/extensions/event/executor/CDSConsentEventExecutorTests.java @@ -19,7 +19,9 @@ package org.wso2.openbanking.cds.consent.extensions.event.executor; import com.wso2.openbanking.accelerator.common.event.executor.model.OBEvent; +import com.wso2.openbanking.accelerator.common.exception.ConsentManagementException; import com.wso2.openbanking.accelerator.common.exception.OpenBankingException; +import com.wso2.openbanking.accelerator.consent.mgt.dao.models.AuthorizationResource; import com.wso2.openbanking.accelerator.consent.mgt.dao.models.ConsentResource; import com.wso2.openbanking.accelerator.consent.mgt.dao.models.DetailedConsentResource; import com.wso2.openbanking.accelerator.data.publisher.common.util.OBDataPublisherUtil; @@ -41,10 +43,13 @@ import org.testng.annotations.Test; import org.wso2.carbon.base.ServerConfiguration; import org.wso2.openbanking.cds.common.config.OpenBankingCDSConfigParser; +import org.wso2.openbanking.cds.consent.extensions.common.CDSConsentExtensionConstants; import org.wso2.openbanking.cds.identity.utils.CDSIdentityUtil; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -62,17 +67,26 @@ @PowerMockIgnore("jdk.internal.reflect.*") public class CDSConsentEventExecutorTests extends PowerMockTestCase { + public static final String USER_ID_PRIMARY = "test-primary-user-id"; + public static final String AUTH_ID_PRIMARY = "test-primary-auth-id"; + private static ByteArrayOutputStream outContent; private static Logger logger = null; private static PrintStream printStream; + private AuthorizationResource authResource; @BeforeClass - public void beforeTests() { + public void beforeTests() throws ConsentManagementException { outContent = new ByteArrayOutputStream(); printStream = new PrintStream(outContent); System.setOut(printStream); logger = LogManager.getLogger(CDSConsentEventExecutorTests.class); + + authResource = new AuthorizationResource(); + authResource.setAuthorizationID(AUTH_ID_PRIMARY); + authResource.setUserID(USER_ID_PRIMARY); + authResource.setAuthorizationType(CDSConsentExtensionConstants.AUTH_RESOURCE_TYPE_PRIMARY); } @Test @@ -110,6 +124,7 @@ public void testProcessEventSuccess() throws Exception { DetailedConsentResource detailedConsentResource = new DetailedConsentResource(); detailedConsentResource.setConsentAttributes(consentAttributes); + detailedConsentResource.setAuthorizationResources(new ArrayList<>(Arrays.asList(authResource))); consentDataMap.put("ConsentResource", consentResource); consentDataMap.put("DetailedConsentResource", detailedConsentResource); @@ -160,6 +175,7 @@ public void testProcessEventFailure() throws Exception { DetailedConsentResource detailedConsentResource = new DetailedConsentResource(); detailedConsentResource.setConsentAttributes(consentAttributes); + detailedConsentResource.setAuthorizationResources(new ArrayList<>(Arrays.asList(authResource))); consentDataMap.put("ConsentResource", consentResource); consentDataMap.put("DetailedConsentResource", detailedConsentResource);