Skip to content

Commit

Permalink
changed implementation to retrieve primary user from auth resources
Browse files Browse the repository at this point in the history
  • Loading branch information
aka4rKO committed Dec 4, 2024
1 parent 2914f79 commit f830093
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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);
}
Expand Down
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,35 +134,27 @@ public void processEvent(OBEvent obEvent) {
AUTHORIZED_STATE.equalsIgnoreCase(obEvent.getEventType())) {

log.debug("Publishing consent data for metrics.");
HashMap<String, Object> 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<String, Object> 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);
}
Expand Down Expand Up @@ -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<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

0 comments on commit f830093

Please sign in to comment.