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

Fixed issues identified in Idempotency QA testing #190

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -204,8 +204,8 @@ protected Map<String, String> getConsentAttributesToPersist(ConsentManageData co
consentAttributesMap.put(ConsentExtensionConstants.TPP_EXPLICIT_AUTH_PREFERRED_HEADER,
headersMap.get(ConsentExtensionConstants.TPP_EXPLICIT_AUTH_PREFERRED_HEADER));
}
consentAttributesMap.put(ConsentExtensionConstants.X_REQUEST_ID,
headersMap.get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
consentAttributesMap.put(CommonConsentUtil.constructAttributeKey(consentManageData.getRequestPath(),
ConsentExtensionConstants.X_REQUEST_ID), headersMap.get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
return consentAttributesMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,21 @@ public void handle(ConsentManageData consentManageData) throws ConsentException
} else {
Map<String, String> attributesToStore = new HashMap<>();
if (StringUtils.equals(AuthTypeEnum.CANCELLATION.toString(), authType)) {
attributesToStore.put(ConsentExtensionConstants.AUTH_CANCEL_X_REQUEST_ID,
consentManageData.getHeaders().get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
attributesToStore.put(ConsentExtensionConstants.AUTH_CANCEL_CREATED_TIME,
String.valueOf(OffsetDateTime.now().toEpochSecond()));
String xRequestIdKey = CommonConsentUtil.constructAttributeKey(
consentManageData.getRequestPath(), ConsentExtensionConstants.AUTH_CANCEL_X_REQUEST_ID);
attributesToStore.put(xRequestIdKey, consentManageData.getHeaders()
.get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
String createdTimeKey = CommonConsentUtil.constructAttributeKey(
consentManageData.getRequestPath(), ConsentExtensionConstants.AUTH_CANCEL_CREATED_TIME);
attributesToStore.put(createdTimeKey, String.valueOf(OffsetDateTime.now().toEpochSecond()));
} else {
attributesToStore.put(ConsentExtensionConstants.EXPLICIT_AUTH_X_REQUEST_ID,
consentManageData.getHeaders().get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
attributesToStore.put(ConsentExtensionConstants.EXPLICIT_AUTH_CREATED_TIME,
String.valueOf(OffsetDateTime.now().toEpochSecond()));
String xRequestIdKey = CommonConsentUtil.constructAttributeKey(
consentManageData.getRequestPath(), ConsentExtensionConstants.EXPLICIT_AUTH_X_REQUEST_ID);
attributesToStore.put(xRequestIdKey, consentManageData.getHeaders()
.get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
String createdTimeKey = CommonConsentUtil.constructAttributeKey(
consentManageData.getRequestPath(), ConsentExtensionConstants.EXPLICIT_AUTH_CREATED_TIME);
attributesToStore.put(createdTimeKey, String.valueOf(OffsetDateTime.now().toEpochSecond()));
}

try {
Expand Down Expand Up @@ -290,15 +296,21 @@ protected Map<String, String> getConsentAttributesToPersist(ConsentManageData co

if (StringUtils.contains(consentManageData.getRequestPath(),
ConsentExtensionConstants.PAYMENT_EXPLICIT_CANCELLATION_AUTHORISATION_PATH_END)) {
consentAttributesMap.put(ConsentExtensionConstants.AUTH_CANCEL_X_REQUEST_ID,
consentManageData.getHeaders().get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
consentAttributesMap.put(ConsentExtensionConstants.AUTH_CANCEL_CREATED_TIME,
String.valueOf(OffsetDateTime.now().toEpochSecond()));
String xRequestIdKey = CommonConsentUtil.constructAttributeKey(
consentManageData.getRequestPath(), ConsentExtensionConstants.AUTH_CANCEL_X_REQUEST_ID);
consentAttributesMap.put(xRequestIdKey, consentManageData.getHeaders()
.get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
String createdTimeKey = CommonConsentUtil.constructAttributeKey(
consentManageData.getRequestPath(), ConsentExtensionConstants.AUTH_CANCEL_CREATED_TIME);
consentAttributesMap.put(createdTimeKey, String.valueOf(OffsetDateTime.now().toEpochSecond()));
} else {
consentAttributesMap.put(ConsentExtensionConstants.EXPLICIT_AUTH_X_REQUEST_ID,
consentManageData.getHeaders().get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
consentAttributesMap.put(ConsentExtensionConstants.EXPLICIT_AUTH_CREATED_TIME,
String.valueOf(OffsetDateTime.now().toEpochSecond()));
String xRequestIdKey = CommonConsentUtil.constructAttributeKey(
consentManageData.getRequestPath(), ConsentExtensionConstants.EXPLICIT_AUTH_X_REQUEST_ID);
attributesToStore.put(xRequestIdKey, consentManageData.getHeaders()
.get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
String createdTimeKey = CommonConsentUtil.constructAttributeKey(
consentManageData.getRequestPath(), ConsentExtensionConstants.EXPLICIT_AUTH_CREATED_TIME);
attributesToStore.put(createdTimeKey, String.valueOf(OffsetDateTime.now().toEpochSecond()));
}

return consentAttributesMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ protected Map<String, String> getConsentAttributesToPersist(ConsentManageData co
consentAttributesMap.put(ConsentExtensionConstants.TPP_EXPLICIT_AUTH_PREFERRED_HEADER,
headersMap.get(ConsentExtensionConstants.TPP_EXPLICIT_AUTH_PREFERRED_HEADER));
}
consentAttributesMap.put(ConsentExtensionConstants.X_REQUEST_ID,
headersMap.get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
consentAttributesMap.put(CommonConsentUtil.constructAttributeKey(consentManageData.getRequestPath(),
ConsentExtensionConstants.X_REQUEST_ID), headersMap.get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
return consentAttributesMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ protected Map<String, String> getConsentAttributesToPersist(ConsentManageData co

consentAttributesMap.put(ConsentExtensionConstants.PAYMENT_SERVICE, paymentService);
consentAttributesMap.put(ConsentExtensionConstants.PAYMENT_PRODUCT, paymentProduct);
consentAttributesMap.put(ConsentExtensionConstants.X_REQUEST_ID,
headersMap.get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));
consentAttributesMap.put(CommonConsentUtil.constructAttributeKey(consentManageData.getRequestPath(),
ConsentExtensionConstants.X_REQUEST_ID), headersMap.get(ConsentExtensionConstants.X_REQUEST_ID_HEADER));

if (!isExplicitAuth) {
CommonConsentUtil.storeInitiationScaInfoToConsentAttributes(consentAttributesMap, createdConsent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ public String getIdempotencyAttributeName(String resourcePath) {
String path = ConsentExtensionUtil.getServiceDifferentiatingRequestPath(resourcePath);
switch (path) {
case ConsentExtensionConstants.EXPLICIT_AUTHORISATION_PATH_END:
return ConsentExtensionConstants.EXPLICIT_AUTH_X_REQUEST_ID;
return CommonConsentUtil.constructAttributeKey(resourcePath,
ConsentExtensionConstants.EXPLICIT_AUTH_X_REQUEST_ID);
case ConsentExtensionConstants.PAYMENT_EXPLICIT_CANCELLATION_AUTHORISATION_PATH_END:
return ConsentExtensionConstants.AUTH_CANCEL_X_REQUEST_ID;
return CommonConsentUtil.constructAttributeKey(resourcePath,
ConsentExtensionConstants.AUTH_CANCEL_X_REQUEST_ID);
default:
return ConsentExtensionConstants.X_REQUEST_ID;
return CommonConsentUtil.constructAttributeKey(resourcePath, ConsentExtensionConstants.X_REQUEST_ID);
}
}

Expand Down Expand Up @@ -74,14 +76,14 @@ public long getCreatedTimeOfPreviousRequest(String resourcePath, String consentI
}

String path = ConsentExtensionUtil.getServiceDifferentiatingRequestPath(resourcePath);
if (ConsentExtensionConstants.PAYMENT_EXPLICIT_CANCELLATION_AUTHORISATION_PATH_END.equals(path) &&
isPresentAsConsentAttribute(consentRequest, ConsentExtensionConstants.AUTH_CANCEL_CREATED_TIME)) {
return getCreatedTimeFromConsentAttributes(consentRequest,
if (ConsentExtensionConstants.PAYMENT_EXPLICIT_CANCELLATION_AUTHORISATION_PATH_END.equals(path)) {
String attributeKey = CommonConsentUtil.constructAttributeKey(resourcePath,
ConsentExtensionConstants.AUTH_CANCEL_CREATED_TIME);
} else if (ConsentExtensionConstants.EXPLICIT_AUTHORISATION_PATH_END.equals(path) &&
isPresentAsConsentAttribute(consentRequest, ConsentExtensionConstants.EXPLICIT_AUTH_CREATED_TIME)) {
return getCreatedTimeFromConsentAttributes(consentRequest,
return getCreatedTimeFromConsentAttributes(consentRequest, attributeKey);
} else if (ConsentExtensionConstants.EXPLICIT_AUTHORISATION_PATH_END.equals(path)) {
String attributeKey = CommonConsentUtil.constructAttributeKey(resourcePath,
ConsentExtensionConstants.EXPLICIT_AUTH_CREATED_TIME);
return getCreatedTimeFromConsentAttributes(consentRequest, attributeKey);
} else {
return consentRequest.getCreatedTime();
}
Expand Down Expand Up @@ -132,10 +134,10 @@ private boolean isPresentAsConsentAttribute(DetailedConsentResource consentReque
* @return created time.
*/
private long getCreatedTimeFromConsentAttributes(DetailedConsentResource consentRequest, String key) {
if (consentRequest.getConsentAttributes() == null || !consentRequest.getConsentAttributes().containsKey(key)) {
return 0L;
if (isPresentAsConsentAttribute(consentRequest, key)) {
return Long.parseLong(consentRequest.getConsentAttributes().get(key));
}
return Long.parseLong(consentRequest.getConsentAttributes().get(key));
return 0L;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.wso2.openbanking.berlin.common.models.TPPMessage;
import com.wso2.openbanking.berlin.common.utils.CommonUtil;
import com.wso2.openbanking.berlin.common.utils.ErrorUtil;
import com.wso2.openbanking.berlin.consent.extensions.common.AuthTypeEnum;
import com.wso2.openbanking.berlin.consent.extensions.common.ConsentExtensionConstants;
import com.wso2.openbanking.berlin.consent.extensions.common.ConsentExtensionUtil;
import com.wso2.openbanking.berlin.consent.extensions.common.HeaderValidator;
Expand Down Expand Up @@ -283,6 +284,7 @@ public static boolean isIdempotent(ConsentManageData consentManageData) {
IdempotencyValidationResult result = idempotencyValidator.validateIdempotency(consentManageData);
if (result.isIdempotent()) {
if (result.isValid()) {
log.debug("Idempotent request. Returning the previous response.");
appendResponsePayload(consentManageData, result.getConsent());
return true;
} else {
Expand Down Expand Up @@ -341,8 +343,8 @@ private static void appendResponsePayload(ConsentManageData consentManageData, D
case ConsentExtensionConstants.PAYMENT_EXPLICIT_CANCELLATION_AUTHORISATION_PATH_END:
consentManageData.setResponsePayload(CommonConsentUtil
.constructStartAuthorisationResponse(consentManageData,
consent.getAuthorizationResources().get(0), true, apiVersion,
isSCARequired));
getCancelAuthResource(consent.getAuthorizationResources()), true,
apiVersion, isSCARequired));
consentManageData.setResponseStatus(ResponseStatus.CREATED);
break;
default:
Expand All @@ -351,6 +353,31 @@ private static void appendResponsePayload(ConsentManageData consentManageData, D

}

/**
* Method to construct the attribute key to store parameters as a consent attribute. It will be constructed as
* requestPath_param.
* @param requestPath Request path
* @param param Parameter name
* @return constructed attribute key
*/
public static String constructAttributeKey(String requestPath, String param) {
return StringUtils.join(requestPath, "_", param);
}

/**
* Method to filter the authorizations based on the cancellation authorization type.
* @param authResources List of authorization resources
* @return List of authorization resources filtered based on the cancellation authorization type
*/
private static AuthorizationResource getCancelAuthResource(ArrayList<AuthorizationResource> authResources) {
for (AuthorizationResource authResource : authResources) {
if (StringUtils.equals(AuthTypeEnum.CANCELLATION.toString(), authResource.getAuthorizationType())) {
return authResource;
}
}
return authResources.get(0);
}

@Generated(message = "Excluded from coverage since this is used for testing purposes")
public static IdempotencyValidator getIdempotencyValidator() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ validationQuery="SELECT 1"
#Use below for oracle
#validationQuery="SELECT 1 FROM DUAL"
validationInterval="30000"
defaultAutoCommit=false
defaultAutoCommit=true

# for api manager data
[database.apim_db]
Expand All @@ -71,7 +71,7 @@ validationQuery="SELECT 1"
#Use below for oracle
#validationQuery="SELECT 1 FROM DUAL"
validationInterval="30000"
defaultAutoCommit=false
defaultAutoCommit=true

# for am-config registry
[database.config]
Expand All @@ -89,7 +89,7 @@ validationQuery="SELECT 1"
#Use below for oracle
#validationQuery="SELECT 1 FROM DUAL"
validationInterval="30000"
defaultAutoCommit=false
defaultAutoCommit=true

# for user management data
[[datasource]]
Expand All @@ -107,7 +107,7 @@ pool_options.validationQuery="SELECT 1"
#Use below for oracle
#validationQuery="SELECT 1 FROM DUAL"
pool_options.validationInterval="30000"
pool_options.defaultAutoCommit=false
pool_options.defaultAutoCommit=true

# for Open Banking data
[[datasource]]
Expand All @@ -125,7 +125,7 @@ pool_options.validationQuery="SELECT 1"
#Use below for oracle
#validationQuery="SELECT 1 FROM DUAL"
pool_options.validationInterval="30000"
pool_options.defaultAutoCommit=false
pool_options.defaultAutoCommit=true

[tenant_mgt]
enable_email_domain = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ validationQuery="SELECT 1"
#Use below for oracle
#validationQuery="SELECT 1 FROM DUAL"
validationInterval="30000"
defaultAutoCommit=false
defaultAutoCommit=true

# for api manager data
[database.identity_db]
Expand All @@ -81,7 +81,7 @@ validationQuery="SELECT 1"
#Use below for oracle
#validationQuery="SELECT 1 FROM DUAL"
validationInterval="30000"
defaultAutoCommit=false
defaultAutoCommit=true

# for km-config registry
[database.config]
Expand All @@ -99,7 +99,7 @@ validationQuery="SELECT 1"
#Use below for oracle
#validationQuery="SELECT 1 FROM DUAL"
validationInterval="30000"
defaultAutoCommit=false
defaultAutoCommit=true

# for user-mgt data
[database.user]
Expand All @@ -117,7 +117,7 @@ validationQuery="SELECT 1"
#Use below for oracle
#validationQuery="SELECT 1 FROM DUAL"
validationInterval="30000"
defaultAutoCommit=false
defaultAutoCommit=true

# for Open Banking data
[[datasource]]
Expand All @@ -135,7 +135,7 @@ pool_options.validationQuery="SELECT 1"
#Use below for oracle
#validationQuery="SELECT 1 FROM DUAL"
pool_options.validationInterval="30000"
pool_options.defaultAutoCommit=false
pool_options.defaultAutoCommit=true

#================configs related to application-authentication.xml=============

Expand Down
Loading