Skip to content

Commit

Permalink
Merge pull request #49 from aka4rKO/cookie-main
Browse files Browse the repository at this point in the history
Getting customer profile type using cookie
  • Loading branch information
amjadhifthikar authored Nov 22, 2024
2 parents 60859bd + 4abc53c commit 6e8f694
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,17 @@ public String getBNRCustomerTypeSelectionMethod() {
((String) getConfigElementFromKey(CommonConstants.CUSTOMER_TYPE_SELECTION_METHOD)).trim();
}

/**
* Get the customer type selection cookie name for cookie_data method.
*
* @return configured value
*/
public String getBNRCustomerTypeSelectionCookieName() {

return getConfigElementFromKey(CommonConstants.CUSTOMER_TYPE_SELECTION_COOKIE_NAME) == null ? "" :
((String) getConfigElementFromKey(CommonConstants.CUSTOMER_TYPE_SELECTION_COOKIE_NAME)).trim();
}

/**
* Get secondary user accounts enabled status.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class CommonConstants {
public static final String VALIDATE_ACCOUNTS_ON_RETRIEVAL = "BNR.ValidateAccountsOnRetrieval";
public static final String ENABLE_CONSENT_REVOCATION = "BNR.EnableConsentRevocation";
public static final String CUSTOMER_TYPE_SELECTION_METHOD = "BNR.CustomerTypeSelectionMethod";
public static final String CUSTOMER_TYPE_SELECTION_COOKIE_NAME = "BNR.CustomerTypeSelectionCookieName";
public static final String EXTERNAL_TRAFFIC_HEADER_NAME = "ExternalTraffic.HeaderName";
public static final String EXTERNAL_TRAFFIC_EXPECTED_VALUE = "ExternalTraffic.ExpectedValue";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<!--Todo:Improve test coverage-->
<minimum>0.64</minimum>
<minimum>0.63</minimum>
</limit>
</limits>
</rule>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ public void execute(ConsentData consentData, JSONObject jsonObject) throws Conse
String customerTypeSelectionMethod = openBankingCDSConfigParser.getBNRCustomerTypeSelectionMethod();
if (CustomerTypeSelectionMethodEnum.CUSTOMER_UTYPE.toString().equals(customerTypeSelectionMethod)) {
customerType = CDSConsentCommonUtil.getCustomerType(consentData);
} else if (CustomerTypeSelectionMethodEnum.COOKIE_DATA.toString().equals(customerTypeSelectionMethod)) {
//ToDo: Implement cookie data based customer type selection.
customerType = null;
}

/* CustomerTypeSelectionMethodEnum.COOKIE_DATA related behaviour moved to
OBCDSAuthServletImpl.updateRequestAttribute method */

Map<String, String> profileMap = new HashMap<>();
Map<String, List<String>> profileIdAccountsMap = new HashMap<>();
String userId = CDSConsentCommonUtil.getUserIdWithTenantDomain(consentData.getUserId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@
import org.json.JSONObject;
import org.wso2.openbanking.cds.common.config.OpenBankingCDSConfigParser;
import org.wso2.openbanking.cds.common.utils.CommonConstants;
import org.wso2.openbanking.cds.consent.extensions.authorize.utils.CustomerTypeSelectionMethodEnum;
import org.wso2.openbanking.cds.consent.extensions.common.CDSConsentExtensionConstants;
import org.wso2.openbanking.cds.consent.extensions.util.CDSConsentExtensionsUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
Expand Down Expand Up @@ -92,6 +96,39 @@ public Map<String, Object> updateRequestAttribute(HttpServletRequest httpServlet
preSelectedProfileId = (String) dataSet.get(CDSConsentExtensionConstants.PRE_SELECTED_PROFILE_ID);
}

// Get Customer Type Selection Method from config
String customerTypeSelectionMethod = OpenBankingCDSConfigParser.getInstance()
.getBNRCustomerTypeSelectionMethod();
if (CustomerTypeSelectionMethodEnum.COOKIE_DATA.toString().equals(customerTypeSelectionMethod)) {
String preSelectedProfileIdFromCookie = null;
String customerTypeCookieName = OpenBankingCDSConfigParser.getInstance()
.getBNRCustomerTypeSelectionCookieName();

Cookie[] cookies = httpServletRequest.getCookies();
for (Cookie cookie : cookies) {
if (customerTypeCookieName.equalsIgnoreCase(cookie.getName())) {
preSelectedProfileIdFromCookie = cookie.getValue();
break;
}
}

if (StringUtils.isNotBlank(preSelectedProfileIdFromCookie)) {
if (log.isDebugEnabled()) {
log.debug(String.format("Retrieved customer profile %s from cookie.",
preSelectedProfileIdFromCookie));
}

// Setting profile ID as a request attribute when profile selection data is sent using cookies
try {
httpServletRequest.setAttribute(CDSConsentExtensionConstants.PRE_SELECTED_PROFILE_ID,
URLEncoder.encode(preSelectedProfileIdFromCookie, "UTF-8"));
preSelectedProfileId = preSelectedProfileIdFromCookie;
} catch (UnsupportedEncodingException e) {
log.error("Unable to encode the profile selection cookie value", e);
}
}
}

//Consent amendment flow
if (isConsentAmendment) {
// Add new data requested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@
{% else %}
<CustomerTypeSelectionMethod>profile_selection</CustomerTypeSelectionMethod>
{% endif %}
{% if open_banking_cds.bnr.customer_type_selection_cookie_name is defined %}
<CustomerTypeSelectionCookieName>{{open_banking_cds.bnr.customer_type_selection_cookie_name}}</CustomerTypeSelectionCookieName>
{% else %}
<CustomerTypeSelectionCookieName>customerProfile</CustomerTypeSelectionCookieName>
{% endif %}
{% if open_banking_cds.bnr.prioritize_sharable_accounts_response is defined %}
<PrioritizeSharableAccountsResponse>{{open_banking_cds.bnr.prioritize_sharable_accounts_response}}</PrioritizeSharableAccountsResponse>
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ class = "com.wso2.openbanking.accelerator.identity.token.validators.MTLSEnforcem

[open_banking_cds.bnr]
customer_type_selection_method="profile_selection" # values: profile_selection, customer_utype, cookie_data
customer_type_selection_cookie_name="customerProfile"
prioritize_sharable_accounts_response=true
enable_consent_revocation=true
validate_accounts_on_retrieval=true
Expand Down

0 comments on commit 6e8f694

Please sign in to comment.