diff --git a/mediation-old/src/main/java/com/wso2telco/dep/mediator/AuthorizeResponseHandlerMediator.java b/mediation-old/src/main/java/com/wso2telco/dep/mediator/AuthorizeResponseHandlerMediator.java index 01b5e3d..2e6abd6 100644 --- a/mediation-old/src/main/java/com/wso2telco/dep/mediator/AuthorizeResponseHandlerMediator.java +++ b/mediation-old/src/main/java/com/wso2telco/dep/mediator/AuthorizeResponseHandlerMediator.java @@ -59,6 +59,9 @@ private void publishResponseData(String retStr, MessageContext messageContext) { messageContext.setProperty(DataPublisherConstants.MSISDN, messageContext.getProperty(MSISDNConstants.USER_MSISDN)); + if(Boolean.valueOf((String)messageContext.getProperty("USER_ANONYMIZATION")).booleanValue()) { + messageContext.setProperty(DataPublisherConstants.MSISDN, messageContext.getProperty(MSISDNConstants.MASKED_MSISDN)); + } boolean isPaymentReq = false; String paymentType=null; diff --git a/mediation-old/src/main/java/com/wso2telco/dep/mediator/MSISDNConstants.java b/mediation-old/src/main/java/com/wso2telco/dep/mediator/MSISDNConstants.java index f3ed204..abb5094 100644 --- a/mediation-old/src/main/java/com/wso2telco/dep/mediator/MSISDNConstants.java +++ b/mediation-old/src/main/java/com/wso2telco/dep/mediator/MSISDNConstants.java @@ -27,7 +27,36 @@ public final class MSISDNConstants { /** The Constant MSISDN. */ public static final String MSISDN = "MSISDN"; - + + /** The Constant MSISDN_LIST. */ + public static final String MSISDN_LIST = "MSISDN_LIST"; + + public static final String MSISDN_SUFFIX_LIST = "MSISDN_SUFFIX_LIST"; + + /** The Constant ADDRESSES. */ + public static final String ADDRESSES = "ADDRESSES"; + + /** The Constant to Actual MSISDN when request user PCR MSISDN */ + public static final String MASKED_MSISDN = "MASKED_MSISDN"; + + /** The Constant MASKED_MSISDN_SUFFIX. */ + public static final String MASKED_MSISDN_SUFFIX = "MASKED_MSISDN_SUFFIX"; + + /** The Constant USER_MASKING_SECRET_KEY. */ + public static final String USER_MASKING_SECRET_KEY = "USER_MASKING_SECRET_KEY"; + + /** The Constant to Actual MSISDNlist when request multiple user PCR MSISDN */ + public static final String MASKED_MSISDN_LIST = "MASKED_MSISDN_LIST"; + + /** The Constant to Actual MASKED_MSISDN_SUFFIX_LIST when request multiple user PCR MSISDN */ + public static final String MASKED_MSISDN_SUFFIX_LIST = "MASKED_MSISDN_SUFFIX_LIST"; + + /** The Constant to Actual MASKED_MSISDN_MAP when request multiple user PCR MSISDN */ + public static final String MASKED_MSISDN_MAP = "MASKED_MSISDN_MAP"; + + /** The Constant to Actual MASKED_MSISDN_SUFFIX_MAP when request multiple user PCR MSISDN */ + public static final String MASKED_MSISDN_SUFFIX_MAP = "MASKED_MSISDN_SUFFIX_MAP"; + /** The Constant AGGRIGATOR_ROLE. */ public static final String AGGRIGATOR_ROLE = "aggregator"; diff --git a/mediation-old/src/main/java/com/wso2telco/dep/mediator/RequestExecutor.java b/mediation-old/src/main/java/com/wso2telco/dep/mediator/RequestExecutor.java index e74216d..ca2f6f0 100644 --- a/mediation-old/src/main/java/com/wso2telco/dep/mediator/RequestExecutor.java +++ b/mediation-old/src/main/java/com/wso2telco/dep/mediator/RequestExecutor.java @@ -95,6 +95,9 @@ public abstract class RequestExecutor { // private String strErr; + /** userAnonymization request header */ + private boolean userAnonymization; + /** * Gets the str err. * @@ -204,6 +207,23 @@ public void setApiContext(String apiContext) { this.apiContext = apiContext; } + /** + * Get the request uses User Anonymization + * @return + */ + public boolean isUserAnonymization() { + return userAnonymization; + } + + /** + * Set the request uses User Anonymization + * @param userAnonymization + */ + public void setUserAnonymization(boolean userAnonymization) { + this.userAnonymization = userAnonymization; + } + + /** * Gets the validoperators. * @@ -308,7 +328,9 @@ public boolean initialize(MessageContext context) throws Exception { subResourcePath = (String) context.getProperty("REST_SUB_REQUEST_PATH"); resourceUrl = (String) context.getProperty("REST_FULL_REQUEST_PATH"); httpMethod = (String) context.getProperty("HTTP_METHOD"); - + if(context.getProperty("USER_ANONYMIZATION") != null) { + this.userAnonymization = Boolean.valueOf((String)context.getProperty("USER_ANONYMIZATION")); + } /*String jsonPayloadToString = JsonUtil .jsonPayloadToString(((Axis2MessageContext) context).getAxis2MessageContext()); diff --git a/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/payment/AmountChargeHandler.java b/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/payment/AmountChargeHandler.java index 10b09ab..fc16208 100644 --- a/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/payment/AmountChargeHandler.java +++ b/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/payment/AmountChargeHandler.java @@ -32,6 +32,7 @@ import com.wso2telco.dep.oneapivalidation.service.IServiceValidate; import com.wso2telco.dep.oneapivalidation.service.impl.payment.ValidatePaymentCharge; import com.wso2telco.dep.subscriptionvalidator.util.ValidatorUtils; +import com.wso2telco.dep.user.masking.UserMaskHandler; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.synapse.MessageContext; @@ -41,6 +42,8 @@ import org.wso2.carbon.utils.CarbonUtils; import java.io.File; +import java.net.URLDecoder; +import java.net.URLEncoder; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -80,7 +83,7 @@ public boolean handle(MessageContext context) throws Exception { HashMap jwtDetails = apiUtils.getJwtTokenDetails(context); OperatorEndpoint endpoint = null; String clientCorrelator = null; - String sending_add = null; + String sendingAddress = null; String requestResourceURL = executor.getResourceUrl(); @@ -101,12 +104,25 @@ public boolean handle(MessageContext context) throws Exception { if (log.isDebugEnabled()) { log.debug("Subscriber Name : " + subscriber); } + JSONObject jsonBody = executor.getJsonBody(); + try { - JSONObject jsonBody = executor.getJsonBody(); - try { - - String endUserId = jsonBody.getJSONObject("amountTransaction").getString("endUserId"); + String endUserId = jsonBody.getJSONObject("amountTransaction").getString("endUserId"); String msisdn = endUserId.substring(5); + + if(executor.isUserAnonymization()) { + context.setProperty(MSISDNConstants.MASKED_MSISDN, endUserId); + endUserId = UserMaskHandler.maskUserId(endUserId, false, + (String)context.getProperty(MSISDNConstants.USER_MASKING_SECRET_KEY) ); + String endUserIdSuffix = HandlerUtils.getMSISDNSuffix(endUserId); + context.setProperty("MSISDN_SUFFIX", endUserIdSuffix); + String maskedEndUserIdSuffix = UserMaskHandler.maskUserId(endUserIdSuffix, true, + (String) context.getProperty(MSISDNConstants.USER_MASKING_SECRET_KEY)); + context.setProperty(MSISDNConstants.MASKED_MSISDN_SUFFIX, maskedEndUserIdSuffix); + context.setProperty("MASKED_RESOURCE", context.getProperty("RESOURCE")); + } + + //Double chargeamount = Double.parseDouble(jsonBody.getJSONObject("amountTransaction").getJSONObject("paymentAmount").getJSONObject("chargingInformation").getString("amount")); context.setProperty(MSISDNConstants.USER_MSISDN, msisdn); @@ -125,10 +141,16 @@ public boolean handle(MessageContext context) throws Exception { } - sending_add = endpoint.getEndpointref().getAddress(); - if (log.isDebugEnabled()) { - log.info("sending endpoint found: " + sending_add + " Request ID: " + UID.getRequestID(context)); - } + sendingAddress = endpoint.getEndpointref().getAddress(); + log.info("sending endpoint found: " + sendingAddress + " Request ID: " + UID.getRequestID(context)); + if(executor.isUserAnonymization()) { + String maskedMsisdnSuffix = ValidationUtils.getMsisdnNumber( + URLDecoder.decode(((String)context.getProperty(MSISDNConstants.MASKED_MSISDN)) + .replaceAll("\\+", "%2b"), "UTF-8")); + String msisdnSuffix = ValidationUtils.getMsisdnNumber( + URLDecoder.decode(endUserId.replaceAll("\\+", "%2b"), "UTF-8")); + sendingAddress = sendingAddress.replace(URLEncoder.encode(maskedMsisdnSuffix, "UTF-8"), msisdnSuffix); + } JSONObject objAmountTransaction = jsonBody.getJSONObject("amountTransaction"); @@ -191,7 +213,7 @@ public boolean handle(MessageContext context) throws Exception { // set information to the message context, to be used in the sequence HandlerUtils.setHandlerProperty(context, this.getClass().getSimpleName()); - HandlerUtils.setEndpointProperty(context, sending_add); + HandlerUtils.setEndpointProperty(context, sendingAddress); HandlerUtils.setGatewayHost(context); HandlerUtils.setAuthorizationHeader(context, executor, endpoint); context.setProperty("operator", endpoint.getOperator()); @@ -207,7 +229,6 @@ public boolean handle(MessageContext context) throws Exception { @Override public boolean validate(String httpMethod, String requestPath, JSONObject jsonBody, MessageContext context) throws Exception { - IServiceValidate validator; String validatorClass = (String) context.getProperty("validatorClass"); if (!httpMethod.equalsIgnoreCase("POST")) { @@ -216,16 +237,10 @@ public boolean validate(String httpMethod, String requestPath, JSONObject jsonBo throw new Exception("Method not allowed"); } - if(validatorClass != null){ - Class clazz = Class.forName(validatorClass); - validator = (IServiceValidate) clazz.newInstance(); - }else{ - validator = new ValidatePaymentCharge(); - } - + IServiceValidate validator = new ValidatePaymentCharge(executor.isUserAnonymization(), (String)context.getProperty("USER_MASKING_SECRET_KEY")); validator.validateUrl(requestPath); validator.validate(jsonBody.toString()); - ValidationUtils.compareMsisdn(executor.getSubResourcePath(), executor.getJsonBody()); + ValidationUtils.compareMsisdn(executor.getSubResourcePath(), executor.getJsonBody(), executor.isUserAnonymization(), context); return true; } diff --git a/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/payment/AmountRefundHandler.java b/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/payment/AmountRefundHandler.java index b459bdd..70500fc 100644 --- a/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/payment/AmountRefundHandler.java +++ b/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/payment/AmountRefundHandler.java @@ -81,7 +81,8 @@ public boolean validate(String httpMethod, String requestPath, IServiceValidate validator = new ValidateRefund(); validator.validateUrl(requestPath); validator.validate(jsonBody.toString()); - ValidationUtils.compareMsisdn(executor.getSubResourcePath(), executor.getJsonBody()); + ValidationUtils.compareMsisdn(executor.getSubResourcePath(), executor.getJsonBody(), executor.isUserAnonymization(), context); + return true; } @@ -253,4 +254,4 @@ public static String nullOrTrimmed(String s) { return rv; } -} \ No newline at end of file +} diff --git a/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/smsmessaging/SendSMSHandler.java b/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/smsmessaging/SendSMSHandler.java index c615783..a0584f7 100644 --- a/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/smsmessaging/SendSMSHandler.java +++ b/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/smsmessaging/SendSMSHandler.java @@ -17,22 +17,6 @@ */ package com.wso2telco.dep.mediator.impl.smsmessaging; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.axis2.AxisFault; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.synapse.MessageContext; -import org.apache.synapse.core.axis2.Axis2MessageContext; -import org.json.JSONArray; -import org.json.JSONObject; - import com.google.common.base.CharMatcher; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -50,12 +34,13 @@ import com.wso2telco.dep.mediator.service.SMSMessagingService; import com.wso2telco.dep.mediator.util.DataPublisherConstants; import com.wso2telco.dep.mediator.util.HandlerUtils; +import com.wso2telco.dep.mediator.util.ValidationUtils; import com.wso2telco.dep.oneapivalidation.exceptions.CustomException; import com.wso2telco.dep.oneapivalidation.service.IServiceValidate; import com.wso2telco.dep.oneapivalidation.service.impl.smsmessaging.ValidateSendSms; import com.wso2telco.dep.operatorservice.model.OperatorApplicationDTO; import com.wso2telco.dep.subscriptionvalidator.exceptions.ValidatorException; -import com.wso2telco.dep.subscriptionvalidator.util.ValidatorUtils; +import com.wso2telco.dep.user.masking.UserMaskHandler; import org.apache.axis2.AxisFault; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -63,8 +48,7 @@ import org.apache.synapse.core.axis2.Axis2MessageContext; import org.json.JSONArray; import org.json.JSONObject; -import org.wso2.carbon.utils.CarbonUtils; -import java.io.File; + import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; @@ -155,6 +139,9 @@ public boolean handle(MessageContext context) throws CustomException, AxisFault, JSONArray addressArray = jsonBody.getJSONObject("outboundSMSMessageRequest").getJSONArray("address"); String firstAddress = addressArray.getString(0); + if (executor.isUserAnonymization() && UserMaskHandler.isMaskedUserId(firstAddress)) { + firstAddress = UserMaskHandler.maskUserId(firstAddress, false, (String)context.getProperty("USER_MASKING_SECRET_KEY")); + } if(firstAddress.contains("tel:+")){ @@ -172,6 +159,9 @@ public boolean handle(MessageContext context) throws CustomException, AxisFault, for (int a = 0; a < addressArray.length(); a++) { String address = addressArray.getString(a); + if (executor.isUserAnonymization() && UserMaskHandler.isMaskedUserId(address)) { + address = UserMaskHandler.maskUserId(address, false, (String)context.getProperty("USER_MASKING_SECRET_KEY")); + } if(address.contains("tel:+")){ @@ -193,9 +183,16 @@ public boolean handle(MessageContext context) throws CustomException, AxisFault, } } - // taking the operator endpoint with first address, since this is for same operator - OperatorEndpoint operatorEndpoint = getEndpoint(jsonBody.getJSONObject - ("outboundSMSMessageRequest").getJSONArray("address").getString(0), context, API_TYPE); + // Get first address to find operator endpoint + String firstAddress = jsonBody.getJSONObject("outboundSMSMessageRequest").getJSONArray("address").getString(0); + // Get resolved MSISDN if request used User Anonymization + if (executor.isUserAnonymization() && UserMaskHandler.isMaskedUserId(firstAddress)) { + context.setProperty(MSISDNConstants.MASKED_MSISDN_SUFFIX, HandlerUtils.getMSISDNSuffix(firstAddress)); + firstAddress = UserMaskHandler.maskUserId(firstAddress, false, (String)context.getProperty("USER_MASKING_SECRET_KEY")); + } + + // taking the operator endpoint with first address, since this is for same o,perator + OperatorEndpoint operatorEndpoint = getEndpoint(firstAddress, context, API_TYPE); String sending_add = operatorEndpoint.getEndpointref().getAddress(); log.info("sending endpoint found: " + sending_add + " Request ID: " + UID.getRequestID(context)); @@ -244,14 +241,48 @@ public boolean handle(MessageContext context) throws CustomException, AxisFault, JSONArray addressElement = jsonBody.getJSONObject("outboundSMSMessageRequest").getJSONArray("address"); String[] addresses = new String[addressElement.length()]; String addressList = ""; + String addressSuffixList = ""; + String maskedAddressList = ""; + String maskedAddressSuffixList = ""; + Map maskedMsisdnMap = new HashMap<>(); + Map maskedMsisdnSuffixMap = new HashMap<>(); + for (int i = 0 ; i < addressElement.length() ; i ++) { addresses[i] = addressElement.getString(i); + String resolvedAddress = addressElement.getString(i); + String resolvedAddressSuffix = HandlerUtils.getMSISDNSuffix(resolvedAddress); + if (executor.isUserAnonymization()) { + maskedAddressList += (addresses[i] + ","); + // Address List may contains unmasked user Ids too for build address list + if (UserMaskHandler.isMaskedUserId(addressElement.getString(i))) { + resolvedAddress = UserMaskHandler.maskUserId(addressElement.getString(i), false, + (String)context.getProperty(MSISDNConstants.USER_MASKING_SECRET_KEY)); + resolvedAddressSuffix = HandlerUtils.getMSISDNSuffix(resolvedAddress); + } + String maskedAddressSuffix = HandlerUtils.getMSISDNSuffix(addresses[i]); + maskedAddressSuffixList += maskedAddressSuffix + ","; + // Update map + maskedMsisdnMap.put(addresses[i], resolvedAddress); + maskedMsisdnSuffixMap.put(maskedAddressSuffix, resolvedAddressSuffix); + addresses[i] = resolvedAddress; + } addressList += (addresses[i] + ","); + addressSuffixList += resolvedAddressSuffix + ","; + } + context.setProperty(MSISDNConstants.ADDRESSES, addresses); + context.setProperty(MSISDNConstants.MSISDN_LIST, addressList.substring(0, addressList.length() - 1)); + context.setProperty(MSISDNConstants.MSISDN_SUFFIX_LIST, addressSuffixList.substring(0, addressSuffixList.length() - 1)); + + context.setProperty(MSISDNConstants.MSISDN_LIST, addressList.substring(0, addressList.length() - 1)); + if (executor.isUserAnonymization()) { + context.setProperty(MSISDNConstants.MASKED_MSISDN_LIST, maskedAddressList.substring(0, maskedAddressList.length() - 1)); + context.setProperty(MSISDNConstants.MASKED_MSISDN_MAP, maskedMsisdnMap); + + context.setProperty(MSISDNConstants.MASKED_MSISDN_SUFFIX_LIST, maskedAddressSuffixList.substring(0, maskedAddressSuffixList.length() - 1)); + context.setProperty(MSISDNConstants.MASKED_MSISDN_SUFFIX_MAP, maskedMsisdnSuffixMap); } - context.setProperty("ADDRESSES", addresses); - context.setProperty("MSISDN_LIST", addressList.substring(0, addressList.length() - 1)); - context.setProperty("RESPONSE_DELIVERY_INFO_RESOURCE_URL", getProperty("hubGateway")+ executor.getApiContext()+ "/" + executor.getApiVersion() + executor.getSubResourcePath()+ "/" +requestid + "/deliveryInfos"); + context.setProperty("RESPONSE_DELIVERY_INFO_RESOURCE_URL", getProperty("hubGateway")+executor.getResourceUrl() + "/" +requestid + "/deliveryInfos"); context.setProperty("OPERATOR_NAME", operatorEndpoint.getOperator()); context.setProperty("OPERATOR_ID", operatorEndpoint.getOperatorId()); @@ -303,10 +334,10 @@ public boolean validate(String httpMethod, String requestPath, JSONObject jsonBo context.setProperty(DataPublisherConstants.OPERATION_TYPE, 200); - IServiceValidate validator = new ValidateSendSms(); + IServiceValidate validator = new ValidateSendSms(executor.isUserAnonymization(), (String)context.getProperty("USER_MASKING_SECRET_KEY")); validator.validateUrl(requestPath); validator.validate(jsonBody.toString()); - + ValidationUtils.compareSenderId(executor.getSubResourcePath(), executor.getJsonBody(), context); String senderName = jsonBody.getJSONObject("outboundSMSMessageRequest").optString("senderName"); if (senderName.equals("") || senderName == null || senderName.length() < 10) { diff --git a/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/smsmessaging/SendSMSRequestIdPersistMediator.java b/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/smsmessaging/SendSMSRequestIdPersistMediator.java index 5594347..02147d5 100644 --- a/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/smsmessaging/SendSMSRequestIdPersistMediator.java +++ b/mediation-old/src/main/java/com/wso2telco/dep/mediator/impl/smsmessaging/SendSMSRequestIdPersistMediator.java @@ -17,6 +17,7 @@ package com.wso2telco.dep.mediator.impl.smsmessaging; import com.wso2telco.core.dbutils.exception.BusinessException; +import com.wso2telco.dep.mediator.MSISDNConstants; import com.wso2telco.dep.mediator.service.SMSMessagingService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -38,7 +39,15 @@ public boolean mediate(MessageContext messageContext) { // create JSON object out of 'ADDRESSES' property JSONArray addressArray; try { - addressArray = new JSONArray(messageContext.getProperty("ADDRESSES")); + if(Boolean.valueOf((String)messageContext.getProperty("USER_ANONYMIZATION")).booleanValue()) { + String[] addresses = ((String)messageContext.getProperty(MSISDNConstants.MASKED_MSISDN_LIST)).split(","); + addressArray = new JSONArray(); + for(String address : addresses) { + addressArray.put(address); + } + } else { + addressArray = new JSONArray(messageContext.getProperty("ADDRESSES")); + } } catch (JSONException e) { log.error("Error in creating json array out of: " + messageContext.getProperty("ADDRESSES"), e); return false; diff --git a/mediation-old/src/main/java/com/wso2telco/dep/mediator/util/HandlerUtils.java b/mediation-old/src/main/java/com/wso2telco/dep/mediator/util/HandlerUtils.java index bfb02fd..8fd9ce3 100644 --- a/mediation-old/src/main/java/com/wso2telco/dep/mediator/util/HandlerUtils.java +++ b/mediation-old/src/main/java/com/wso2telco/dep/mediator/util/HandlerUtils.java @@ -16,6 +16,7 @@ package com.wso2telco.dep.mediator.util; import com.wso2telco.core.dbutils.fileutils.FileReader; +import com.wso2telco.dep.mediator.MSISDNConstants; import com.wso2telco.dep.mediator.OperatorEndpoint; import com.wso2telco.dep.mediator.RequestExecutor; import org.apache.synapse.MessageContext; @@ -83,4 +84,20 @@ public static void setGatewayHost(MessageContext messageContext){ Map mediatorConfMap = fileReader.readPropertyFile(file); messageContext.setProperty("hubGateway", mediatorConfMap.get("hubGateway")); } + + + public static String getMSISDNSuffix(String msisdn) { + if (msisdn != null && !msisdn.isEmpty()) { + if (msisdn.contains(MSISDNConstants.TEL_1)) { + return msisdn.replace(MSISDNConstants.TEL_1, ""); + } else if (msisdn.contains(MSISDNConstants.TEL_2)) { + return msisdn.replace(MSISDNConstants.TEL_2, ""); + } else if (msisdn.contains(MSISDNConstants.TEL_3)) { + return msisdn.replace(MSISDNConstants.TEL_3, ""); + } else if (msisdn.contains(MSISDNConstants.PLUS)) { + return msisdn.replace(MSISDNConstants.PLUS, ""); + } + } + return msisdn; + } } diff --git a/mediation-old/src/main/java/com/wso2telco/dep/mediator/util/ValidationUtils.java b/mediation-old/src/main/java/com/wso2telco/dep/mediator/util/ValidationUtils.java index 782511f..fffeede 100644 --- a/mediation-old/src/main/java/com/wso2telco/dep/mediator/util/ValidationUtils.java +++ b/mediation-old/src/main/java/com/wso2telco/dep/mediator/util/ValidationUtils.java @@ -24,8 +24,10 @@ import java.util.ArrayList; import java.util.List; +import com.wso2telco.dep.user.masking.UserMaskHandler; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.synapse.MessageContext; import org.json.JSONObject; /** @@ -41,11 +43,18 @@ public final class ValidationUtils { * This method extracts userId from payload and resource url and passed to * validate whether they are same */ - public static void compareMsisdn(String resourcePath, JSONObject jsonBody) { + public static void compareMsisdn(String resourcePath, JSONObject jsonBody, boolean userAnonymization, MessageContext context) { String urlmsisdn = null; try { urlmsisdn = URLDecoder.decode(resourcePath.substring(1, resourcePath.indexOf("transactions") - 1), "UTF-8"); + if (userAnonymization) { + try { + urlmsisdn = UserMaskHandler.maskUserId(urlmsisdn, false, (String)context.getProperty("USER_MASKING_SECRET_KEY")); + } catch (Exception e) { + log.debug("Error while decoeing user ID"); + } + } } catch (UnsupportedEncodingException e) { log.debug("Url MSISDN can not be decoded "); @@ -53,25 +62,49 @@ public static void compareMsisdn(String resourcePath, JSONObject jsonBody) { // This validation assumes that userID should be with the prefix "tel:+" and back end // still does not support with other prefixes for this API. // Therefore below line should be modified in future depending on requirements - String payloadMsisdn = jsonBody.getJSONObject("amountTransaction").getString("endUserId") - .substring(5); - + String payloadMsisdn = jsonBody.getJSONObject("amountTransaction").getString("endUserId"); + if (userAnonymization) { + try { + payloadMsisdn = UserMaskHandler.maskUserId(payloadMsisdn, false, (String)context.getProperty("USER_MASKING_SECRET_KEY")); + } catch (Exception e) { + log.debug("Error while decoeing user ID"); + } + } + payloadMsisdn = payloadMsisdn.substring(5); + compaireUserIds(urlmsisdn, payloadMsisdn, "endUserId"); + } + + + /** + * This method extracts SenderId of sendSMS API from payload and resource url and passed to + * validate whether they are same + */ + public static void compareSenderId(String resourcePath, JSONObject jsonBody, MessageContext context) { + String urlmsisdn = null; + try { + urlmsisdn = URLDecoder.decode(resourcePath.substring(resourcePath.indexOf("outbound") + 9, resourcePath.indexOf("requests") - 1), "UTF-8"); + + } catch (UnsupportedEncodingException e) { + log.debug("Url MSISDN can not be decoded "); + } + compaireUserIds(urlmsisdn, jsonBody.getJSONObject("outboundSMSMessageRequest").getString("senderAddress").substring(5), "senderAddress"); + } + + private static void compaireUserIds(String urlmsisdn, String payloadMsisdn, String inputName) { if(urlmsisdn != null){ urlmsisdn = getMsisdnNumber(urlmsisdn); - } else { - log.debug("Not valid msisdn in resourceURL"); - throw new CustomException(MSISDNConstants.SVC0002, "", new String[] {"Not valid msisdn in URL"}); - } - - if(payloadMsisdn.equalsIgnoreCase(urlmsisdn.trim()) ){ - log.debug("msisdn in resourceURL and payload msisdn are same"); - } else { - log.debug("msisdn in resourceURL and payload msisdn are not same"); - throw new CustomException(MSISDNConstants.SVC0002, "", new String[] { "Two different endUserId provided" }); - } + } else { + log.debug("Not valid msisdn in resourceURL"); + throw new CustomException(MSISDNConstants.SVC0002, "", new String[] {"Not valid msisdn in URL"}); + } + if(payloadMsisdn.equalsIgnoreCase(urlmsisdn.trim()) ){ + log.debug("msisdn in resourceURL and payload msisdn are same"); + } else { + log.debug("msisdn in resourceURL and payload msisdn are not same"); + throw new CustomException(MSISDNConstants.SVC0002, "", new String[] { "Two different " + inputName + " provided" }); + } } - /** * Returns array of MSISDNs without "tel:+" prefix