Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for recipientCount not updating issue #57

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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<String, Object> 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);
}
Expand Down Expand Up @@ -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<AuthorizationResource> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down