Skip to content

Commit

Permalink
[INJICERT-434] only fetch SVG template if VC 2.0 is requested
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Vardhan <[email protected]>
  • Loading branch information
vharsh committed Nov 8, 2024
1 parent d9001ff commit 4f428e2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.mosip.certify.core.constants;

public class VCDM1Constants {
public static final String URL = "https://www.w3.org/2018/credentials/v1";
public static final String ISSUANCE_DATE = "issuanceDate";
public static final String EXPIRATION_DATE = "expirationDate";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* ref:
*/
public class VCDM2Constants {
public static final String URL = "https://www.w3.org/ns/credentials/v2";
public static final String VALID_UNITL = "validUntil";
public static final String VALID_FROM = "validFrom";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import io.mosip.certify.api.spi.VCFormatter;
import io.mosip.certify.core.constants.Constants;
import io.mosip.certify.core.constants.VCDM2Constants;
import io.mosip.certify.core.exception.TemplateException;
import io.mosip.certify.core.repository.TemplateRepository;
import io.mosip.certify.core.spi.SvgTemplateService;
import io.mosip.certify.services.SVGRenderUtils;
import jakarta.annotation.PostConstruct;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
Expand All @@ -32,6 +34,7 @@

import static io.mosip.certify.services.templating.VelocityTemplatingConstants.*;

@Slf4j
@Service
public class VelocityTemplatingEngineImpl implements VCFormatter {
VelocityEngine engine;
Expand Down Expand Up @@ -104,10 +107,14 @@ public String format(JSONObject templateInput, Map<String, Object> defaultSettin
finalTemplate.put("_esc", new EscapeTool());
// add the issuer value
finalTemplate.put("issuer", issuer);
if (defaultSettings.containsKey(SVG_TEMPLATE)) {
finalTemplate.put("_renderMethodSVGdigest",
SVGRenderUtils.getDigestMultibase(svgTemplateService.getSvgTemplate(
UUID.fromString((String) defaultSettings.get(SVG_TEMPLATE))).getTemplate()));
if (defaultSettings.containsKey(SVG_TEMPLATE) && templateName.contains(VCDM2Constants.URL)) {
try {
finalTemplate.put("_renderMethodSVGdigest",
SVGRenderUtils.getDigestMultibase(svgTemplateService.getSvgTemplate(
UUID.fromString((String) defaultSettings.get(SVG_TEMPLATE))).getTemplate()));
} catch (TemplateException e) {
log.error("SVG Template: " + defaultSettings.get(SVG_TEMPLATE) + " not available in DB", e);
}
}
if (shouldHaveDates && !(templateInput.has(VCDM2Constants.VALID_FROM)
&& templateInput.has(VCDM2Constants.VALID_UNITL))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.mosip.certify.services;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class SVGRenderUtilsTest {

@Test
void getDigestMultibase() {
String svg = """
<svg viewBox=".5 .5 3 4" fill="none" stroke="#20b2a" stroke-linecap="round"> <path d=" M1 4h-.001 V1h2v.001 M1 2.6 h1v.001"/> </svg>
""";
String actual = SVGRenderUtils.getDigestMultibase(svg);
String expected = "z4po9QkJj1fhMt6cxHSnDnAUat4PEVrerUGGsPHLxJnK5";
assertEquals(expected, actual);
}

}

0 comments on commit 4f428e2

Please sign in to comment.