Skip to content

Commit

Permalink
Merge pull request #16 from kaifk468/es-794
Browse files Browse the repository at this point in the history
[ES-794]added a new fetchRegistryObjectByPSUT method to get the registry obje…
  • Loading branch information
vishwa-vyom authored Feb 23, 2024
2 parents 22ac2ae + 5f3efcc commit 49d9215
Showing 1 changed file with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.mosip.esignet.api.exception.VCIExchangeException;
import io.mosip.esignet.api.util.ErrorConstants;
import io.mosip.esignet.sunbirdrc.integration.dto.RegistrySearchRequestDto;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
Expand Down Expand Up @@ -58,6 +59,8 @@ public class SunbirdRCVCIssuancePlugin implements VCIssuancePlugin {

private static final String REGISTRY_GET_URL = "registry-get-url";

private static final String REGISTRY_SEARCH_URL= "registry-search-url";

private static final String CRED_SCHEMA_ID = "cred-schema-id";

private static final String CRED_SCHEMA_VESRION = "cred-schema-version";
Expand All @@ -66,6 +69,10 @@ public class SunbirdRCVCIssuancePlugin implements VCIssuancePlugin {

private static final String CREDENTIAL_OBJECT_KEY = "credential";

private final String FILTER_EQUALS_OPERATOR="eq";

private final String PSUT_TOKEN="psut";

@Autowired
Environment env;

Expand All @@ -78,6 +85,9 @@ public class SunbirdRCVCIssuancePlugin implements VCIssuancePlugin {
@Value("${mosip.esignet.vciplugin.sunbird-rc.issue-credential-url}")
String issueCredentialUrl;

@Value("${mosip.esignet.vciplugin.sunbird-rc.enable-psut-based-registry-search:false}")
private boolean enablePSUTBasedRegistrySearch;

@Value("#{'${mosip.esignet.vciplugin.sunbird-rc.supported-credential-types}'.split(',')}")
List<String> supportedCredentialTypes;

Expand Down Expand Up @@ -135,13 +145,20 @@ public VCResult<JsonLDObject> getVerifiableCredentialWithLinkedDataProof(VCReque
Template template=credentialTypeTemplates.get(supportedType);
validateContextUrl(template,contextList);
}
String osid = (identityDetails.containsKey("sub")) ? (String) identityDetails.get("sub") : null;
if (osid == null) {
log.error("Invalid request: osid is null");

String registrySearchField = (identityDetails.containsKey("sub")) ? (String) identityDetails.get("sub") : null;
if (registrySearchField == null) {
log.error("Invalid request: registrySearchField is null");
throw new VCIExchangeException(ErrorConstants.VCI_EXCHANGE_FAILED);
}
String registryUrl=credentialTypeConfigMap.get(requestedCredentialType).get(REGISTRY_GET_URL);
Map<String,Object> responseRegistryMap =fetchRegistryObject(registryUrl+osid);
Map<String,Object> responseRegistryMap;
if(enablePSUTBasedRegistrySearch){
String registrySearchUrl=credentialTypeConfigMap.get(requestedCredentialType).get(REGISTRY_SEARCH_URL);
responseRegistryMap= fetchRegistryObjectByPSUT(registrySearchUrl,registrySearchField);
}else {
String registryUrl=credentialTypeConfigMap.get(requestedCredentialType).get(REGISTRY_GET_URL);
responseRegistryMap =fetchRegistryObject(registryUrl+ registrySearchField);
}
Map<String,Object> credentialRequestMap = createCredentialIssueRequest(requestedCredentialType, responseRegistryMap,vcRequestDto,holderId);
Map<String,Object> vcResponseMap =sendCredentialIssueRequest(credentialRequestMap);

Expand All @@ -152,7 +169,6 @@ public VCResult<JsonLDObject> getVerifiableCredentialWithLinkedDataProof(VCReque
return vcResult;
}


@Override
public VCResult<String> getVerifiableCredential(VCRequestDto vcRequestDto, String holderId, Map<String, Object> identityDetails) throws VCIExchangeException {
throw new VCIExchangeException(ErrorConstants.NOT_IMPLEMENTED);
Expand All @@ -171,6 +187,41 @@ private Map<String,Object> fetchRegistryObject(String entityUrl) throws VCIExcha
}
}

private Map<String, Object> fetchRegistryObjectByPSUT(String registrySearchUrl, String psut) throws VCIExchangeException {

RegistrySearchRequestDto registrySearchRequestDto=new RegistrySearchRequestDto();
registrySearchRequestDto.setOffset(0);
registrySearchRequestDto.setLimit(2);
Map<String,Map<String,String>> filter=new HashMap<>();
Map<String,String> psutFilter=new HashMap<>();
psutFilter.put(FILTER_EQUALS_OPERATOR,psut);
filter.put(PSUT_TOKEN,psutFilter);
registrySearchRequestDto.setFilters(filter);

RequestEntity requestEntity =RequestEntity.post(UriComponentsBuilder.fromUriString(registrySearchUrl).build().toUri())
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body(registrySearchRequestDto);

ResponseEntity<List<Map<String,Object>>> responseEntity = restTemplate.exchange(requestEntity,
new ParameterizedTypeReference<List<Map<String,Object>>>() {});
if (responseEntity.getStatusCode().is2xxSuccessful() && responseEntity.getBody() != null) {
List<Map<String,Object>> responseList = responseEntity.getBody();
if(responseList.size()==1){
//TODO This need to be removed since it can contain PII
log.debug("getting response {}", responseEntity);
}else{
log.debug("Registry search returns more than one match,So taking the first. Result size: " + responseList.size());
log.debug("getting response {}", responseEntity);
//Todo we should find a generic way to sort desc based on created date. The idea is we give out the most recent record as VC
}
return responseList.get(0);
}else {
log.error("Sunbird service is not running. Status Code: " ,responseEntity.getStatusCode());
throw new VCIExchangeException(ErrorConstants.VCI_EXCHANGE_FAILED);
}

}

private Map<String,Object> createCredentialIssueRequest(String requestedCredentialType, Map<String,Object> registryObjectMap, VCRequestDto vcRequestDto, String holderId) throws VCIExchangeException {

Template template=credentialTypeTemplates.get(requestedCredentialType);
Expand Down Expand Up @@ -237,6 +288,7 @@ private void validateAndCachePropertiesForCredentialType(String credentialType)
validateAndLoadProperty(CREDENTIAL_TYPE_PROPERTY_PREFIX + "." + credentialType + "." + CRED_SCHEMA_ID,CRED_SCHEMA_ID,configMap);
validateAndLoadProperty(CREDENTIAL_TYPE_PROPERTY_PREFIX + "." + credentialType + "." + CRED_SCHEMA_VESRION,CRED_SCHEMA_VESRION,configMap);
validateAndLoadProperty(CREDENTIAL_TYPE_PROPERTY_PREFIX + "." + credentialType + "." + STATIC_VALUE_MAP_ISSUER_ID,STATIC_VALUE_MAP_ISSUER_ID,configMap);
validateAndLoadProperty(CREDENTIAL_TYPE_PROPERTY_PREFIX + "." + credentialType + "." + REGISTRY_SEARCH_URL,REGISTRY_SEARCH_URL,configMap);

String templateUrl = env.getProperty(CREDENTIAL_TYPE_PROPERTY_PREFIX +"." + credentialType + "." + TEMPLATE_URL);
validateAndCacheTemplate(templateUrl,credentialType);
Expand Down

0 comments on commit 49d9215

Please sign in to comment.