Search in sources :

Example 1 with PagaendeSignering

use of se.inera.intyg.webcert.persistence.utkast.model.PagaendeSignering in project webcert by sklintyg.

the class PagaendeSigneringCleanupServiceImplTest method testCleanupKeepAll.

@Test
public void testCleanupKeepAll() {
    List<PagaendeSignering> list = new ArrayList<>();
    PagaendeSignering ps1 = buildOngoingSignature("1", 1L, LocalDateTime.now().minusMinutes(7));
    PagaendeSignering ps2 = buildOngoingSignature("2", 2L, LocalDateTime.now().minusMinutes(5));
    list.add(ps1);
    list.add(ps2);
    when(pagaendeSigneringRepository.findAll()).thenReturn(list);
    testee.cleanup();
    verify(pagaendeSigneringRepository, times(0)).delete(anyLong());
}
Also used : ArrayList(java.util.ArrayList) PagaendeSignering(se.inera.intyg.webcert.persistence.utkast.model.PagaendeSignering) Test(org.junit.Test)

Example 2 with PagaendeSignering

use of se.inera.intyg.webcert.persistence.utkast.model.PagaendeSignering in project webcert by sklintyg.

the class PagaendeSigneringCleanupServiceImplTest method testCleanupOneKeepOne.

@Test
public void testCleanupOneKeepOne() {
    List<PagaendeSignering> list = new ArrayList<>();
    PagaendeSignering ps1 = buildOngoingSignature("1", 1L, LocalDateTime.now().minusMinutes(20));
    PagaendeSignering ps2 = buildOngoingSignature("2", 2L, LocalDateTime.now().minusMinutes(5));
    list.add(ps1);
    list.add(ps2);
    when(pagaendeSigneringRepository.findAll()).thenReturn(list);
    testee.cleanup();
    verify(pagaendeSigneringRepository, times(1)).delete(anyLong());
}
Also used : ArrayList(java.util.ArrayList) PagaendeSignering(se.inera.intyg.webcert.persistence.utkast.model.PagaendeSignering) Test(org.junit.Test)

Example 3 with PagaendeSignering

use of se.inera.intyg.webcert.persistence.utkast.model.PagaendeSignering in project webcert by sklintyg.

the class SignaturServiceImpl method createDraftHash.

/**
 * Called from the Controller when initiating a client (e.g. NetID) signature. Rewritten in INTYG-5048 so
 * <i>starting</i> a signature process does NOT mutate the Utkast in any way. Instead, a temporary intyg JSON model
 * including the signatureDate and signing identity is stored in a {@link PagaendeSignering} entity.
 * <p>
 * Once the signing has been completed
 * (see {@link SignaturServiceImpl#createAndPersistSignature(Utkast, SignaturTicket, String, WebCertUser)}) the
 * hash, intygsId and version from the JSON model in the PagaendeSignatur is validated and if everything works out,
 * the final state is written to the Utkast table.
 * <p>
 * If the user for some reason failed to finish the signing (cancelled in NetID etc.), the Utkast table won't be
 * affected or contain a signingDate even though it wasn't signed. A stale entry may remain in PAGAENDE_SIGNERING
 * but since those cannot be reused such entries can remain there indefinitely or until cleaned up by a janitor
 * task.
 *
 * @param intygId The id of the draft to generate signing ticket for
 * @param version version
 * @return
 */
@Override
@Transactional("jpaTransactionManager")
public SignaturTicket createDraftHash(String intygId, long version) {
    LOG.debug("Hash for clientsignature of draft '{}'", intygId);
    // Fetch Webcert user
    WebCertUser user = getWebcertUserForSignering();
    // Fetch the certificate draft
    Utkast utkast = getUtkastForSignering(intygId, version, user);
    LocalDateTime signeringstid = LocalDateTime.now();
    try {
        VardpersonReferens vardpersonReferens = UpdateUserUtil.createVardpersonFromWebCertUser(user);
        ModuleApi moduleApi = moduleRegistry.getModuleApi(utkast.getIntygsTyp());
        Vardenhet vardenhetFromJson = moduleApi.getUtlatandeFromJson(utkast.getModel()).getGrundData().getSkapadAv().getVardenhet();
        String updatedInternal = moduleApi.updateBeforeSigning(utkast.getModel(), IntygConverterUtil.buildHosPersonalFromWebCertUser(user, vardenhetFromJson), signeringstid);
        // Skapa ny PagaendeSignering
        PagaendeSignering pagaendeSignering = new PagaendeSignering();
        pagaendeSignering.setIntygData(updatedInternal);
        pagaendeSignering.setIntygsId(utkast.getIntygsId());
        pagaendeSignering.setSigneradAvHsaId(vardpersonReferens.getHsaId());
        pagaendeSignering.setSigneradAvNamn(vardpersonReferens.getNamn());
        pagaendeSignering.setSigneringsDatum(signeringstid);
        pagaendeSignering = pagaendeSigneringRepository.save(pagaendeSignering);
        return createSignaturTicket(utkast.getIntygsId(), pagaendeSignering.getInternReferens(), utkast.getVersion(), updatedInternal, signeringstid);
    } catch (ModuleNotFoundException | IOException | ModuleException e) {
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INTERNAL_PROBLEM, "Unable to sign certificate: " + e.getMessage());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) ModuleApi(se.inera.intyg.common.support.modules.support.api.ModuleApi) ModuleNotFoundException(se.inera.intyg.common.support.modules.registry.ModuleNotFoundException) PagaendeSignering(se.inera.intyg.webcert.persistence.utkast.model.PagaendeSignering) IOException(java.io.IOException) Vardenhet(se.inera.intyg.common.support.model.common.internal.Vardenhet) WebCertServiceException(se.inera.intyg.webcert.common.service.exception.WebCertServiceException) Utkast(se.inera.intyg.webcert.persistence.utkast.model.Utkast) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) WebCertUser(se.inera.intyg.webcert.web.service.user.dto.WebCertUser) VardpersonReferens(se.inera.intyg.webcert.persistence.utkast.model.VardpersonReferens) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with PagaendeSignering

