Skip to content

Commit

Permalink
update identity filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Schwarz committed Sep 17, 2021
1 parent c34b0d6 commit 49e0380
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<VerificationIdentityResponse> getVerificationMethods(final String element, final String type) {
final String identityPath = String.format("%s%s", dgcProperties.getServiceUrl(), IDENTITY_PATH);

final List<VerificationIdentityResponse> 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());
}

/**
Expand All @@ -96,22 +106,23 @@ public ServiceProperties getServicePropertiesById(final String serviceId) {
throw new NotFoundException("Verification method not found. No ID available.");
}

private List<ServiceIdentityResponse> getServices(final String element, final String id) {
// TODO impl filter for id
private List<ServiceIdentityResponse> 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());
response.setType(service.getType());
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()));
Expand Down

0 comments on commit 49e0380

Please sign in to comment.