Search in sources :

Example 1 with SormasToSormasEncryptedDataDto

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());
    }
}
Also used : SormasToSormasEncryptedDataDto(de.symeda.sormas.api.sormastosormas.SormasToSormasEncryptedDataDto) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) VerificationException(org.keycloak.common.VerificationException) IOException(java.io.IOException)

Example 2 with SormasToSormasEncryptedDataDto

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);
    }
}
Also used : SormasToSormasEncryptedDataDto(de.symeda.sormas.api.sormastosormas.SormasToSormasEncryptedDataDto) Response(javax.ws.rs.core.Response) SormasToSormasErrorResponse(de.symeda.sormas.api.sormastosormas.SormasToSormasErrorResponse) Invocation(javax.ws.rs.client.Invocation) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) ConnectException(java.net.ConnectException)

Example 3 with SormasToSormasEncryptedDataDto

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);
    }
}
Also used : SormasToSormasEncryptedDataDto(de.symeda.sormas.api.sormastosormas.SormasToSormasEncryptedDataDto) CmsPlaintext(de.symeda.sormas.backend.crypt.CmsPlaintext) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SormasToSormasException(de.symeda.sormas.api.sormastosormas.SormasToSormasException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 4 with SormasToSormasEncryptedDataDto

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);
}
Also used : SormasToSormasShareRequestDto(de.symeda.sormas.api.sormastosormas.sharerequest.SormasToSormasShareRequestDto) SormasToSormasEncryptedDataDto(de.symeda.sormas.api.sormastosormas.SormasToSormasEncryptedDataDto) Date(java.util.Date) Transactional(javax.transaction.Transactional)

Example 5 with SormasToSormasEncryptedDataDto

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));
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) SormasToSormasOriginInfoDto(de.symeda.sormas.api.sormastosormas.SormasToSormasOriginInfoDto) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Date(java.util.Date) SormasToSormasSampleDto(de.symeda.sormas.api.sormastosormas.sample.SormasToSormasSampleDto) SormasToSormasOptionsDto(de.symeda.sormas.api.sormastosormas.SormasToSormasOptionsDto) ExposureType(de.symeda.sormas.api.exposure.ExposureType) PersonDto(de.symeda.sormas.api.person.PersonDto) Matchers.nullValue(org.hamcrest.Matchers.nullValue) UserRole(de.symeda.sormas.api.user.UserRole) SormasToSormasTest(de.symeda.sormas.backend.sormastosormas.SormasToSormasTest) SormasToSormasShareInfoCriteria(de.symeda.sormas.api.sormastosormas.shareinfo.SormasToSormasShareInfoCriteria) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) SormasToSormasException(de.symeda.sormas.api.sormastosormas.SormasToSormasException) ShareTreeCriteria(de.symeda.sormas.api.sormastosormas.ShareTreeCriteria) List(java.util.List) User(de.symeda.sormas.backend.user.User) Response(javax.ws.rs.core.Response) PathogenTestResultType(de.symeda.sormas.api.sample.PathogenTestResultType) ContactDto(de.symeda.sormas.api.contact.ContactDto) SormasToSormasDto(de.symeda.sormas.api.sormastosormas.SormasToSormasDto) SormasToSormasContactDto(de.symeda.sormas.api.sormastosormas.contact.SormasToSormasContactDto) Matchers.is(org.hamcrest.Matchers.is) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) SamplePurpose(de.symeda.sormas.api.sample.SamplePurpose) SormasServerDescriptor(de.symeda.sormas.api.sormastosormas.SormasServerDescriptor) MockProducer(de.symeda.sormas.backend.MockProducer) SormasToSormasValidationException(de.symeda.sormas.api.sormastosormas.validation.SormasToSormasValidationException) SormasToSormasShareInfoDto(de.symeda.sormas.api.sormastosormas.shareinfo.SormasToSormasShareInfoDto) RunWith(org.junit.runner.RunWith) TestDataCreator(de.symeda.sormas.backend.TestDataCreator) PathogenTestType(de.symeda.sormas.api.sample.PathogenTestType) QuarantineType(de.symeda.sormas.api.contact.QuarantineType) SormasToSormasEncryptedDataDto(de.symeda.sormas.api.sormastosormas.SormasToSormasEncryptedDataDto) Calendar(java.util.Calendar) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AnimalCondition(de.symeda.sormas.api.epidata.AnimalCondition) DataHelper(de.symeda.sormas.api.utils.DataHelper) SormasToSormasShareRequestDto(de.symeda.sormas.api.sormastosormas.sharerequest.SormasToSormasShareRequestDto) Test(org.junit.Test) ShareRequestStatus(de.symeda.sormas.api.sormastosormas.sharerequest.ShareRequestStatus) FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) SormasToSormasConfig(de.symeda.sormas.api.sormastosormas.SormasToSormasConfig) SampleMaterial(de.symeda.sormas.api.sample.SampleMaterial) UserReferenceDto(de.symeda.sormas.api.user.UserReferenceDto) Mockito(org.mockito.Mockito) ExposureDto(de.symeda.sormas.api.exposure.ExposureDto) Disease(de.symeda.sormas.api.Disease) SampleDto(de.symeda.sormas.api.sample.SampleDto) ContactReferenceDto(de.symeda.sormas.api.contact.ContactReferenceDto) Collections(java.util.Collections) SormasToSormasEncryptedDataDto(de.symeda.sormas.api.sormastosormas.SormasToSormasEncryptedDataDto) SormasToSormasShareInfoDto(de.symeda.sormas.api.sormastosormas.shareinfo.SormasToSormasShareInfoDto) User(de.symeda.sormas.backend.user.User) SormasToSormasDto(de.symeda.sormas.api.sormastosormas.SormasToSormasDto) PersonDto(de.symeda.sormas.api.person.PersonDto) Calendar(java.util.Calendar) SormasToSormasSampleDto(de.symeda.sormas.api.sormastosormas.sample.SormasToSormasSampleDto) SormasToSormasShareInfoCriteria(de.symeda.sormas.api.sormastosormas.shareinfo.SormasToSormasShareInfoCriteria) SormasToSormasContactDto(de.symeda.sormas.api.sormastosormas.contact.SormasToSormasContactDto) UserReferenceDto(de.symeda.sormas.api.user.UserReferenceDto) ContactDto(de.symeda.sormas.api.contact.ContactDto) SormasToSormasContactDto(de.symeda.sormas.api.sormastosormas.contact.SormasToSormasContactDto) SormasToSormasSampleDto(de.symeda.sormas.api.sormastosormas.sample.SormasToSormasSampleDto) SampleDto(de.symeda.sormas.api.sample.SampleDto) SormasToSormasTest(de.symeda.sormas.backend.sormastosormas.SormasToSormasTest) Test(org.junit.Test)

