Skip to content

Commit

Permalink
[INJICERT-657] add optional expiry of VC in template
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Vardhan <[email protected]>
  • Loading branch information
vharsh committed Dec 17, 2024
1 parent a428451 commit 260dcce
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ public class Constants {
public static final String ED25519_REF_ID = "ED25519_SIGN";
public static final String TEMPLATE_NAME = "templateName";
public static final String ISSUER_URI = "issuerURI";
public static final String RENDERING_TEMPLATE = "renderingTemplateId";
public static final String RENDERING_TEMPLATE_ID = "renderingTemplateId";
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.mosip.certify.proof.ProofValidator;
import io.mosip.certify.proof.ProofValidatorFactory;
import io.mosip.certify.utils.CredentialUtils;
import io.mosip.certify.vcsigners.VCSigner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
Expand Down Expand Up @@ -291,7 +292,7 @@ private VCResult<?> getVerifiableCredential(CredentialRequest credentialRequest,
templateParams.put(Constants.TEMPLATE_NAME, CredentialUtils.getTemplateName(vcRequestDto));
templateParams.put(Constants.ISSUER_URI, issuerURI);
if (!StringUtils.isEmpty(renderTemplateId)) {
templateParams.put(Constants.RENDERING_TEMPLATE, renderTemplateId);
templateParams.put(Constants.RENDERING_TEMPLATE_ID, renderTemplateId);
}
String unSignedVC = vcFormatter.format(jsonObject, templateParams);
Map<String, String> signerSettings = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
package io.mosip.certify.services.vcformatters;

import java.io.*;
import java.time.Duration;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;

import io.mosip.certify.core.constants.ErrorConstants;
Expand Down Expand Up @@ -47,8 +49,10 @@ public class VelocityTemplatingEngineImpl implements VCFormatter {
CredentialTemplateRepository credentialTemplateRepository;
@Autowired
RenderingTemplateService renderingTemplateService;
@Value("${mosip.certify.vcformat.vc.expiry:true}")
boolean shouldHaveDates;

@Value("${mosip.certify.data-provider-plugin.vc-expiry-duration:P730d}")
String defaultExpiryDuration;

@Value("${mosip.certify.issuer.id.field.prefix.url:}")
String idPrefix;

Expand Down Expand Up @@ -113,21 +117,24 @@ public String format(JSONObject valueMap, Map<String, Object> templateSettings)
finalTemplate.put("_esc", new EscapeTool());
// add the issuer value
finalTemplate.put("_issuer", issuer);
if (templateSettings.containsKey(Constants.RENDERING_TEMPLATE) && templateName.contains(VCDM2Constants.URL)) {
if (templateSettings.containsKey(Constants.RENDERING_TEMPLATE_ID) && templateName.contains(VCDM2Constants.URL)) {
try {
finalTemplate.put("_renderMethodSVGdigest",
CredentialUtils.getDigestMultibase(renderingTemplateService.getSvgTemplate(
(String) templateSettings.get(Constants.RENDERING_TEMPLATE)).getTemplate()));
(String) templateSettings.get(Constants.RENDERING_TEMPLATE_ID)).getTemplate()));
} catch (RenderingTemplateException e) {
log.error("SVG Template: " + templateSettings.get(Constants.RENDERING_TEMPLATE) + " not available in DB", e);
log.error("SVG Template: " + templateSettings.get(Constants.RENDERING_TEMPLATE_ID) + " not available in DB", e);
}
}
if (shouldHaveDates && !(valueMap.has(VCDM2Constants.VALID_FROM)
&& valueMap.has(VCDM2Constants.VALID_UNITL))) {
String time = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern(Constants.UTC_DATETIME_PATTERN));
// hardcoded time
String expiryTime = ZonedDateTime.now(ZoneOffset.UTC).plusYears(2).format(DateTimeFormatter.ofPattern(Constants.UTC_DATETIME_PATTERN));
finalTemplate.put(VCDM2Constants.VALID_FROM, time);
if (!valueMap.has(VCDM2Constants.VALID_UNITL) && StringUtils.isNotEmpty(defaultExpiryDuration)) {
Duration duration;
try {
duration = Duration.parse(defaultExpiryDuration);
} catch (DateTimeParseException e) {
// set 365days as default VC expiry
duration = Duration.parse("P200D");
}
String expiryTime = ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(duration.getSeconds()).format(DateTimeFormatter.ofPattern(Constants.UTC_DATETIME_PATTERN));
finalTemplate.put(VCDM2Constants.VALID_UNITL, expiryTime);
}
VelocityContext context = new VelocityContext(finalTemplate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package io.mosip.certify.services;
package io.mosip.certify.vcsigners;

import foundation.identity.jsonld.JsonLDException;
import foundation.identity.jsonld.JsonLDObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.mosip.certify.services;
package io.mosip.certify.vcsigners;

import io.mosip.certify.api.dto.VCResult;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.mosip.certify.core.exception.NotAuthenticatedException;
import io.mosip.certify.core.util.SecurityHelperService;
import io.mosip.certify.proof.ProofValidatorFactory;
import io.mosip.certify.vcsigners.VCSigner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.util.*;

import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -117,7 +118,6 @@ public void setUp() {
//when(templateRepository.findByCredentialTypeAndContext("MockVerifiableCredential,VerifiableCredential", "https://schema.org,https://www.w3.org/2018/credentials/v1")).thenReturn(Optional.of(vc1));
when(credentialTemplateRepository.findByCredentialTypeAndContext("MockVerifiableCredential,VerifiableCredential", "https://example.org/Person.json,https://www.w3.org/ns/credentials/v2")).thenReturn(Optional.of(vc2));
//when(templateRepository.findByCredentialTypeAndContext("MockVerifiableCredential,VerifiableCredential", "https://vharsh.github.io/DID/mock-context.json,https://www.w3.org/2018/credentials/v1")).thenReturn(Optional.of(vc3));
ReflectionTestUtils.setField(formatter, "shouldHaveDates", true);
formatter.initialize();
// engine = new VelocityEngine();
// engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.mosip.certify.services;
package io.mosip.certify.vcsigners;

import foundation.identity.jsonld.JsonLDObject;
import info.weboftrust.ldsignatures.LdProof;
Expand Down

0 comments on commit 260dcce

Please sign in to comment.