Skip to content

Commit

Permalink
fix spring circular dependencies (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-trzewik authored May 10, 2021
1 parent c5d4723 commit c3cc3a2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
@Configuration
@RequiredArgsConstructor
public class HcertLibConfig {

private final EhdCryptoService ehdCryptoService;

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,6 @@ public class DgciController {

private final DgciService dgciService;


@Operation(
summary = "Creates new dgci (deprecated)",
description = "Creates new dgci and return meta data for certificate creation"
)
@PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE)
@Deprecated
public ResponseEntity<DgciIdentifier> initDgciDepr(@Valid @RequestBody DgciInit dgciInit) {
return ResponseEntity.ok(dgciService.initDgci(dgciInit));
}

@Operation(
summary = "calculate cose signature for edgc (deprecated)",
description = "calculate cose signature for given certificate hash, "
+ "generate TAN and update DGCI Registry database"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "signature created"),
@ApiResponse(responseCode = "404", description = "dgci with related id not found"),
@ApiResponse(responseCode = "400", description = "wrong issue data")})
@PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
@Deprecated
public ResponseEntity<SignatureData> finalizeDgciDepr(@PathVariable long id,
@Valid @RequestBody IssueData issueData)
throws Exception {
return ResponseEntity.ok(dgciService.finishDgci(id, issueData));
}

@Operation(
summary = "Prepares an DGCI for the Code Generation in Frontend",
description = "Creates new dgci and return meta data for certificate creation"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
package eu.europa.ec.dgc.issuance.restapi.dto;

import eu.europa.ec.dgc.issuance.entity.GreenCertificateType;
import javax.validation.constraints.NotNull;
import lombok.Data;

@Data
public class DgciInit {
@NotNull
private GreenCertificateType greenCertificateType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.time.temporal.ChronoUnit;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
Expand All @@ -27,10 +26,8 @@ public class ConfigurableCborService extends DefaultCborService {
public static final int EXPIRATION = 4;
public static final int HCERT = -260;
public static final int HCERT_VERSION = 1;
// Need autowired because there is circular reference
@Autowired
private DgciService dgciService;

private final ExpirationService expirationService;
private final IssuanceConfigProperties issuanceConfigProperties;

@Override
Expand All @@ -50,7 +47,8 @@ public byte[] encode(@NotNull Eudgc input) {
greenCertificateType = GreenCertificateType.Vaccination;
}
long issueTime = Instant.now().getEpochSecond();
long expirationTime = issueTime + dgciService.expirationForType(greenCertificateType).get(ChronoUnit.SECONDS);
long expirationTime = issueTime
+ expirationService.expirationForType(greenCertificateType).get(ChronoUnit.SECONDS);
CBORObject coseContainer = CBORObject.NewMap();
coseContainer.set(CBORObject.FromObject(ISSUER),
CBORObject.FromObject(issuanceConfigProperties.getCountryCode()));
Expand Down
37 changes: 3 additions & 34 deletions src/main/java/eu/europa/ec/dgc/issuance/service/DgciService.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
Expand Down Expand Up @@ -94,6 +92,7 @@ public enum DgciStatus {
private final ContextIdentifierService contextIdentifierService;
private final CompressorService compressorService;
private final Base45Service base45Service;
private final ExpirationService expirationService;

private static final int MAX_CLAIM_RETRY_TAN = 3;

Expand All @@ -114,7 +113,7 @@ public DgciIdentifier initDgci(DgciInit dgciInit) {
log.info("init dgci: {} id: {}", dgci, dgciEntity.getId());

ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime expiration = now.plus(expirationForType(dgciInit.getGreenCertificateType()));
ZonedDateTime expiration = now.plus(expirationService.expirationForType(dgciInit.getGreenCertificateType()));
long expirationSec = expiration.toInstant().getEpochSecond();

dgciEntity.setExpiresAt(expiration);
Expand All @@ -140,36 +139,6 @@ private String dgciHash(String dgci) {
}
}

/**
* expiration duration for given edgc type.
* @param greenCertificateType edgc type
* @return Duration
*/
public Duration expirationForType(GreenCertificateType greenCertificateType) {
Duration duration;
if (issuanceConfigProperties.getExpiration() != null) {
switch (greenCertificateType) {
case Test:
duration = issuanceConfigProperties.getExpiration().getTest();
break;
case Vaccination:
duration = issuanceConfigProperties.getExpiration().getVaccination();
break;
case Recovery:
duration = issuanceConfigProperties.getExpiration().getRecovery();
break;
default:
throw new IllegalArgumentException("unsupported cert type for expiration: " + greenCertificateType);
}
} else {
duration = null;
}
if (duration == null) {
duration = Duration.of(365, ChronoUnit.DAYS);
}
return duration;
}

@NotNull
private String generateDgci() {
return dgciGenerator.newDgci();
Expand Down Expand Up @@ -417,7 +386,7 @@ public EgdcCodeData createEdgc(Eudgc eudgc) {
dgciEntity.setHashedTan(tanService.hashTan(tan));
dgciEntity.setGreenCertificateType(greenCertificateType);
dgciEntity.setCreatedAt(ZonedDateTime.now());
dgciEntity.setExpiresAt(ZonedDateTime.now().plus(expirationForType(greenCertificateType)));
dgciEntity.setExpiresAt(ZonedDateTime.now().plus(expirationService.expirationForType(greenCertificateType)));
dgciRepository.saveAndFlush(dgciEntity);

return egdcCodeData;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package eu.europa.ec.dgc.issuance.service;

import eu.europa.ec.dgc.issuance.config.IssuanceConfigProperties;
import eu.europa.ec.dgc.issuance.entity.GreenCertificateType;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ExpirationService {

private final IssuanceConfigProperties issuanceConfigProperties;

/**
* expiration duration for given edgc type.
* @param greenCertificateType edgc type
* @return Duration
*/
public Duration expirationForType(GreenCertificateType greenCertificateType) {
Duration duration;
if (issuanceConfigProperties.getExpiration() != null) {
switch (greenCertificateType) {
case Test:
duration = issuanceConfigProperties.getExpiration().getTest();
break;
case Vaccination:
duration = issuanceConfigProperties.getExpiration().getVaccination();
break;
case Recovery:
duration = issuanceConfigProperties.getExpiration().getRecovery();
break;
default:
throw new IllegalArgumentException("unsupported cert type for expiration: " + greenCertificateType);
}
} else {
duration = null;
}
if (duration == null) {
duration = Duration.of(365, ChronoUnit.DAYS);
}
return duration;
}
}

0 comments on commit c3cc3a2

Please sign in to comment.