Aggregations

SormasToSormasEncryptedDataDto (de.symeda.sormas.api.sormastosormas.SormasToSormasEncryptedDataDto)32 SormasToSormasTest (de.symeda.sormas.backend.sormastosormas.SormasToSormasTest)25 Test (org.junit.Test)25 SormasToSormasDto (de.symeda.sormas.api.sormastosormas.SormasToSormasDto)23 PersonDto (de.symeda.sormas.api.person.PersonDto)19 CaseDataDto (de.symeda.sormas.api.caze.CaseDataDto)14 SormasToSormasCaseDto (de.symeda.sormas.api.sormastosormas.caze.SormasToSormasCaseDto)13 Date (java.util.Date)12 SampleDto (de.symeda.sormas.api.sample.SampleDto)11 ShareTreeCriteria (de.symeda.sormas.api.sormastosormas.ShareTreeCriteria)11 SormasToSormasSampleDto (de.symeda.sormas.api.sormastosormas.sample.SormasToSormasSampleDto)11 UserReferenceDto (de.symeda.sormas.api.user.UserReferenceDto)11 SormasToSormasException (de.symeda.sormas.api.sormastosormas.SormasToSormasException)10 SormasToSormasOriginInfoDto (de.symeda.sormas.api.sormastosormas.SormasToSormasOriginInfoDto)10 ContactDto (de.symeda.sormas.api.contact.ContactDto)9 FacilityReferenceDto (de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto)9 SormasServerDescriptor (de.symeda.sormas.api.sormastosormas.SormasServerDescriptor)9 SormasToSormasContactDto (de.symeda.sormas.api.sormastosormas.contact.SormasToSormasContactDto)9 SormasToSormasShareInfoDto (de.symeda.sormas.api.sormastosormas.shareinfo.SormasToSormasShareInfoDto)9 SormasToSormasShareRequestDto (de.symeda.sormas.api.sormastosormas.sharerequest.SormasToSormasShareRequestDto)9