use of se.inera.intyg.webcert.persistence.utkast.model.PagaendeSignering in project webcert by sklintyg.

the class SignaturServiceImpl method createAndPersistSignature.

private SignaturTicket createAndPersistSignature(Utkast utkast, SignaturTicket ticket, String rawSignature, WebCertUser user) {
    validateUniqueIntyg(user, utkast.getIntygsTyp(), utkast.getPatientPersonnummer());
    PagaendeSignering pagaendeSignering = pagaendeSigneringRepository.findOne(ticket.getPagaendeSigneringId());
    if (pagaendeSignering == null) {
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Can't complete signing of certificate, no PagaendeSignering found for interreferens " + ticket.getPagaendeSigneringId());
    }
    String payload = pagaendeSignering.getIntygData();
    if (!pagaendeSignering.getIntygsId().equals(utkast.getIntygsId())) {
        LOG.error("Signing of utkast '{}' failed since the intygsId ({}) on the Utkast is different from the one " + "on the signing operation ({})", utkast.getIntygsId(), pagaendeSignering.getIntygsId());
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Internal error signing utkast, the payload of utkast " + utkast.getIntygsId() + " has been modified since signing was initialized");
    }
    if (!ticket.getHash().equals(createHash(payload))) {
        LOG.error("Signing of utkast '{}' failed since the payload has been modified since signing was initialized", utkast.getIntygsId());
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Internal error signing utkast, the payload of utkast " + utkast.getIntygsId() + " has been modified since signing was initialized");
    }
    if (utkast.getVersion() != ticket.getVersion()) {
        LOG.error("Signing of utkast '{}' failed since the version on the utkast ({}) differs from when the signing was initialized ({})", utkast.getIntygsId(), utkast.getVersion(), ticket.getVersion());
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.CONCURRENT_MODIFICATION, "Cannot complete signing, Utkast version differs from signature ticket version.");
    }
    Signatur signatur = new Signatur(ticket.getSigneringstid(), user.getHsaId(), ticket.getIntygsId(), payload, ticket.getHash(), rawSignature);
    // Update user information ("senast sparat av")
    // Add signature to the utkast and set status as signed
    utkast.setSenastSparadAv(new VardpersonReferens(pagaendeSignering.getSigneradAvHsaId(), pagaendeSignering.getSigneradAvNamn()));
    utkast.setModel(payload);
    utkast.setSignatur(signatur);
    utkast.setStatus(UtkastStatus.SIGNED);
    // Persist utkast with added signature
    Utkast savedUtkast = utkastRepository.save(utkast);
    // Send to Intygstjanst
    intygService.storeIntyg(savedUtkast);
    // Remove PagaendeSignering
    pagaendeSigneringRepository.delete(ticket.getPagaendeSigneringId());
    return ticket;
}
Also used : Signatur(se.inera.intyg.webcert.persistence.utkast.model.Signatur) Utkast(se.inera.intyg.webcert.persistence.utkast.model.Utkast) PagaendeSignering(se.inera.intyg.webcert.persistence.utkast.model.PagaendeSignering) WebCertServiceException(se.inera.intyg.webcert.common.service.exception.WebCertServiceException) VardpersonReferens(se.inera.intyg.webcert.persistence.utkast.model.VardpersonReferens)

Example 5 with PagaendeSignering

use of se.inera.intyg.webcert.persistence.utkast.model.PagaendeSignering in project webcert by sklintyg.

the class PagaendeSigneringCleanupServiceImplTest method buildOngoingSignature.

private PagaendeSignering buildOngoingSignature(String intygsId, long internReferens, LocalDateTime signeringsDatum) {
    PagaendeSignering ps1 = new PagaendeSignering();
    ps1.setIntygsId(intygsId);
    ps1.setInternReferens(internReferens);
    ps1.setSigneringsDatum(signeringsDatum);
    return ps1;
}
Also used : PagaendeSignering(se.inera.intyg.webcert.persistence.utkast.model.PagaendeSignering)

Aggregations

PagaendeSignering (se.inera.intyg.webcert.persistence.utkast.model.PagaendeSignering)7 VardpersonReferens (se.inera.intyg.webcert.persistence.utkast.model.VardpersonReferens)3 LocalDateTime (java.time.LocalDateTime)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Transactional (org.springframework.transaction.annotation.Transactional)2 WebCertServiceException (se.inera.intyg.webcert.common.service.exception.WebCertServiceException)2 Utkast (se.inera.intyg.webcert.persistence.utkast.model.Utkast)2 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Before (org.junit.Before)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Profile (org.springframework.context.annotation.Profile)1 EnableScheduling (org.springframework.scheduling.annotation.EnableScheduling)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1 Service (org.springframework.stereotype.Service)1 GrundData (se.inera.intyg.common.support.model.common.internal.GrundData)1