Skip to content

Commit

Permalink
Merge pull request mosip#521 from Infosys/injiweb-1106-car-theme
Browse files Browse the repository at this point in the history
[INJIWEB-1106]: add changes according to release 0.15.x
  • Loading branch information
vijay151096 authored Nov 27, 2024
2 parents dc68eb6 + eaf842a commit 386f7d8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 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 @@ -117,7 +117,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 +126,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 +155,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 +173,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<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 +197,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 +215,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 +239,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8" />
<title>Identity Card</title>
<title>New Template Identity Card </title>
</head>
<body #if ($textColor) style="color: $textColor;" #end>
<div #if ($backgroundColor) style="background: $backgroundColor; border: 1px $textolor solid; border-radius: 15px; padding: 10px;" #else style="border: 2px black solid; border-radius: 15px; padding: 10px;" #end>
Expand Down Expand Up @@ -43,4 +43,4 @@
#end
</div>
</body>
</html>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void downloadPDFSuccessfully() throws Exception {
TokenResponseDTO tokenResponseDTO = TestUtilities.getTokenResponseDTO();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream("test-data".getBytes());
Mockito.when(credentialService.getTokenResponse(Mockito.anyMap(), Mockito.eq(issuer))).thenReturn(tokenResponseDTO);
Mockito.when(credentialService.downloadCredentialAsPDF(Mockito.eq(issuer), Mockito.eq(credential), Mockito.eq(tokenResponseDTO), Mockito.eq("3"))).thenReturn(byteArrayInputStream);
Mockito.when(credentialService.downloadCredentialAsPDF(Mockito.eq(issuer), Mockito.eq(credential), Mockito.eq(tokenResponseDTO), Mockito.eq("3"),Mockito.eq("en"))).thenReturn(byteArrayInputStream);

mockMvc.perform(post("/credentials/download")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
Expand All @@ -57,6 +57,7 @@ public void downloadPDFSuccessfully() throws Exception {
new BasicNameValuePair("code_verifier", "test-code_verifier"),
new BasicNameValuePair("issuer", issuer),
new BasicNameValuePair("vcStorageExpiryLimitInTimes", "3"),
new BasicNameValuePair("locale", "en"),
new BasicNameValuePair("credential", credential)
)))))
.andExpect(status().isOk())
Expand All @@ -83,6 +84,7 @@ public void throwExceptionWhenTokenIsNotFetched() throws Exception {
new BasicNameValuePair("code_verifier", "test-code_verifier"),
new BasicNameValuePair("issuer", issuer),
new BasicNameValuePair("vcStorageExpiryLimitInTimes", "3"),
new BasicNameValuePair("locale", "en"),
new BasicNameValuePair("credential", credential)
)))))
.andExpect(status().isBadRequest())
Expand All @@ -98,7 +100,7 @@ public void throwExceptionWhenPDFGenerationFailed() throws Exception {
String credential = "test-credential";
TokenResponseDTO tokenResponseDTO = TestUtilities.getTokenResponseDTO();
Mockito.when(credentialService.getTokenResponse(Mockito.anyMap(), Mockito.eq(issuer))).thenReturn(tokenResponseDTO);
Mockito.when(credentialService.downloadCredentialAsPDF(Mockito.eq(issuer), Mockito.eq(credential), Mockito.eq(tokenResponseDTO), Mockito.eq("3") )).thenThrow(ApiNotAccessibleException.class);
Mockito.when(credentialService.downloadCredentialAsPDF(Mockito.eq(issuer), Mockito.eq(credential), Mockito.eq(tokenResponseDTO), Mockito.eq("3"),Mockito.eq("en") )).thenThrow(ApiNotAccessibleException.class);

mockMvc.perform(post("/credentials/download")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
Expand All @@ -109,6 +111,7 @@ public void throwExceptionWhenPDFGenerationFailed() throws Exception {
new BasicNameValuePair("code_verifier", "test-code_verifier"),
new BasicNameValuePair("issuer", issuer),
new BasicNameValuePair("vcStorageExpiryLimitInTimes", "3"),
new BasicNameValuePair("locale", "en"),
new BasicNameValuePair("credential", credential)
)))))
.andExpect(status().isBadRequest())
Expand Down

0 comments on commit 386f7d8

Please sign in to comment.