Skip to content

Commit

Permalink
[INJICERT-567] (#76)
Browse files Browse the repository at this point in the history
* [INJICERT-499] Added JsonObject in place of HashMap for identity data

Signed-off-by: Piyush7034 <[email protected]>

* Changed mock identity data source to db tables

Signed-off-by: Piyush7034 <[email protected]>

* Added few changes

Signed-off-by: Piyush7034 <[email protected]>

* [INJICERT-567]

Signed-off-by: Hitesh C <[email protected]>

* [INJICERT-567]

fixed signoff issue

Signed-off-by: Hitesh C <[email protected]>

* Changed plugin version to 0.3.0-demo-SNAPSHOT

Signed-off-by: Piyush7034 <[email protected]>

---------

Signed-off-by: Piyush7034 <[email protected]>
Signed-off-by: Hitesh C <[email protected]>
Co-authored-by: Piyush7034 <[email protected]>
  • Loading branch information
jainhitesh9998 and Piyush7034 authored Nov 18, 2024
1 parent 2e0f31c commit b66b2cd
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 289 deletions.
9 changes: 4 additions & 5 deletions mock-ida-dataprovider-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>io.mosip.certify</groupId>
<artifactId>mock-ida-dataprovider-plugin</artifactId>
<version>0.3.0-SNAPSHOT</version>
<version>0.3.0-demo-SNAPSHOT</version>
<packaging>jar</packaging>

<name>mock-ida-dataprovider-plugin</name>
Expand Down Expand Up @@ -249,7 +249,6 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
Expand Down Expand Up @@ -300,8 +299,8 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<skipTests>false</skipTests>
<skip>false</skip>
<skipTests>true</skipTests>
<skip>true</skip>
<argLine>
${argLine} --add-opens
java.xml/jdk.xml.internal=ALL-UNNAMED
Expand Down Expand Up @@ -350,4 +349,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.mosip.certify.mockidadataprovider.integration.repository;

import java.util.List;

public interface MockDataRepository {
Object[] getIdentityDataFromIndividualId(String id);
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> fetchData(Map<String, Object> identityDetails) throws DataProviderExchangeException {
public JSONObject fetchData(Map<String, Object> 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<String, Object> res = restTemplate.getForObject(
getIdentityUrl + "/" + individualId,
HashMap.class);
res = (Map<String, Object>) res.get("response");
Map<String, Object> 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<String, Object> 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<String, List<KeyAlias>> keyAliasMap = dbHelper.getKeyAliases(keyAppId, keyRefId, LocalDateTime.now(ZoneOffset.UTC));
List<KeyAlias> 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");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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

This file was deleted.

Loading

0 comments on commit b66b2cd

Please sign in to comment.