-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [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
1 parent
2e0f31c
commit b66b2cd
Showing
8 changed files
with
66 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
.../java/io/mosip/certify/mockidadataprovider/integration/repository/MockDataRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
21 changes: 21 additions & 0 deletions
21
...a/io/mosip/certify/mockidadataprovider/integration/repository/MockDataRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
134 changes: 24 additions & 110 deletions
134
...a/io/mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
.../java/io/mosip/certify/mockidadataprovider/integration/service/MockTransactionHelper.java
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
mock-ida-dataprovider-plugin/src/main/resources/application-local.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
92 changes: 0 additions & 92 deletions
92
.../mosip/certify/mockidadataprovider/integration/service/MockIdaDataProviderPluginTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.