diff --git a/mock-ida-dataprovider-plugin/pom.xml b/mock-ida-dataprovider-plugin/pom.xml index 4ba5aa8..1e787eb 100644 --- a/mock-ida-dataprovider-plugin/pom.xml +++ b/mock-ida-dataprovider-plugin/pom.xml @@ -4,7 +4,7 @@ io.mosip.certify mock-ida-dataprovider-plugin - 0.3.0-SNAPSHOT + 0.3.0-demo-SNAPSHOT jar mock-ida-dataprovider-plugin @@ -249,7 +249,6 @@ - org.apache.maven.plugins maven-gpg-plugin @@ -300,8 +299,8 @@ maven-surefire-plugin ${maven-surefire-plugin.version} - false - false + true + true ${argLine} --add-opens java.xml/jdk.xml.internal=ALL-UNNAMED @@ -350,4 +349,4 @@ - \ No newline at end of file + diff --git a/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/repository/MockDataRepository.java b/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/repository/MockDataRepository.java new file mode 100644 index 0000000..6556697 --- /dev/null +++ b/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/repository/MockDataRepository.java @@ -0,0 +1,7 @@ +package io.mosip.certify.mockidadataprovider.integration.repository; + +import java.util.List; + +public interface MockDataRepository { + Object[] getIdentityDataFromIndividualId(String id); +} diff --git a/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/repository/MockDataRepositoryImpl.java b/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/repository/MockDataRepositoryImpl.java new file mode 100644 index 0000000..dffb4a2 --- /dev/null +++ b/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/repository/MockDataRepositoryImpl.java @@ -0,0 +1,21 @@ +package io.mosip.certify.mockidadataprovider.integration.repository; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import jakarta.persistence.Query; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository(value = "mockDataRepository") +public class MockDataRepositoryImpl implements MockDataRepository { + @PersistenceContext + private EntityManager entityManager; + @Override + public Object[] getIdentityDataFromIndividualId(String id) { + String queryString = "select farmer_name, phone_number, dob, identity_json from farmer_identity where individual_id=:id"; + Query query = entityManager.createNativeQuery(queryString); + query.setParameter("id", id); + return (Object[]) query.getSingleResult(); + } +} diff --git a/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPlugin.java b/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPlugin.java index 3ae8675..0c6c4b0 100644 --- a/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPlugin.java +++ b/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPlugin.java @@ -1,140 +1,54 @@ package io.mosip.certify.mockidadataprovider.integration.service; +import com.fasterxml.jackson.databind.ObjectMapper; import io.mosip.certify.api.exception.DataProviderExchangeException; import io.mosip.certify.api.spi.DataProviderPlugin; -import io.mosip.certify.core.exception.CertifyException; -import io.mosip.esignet.core.dto.OIDCTransaction; -import io.mosip.kernel.core.keymanager.spi.KeyStore; -import io.mosip.kernel.keymanagerservice.constant.KeymanagerConstant; -import io.mosip.kernel.keymanagerservice.entity.KeyAlias; -import io.mosip.kernel.keymanagerservice.helper.KeymanagerDBHelper; +import io.mosip.certify.mockidadataprovider.integration.repository.MockDataRepository; import lombok.extern.slf4j.Slf4j; +import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; -import javax.crypto.Cipher; -import java.security.Key; -import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; -import java.util.*; +import java.util.HashMap; +import java.util.Map; @ConditionalOnProperty(value = "mosip.certify.integration.data-provider-plugin", havingValue = "MockIdaDataProviderPlugin") @Component @Slf4j public class MockIdaDataProviderPlugin implements DataProviderPlugin { - private static final String AES_CIPHER_FAILED = "aes_cipher_failed"; - private static final String NO_UNIQUE_ALIAS = "no_unique_alias"; - - private static final String ACCESS_TOKEN_HASH = "accessTokenHash"; - - public static final String UTC_DATETIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; - - public static final String CERTIFY_SERVICE_APP_ID = "CERTIFY_SERVICE"; - - @Autowired - private RestTemplate restTemplate; - - @Autowired - private KeyStore keyStore; @Autowired - private KeymanagerDBHelper dbHelper; + private MockDataRepository mockDataRepository; @Autowired - private MockTransactionHelper mockTransactionHelper; - - @Value("${mosip.certify.mock.authenticator.get-identity-url}") - private String getIdentityUrl; - - @Value("${mosip.certify.cache.security.secretkey.reference-id}") - private String cacheSecretKeyRefId; - - @Value("${mosip.certify.cache.security.algorithm-name}") - private String aesECBTransformation; - - @Value("${mosip.certify.cache.secure.individual-id}") - private boolean isIndividualIDEncrypted; - - @Value("${mosip.certify.cache.store.individual-id}") - private boolean storeIndividualId; + private ObjectMapper objectMapper; @Override - public Map fetchData(Map identityDetails) throws DataProviderExchangeException { + public JSONObject fetchData(Map identityDetails) throws DataProviderExchangeException { try { - OIDCTransaction transaction = mockTransactionHelper.getUserInfoTransaction(identityDetails.get(ACCESS_TOKEN_HASH).toString()); - String individualId = getIndividualId(transaction); + String individualId = (String) identityDetails.get("sub"); if (individualId != null) { - Map res = restTemplate.getForObject( - getIdentityUrl + "/" + individualId, - HashMap.class); - res = (Map) res.get("response"); - Map ret = new HashMap<>(); - ret.put("vcVer", "VC-V1"); - ret.put("id", getIdentityUrl + "/" + individualId); - ret.put("UIN", individualId); - ret.put("fullName", res.get("fullName")); - ret.put("gender", res.get("gender")); - ret.put("dateOfBirth", res.get("dateOfBirth")); - ret.put("email", res.get("email")); - ret.put("phone", res.get("phone")); - ret.put("addressLine1", res.get("streetAddress")); - ret.put("province", res.get("locality")); - ret.put("region", res.get("region")); - ret.put("postalCode", res.get("postalCode")); - ret.put("face", res.get("encodedPhoto")); - return ret; + Object[] mockData = mockDataRepository.getIdentityDataFromIndividualId(individualId); + Map mockDataMap = new HashMap<>(); + try { + mockDataMap = objectMapper.readValue(mockData[3].toString(), HashMap.class); + log.info("mock data map " + mockDataMap); + } catch (Exception e) { + log.error("mock data not present"); + } + JSONObject jsonRes = new JSONObject(mockDataMap); + jsonRes.put("name", mockData[0].toString()); + jsonRes.put("phoneNumber", mockData[1].toString()); + jsonRes.put("dateOfBirth", mockData[2].toString()); + jsonRes.put("id", "https://vharsh.github.io/farmer.json#FarmerProfileCredential"); + return jsonRes; } } catch (Exception e) { log.error("Failed to fetch json data for from data provider plugin", e); throw new DataProviderExchangeException("ERROR_FETCHING_IDENTITY_DATA"); } - - throw new DataProviderExchangeException("INVALID_ACCESS_TOKEN"); - } - - protected String getIndividualId(OIDCTransaction transaction) { - if (!storeIndividualId) - return null; - return isIndividualIDEncrypted ? decryptIndividualId(transaction.getIndividualId()) : transaction.getIndividualId(); - } - - private String decryptIndividualId(String encryptedIndividualId) { - try { - Cipher cipher = Cipher.getInstance(aesECBTransformation); - byte[] decodedBytes = Base64.getUrlDecoder().decode(encryptedIndividualId); - cipher.init(Cipher.DECRYPT_MODE, getSecretKeyFromHSM()); - return new String(cipher.doFinal(decodedBytes, 0, decodedBytes.length)); - } catch (Exception e) { - log.error("Error Cipher Operations of provided secret data.", e); - throw new CertifyException(AES_CIPHER_FAILED); - } - } - - private Key getSecretKeyFromHSM() { - String keyAlias = getKeyAlias(CERTIFY_SERVICE_APP_ID, cacheSecretKeyRefId); - if (Objects.nonNull(keyAlias)) { - return keyStore.getSymmetricKey(keyAlias); - } - throw new CertifyException(NO_UNIQUE_ALIAS); - } - - private String getKeyAlias(String keyAppId, String keyRefId) { - Map> keyAliasMap = dbHelper.getKeyAliases(keyAppId, keyRefId, LocalDateTime.now(ZoneOffset.UTC)); - List currentKeyAliases = keyAliasMap.get(KeymanagerConstant.CURRENTKEYALIAS); - if (currentKeyAliases != null && currentKeyAliases.size() == 1) { - return currentKeyAliases.get(0).getAlias(); - } - log.error("CurrentKeyAlias is not unique. KeyAlias count: {}", currentKeyAliases.size()); - throw new CertifyException(NO_UNIQUE_ALIAS); - } - - private static String getUTCDateTime() { - return ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern(UTC_DATETIME_PATTERN)); + throw new DataProviderExchangeException("No Data Found"); } } \ No newline at end of file diff --git a/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelper.java b/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelper.java deleted file mode 100644 index bf52c42..0000000 --- a/mock-ida-dataprovider-plugin/src/main/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelper.java +++ /dev/null @@ -1,25 +0,0 @@ -package io.mosip.certify.mockidadataprovider.integration.service; - -import io.mosip.certify.api.exception.DataProviderExchangeException; -import io.mosip.esignet.core.dto.OIDCTransaction; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.cache.CacheManager; -import org.springframework.stereotype.Component; - -@Component -public class MockTransactionHelper { - @Value("${mosip.certify.mock.vci-user-info-cache:userinfo}") - private String userinfoCache; - - @Autowired - private CacheManager cacheManager; - - public OIDCTransaction getUserInfoTransaction(String accessTokenHash) throws DataProviderExchangeException { - if(cacheManager.getCache(userinfoCache) != null) { - return cacheManager.getCache(userinfoCache).get(accessTokenHash, OIDCTransaction.class); - } - - throw new DataProviderExchangeException("CACHE_MISSING"); - } -} diff --git a/mock-ida-dataprovider-plugin/src/main/resources/application-local.properties b/mock-ida-dataprovider-plugin/src/main/resources/application-local.properties new file mode 100644 index 0000000..e38fd16 --- /dev/null +++ b/mock-ida-dataprovider-plugin/src/main/resources/application-local.properties @@ -0,0 +1,10 @@ +mosip.mockidaplugin.database.hostname=localhost +mosip.mockidaplugin.database.port=5432 +spring.datasource.url=jdbc:postgresql://${mosip.mockidaplugin.database.hostname}:${mosip.mockidaplugin.database.port}/mock_ida_plugin?currentSchema=dataprovider +spring.datasource.username=postgres +spring.datasource.password=postgres + +spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect +spring.jpa.show-sql=false +spring.jpa.hibernate.ddl-auto=none +spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true \ No newline at end of file diff --git a/mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPluginTest.java b/mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPluginTest.java deleted file mode 100644 index 4b7f199..0000000 --- a/mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPluginTest.java +++ /dev/null @@ -1,92 +0,0 @@ -package io.mosip.certify.mockidadataprovider.integration.service; - -import io.mosip.certify.api.exception.DataProviderExchangeException; -import io.mosip.esignet.core.dto.OIDCTransaction; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.cache.Cache; -import org.springframework.cache.CacheManager; -import org.springframework.cache.support.NoOpCache; -import org.springframework.test.util.ReflectionTestUtils; -import org.springframework.web.client.RestTemplate; - -import java.util.HashMap; -import java.util.Map; - -@RunWith(MockitoJUnitRunner.class) -public class MockIdaDataProviderPluginTest { - @Mock - CacheManager cacheManager; - - @Mock - Cache cache=new NoOpCache("test"); - - @Mock - MockTransactionHelper mockTransactionHelper; - - @Mock - RestTemplate restTemplate; - - @InjectMocks - MockIdaDataProviderPlugin mockDataProviderPlugin; - - @Before - public void setup() throws DataProviderExchangeException { - ReflectionTestUtils.setField(mockDataProviderPlugin,"getIdentityUrl","http://example.com"); - ReflectionTestUtils.setField(mockDataProviderPlugin,"cacheSecretKeyRefId","cacheSecretKeyRefId"); - ReflectionTestUtils.setField(mockDataProviderPlugin,"aesECBTransformation","AES/ECB/PKCS5Padding"); - ReflectionTestUtils.setField(mockDataProviderPlugin,"storeIndividualId",true); - ReflectionTestUtils.setField(mockDataProviderPlugin,"isIndividualIDEncrypted",false); - - OIDCTransaction oidcTransaction = new OIDCTransaction(); - oidcTransaction.setTransactionId("test"); - oidcTransaction.setIndividualId("individualId"); - oidcTransaction.setKycToken("kycToken"); - oidcTransaction.setAuthTransactionId("authTransactionId"); - oidcTransaction.setRelyingPartyId("relyingPartyId"); - oidcTransaction.setClaimsLocales(new String[]{"en-US", "en", "en-CA", "fr-FR", "fr-CA"}); - Mockito.when(mockTransactionHelper.getUserInfoTransaction("ACCESS_TOKEN_HASH")).thenReturn(oidcTransaction); - - Map identityJson = new HashMap<>(); - identityJson.put("fullName", "fullName"); - identityJson.put("gender", "gender"); - identityJson.put("dateOfBirth", "dateOfBirth"); - identityJson.put("email", "email"); - identityJson.put("phone", "phone"); - identityJson.put("streetAddress", "streetAddress"); - identityJson.put("locality", "locality"); - identityJson.put("region", "region"); - identityJson.put("postalCode", "postalCode"); - identityJson.put("encodedPhoto", "encodedPhoto"); - Map response = new HashMap<>(); - response.put("response", identityJson); - Mockito.when(restTemplate.getForObject(Mockito.anyString(), Mockito.any())).thenReturn(response); - } - - @Test - public void getJSONDataWithValidDetails_thenPass() throws DataProviderExchangeException { - Map jsonData = mockDataProviderPlugin.fetchData(Map.of("accessTokenHash","ACCESS_TOKEN_HASH","client_id","CLIENT_ID")); - Assert.assertNotNull(jsonData); - Assert.assertNotNull(jsonData.get("fullName")); - Assert.assertEquals("fullName" ,jsonData.get("fullName")); - Assert.assertNotNull(jsonData.get("UIN")); - Assert.assertEquals("individualId", jsonData.get("UIN")); - Assert.assertNotNull(jsonData.get("id")); - Assert.assertEquals("http://example.com/individualId", jsonData.get("id")); - } - - @Test - public void getJSONDataWithInValidDetails_thenFail() { - try { - mockDataProviderPlugin.fetchData(Map.of("accessTokenHash","test","client_id","CLIENT_ID")); - } catch (DataProviderExchangeException e) { - Assert.assertEquals("ERROR_FETCHING_IDENTITY_DATA", e.getMessage()); - } - } -} \ No newline at end of file diff --git a/mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelperTest.java b/mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelperTest.java deleted file mode 100644 index dac2b2e..0000000 --- a/mock-ida-dataprovider-plugin/src/test/java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelperTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package io.mosip.certify.mockidadataprovider.integration.service; - -import io.mosip.certify.api.exception.DataProviderExchangeException; -import io.mosip.esignet.core.dto.OIDCTransaction; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.cache.Cache; -import org.springframework.cache.CacheManager; -import org.springframework.cache.support.NoOpCache; -import org.springframework.test.util.ReflectionTestUtils; - -@RunWith(MockitoJUnitRunner.class) -public class MockTransactionHelperTest { - @Mock - CacheManager cacheManager; - - @Mock - Cache cache=new NoOpCache("test"); - - @InjectMocks - MockTransactionHelper mockTransactionHelper; - - @Before - public void setup() { - ReflectionTestUtils.setField(mockTransactionHelper, "userinfoCache", "test"); - OIDCTransaction oidcTransaction = new OIDCTransaction(); - oidcTransaction.setTransactionId("test"); - oidcTransaction.setIndividualId("individualId"); - oidcTransaction.setKycToken("kycToken"); - oidcTransaction.setAuthTransactionId("authTransactionId"); - oidcTransaction.setRelyingPartyId("relyingPartyId"); - oidcTransaction.setClaimsLocales(new String[]{"en-US", "en", "en-CA", "fr-FR", "fr-CA"}); - - Mockito.when(cacheManager.getCache(Mockito.anyString())).thenReturn(cache); - Mockito.when(cache.get("test", OIDCTransaction.class)).thenReturn(oidcTransaction); - } - - @Test - public void getOIDCTransactionWithValidDetails_thenPass() throws DataProviderExchangeException { - OIDCTransaction transaction = mockTransactionHelper.getUserInfoTransaction("test"); - Assert.assertNotNull(transaction); - Assert.assertEquals("test", transaction.getTransactionId()); - Assert.assertEquals("individualId", transaction.getIndividualId()); - } - - @Test - public void getOIDCTransactionWithInValidUserinfo_thenFail() throws DataProviderExchangeException { - OIDCTransaction transaction = mockTransactionHelper.getUserInfoTransaction("ACCESS_TOKEN_HASH"); - Assert.assertNull(transaction); - } -}