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

Mosip 21385 formatting masking #728

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public Map<AllowedKycDto, Object> prepareSharableAttributes(IdResponseDTO idResp
.filter(allowedKycDto -> userRequestedAttributes.contains(allowedKycDto.getAttributeName()))
.collect(Collectors.toList());
sharableAttributeList = userRequestedSharableAttributesList;

}
if (userRequestedAttributes != null && !userRequestedAttributes.isEmpty()) {
LOGGER.debug(IdRepoSecurityManager.getUser(), LoggerFileConstant.REQUEST_ID.toString(), requestId,
Expand Down Expand Up @@ -289,14 +289,19 @@ public Map<AllowedKycDto, Object> prepareSharableAttributes(IdResponseDTO idResp
Object formattedObject=object;

if (object != null) {
if(userReqMaskingAttributes!=null && userReqMaskingAttributes.contains(attribute)) {
formattedObject=maskData(object.toString());
}else if(userReqFormatingAttributes != null && userReqFormatingAttributes.containsKey(attribute)) {
formattedObject = filterAndFormat(key,identity,userReqFormatingAttributes);
}
if (userReqMaskingAttributes != null && !userReqMaskingAttributes.isEmpty()
&& userReqMaskingAttributes.contains(attribute)) {
formattedObject = maskData(object.toString());
}
if (userReqFormatingAttributes != null && !userReqFormatingAttributes.isEmpty()
&& userReqFormatingAttributes.containsKey(attribute)) {
formattedObject = filterAndFormat(key, identity, userReqFormatingAttributes);
}
attributesMap.put(key, formattedObject);
} else {
if (attribute.equalsIgnoreCase(CredentialConstants.ENCRYPTIONKEY)) {
} else if (attribute.equalsIgnoreCase(CredentialConstants.FULLNAME)) {
formattedObject = filterAndFormat(key, identity, userReqFormatingAttributes);
attributesMap.put(key, formattedObject);
}else if (attribute.equalsIgnoreCase(CredentialConstants.ENCRYPTIONKEY)) {
additionalData.put(key.getAttributeName(), credentialServiceRequestDto.getEncryptionKey());
}else if(attribute.equalsIgnoreCase(CredentialConstants.VID)){
VidInfoDTO vidInfoDTO;
Expand All @@ -317,7 +322,7 @@ public Map<AllowedKycDto, Object> prepareSharableAttributes(IdResponseDTO idResp
additionalData.put("TransactionLimit", vidInfoDTO.getTransactionLimit());
}
}
}

credentialServiceRequestDto.setAdditionalData(additionalData);
String individualBiometricsValue = null;
List<DocumentsDTO> documents = idResponseDto.getResponse().getDocuments();
Expand Down Expand Up @@ -509,7 +514,7 @@ private Object filterAndFormat(AllowedKycDto key, JSONObject identity,
if (attribute.equals(CredentialConstants.DATEOFBIRTH)) {
String dateOfBirth = (String) userReqFormatingAttributes.get(CredentialConstants.DATEOFBIRTH);
formattedObject = formatDate(identity.get(CredentialConstants.DATEOFBIRTH), dateOfBirth);
} else if (attribute.equals(CredentialConstants.NAME) || attribute.equals(CredentialConstants.FULLADDRESS))
} else if (attribute.equals(CredentialConstants.FULLNAME) || attribute.equals(CredentialConstants.NAME) || attribute.equals(CredentialConstants.FULLADDRESS))
formattedObject = formatData(identity, attribute);
return formattedObject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.junit.Before;
Expand All @@ -17,13 +20,16 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext;
import org.mvel2.MVEL;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.test.util.ReflectionTestUtils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.mosip.credentialstore.dto.AllowedKycDto;
Expand All @@ -38,16 +44,18 @@
import io.mosip.credentialstore.provider.CredentialProvider;
import io.mosip.credentialstore.util.EncryptionUtil;
import io.mosip.credentialstore.util.Utilities;
import io.mosip.idrepository.core.builder.IdentityIssuanceProfileBuilder;
import io.mosip.idrepository.core.dto.CredentialServiceRequestDto;
import io.mosip.idrepository.core.dto.DocumentsDTO;
import io.mosip.idrepository.core.dto.IdResponseDTO;
import io.mosip.idrepository.core.dto.IdentityMapping;
import io.mosip.idrepository.core.dto.ResponseDTO;
import io.mosip.idrepository.core.util.EnvUtil;

@RunWith(SpringRunner.class)
@WebMvcTest @Import(EnvUtil.class)
@ContextConfiguration(classes = { TestContext.class, WebApplicationContext.class})
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({ "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*" })
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest(value = MVEL.class)
public class CredentialProviderTest {
/** The environment. */
@Mock
Expand Down Expand Up @@ -75,13 +83,30 @@ public class CredentialProviderTest {

PartnerCredentialTypePolicyDto policyResponse;

IdentityMapping identityMapping;

private ObjectMapper mapper = new ObjectMapper();

@Before
public void setUp() throws DataEncryptionFailureException, ApiNotAccessibleException, SignatureException,Exception {

ReflectionTestUtils.setField(credentialDefaultProvider, "mapper", mapper);
PowerMockito.mockStatic(MVEL.class);

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
identityMapping = mapper.readValue(
IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("identity-mapping.json"),
StandardCharsets.UTF_8),
IdentityMapping.class);
IdentityIssuanceProfileBuilder.setIdentityMapping(identityMapping);
IdentityIssuanceProfileBuilder.setDateFormat("uuuu/MM/dd");

EnvUtil.setDateTimePattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Mockito.when(encryptionUtil.encryptDataWithPin(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn("testdata");


Mockito.when(MVEL.executeExpression(Mockito.any(), Mockito.any(), Mockito.anyMap(), Mockito.any()))
.thenReturn("test");
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Mockito.when(utilities.generateId()).thenReturn("test123");


Expand Down Expand Up @@ -130,7 +155,8 @@ public void setUp() throws DataEncryptionFailureException, ApiNotAccessibleExcep
shareableAttributes.add(kyc4);
PolicyAttributesDto dto = new PolicyAttributesDto();
dto.setShareableAttributes(shareableAttributes);
policyResponse.setPolicies(dto);
policyResponse.setPolicies(dto);

}

@Test
Expand Down Expand Up @@ -286,6 +312,179 @@ public void testPrepareSharableAttributesSuccess() throws CredentialFormatterExc
assertTrue("preparedsharableattribute smap", sharabaleAttrubutesMap.size() >= 1);
}


@Test
public void testPrepareSharableAttributesEmptyListCheck() throws CredentialFormatterException {
LinkedHashMap<String, Object> identityMap = new LinkedHashMap<>();
Map<String, String> map = new HashMap<>();
map.put("language", "eng");
map.put("value", "raghav");
JSONObject j1 = new JSONObject(map);

Map<String, String> map2 = new HashMap<>();
map2.put("language", "ara");
map2.put("value", "Alok");
JSONObject j2 = new JSONObject(map2);
JSONArray array = new JSONArray();
array.add(j1);
array.add(j2);
identityMap.put("fullName", array);

identityMap.put("dateOfBirth", "1980/11/14");

Object identity = identityMap;
response.setIdentity(identity);

DocumentsDTO doc1 = new DocumentsDTO();
doc1.setCategory("individualBiometrics");

doc1.setValue("text biomterics");
List<DocumentsDTO> docList = new ArrayList<>();
docList.add(doc1);

response.setDocuments(docList);
idResponse.setResponse(response);
CredentialServiceRequestDto credentialServiceRequestDto = getCredentialServiceRequestDto();
List<String> sharableAttributesList = Collections.EMPTY_LIST;
credentialServiceRequestDto.setSharableAttributes(sharableAttributesList);
Map<AllowedKycDto, Object> sharabaleAttrubutesMap = credentialDefaultProvider
.prepareSharableAttributes(idResponse, policyResponse, credentialServiceRequestDto);
assertTrue("preparedsharableattribute smap", sharabaleAttrubutesMap.size() >= 1);
}

@Test
public void testPrepareMaskingAndFormattingEmptyListCheck() throws CredentialFormatterException {
LinkedHashMap<String, Object> identityMap = new LinkedHashMap<>();
Map<String, String> map = new HashMap<>();
map.put("language", "eng");
map.put("value", "raghav");
JSONObject j1 = new JSONObject(map);

Map<String, String> map2 = new HashMap<>();
map2.put("language", "ara");
map2.put("value", "Alok");
JSONObject j2 = new JSONObject(map2);
JSONArray array = new JSONArray();
array.add(j1);
array.add(j2);
identityMap.put("fullName", array);

identityMap.put("dateOfBirth", "1980/11/14");

Object identity = identityMap;
response.setIdentity(identity);

DocumentsDTO doc1 = new DocumentsDTO();
doc1.setCategory("individualBiometrics");

doc1.setValue("text biomterics");
List<DocumentsDTO> docList = new ArrayList<>();
docList.add(doc1);

response.setDocuments(docList);
idResponse.setResponse(response);
CredentialServiceRequestDto credentialServiceRequestDto = getCredentialServiceRequestDto();
List<String> sharableAttributesList = Collections.EMPTY_LIST;
List<String> maskingAttributes = Collections.EMPTY_LIST;
Map<String,String> formatingAttributes = Collections.EMPTY_MAP;
Map<String,Object> additionalAttributes = new HashMap<String,Object>();
additionalAttributes.put("maskingAttributes", maskingAttributes);
additionalAttributes.put("formatingAttributes", formatingAttributes);

credentialServiceRequestDto.setSharableAttributes(sharableAttributesList);
credentialServiceRequestDto.setAdditionalData(additionalAttributes);
Map<AllowedKycDto, Object> sharabaleAttrubutesMap = credentialDefaultProvider
.prepareSharableAttributes(idResponse, policyResponse, credentialServiceRequestDto);
assertTrue("preparedsharableattribute smap", sharabaleAttrubutesMap.size() >= 1);
}

@Test
public void testPrepareMaskingAndFormattingNullCheck() throws CredentialFormatterException {
LinkedHashMap<String, Object> identityMap = new LinkedHashMap<>();
Map<String, String> map = new HashMap<>();
map.put("language", "eng");
map.put("value", "raghav");
JSONObject j1 = new JSONObject(map);

Map<String, String> map2 = new HashMap<>();
map2.put("language", "ara");
map2.put("value", "Alok");
JSONObject j2 = new JSONObject(map2);
JSONArray array = new JSONArray();
array.add(j1);
array.add(j2);
identityMap.put("fullName", array);

identityMap.put("dateOfBirth", "1980/11/14");

Object identity = identityMap;
response.setIdentity(identity);

DocumentsDTO doc1 = new DocumentsDTO();
doc1.setCategory("individualBiometrics");

doc1.setValue("text biomterics");
List<DocumentsDTO> docList = new ArrayList<>();
docList.add(doc1);

response.setDocuments(docList);
idResponse.setResponse(response);
CredentialServiceRequestDto credentialServiceRequestDto = getCredentialServiceRequestDto();
List<String> sharableAttributesList = Collections.EMPTY_LIST;
List<String> maskingAttributes = null;
List<String> formatingAttributes = null;

Map<String,Object> additionalAttributes = new HashMap<String,Object>();
additionalAttributes.put("maskingAttributes", maskingAttributes);
additionalAttributes.put("formatingAttributes", formatingAttributes);

credentialServiceRequestDto.setSharableAttributes(sharableAttributesList);
credentialServiceRequestDto.setAdditionalData(additionalAttributes);
Map<AllowedKycDto, Object> sharabaleAttrubutesMap = credentialDefaultProvider
.prepareSharableAttributes(idResponse, policyResponse, credentialServiceRequestDto);
assertTrue("preparedsharableattribute smap", sharabaleAttrubutesMap.size() >= 1);
}

@Test
public void testPrepareSharableAttributesNULLCheck() throws CredentialFormatterException {
LinkedHashMap<String, Object> identityMap = new LinkedHashMap<>();
Map<String, String> map = new HashMap<>();
map.put("language", "eng");
map.put("value", "raghav");
JSONObject j1 = new JSONObject(map);

Map<String, String> map2 = new HashMap<>();
map2.put("language", "ara");
map2.put("value", "Alok");
JSONObject j2 = new JSONObject(map2);
JSONArray array = new JSONArray();
array.add(j1);
array.add(j2);
identityMap.put("fullName", array);

identityMap.put("dateOfBirth", "1980/11/14");

Object identity = identityMap;
response.setIdentity(identity);

DocumentsDTO doc1 = new DocumentsDTO();
doc1.setCategory("individualBiometrics");

doc1.setValue("text biomterics");
List<DocumentsDTO> docList = new ArrayList<>();
docList.add(doc1);

response.setDocuments(docList);
idResponse.setResponse(response);
CredentialServiceRequestDto credentialServiceRequestDto = getCredentialServiceRequestDto();
List<String> sharableAttributesList = null;
credentialServiceRequestDto.setSharableAttributes(sharableAttributesList);
Map<AllowedKycDto, Object> sharabaleAttrubutesMap = credentialDefaultProvider
.prepareSharableAttributes(idResponse, policyResponse, credentialServiceRequestDto);
assertTrue("preparedsharableattribute smap", sharabaleAttrubutesMap.size() >= 1);
}


@Test
public void testPrepareSharableAttributesSuccessWithUserRequestedAttributes() throws CredentialFormatterException {
LinkedHashMap<String, Object> identityMap = new LinkedHashMap<>();
Expand Down
Loading