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..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,24 +134,27 @@ public void processEvent(OBEvent obEvent) { AUTHORIZED_STATE.equalsIgnoreCase(obEvent.getEventType())) { log.debug("Publishing consent data for metrics."); + + 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(primaryUserId)) { + return; + } + + long expiryTime = getExpiryTime(obEvent, consentResource, detailedConsentResource); + HashMap consentData = new HashMap<>(); - consentData.put(CONSENT_ID_KEY, eventData.get(CONSENT_ID)); - consentData.put(USER_ID_KEY, eventData.get(USER_ID)); + 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()); - - 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; - } consentData.put(EXPIRY_TIME_KEY, expiryTime); dataPublishingService.publishConsentData(consentData); } @@ -196,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); 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"