Skip to content

Commit

Permalink
[INJIWEB-1106]: add changes according to release 0.15.x
Browse files Browse the repository at this point in the history
Signed-off-by: SAIRAM GIRIRAO <[email protected]>
  • Loading branch information
Sairam-g9162 committed Nov 27, 2024
1 parent dc68eb6 commit 7444df6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ public ResponseEntity<?> downloadCredentialAsPDF(@RequestParam Map<String, Strin
String issuerId = params.get("issuer");
String credentialType = params.get("credential");
String credentialValidity = params.get("vcStorageExpiryLimitInTimes");
String locale = params.get("locale");

log.info("Initiated Token Call");
TokenResponseDTO response = credentialService.getTokenResponse(params, issuerId);

log.info("Initiated Download Credential Call");
ByteArrayInputStream inputStream = credentialService.downloadCredentialAsPDF(issuerId, credentialType, response, credentialValidity);
ByteArrayInputStream inputStream = credentialService.downloadCredentialAsPDF(issuerId, credentialType, response, credentialValidity,locale);

return ResponseEntity
.ok()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
public interface CredentialService {
TokenResponseDTO getTokenResponse(Map<String, String> params, String issuerId) throws ApiNotAccessibleException, IOException;

ByteArrayInputStream downloadCredentialAsPDF(String issuerId, String credentialType, TokenResponseDTO response, String credentialValidity) throws Exception;
ByteArrayInputStream downloadCredentialAsPDF(String issuerId, String credentialType, TokenResponseDTO response, String credentialValidity, String locale) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.mosip.vercred.vcverifier.constants.CredentialFormat;
import io.mosip.vercred.vcverifier.data.VerificationResult;
import jakarta.annotation.PostConstruct;
import jakarta.validation.valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
Expand Down Expand Up @@ -117,7 +118,7 @@ public TokenResponseDTO getTokenResponse(Map<String, String> params, String issu
}

@Override
public ByteArrayInputStream downloadCredentialAsPDF(String issuerId, String credentialType, TokenResponseDTO response, String credentialValidity) throws Exception {
public ByteArrayInputStream downloadCredentialAsPDF(String issuerId, String credentialType, TokenResponseDTO response, String credentialValidity, String locale) throws Exception {
IssuerDTO issuerConfig = issuerService.getIssuerConfig(issuerId);
CredentialIssuerWellKnownResponse credentialIssuerWellKnownResponse = issuerService.getIssuerWellknown(issuerId);
CredentialsSupportedResponse credentialsSupportedResponse = issuerService.getIssuerWellknownForCredentialType(issuerId, credentialType);
Expand All @@ -126,7 +127,7 @@ public ByteArrayInputStream downloadCredentialAsPDF(String issuerId, String cred
boolean verificationStatus = issuerId.toLowerCase().contains("mock") || verifyCredential(vcCredentialResponse);
if(verificationStatus) {
String dataShareUrl = QRCodeType.OnlineSharing.equals(issuerConfig.getQr_code_type()) ? dataShareService.storeDataInDataShare(objectMapper.writeValueAsString(vcCredentialResponse), credentialValidity) : "";
return generatePdfForVerifiableCredentials(vcCredentialResponse, issuerConfig, credentialsSupportedResponse, dataShareUrl, credentialValidity);
return generatePdfForVerifiableCredentials(issuerId, credentialType, vcCredentialResponse, issuerConfig, credentialsSupportedResponse, dataShareUrl, credentialValidity, locale);
}
throw new VCVerificationException(SIGNATURE_VERIFICATION_EXCEPTION.getErrorCode(),
SIGNATURE_VERIFICATION_EXCEPTION.getErrorMessage());
Expand Down Expand Up @@ -155,10 +156,10 @@ public VCCredentialRequest generateVCCredentialRequest(IssuerDTO issuerDTO, Cred
.build();
}

public ByteArrayInputStream generatePdfForVerifiableCredentials(VCCredentialResponse vcCredentialResponse, IssuerDTO issuerDTO, CredentialsSupportedResponse credentialsSupportedResponse, String dataShareUrl, String credentialValidity) throws Exception {
LinkedHashMap<String, Object> displayProperties = loadDisplayPropertiesFromWellknown(vcCredentialResponse, credentialsSupportedResponse);
Map<String, Object> data = getPdfResourceFromVcProperties(displayProperties, credentialsSupportedResponse, vcCredentialResponse, issuerDTO, dataShareUrl, credentialValidity);
return renderVCInCredentialTemplate(data);
public ByteArrayInputStream generatePdfForVerifiableCredentials(String issuerId, String credentialType, VCCredentialResponse vcCredentialResponse, IssuerDTO issuerDTO, CredentialsSupportedResponse credentialsSupportedResponse, String dataShareUrl, String credentialValidity, String locale) throws Exception {
LinkedHashMap<String, Object> displayProperties = loadDisplayPropertiesFromWellknown(vcCredentialResponse, credentialsSupportedResponse, locale);
Map<String, Object> data = getPdfResourceFromVcProperties(displayProperties, credentialsSupportedResponse, vcCredentialResponse, issuerDTO, dataShareUrl, credentialValidity, locale);
return renderVCInCredentialTemplate(data, issuerId, credentialType);
}

public Boolean verifyCredential(VCCredentialResponse vcCredentialResponse) throws VCVerificationException, JsonProcessingException {
Expand All @@ -173,14 +174,18 @@ public Boolean verifyCredential(VCCredentialResponse vcCredentialResponse) throw
}

@NotNull
private static LinkedHashMap<String, Object> loadDisplayPropertiesFromWellknown(VCCredentialResponse vcCredentialResponse, CredentialsSupportedResponse credentialsSupportedResponse) {
private static LinkedHashMap<String, Object> loadDisplayPropertiesFromWellknown(VCCredentialResponse vcCredentialResponse, CredentialsSupportedResponse credentialsSupportedResponse,String locale) {
LinkedHashMap<String,Object> displayProperties = new LinkedHashMap<>();
Map<String, Object> credentialProperties = vcCredentialResponse.getCredential().getCredentialSubject();

LinkedHashMap<String, String> vcPropertiesFromWellKnown = new LinkedHashMap<>();
Map<String, CredentialDisplayResponseDto> credentialSubject = credentialsSupportedResponse.getCredentialDefinition().getCredentialSubject();
credentialSubject.keySet().forEach(VCProperty -> vcPropertiesFromWellKnown.put(VCProperty, credentialSubject.get(VCProperty).getDisplay().get(0).getName()));

// credentialSubject.keySet().forEach(VCProperty -> vcPropertiesFromWellKnown.put(VCProperty, credentialSubject.get(VCProperty).getDisplay().get(0).getName()));
credentialSubject.keySet().forEach(VCProperty -> {
Optional<@Valid CredentialIssuerDisplayResponse> filteredResponse = credentialSubject.get(VCProperty).getDisplay().stream().filter(obj -> obj.getLocale().equals(locale)).findFirst();
String filteredValue = filteredResponse.isPresent() ? filteredResponse.get().getName() : "" ;
vcPropertiesFromWellKnown.put(VCProperty, filteredValue);
});
List<String> orderProperty = credentialsSupportedResponse.getOrder();

List<String> fieldProperties = orderProperty == null ? new ArrayList<>(vcPropertiesFromWellKnown.keySet()) : orderProperty;
Expand All @@ -193,7 +198,7 @@ private static LinkedHashMap<String, Object> loadDisplayPropertiesFromWellknown(
}


private Map<String, Object> getPdfResourceFromVcProperties(LinkedHashMap<String, Object> displayProperties, CredentialsSupportedResponse credentialsSupportedResponse, VCCredentialResponse vcCredentialResponse, IssuerDTO issuerDTO, String dataShareUrl, String credentialValidity) throws IOException, WriterException {
private Map<String, Object> getPdfResourceFromVcProperties(LinkedHashMap<String, Object> displayProperties, CredentialsSupportedResponse credentialsSupportedResponse, VCCredentialResponse vcCredentialResponse, IssuerDTO issuerDTO, String dataShareUrl, String credentialValidity, String locale) throws IOException, WriterException {
Map<String, Object> data = new HashMap<>();
LinkedHashMap<String, Object> rowProperties = new LinkedHashMap<>();
String backgroundColor = credentialsSupportedResponse.getDisplay().get(0).getBackgroundColor();
Expand All @@ -211,7 +216,8 @@ private Map<String, Object> getPdfResourceFromVcProperties(LinkedHashMap<String,
if( ((List<?>) entry.getValue()).get(0) instanceof String) {
value = ((List<String>) entry.getValue()).stream().reduce((field1, field2) -> field1 + ", " + field2 ).get();
} else {
value = (String) ((Map<?, ?>) ((List<?>) entry.getValue()).get(0)).get("value");
Optional<Map<?, ?>> valueMap = ((List<Map<?, ?>>) entry.getValue()).stream().filter(obj -> obj.get("language").equals(locale)).findFirst();
value = valueMap.isPresent() ? valueMap.get().get("value").toString() : "" ;
}
rowProperties.put(entry.getKey(), value);
} else {
Expand All @@ -234,8 +240,8 @@ private Map<String, Object> getPdfResourceFromVcProperties(LinkedHashMap<String,
}

@NotNull
private ByteArrayInputStream renderVCInCredentialTemplate(Map<String, Object> data) throws IOException {
String credentialTemplate = utilities.getCredentialSupportedTemplateString();
private ByteArrayInputStream renderVCInCredentialTemplate(Map<String, Object> data,String issuerId, String credentialType) throws IOException {
String credentialTemplate = utilities.getCredentialSupportedTemplateString(issuerId,credentialType);

Properties props = new Properties();
props.setProperty("resource.loader", "class");
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/mosip/mimoto/util/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ public String getIssuersConfigJsonValue() {
public String getTrustedVerifiersJsonValue() {
return getJson(trustedVerifiersJsonString, trustedVerifiersPath);
}
public String getCredentialSupportedTemplateString() {
return getJson(credentialTemplateHtmlString, credentialTemplatePath);
public String getCredentialSupportedTemplateString(String issuerId, String credentialType) {
String specificCredentialPDFTemplate = getJson(configServerFileStorageURL, String.format("%s-%s-template.html", issuerId.toLowerCase(), credentialType.toLowerCase()));
return !StringUtils.isEmpty(specificCredentialPDFTemplate)? specificCredentialPDFTemplate : getJson(credentialTemplateHtmlString, credentialTemplatePath);
}
public static String[] handleExceptionWithErrorCode(Exception exception) {
String errorMessage = exception.getMessage();
Expand Down
46 changes: 0 additions & 46 deletions src/main/resources/templates/credential-template.html

This file was deleted.

0 comments on commit 7444df6

Please sign in to comment.