From 49e0380977437dd203992b844b874b85b9704c3d Mon Sep 17 00:00:00 2001 From: Alexander Schwarz Date: Fri, 17 Sep 2021 08:46:16 +0200 Subject: [PATCH] update identity filters --- .../controller/IdentityController.java | 59 +++---------------- .../ValidationStatusController.java | 2 +- .../decorator/service/IdentityService.java | 55 ++++++++++------- 3 files changed, 42 insertions(+), 74 deletions(-) diff --git a/src/main/java/eu/europa/ec/dgc/validation/decorator/controller/IdentityController.java b/src/main/java/eu/europa/ec/dgc/validation/decorator/controller/IdentityController.java index 5687ca2..46ddd66 100644 --- a/src/main/java/eu/europa/ec/dgc/validation/decorator/controller/IdentityController.java +++ b/src/main/java/eu/europa/ec/dgc/validation/decorator/controller/IdentityController.java @@ -41,13 +41,15 @@ public class IdentityController { private static final String PATH_ELEMENT = "/identity/{element}"; - private static final String PATH_ELEMENT_ID = "/identity/{element}/{id}"; + private static final String PATH_ELEMENT_TYPE = "/identity/{element}/{type}"; private final IdentityService identityService; /** * Delivers a JSON description of public keys and endpoints. * + * @param element Name of element (optional) + * @param type Type of element (optional) * @return {@link IdentityResponse} */ @Operation(summary = "The identity document endpoint delivers a JSON description of public keys and endpoints", @@ -59,57 +61,12 @@ public class IdentityController { @ApiResponse(responseCode = "404", description = "Not Found"), @ApiResponse(responseCode = "500", description = "Internal Server Error"), }) - @GetMapping(value = PATH_ALL, produces = MediaType.APPLICATION_JSON_VALUE) - public IdentityResponse identityAll() { - log.debug("Incoming GET request to '{}' with element '{}' and id '{}'", PATH_ALL); - - return identityService.getIdentity(null, null); - } - - /** - * Delivers a JSON description of public keys and endpoints. - * - * @param element Name of element - * @return {@link IdentityResponse} - */ - @Operation(summary = "The identity document endpoint delivers a JSON description of public keys and endpoints", - description = "The identity document endpoint delivers a JSON description of public keys and endpoints") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "OK"), - @ApiResponse(responseCode = "400", description = "Bad Request / Validation errors"), - @ApiResponse(responseCode = "401", description = "Unauthorized, if no active session is attached"), - @ApiResponse(responseCode = "404", description = "Not Found"), - @ApiResponse(responseCode = "500", description = "Internal Server Error"), - }) - @GetMapping(value = PATH_ELEMENT, produces = MediaType.APPLICATION_JSON_VALUE) - public IdentityResponse identity(@PathVariable(name = "element", required = true) final String element) { - log.debug("Incoming GET request to '{}' with element '{}'", PATH_ELEMENT, element); - - return identityService.getIdentity(element, null); - } - - /** - * Delivers a JSON description of public keys and endpoints. - * - * @param element Name of element - * @param id ID of element - * @return {@link IdentityResponse} - */ - @Operation(summary = "The identity document endpoint delivers a JSON description of public keys and endpoints", - description = "The identity document endpoint delivers a JSON description of public keys and endpoints") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "OK"), - @ApiResponse(responseCode = "400", description = "Bad Request / Validation errors"), - @ApiResponse(responseCode = "401", description = "Unauthorized, if no active session is attached"), - @ApiResponse(responseCode = "404", description = "Not Found"), - @ApiResponse(responseCode = "500", description = "Internal Server Error"), - }) - @GetMapping(value = PATH_ELEMENT_ID, produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping(value = {PATH_ALL, PATH_ELEMENT, PATH_ELEMENT_TYPE}, produces = MediaType.APPLICATION_JSON_VALUE) public IdentityResponse identity( - @PathVariable(name = "element", required = true) final String element, - @PathVariable(name = "id", required = true) final String id) { - log.debug("Incoming GET request to '{}' with element '{}' and id '{}'", PATH_ELEMENT_ID, element, id); + @PathVariable(name = "element", required = false) final String element, + @PathVariable(name = "type", required = false) final String type) { + log.debug("Incoming GET request to '{}' with element '{}' and type '{}'", PATH_ELEMENT_TYPE, element, type); - return identityService.getIdentity(element, id); + return identityService.getIdentity(element, type); } } diff --git a/src/main/java/eu/europa/ec/dgc/validation/decorator/controller/ValidationStatusController.java b/src/main/java/eu/europa/ec/dgc/validation/decorator/controller/ValidationStatusController.java index f16b7ce..f2eab99 100644 --- a/src/main/java/eu/europa/ec/dgc/validation/decorator/controller/ValidationStatusController.java +++ b/src/main/java/eu/europa/ec/dgc/validation/decorator/controller/ValidationStatusController.java @@ -63,7 +63,7 @@ public class ValidationStatusController { @ApiResponse(responseCode = "500", description = "Internal Server Error") }) @GetMapping(value = PATH, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity reject(@RequestHeader("Authorization") final String token) { + public ResponseEntity status(@RequestHeader("Authorization") final String token) { log.debug("Incoming GET request to '{}' with token '{}'", PATH, token); if (accessTokenService.isValid(token)) { diff --git a/src/main/java/eu/europa/ec/dgc/validation/decorator/service/IdentityService.java b/src/main/java/eu/europa/ec/dgc/validation/decorator/service/IdentityService.java index e1c0686..70189e7 100644 --- a/src/main/java/eu/europa/ec/dgc/validation/decorator/service/IdentityService.java +++ b/src/main/java/eu/europa/ec/dgc/validation/decorator/service/IdentityService.java @@ -43,8 +43,14 @@ public class IdentityService { private static final String VERIFICATION_TYPE = "JsonWebKey2020"; - - private static final String IDENTITY_PATH = "/identity/verificationMethod/" + VERIFICATION_TYPE; + + private static final String IDENTITY_ROOT = "/identity"; + + private static final String IDENTITY_PATH = IDENTITY_ROOT + "/verificationMethod/" + VERIFICATION_TYPE; + + private static final String ELEMENT_VERIFICATION_METHOD = "verificationMethod"; + + private static final String ELEMENT_SERVICE = "service"; private final DgcProperties dgcProperties; @@ -54,30 +60,34 @@ public class IdentityService { * Create identity Object with given informations. * * @param element Element - * @param id ID + * @param type Type * @return {@link IdentityResponse} */ - public IdentityResponse getIdentity(final String element, final String id) { - // TODO impl filter for id + public IdentityResponse getIdentity(final String element, final String type) { + final String identityId = String.format("%s%s", dgcProperties.getServiceUrl(), IDENTITY_ROOT); - final String identityId = String.format("%s%s", dgcProperties.getServiceUrl(), IDENTITY_PATH); + final IdentityResponse identityResponse = new IdentityResponse(); + identityResponse.setId(identityId); + identityResponse.setVerificationMethod(getVerificationMethods(element, type)); + identityResponse.setService(getServices(element, type)); + return identityResponse; + } + + private List getVerificationMethods(final String element, final String type) { + final String identityPath = String.format("%s%s", dgcProperties.getServiceUrl(), IDENTITY_PATH); - final List verificationMethods = keyProvider.getKeyNames(KeyType.ALL).stream() - .filter(keyName -> element == null || element.equalsIgnoreCase(keyName)) + return keyProvider.getKeyNames(KeyType.ALL).stream() + .filter(keyName -> element == null || ELEMENT_VERIFICATION_METHOD.equalsIgnoreCase(element)) .map(keyName -> { final VerificationIdentityResponse verificationMethod = new VerificationIdentityResponse(); - verificationMethod.setId(String.format("%s/%s", identityId, keyName)); - verificationMethod.setController(identityId); + verificationMethod.setId(String.format("%s/%s", identityPath, keyName)); + verificationMethod.setController(identityPath); verificationMethod.setType(VERIFICATION_TYPE); verificationMethod.setPublicKeyJwk(buildPublicKey(keyName)); return verificationMethod; - }).collect(Collectors.toList()); - - final IdentityResponse identityResponse = new IdentityResponse(); - identityResponse.setId(identityId); - identityResponse.setVerificationMethod(verificationMethods); - identityResponse.setService(getServices(element, id)); - return identityResponse; + }) + .filter(method -> type == null || type.equalsIgnoreCase(method.getType())) + .collect(Collectors.toList()); } /** @@ -96,10 +106,9 @@ public ServiceProperties getServicePropertiesById(final String serviceId) { throw new NotFoundException("Verification method not found. No ID available."); } - private List getServices(final String element, final String id) { - // TODO impl filter for id + private List getServices(final String element, final String type) { return Stream.concat(dgcProperties.getServices(), dgcProperties.getEndpoints()) - .filter(service -> element == null || element.equalsIgnoreCase(service.getType())) + .filter(service -> element == null || ELEMENT_SERVICE.equalsIgnoreCase(element)) .map(service -> { final ServiceIdentityResponse response = new ServiceIdentityResponse(); response.setId(service.getId()); @@ -107,11 +116,13 @@ private List getServices(final String element, final St response.setServiceEndpoint(service.getServiceEndpoint()); response.setName(service.getName()); return response; - }).collect(Collectors.toList()); + }) + .filter(method -> type == null || type.equalsIgnoreCase(method.getType())) + .collect(Collectors.toList()); } private PublicKeyJwkIdentityResponse buildPublicKey(String keyName) { - final Certificate certificate = keyProvider.receiveCertificate(keyName); + final Certificate certificate = keyProvider.receiveCertificate(keyName); try { final PublicKeyJwkIdentityResponse publicKeyJwk = new PublicKeyJwkIdentityResponse(); publicKeyJwk.setX5c(Base64.getEncoder().encodeToString(certificate.getEncoded()));