-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/context, cbor service, expiration time (#47)
* feat/context, cbor service, expiration time * sonar * feat/did document with jwk
- Loading branch information
Showing
14 changed files
with
411 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/eu/europa/ec/dgc/issuance/restapi/controller/ContextController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package eu.europa.ec.dgc.issuance.restapi.controller; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import eu.europa.ec.dgc.issuance.service.ContextService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/context") | ||
@AllArgsConstructor | ||
public class ContextController { | ||
private final ContextService contextService; | ||
|
||
@Operation( | ||
summary = "provide configuration information for wallet app", | ||
description = "list of claim endpoints for wallet app" | ||
) | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "server list")} | ||
) | ||
@GetMapping(value = "") | ||
public ResponseEntity<JsonNode> context() { | ||
return ResponseEntity.ok(contextService.getContextDefintion()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/eu/europa/ec/dgc/issuance/service/ConfigurableCborService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package eu.europa.ec.dgc.issuance.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper; | ||
import com.upokecenter.cbor.CBORObject; | ||
import ehn.techiop.hcert.data.Eudgc; | ||
import ehn.techiop.hcert.kotlin.chain.impl.DefaultCborService; | ||
import eu.europa.ec.dgc.issuance.config.IssuanceConfigProperties; | ||
import eu.europa.ec.dgc.issuance.entity.GreenCertificateType; | ||
import java.time.Instant; | ||
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; | ||
|
||
/** | ||
* own cbor service. | ||
* The default one inject fixed country code and expiration period | ||
* | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
public class ConfigurableCborService extends DefaultCborService { | ||
public static final int ISSUER = 1; | ||
public static final int ISSUED_AT = 6; | ||
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 IssuanceConfigProperties issuanceConfigProperties; | ||
|
||
@Override | ||
public byte[] encode(@NotNull Eudgc input) { | ||
byte[] cbor; | ||
try { | ||
cbor = new CBORMapper().writeValueAsBytes(input); | ||
} catch (JsonProcessingException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
GreenCertificateType greenCertificateType; | ||
if (input.getT() != null && !input.getT().isEmpty()) { | ||
greenCertificateType = GreenCertificateType.Test; | ||
} else if (input.getR() != null && !input.getR().isEmpty()) { | ||
greenCertificateType = GreenCertificateType.Recovery; | ||
} else { | ||
greenCertificateType = GreenCertificateType.Vaccination; | ||
} | ||
long issueTime = Instant.now().getEpochSecond(); | ||
long expirationTime = issueTime + dgciService.expirationForType(greenCertificateType).get(ChronoUnit.SECONDS); | ||
CBORObject coseContainer = CBORObject.NewMap(); | ||
coseContainer.set(CBORObject.FromObject(ISSUER), | ||
CBORObject.FromObject(issuanceConfigProperties.getCountryCode())); | ||
coseContainer.set(CBORObject.FromObject(ISSUED_AT),CBORObject.FromObject(issueTime)); | ||
coseContainer.set(CBORObject.FromObject(EXPIRATION),CBORObject.FromObject(expirationTime)); | ||
CBORObject hcert = CBORObject.NewMap(); | ||
hcert.set(CBORObject.FromObject(HCERT_VERSION),CBORObject.DecodeFromBytes(cbor)); | ||
coseContainer.set(CBORObject.FromObject(HCERT),hcert); | ||
return coseContainer.EncodeToBytes(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/eu/europa/ec/dgc/issuance/service/ContextService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package eu.europa.ec.dgc.issuance.service; | ||
|
||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import eu.europa.ec.dgc.issuance.config.IssuanceConfigProperties; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import javax.annotation.PostConstruct; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class ContextService { | ||
private final IssuanceConfigProperties issuanceConfigProperties; | ||
private JsonNode contextDefinition; | ||
|
||
/** | ||
* load json context file. | ||
*/ | ||
@PostConstruct | ||
public void loadContextFile() { | ||
if (issuanceConfigProperties.getContextFile() != null | ||
&& issuanceConfigProperties.getContextFile().length() > 0) { | ||
File contextFile = new File(issuanceConfigProperties.getContextFile()); | ||
if (!contextFile.isFile()) { | ||
throw new IllegalArgumentException("configured context file can not be found: " + contextFile); | ||
} | ||
ObjectMapper mapper = new ObjectMapper(); | ||
try { | ||
contextDefinition = mapper.readTree(contextFile); | ||
log.info("context file loaded from: " + contextFile); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("can not read json context file: " + contextFile, e); | ||
} | ||
} else { | ||
log.warn("the context json file not configured (property: issuance.contextFile)." | ||
+ " The empty context file is generated instead"); | ||
JsonNodeFactory jsonNodeFactory = JsonNodeFactory.instance; | ||
ObjectNode contextObj = jsonNodeFactory.objectNode(); | ||
contextObj.set("Origin", jsonNodeFactory.textNode(issuanceConfigProperties.getCountryCode())); | ||
contextObj.set("claimEndpoints", jsonNodeFactory.arrayNode()); | ||
contextDefinition = contextObj; | ||
} | ||
} | ||
|
||
public JsonNode getContextDefintion() { | ||
return contextDefinition; | ||
} | ||
} |
Oops, something went wrong.