use of de.symeda.sormas.api.sormastosormas.SormasToSormasEncryptedDataDto in project SORMAS-Project by hzi-braunschweig.
the class S2SAuthFilter method filter.
@Override
public void filter(ContainerRequestContext requestContext) {
String authorizationHeader = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION);
boolean validHeader = authorizationHeader != null && authorizationHeader.startsWith(String.format("%s ", BEARER));
if (!validHeader) {
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED.getStatusCode(), "Invalid header").build());
return;
}
String token = authorizationHeader.substring(BEARER.length()).trim();
String senderId = "";
if (requestContext.getMethod().equals(HttpMethod.GET)) {
senderId = requestContext.getUriInfo().getQueryParameters().getFirst(SormasToSormasConfig.SENDER_SERVER_ID);
} else {
ContainerRequest cr = (ContainerRequest) requestContext;
cr.bufferEntity();
SormasToSormasEncryptedDataDto dto = cr.readEntity(SormasToSormasEncryptedDataDto.class);
senderId = dto.getSenderId();
}
try {
if (!isValidToken(token, senderId)) {
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED.getStatusCode(), "Invalid token").build());
}
} catch (Exception e) {
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED.getStatusCode(), e.getMessage()).build());
}
}
use of de.symeda.sormas.api.sormastosormas.SormasToSormasEncryptedDataDto in project SORMAS-Project by hzi-braunschweig.
the class SormasToSormasRestClient method sendRequest.
private <T> T sendRequest(String receiverId, String endpoint, Object body, Class<T> responseType, String method) throws SormasToSormasException {
try {
Entity<String> entity = null;
if (body != null) {
SormasToSormasEncryptedDataDto encryptedBody = sormasToSormasEncryptionEjb.signAndEncrypt(body, receiverId);
entity = Entity.entity(mapper.writeValueAsString(encryptedBody), MediaType.APPLICATION_JSON_TYPE);
} else {
// no sender org id in the encrypted DTP, therefore, we pass it as query parameter
String ownId = configFacadeEjb.getS2SConfig().getId();
// safely append the parameter
endpoint = UriBuilder.fromUri(endpoint).queryParam(SormasToSormasConfig.SENDER_SERVER_ID, ownId).build().toString();
}
Invocation.Builder invocation = buildRestClient(receiverId, endpoint);
Response response;
switch(method) {
case HttpMethod.POST:
response = invocation.post(entity);
break;
case HttpMethod.PUT:
response = invocation.put(entity);
break;
case HttpMethod.GET:
response = invocation.get();
break;
default:
throw SormasToSormasException.fromStringProperty(Strings.errorSormasToSormasInvalidRequestMethod);
}
return handleResponse(response, responseType);
} catch (JsonProcessingException e) {
LOGGER.error("Unable to send data sormas", e);
throw SormasToSormasException.fromStringProperty(Strings.errorSormasToSormasSend);
} catch (ResponseProcessingException e) {
LOGGER.error("Unable to process sormas response", e);
throw SormasToSormasException.fromStringProperty(Strings.errorSormasToSormasResult);
} catch (ProcessingException e) {
LOGGER.error("Unable to send data to sormas", e);
String processingErrorStringProperty = Strings.errorSormasToSormasSend;
if (ConnectException.class.isAssignableFrom(e.getCause().getClass())) {
processingErrorStringProperty = Strings.errorSormasToSormasConnection;
}
throw SormasToSormasException.fromStringProperty(processingErrorStringProperty);
}
}
use of de.symeda.sormas.api.sormastosormas.SormasToSormasEncryptedDataDto in project SORMAS-Project by hzi-braunschweig.
the class SormasToSormasEncryptionFacadeEjb method signAndEncrypt.
@Override
public SormasToSormasEncryptedDataDto signAndEncrypt(Object entities, String recipientId) throws SormasToSormasException {
LOGGER.info("Sign and encrypt data for {}", recipientId);
try {
final String ownId = configFacadeEjb.getS2SConfig().getId();
CmsPlaintext plaintext = new CmsPlaintext(ownId, recipientId, entities);
S2SCertificateConfig config = new S2SCertificateConfig(recipientId);
byte[] encryptedData = CmsCreator.signAndEncrypt(plaintext, config, true);
return new SormasToSormasEncryptedDataDto(ownId, encryptedData);
} catch (Exception e) {
LOGGER.error("Could not sign and encrypt data", e);
throw SormasToSormasException.fromStringProperty(Strings.errorSormasToSormasEncrypt);
}
}
use of de.symeda.sormas.api.sormastosormas.SormasToSormasEncryptedDataDto in project SORMAS-Project by hzi-braunschweig.
the class AbstractSormasToSormasInterface method acceptShareRequest.
@Override
@Transactional(rollbackOn = { Exception.class })
public void acceptShareRequest(String uuid) throws SormasToSormasException, SormasToSormasValidationException {
SormasToSormasShareRequestDto shareRequest = shareRequestFacade.getShareRequestByUuid(uuid);
if (shareRequest.getStatus() != ShareRequestStatus.PENDING) {
throw SormasToSormasException.fromStringProperty(Strings.errorSormasToSormasAcceptNotPending);
}
String organizationId = shareRequest.getOriginInfo().getOrganizationId();
SormasToSormasEncryptedDataDto encryptedData = sormasToSormasRestClient.post(organizationId, requestGetDataEndpoint, uuid, SormasToSormasEncryptedDataDto.class);
decryptAndPersist(encryptedData, (data, existingData) -> processedEntitiesPersister.persistSharedData(data, shareRequest.getOriginInfo(), existingData));
// notify the sender that the request has been accepted
sormasToSormasRestClient.post(organizationId, REQUEST_ACCEPTED_ENDPOINT, uuid, null);
shareRequest.setChangeDate(new Date());
shareRequest.setStatus(ShareRequestStatus.ACCEPTED);
shareRequestFacade.saveShareRequest(shareRequest);
}
use of de.symeda.sormas.api.sormastosormas.SormasToSormasEncryptedDataDto in project SORMAS-Project by hzi-braunschweig.
the class SormasToSormasContactFacadeEjbTest method testSaveReturnedContact.
@Test
public void testSaveReturnedContact() throws SormasToSormasException, SormasToSormasValidationException {
UserReferenceDto officer = creator.createUser(rdcf, UserRole.SURVEILLANCE_OFFICER).toReference();
PersonDto contactPerson = creator.createPerson();
ContactDto contact = creator.createContact(rdcf, officer, contactPerson.toReference());
SampleDto sharedSample = creator.createSample(contact.toReference(), officer, rdcf.facility, null);
SampleDto newSample = createRemoteSample(contact.toReference(), officer, rdcf.facility);
User officerUser = getUserService().getByReferenceDto(officer);
getShareRequestInfoService().persist(createShareRequestInfo(officerUser, DEFAULT_SERVER_ID, true, i -> i.setContact(getContactService().getByReferenceDto(contact.toReference()))));
getShareRequestInfoService().persist(createShareRequestInfo(officerUser, DEFAULT_SERVER_ID, true, i -> i.setSample(getSampleService().getByReferenceDto(sharedSample.toReference()))));
contact.setQuarantine(QuarantineType.HOTEL);
Calendar calendar = Calendar.getInstance();
calendar.setTime(contact.getChangeDate());
calendar.add(Calendar.DAY_OF_MONTH, 1);
contact.setChangeDate(calendar.getTime());
SormasToSormasDto shareData = new SormasToSormasDto();
shareData.setOriginInfo(createSormasToSormasOriginInfo(DEFAULT_SERVER_ID, true));
shareData.setContacts(Collections.singletonList(new SormasToSormasContactDto(contactPerson, contact)));
shareData.setSamples(Arrays.asList(new SormasToSormasSampleDto(sharedSample, Collections.emptyList(), Collections.emptyList()), new SormasToSormasSampleDto(newSample, Collections.emptyList(), Collections.emptyList())));
SormasToSormasEncryptedDataDto encryptedData = encryptShareData(shareData);
getSormasToSormasContactFacade().saveSharedEntities(encryptedData);
ContactDto returnedContact = getContactFacade().getByUuid(contact.getUuid());
assertThat(returnedContact.getQuarantine(), is(QuarantineType.HOTEL));
assertThat(returnedContact.getReportingUser(), is(officer));
List<SormasToSormasShareInfoDto> contactShares = getSormasToSormasShareInfoFacade().getIndexList(new SormasToSormasShareInfoCriteria().contact(contact.toReference()), 0, 100);
assertThat(contactShares.get(0).isOwnershipHandedOver(), is(false));
List<SormasToSormasShareInfoDto> sampleShares = getSormasToSormasShareInfoFacade().getIndexList(new SormasToSormasShareInfoCriteria().sample(sharedSample.toReference()), 0, 100);
assertThat(sampleShares.get(0).isOwnershipHandedOver(), is(false));
SampleDto returnedNewSample = getSampleFacade().getSampleByUuid(newSample.getUuid());
assertThat(returnedNewSample.getSormasToSormasOriginInfo().isOwnershipHandedOver(), is(true));
}
Aggregations